KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.log4j.Logger;
30 import org.exolab.castor.jdo.Database;
31 import org.exolab.castor.jdo.OQLQuery;
32 import org.exolab.castor.jdo.QueryResults;
33 import org.infoglue.cms.entities.kernel.BaseEntityVO;
34 import org.infoglue.cms.entities.management.AvailableServiceBinding;
35 import org.infoglue.cms.entities.management.AvailableServiceBindingVO;
36 import org.infoglue.cms.entities.management.SiteNodeTypeDefinition;
37 import org.infoglue.cms.entities.management.impl.simple.AvailableServiceBindingImpl;
38 import org.infoglue.cms.entities.management.impl.simple.ServiceDefinitionImpl;
39 import org.infoglue.cms.exception.Bug;
40 import org.infoglue.cms.exception.ConstraintException;
41 import org.infoglue.cms.exception.SystemException;
42 import org.infoglue.cms.util.ConstraintExceptionBuffer;
43 import org.infoglue.deliver.util.CacheController;
44
45 /**
46  * This controller handles all available service bindings persistence and logic
47  *
48  * @author mattias
49  */

50
51 public class AvailableServiceBindingController extends BaseController
52 {
53     private final static Logger logger = Logger.getLogger(AvailableServiceBindingController.class.getName());
54
55     /*
56      * Factory method
57      */

58     
59     public static AvailableServiceBindingController getController()
60     {
61         return new AvailableServiceBindingController();
62     }
63     
64     public AvailableServiceBindingVO getAvailableServiceBindingVOWithId(Integer JavaDoc availableServiceBindingId) throws SystemException, Bug
65     {
66         return (AvailableServiceBindingVO)getVOWithId(AvailableServiceBindingImpl.class, availableServiceBindingId);
67     }
68
69     
70     public AvailableServiceBindingVO create(AvailableServiceBindingVO vo) throws ConstraintException, SystemException
71     {
72         AvailableServiceBinding ent = new AvailableServiceBindingImpl();
73         ent.setValueObject(vo);
74         ent = (AvailableServiceBinding) createEntity(ent);
75         return ent.getValueObject();
76     }
77
78     /**
79      * This method deletes an available service binding but only as long as
80      * there are no siteNodes which has serviceBindings referencing it.
81      */

82     
83     public void delete(AvailableServiceBindingVO availableServiceBindingVO) throws ConstraintException, SystemException
84     {
85         Database db = CastorDatabaseService.getDatabase();
86         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
87         
88         beginTransaction(db);
89
90         try
91         {
92             AvailableServiceBinding availableServiceBinding = getAvailableServiceBindingWithId(availableServiceBindingVO.getAvailableServiceBindingId(), db);
93             if(availableServiceBinding.getServiceBindings() != null && availableServiceBinding.getServiceBindings().size() > 0)
94             {
95                 throw new ConstraintException("AvailableServiceBinding.deleteAction", "3100");
96             }
97         }
98         catch(ConstraintException ce)
99         {
100             throw ce;
101         }
102         catch(SystemException se)
103         {
104             throw se;
105         }
106         catch(Exception JavaDoc e)
107         {
108             throw new SystemException("An error occurred in AvailableServiceBindingController.delete(). Reason:" + e.getMessage(), e);
109         }
110         finally
111         {
112             commitTransaction(db);
113         }
114         
115         deleteEntity(AvailableServiceBindingImpl.class, availableServiceBindingVO.getAvailableServiceBindingId());
116     }
117
118
119     public AvailableServiceBinding getAvailableServiceBindingWithId(Integer JavaDoc availableServiceBindingId, Database db) throws SystemException, Bug
120     {
121         return (AvailableServiceBinding) getObjectWithId(AvailableServiceBindingImpl.class, availableServiceBindingId, db);
122     }
123
124
125     public AvailableServiceBinding getReadOnlyAvailableServiceBindingWithId(Integer JavaDoc availableServiceBindingId, Database db) throws SystemException, Bug
126     {
127         return (AvailableServiceBinding) getObjectWithIdAsReadOnly(AvailableServiceBindingImpl.class, availableServiceBindingId, db);
128     }
129
130     public List JavaDoc getAvailableServiceBindingVOList() throws SystemException, Bug
131     {
132         return getAllVOObjects(AvailableServiceBindingImpl.class, "availableServiceBindingId");
133     }
134     
135  
136     /**
137      * This method fetches an available service binding with the given name.
138      *
139      * @throws SystemException
140      * @throws Bug
141      */

142     
143     public AvailableServiceBindingVO getAvailableServiceBindingVOWithName(String JavaDoc name) throws SystemException, Bug
144     {
145         String JavaDoc key = "" + name;
146         logger.info("key:" + key);
147         AvailableServiceBindingVO availableServiceBindingVO = (AvailableServiceBindingVO)CacheController.getCachedObject("availableServiceBindingCache", key);
148         if(availableServiceBindingVO != null)
149         {
150             logger.info("There was an cached availableServiceBindingVO:" + availableServiceBindingVO);
151         }
152         else
153         {
154         
155             Database db = CastorDatabaseService.getDatabase();
156             ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
157     
158             beginTransaction(db);
159     
160             try
161             {
162                 AvailableServiceBinding AvailableServiceBinding = getAvailableServiceBindingWithName(name, db, true);
163                 if(AvailableServiceBinding != null)
164                     availableServiceBindingVO = AvailableServiceBinding.getValueObject();
165         
166                 CacheController.cacheObject("availableServiceBindingCache", key, availableServiceBindingVO);
167
168                 commitTransaction(db);
169             }
170             catch(Exception JavaDoc e)
171             {
172                 rollbackTransaction(db);
173                 throw new SystemException("An error occurred when we tried to fetch a list of AvailableServiceBinding. Reason:" + e.getMessage(), e);
174             }
175         
176         }
177         
178         return availableServiceBindingVO;
179     }
180
181
182     /**
183      * This method fetches an available service binding with the given name.
184      *
185      * @throws SystemException
186      * @throws Bug
187      */

188     
189     public AvailableServiceBindingVO getAvailableServiceBindingVOWithName(String JavaDoc name, Database db) throws SystemException, Bug
190     {
191         String JavaDoc key = "" + name;
192         logger.info("key:" + key);
193         AvailableServiceBindingVO availableServiceBindingVO = (AvailableServiceBindingVO)CacheController.getCachedObject("availableServiceBindingCache", key);
194         if(availableServiceBindingVO != null)
195         {
196             logger.info("There was an cached availableServiceBindingVO:" + availableServiceBindingVO);
197         }
198         else
199         {
200         
201             AvailableServiceBinding AvailableServiceBinding = getAvailableServiceBindingWithName(name, db, true);
202             if(AvailableServiceBinding != null)
203                 availableServiceBindingVO = AvailableServiceBinding.getValueObject();
204     
205             CacheController.cacheObject("availableServiceBindingCache", key, availableServiceBindingVO);
206         }
207         
208         return availableServiceBindingVO;
209     }
210
211     
212     /**
213      * Returns the AvailableServiceBinding with the given name fetched within a given transaction.
214      *
215      * @param name
216      * @param database
217      * @return
218      * @throws SystemException
219      * @throws Bug
220      */

221
222     public AvailableServiceBinding getAvailableServiceBindingWithName(String JavaDoc name, Database db, boolean readOnly) throws SystemException, Bug
223     {
224         AvailableServiceBinding availableServiceBinding = null;
225         
226         try
227         {
228             OQLQuery oql = db.getOQLQuery("SELECT a FROM org.infoglue.cms.entities.management.impl.simple.AvailableServiceBindingImpl a WHERE a.name = $1");
229             oql.bind(name);
230                         
231             QueryResults results = null;
232             
233             if(readOnly)
234                 results = oql.execute(Database.ReadOnly);
235             else
236             {
237                 this.logger.info("Fetching entity in read/write mode:" + name);
238                 results = oql.execute();
239             }
240             
241             if(results.hasMore())
242             {
243                 availableServiceBinding = (AvailableServiceBinding)results.next();
244             }
245             
246             results.close();
247             oql.close();
248         }
249         catch(Exception JavaDoc e)
250         {
251             throw new SystemException("An error occurred when we tried to fetch a named AvailableServiceBinding. Reason:" + e.getMessage(), e);
252         }
253
254         //try{ throw new Exception("Hepp1"); }catch(Exception e){e.printStackTrace();}
255

256         return availableServiceBinding;
257     }
258     
259     
260     /**
261      * This method returns a List of all assigned AvailableServiceBindings available for a certain Repository.
262      */

263
264     public List JavaDoc getAssignedAvailableServiceBindings(Integer JavaDoc siteNodeTypeDefinitionId) throws ConstraintException, SystemException
265     {
266         Database db = CastorDatabaseService.getDatabase();
267         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
268
269         List JavaDoc assignedAvailableServiceBindingVOList = null;
270
271         beginTransaction(db);
272
273         try
274         {
275             SiteNodeTypeDefinition siteNodeTypeDefinition = SiteNodeTypeDefinitionController.getController().getSiteNodeTypeDefinitionWithId(siteNodeTypeDefinitionId, db);
276             Collection JavaDoc assignedAvailableServiceBinding = siteNodeTypeDefinition.getAvailableServiceBindings();
277             assignedAvailableServiceBindingVOList = AvailableServiceBindingController.toVOList(assignedAvailableServiceBinding);
278             
279             //If any of the validations or setMethods reported an error, we throw them up now before create.
280
ceb.throwIfNotEmpty();
281             
282             commitTransaction(db);
283         }
284         catch(ConstraintException ce)
285         {
286             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
287             rollbackTransaction(db);
288             throw ce;
289         }
290         catch(Exception JavaDoc e)
291         {
292             logger.error("An error occurred so we should not complete the transaction:" + e, e);
293             rollbackTransaction(db);
294             throw new SystemException(e.getMessage());
295         }
296
297         return assignedAvailableServiceBindingVOList;
298     }
299     
300     public AvailableServiceBindingVO update(AvailableServiceBindingVO availableServiceBindingVO) throws ConstraintException, SystemException
301     {
302         Database db = CastorDatabaseService.getDatabase();
303         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
304
305         AvailableServiceBinding availableServiceBinding = null;
306
307         beginTransaction(db);
308
309         try
310         {
311             //add validation here if needed
312
availableServiceBinding = getAvailableServiceBindingWithId(availableServiceBindingVO.getAvailableServiceBindingId(), db);
313             availableServiceBinding.setValueObject(availableServiceBindingVO);
314
315             //If any of the validations or setMethods reported an error, we throw them up now before create.
316
ceb.throwIfNotEmpty();
317             
318             commitTransaction(db);
319         }
320         catch(ConstraintException ce)
321         {
322             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
323             rollbackTransaction(db);
324             throw ce;
325         }
326         catch(Exception JavaDoc e)
327         {
328             logger.error("An error occurred so we should not complete the transaction:" + e, e);
329             rollbackTransaction(db);
330             throw new SystemException(e.getMessage());
331         }
332
333
334         return availableServiceBinding.getValueObject();
335     }
336     
337     public AvailableServiceBindingVO update(AvailableServiceBindingVO availableServiceBindingVO, String JavaDoc[] values) throws ConstraintException, SystemException
338     {
339         return (AvailableServiceBindingVO) updateEntity(AvailableServiceBindingImpl.class, (BaseEntityVO)availableServiceBindingVO, "setServiceDefinitions", ServiceDefinitionImpl.class, values );
340     }
341
342     public AvailableServiceBinding update(Integer JavaDoc availableServiceBindingId, String JavaDoc[] values, Database db) throws ConstraintException, SystemException
343     {
344         AvailableServiceBinding availableServiceBinding = getAvailableServiceBindingWithId(availableServiceBindingId, db);
345         return (AvailableServiceBinding) updateEntity(AvailableServiceBindingImpl.class, (BaseEntityVO)availableServiceBinding.getVO(), "setServiceDefinitions", ServiceDefinitionImpl.class, values );
346     }
347
348     /**
349      * This method returns a list of ServiceDefinitionVO-objects which are available for the
350      * availableServiceBinding sent in.
351      */

352     
353     public List JavaDoc getServiceDefinitionVOList(Integer JavaDoc availableServiceBindingId) throws ConstraintException, SystemException
354     {
355         Database db = CastorDatabaseService.getDatabase();
356         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
357
358         List JavaDoc serviceDefinitionVOList = null;
359
360         beginTransaction(db);
361
362         try
363         {
364             serviceDefinitionVOList = getServiceDefinitionVOList(db, availableServiceBindingId);
365             
366             //If any of the validations or setMethods reported an error, we throw them up now before create.
367
ceb.throwIfNotEmpty();
368             
369             commitTransaction(db);
370         }
371         catch(ConstraintException ce)
372         {
373             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
374             rollbackTransaction(db);
375             throw ce;
376         }
377         catch(Exception JavaDoc e)
378         {
379             logger.error("An error occurred so we should not complete the transaction:" + e, e);
380             rollbackTransaction(db);
381             throw new SystemException(e.getMessage());
382         }
383
384         return serviceDefinitionVOList;
385     }
386     
387     /**
388      * This method returns a list of ServiceDefinitionVO-objects which are available for the
389      * availableServiceBinding sent in.
390      */

391     
392     public List JavaDoc getServiceDefinitionVOList(Database db, Integer JavaDoc availableServiceBindingId) throws ConstraintException, SystemException
393     {
394         List JavaDoc serviceDefinitionVOList = null;
395
396         AvailableServiceBinding availableServiceBinding = getReadOnlyAvailableServiceBindingWithId(availableServiceBindingId, db);
397         Collection JavaDoc serviceDefinitionList = availableServiceBinding.getServiceDefinitions();
398         serviceDefinitionVOList = toVOList(serviceDefinitionList);
399
400         return serviceDefinitionVOList;
401     }
402     
403     /**
404      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
405      * is handling.
406      */

407
408     public BaseEntityVO getNewVO()
409     {
410         return new AvailableServiceBindingVO();
411     }
412
413 }
414  
415
Popular Tags