KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > ServiceDefinitionController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.util.List JavaDoc;
27
28 import org.apache.log4j.Logger;
29 import org.exolab.castor.jdo.Database;
30 import org.exolab.castor.jdo.OQLQuery;
31 import org.exolab.castor.jdo.QueryResults;
32 import org.infoglue.cms.entities.kernel.BaseEntityVO;
33 import org.infoglue.cms.entities.management.ServiceDefinition;
34 import org.infoglue.cms.entities.management.ServiceDefinitionVO;
35 import org.infoglue.cms.entities.management.impl.simple.ServiceDefinitionImpl;
36 import org.infoglue.cms.exception.Bug;
37 import org.infoglue.cms.exception.ConstraintException;
38 import org.infoglue.cms.exception.SystemException;
39 import org.infoglue.cms.util.ConstraintExceptionBuffer;
40
41 public class ServiceDefinitionController extends BaseController
42 {
43     private final static Logger logger = Logger.getLogger(ServiceDefinitionController.class.getName());
44
45     /*
46      * Factory method
47      */

48     
49     public static ServiceDefinitionController getController()
50     {
51         return new ServiceDefinitionController();
52     }
53
54     public ServiceDefinitionVO getServiceDefinitionVOWithId(Integer JavaDoc serviceDefinitionId) throws SystemException, Bug
55     {
56         return (ServiceDefinitionVO) getVOWithId(ServiceDefinitionImpl.class, serviceDefinitionId);
57     }
58
59     public ServiceDefinitionVO create(ServiceDefinitionVO vo) throws ConstraintException, SystemException
60     {
61         ServiceDefinition ent = new ServiceDefinitionImpl();
62         ent.setValueObject(vo);
63         ent = (ServiceDefinition) createEntity(ent);
64         return ent.getValueObject();
65     }
66
67     /**
68      * The service definition can only be removed if no serviceBinding uses it.
69      */

70     
71     public void delete(ServiceDefinitionVO vo) throws ConstraintException, SystemException
72     {
73         Database db = CastorDatabaseService.getDatabase();
74         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
75         
76         beginTransaction(db);
77
78         try
79         {
80             ServiceDefinition serviceDefinition = getServiceDefinitionWithId(vo.getServiceDefinitionId(), db);
81             if(serviceDefinition.getName().equalsIgnoreCase("Core content service") || serviceDefinition.getName().equalsIgnoreCase("Core structure service"))
82             {
83                 throw new ConstraintException("ServiceDefinition.deleteAction", "3200");
84             }
85         }
86         catch(ConstraintException ce)
87         {
88             throw ce;
89         }
90         catch(SystemException se)
91         {
92             throw se;
93         }
94         catch(Exception JavaDoc e)
95         {
96             throw new SystemException("An error occurred in ServiceDefinitionController.delete(). Reason:" + e.getMessage(), e);
97         }
98         finally
99         {
100             commitTransaction(db);
101         }
102         
103         deleteEntity(ServiceDefinitionImpl.class, vo.getServiceDefinitionId());
104     }
105
106     public ServiceDefinition getServiceDefinitionWithId(Integer JavaDoc serviceDefinitionId, Database db) throws SystemException, Bug
107     {
108         return (ServiceDefinition) getObjectWithId(ServiceDefinitionImpl.class, serviceDefinitionId, db);
109     }
110
111     public List JavaDoc getServiceDefinitionVOList() throws SystemException, Bug
112     {
113         return getAllVOObjects(ServiceDefinitionImpl.class, "serviceDefinitionId");
114     }
115
116     /**
117      * This method deletes the ServiceDefinition sent in from the system.
118      */

119     
120     public void deleteServiceDefinition(Integer JavaDoc serviceDefinitionId, Database db) throws SystemException, Bug
121     {
122         try
123         {
124             db.remove(getServiceDefinitionWithId(serviceDefinitionId, db));
125         }
126         catch(Exception JavaDoc e)
127         {
128             throw new SystemException("An error occurred when we tried to delete ServiceDefinition in the database. Reason: " + e.getMessage(), e);
129         }
130     }
131     
132     public ServiceDefinitionVO update(ServiceDefinitionVO serviceDefinitionVO) throws ConstraintException, SystemException
133     {
134         Database db = CastorDatabaseService.getDatabase();
135         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
136
137         ServiceDefinition serviceDefinition = null;
138
139         beginTransaction(db);
140
141         try
142         {
143             //add validation here if needed
144
serviceDefinition = getServiceDefinitionWithId(serviceDefinitionVO.getServiceDefinitionId(), db);
145             serviceDefinition.setValueObject(serviceDefinitionVO);
146
147             //If any of the validations or setMethods reported an error, we throw them up now before create.
148
ceb.throwIfNotEmpty();
149             
150             commitTransaction(db);
151         }
152         catch(ConstraintException ce)
153         {
154             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
155             rollbackTransaction(db);
156             throw ce;
157         }
158         catch(Exception JavaDoc e)
159         {
160             logger.error("An error occurred so we should not complete the transaction:" + e, e);
161             rollbackTransaction(db);
162             throw new SystemException(e.getMessage());
163         }
164
165
166         return serviceDefinition.getValueObject();
167     }
168     
169     
170     /**
171      * This method fetches an ServiceDefinition with the given name.
172      *
173      * @throws SystemException
174      * @throws Bug
175      */

176     
177     public ServiceDefinitionVO getServiceDefinitionVOWithName(String JavaDoc name) throws SystemException, Bug
178     {
179         ServiceDefinitionVO serviceDefinitionVO = null;
180         
181         Database db = CastorDatabaseService.getDatabase();
182         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
183
184         beginTransaction(db);
185
186         try
187         {
188             ServiceDefinition serviceDefinition = getServiceDefinitionWithName(name, db, true);
189             if(serviceDefinition != null)
190                 serviceDefinitionVO = serviceDefinition.getValueObject();
191             
192             commitTransaction(db);
193         }
194         catch(Exception JavaDoc e)
195         {
196             rollbackTransaction(db);
197             throw new SystemException("An error occurred when we tried to fetch a ServiceDefinition by name. Reason:" + e.getMessage(), e);
198         }
199     
200         return serviceDefinitionVO;
201     }
202
203     
204     /**
205      * Returns the ServiceDefinition with the given name fetched within a given transaction.
206      *
207      * @param name
208      * @param database
209      * @return
210      * @throws SystemException
211      * @throws Bug
212      */

213
214     public ServiceDefinition getServiceDefinitionWithName(String JavaDoc name, Database db, boolean readOnly) throws SystemException, Bug
215     {
216         ServiceDefinition serviceDefinition = null;
217         
218         try
219         {
220             OQLQuery oql = db.getOQLQuery("SELECT a FROM org.infoglue.cms.entities.management.impl.simple.ServiceDefinitionImpl a WHERE a.name = $1");
221             oql.bind(name);
222                         
223             QueryResults results = null;
224             if(readOnly)
225                 results = oql.execute(Database.ReadOnly);
226             else
227             {
228                 this.logger.info("Fetching entity in read/write mode" + name);
229                 results = oql.execute();
230             }
231             
232             if(results.hasMore())
233             {
234                 serviceDefinition = (ServiceDefinition)results.next();
235             }
236
237             results.close();
238             oql.close();
239         }
240         catch(Exception JavaDoc e)
241         {
242             throw new SystemException("An error occurred when we tried to fetch a named AvailableServiceBinding. Reason:" + e.getMessage(), e);
243         }
244         
245         return serviceDefinition;
246     }
247     
248     
249     /**
250      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
251      * is handling.
252      */

253
254     public BaseEntityVO getNewVO()
255     {
256         return new ServiceDefinitionVO();
257     }
258
259 }
260  
261
Popular Tags