KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.apache.log4j.Logger;
31 import org.exolab.castor.jdo.Database;
32 import org.exolab.castor.jdo.OQLQuery;
33 import org.exolab.castor.jdo.QueryResults;
34 import org.infoglue.cms.entities.content.Content;
35 import org.infoglue.cms.entities.kernel.BaseEntityVO;
36 import org.infoglue.cms.entities.management.impl.simple.AvailableServiceBindingImpl;
37 import org.infoglue.cms.entities.management.impl.simple.ServiceDefinitionImpl;
38 import org.infoglue.cms.entities.structure.Qualifyer;
39 import org.infoglue.cms.entities.structure.ServiceBinding;
40 import org.infoglue.cms.entities.structure.ServiceBindingVO;
41 import org.infoglue.cms.entities.structure.SiteNode;
42 import org.infoglue.cms.entities.structure.SiteNodeVersion;
43 import org.infoglue.cms.entities.structure.impl.simple.ServiceBindingImpl;
44 import org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl;
45 import org.infoglue.cms.exception.Bug;
46 import org.infoglue.cms.exception.ConstraintException;
47 import org.infoglue.cms.exception.SystemException;
48 import org.infoglue.cms.util.ConstraintExceptionBuffer;
49
50
51 /**
52  * @author ss
53  *
54  * To change this generated comment edit the template variable "typecomment":
55  * Window>Preferences>Java>Templates.
56  * To enable and disable the creation of type comments go to
57  * Window>Preferences>Java>Code Generation.
58  */

59 public class ServiceBindingController extends BaseController
60 {
61     private final static Logger logger = Logger.getLogger(ServiceBindingController.class.getName());
62
63     public static ServiceBindingController getController()
64     {
65         return new ServiceBindingController();
66     }
67     
68     public static ServiceBindingVO getServiceBindingVOWithId(Integer JavaDoc serviceBindingId) throws SystemException, Bug
69     {
70         return (ServiceBindingVO) getVOWithId(ServiceBindingImpl.class, serviceBindingId);
71     }
72
73     /*
74     public static ServiceBinding getServiceBindingWithId(Integer serviceBindingId) throws SystemException, Bug
75     {
76         return (ServiceBinding) getObjectWithId(ServiceBindingImpl.class, serviceBindingId);
77     }
78     */

79     
80     public static ServiceBinding getServiceBindingWithId(Integer JavaDoc serviceBindingId, Database db) throws SystemException, Bug
81     {
82         return (ServiceBinding) getObjectWithId(ServiceBindingImpl.class, serviceBindingId, db);
83     }
84
85     public List JavaDoc getServiceBindingVOList() throws SystemException, Bug
86     {
87         return getAllVOObjects(ServiceBindingImpl.class, "serviceBindingId");
88     }
89
90     public static ServiceBindingVO create(ServiceBindingVO serviceBindingVO, String JavaDoc qualifyerXML, Integer JavaDoc availableServiceBindingId, Integer JavaDoc siteNodeVersionId, Integer JavaDoc serviceDefinitionId) throws ConstraintException, SystemException
91     {
92         logger.info("Creating a serviceBinding with the following...");
93
94         logger.info("name:" + serviceBindingVO.getName());
95         logger.info("bindingTypeId:" + serviceBindingVO.getBindingTypeId());
96         logger.info("availableServiceBindingId:" + availableServiceBindingId);
97         logger.info("siteNodeVersionId:" + siteNodeVersionId);
98         logger.info("serviceDefinitionId:" + serviceDefinitionId);
99
100         Database db = CastorDatabaseService.getDatabase();
101         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
102         
103         ServiceBinding serviceBinding = null;
104         
105         beginTransaction(db);
106
107         try
108         {
109             SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(siteNodeVersionId, db);
110             serviceBinding = new ServiceBindingImpl();
111             serviceBinding.setValueObject(serviceBindingVO);
112             serviceBinding.setAvailableServiceBinding((AvailableServiceBindingImpl)AvailableServiceBindingController.getController().getAvailableServiceBindingWithId(availableServiceBindingId, db));
113             serviceBinding.setServiceDefinition((ServiceDefinitionImpl)ServiceDefinitionController.getController().getServiceDefinitionWithId(serviceDefinitionId, db));
114             serviceBinding.setSiteNodeVersion((SiteNodeVersionImpl)siteNodeVersion);
115             
116             //siteNodeVersion.getServiceBindings().add(serviceBinding);
117

118             logger.info("createEntity: " + serviceBinding.getSiteNodeVersion().getSiteNodeVersionId());
119                         
120             serviceBinding.setBindingQualifyers(QualifyerController.createQualifyers(qualifyerXML, serviceBinding));
121             db.create(serviceBinding);
122             
123             siteNodeVersion.getServiceBindings().add(serviceBinding);
124
125             RegistryController.getController().updateSiteNodeVersion(siteNodeVersion, db);
126
127             commitTransaction(db);
128         }
129         catch(Exception JavaDoc e)
130         {
131             logger.error("An error occurred so we should not complete the transaction:" + e, e);
132             e.printStackTrace();
133             rollbackTransaction(db);
134             throw new SystemException(e.getMessage());
135         }
136
137         return serviceBinding.getValueObject();
138     }
139
140     /**
141      * This is a method that lets you create a new service binding within a transaction.
142      */

143     
144     public ServiceBindingVO create(Database db, ServiceBindingVO serviceBindingVO, String JavaDoc qualifyerXML, Integer JavaDoc availableServiceBindingId, Integer JavaDoc siteNodeVersionId, Integer JavaDoc serviceDefinitionId) throws ConstraintException, SystemException
145     {
146         logger.info("Creating a serviceBinding with the following...");
147
148         logger.info("name:" + serviceBindingVO.getName());
149         logger.info("bindingTypeId:" + serviceBindingVO.getBindingTypeId());
150         logger.info("availableServiceBindingId:" + availableServiceBindingId);
151         logger.info("siteNodeVersionId:" + siteNodeVersionId);
152         logger.info("serviceDefinitionId:" + serviceDefinitionId);
153
154         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
155         
156         ServiceBinding serviceBinding = null;
157         
158         try
159         {
160             SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(siteNodeVersionId, db);
161             serviceBinding = new ServiceBindingImpl();
162             serviceBinding.setValueObject(serviceBindingVO);
163             serviceBinding.setAvailableServiceBinding((AvailableServiceBindingImpl)AvailableServiceBindingController.getController().getAvailableServiceBindingWithId(availableServiceBindingId, db));
164             serviceBinding.setServiceDefinition((ServiceDefinitionImpl)ServiceDefinitionController.getController().getServiceDefinitionWithId(serviceDefinitionId, db));
165             serviceBinding.setSiteNodeVersion((SiteNodeVersionImpl)siteNodeVersion);
166             
167             //siteNodeVersion.getServiceBindings().add(serviceBinding);
168

169             logger.info("createEntity: " + serviceBinding.getSiteNodeVersion().getSiteNodeVersionId());
170                         
171             serviceBinding.setBindingQualifyers(QualifyerController.createQualifyers(qualifyerXML, serviceBinding));
172             db.create((ServiceBinding)serviceBinding);
173             
174             siteNodeVersion.getServiceBindings().add(serviceBinding);
175             
176             RegistryController.getController().updateSiteNodeVersion(siteNodeVersion, db);
177         }
178         catch(Exception JavaDoc e)
179         {
180             logger.error("An error occurred so we should not complete the transaction:" + e, e);
181             e.printStackTrace();
182             throw new SystemException(e.getMessage());
183         }
184
185         return serviceBinding.getValueObject();
186     }
187
188
189     protected static ServiceBinding create(ServiceBindingVO serviceBindingVO, Integer JavaDoc availableServiceBindingId, Integer JavaDoc siteNodeVersionId, Integer JavaDoc serviceDefinitionId, Database db) throws ConstraintException, SystemException, Exception JavaDoc
190     {
191         logger.info("Creating a serviceBinding with the following...");
192
193         logger.info("name:" + serviceBindingVO.getName());
194         logger.info("bindingTypeId:" + serviceBindingVO.getBindingTypeId());
195         logger.info("availableServiceBindingId:" + availableServiceBindingId);
196         logger.info("siteNodeVersionId:" + siteNodeVersionId);
197         logger.info("serviceDefinitionId:" + serviceDefinitionId);
198
199         SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getSiteNodeVersionWithId(siteNodeVersionId, db);
200         
201         ServiceBinding serviceBinding = null;
202         
203         serviceBinding = new ServiceBindingImpl();
204         serviceBinding.setValueObject(serviceBindingVO);
205         serviceBinding.setAvailableServiceBinding((AvailableServiceBindingImpl)AvailableServiceBindingController.getController().getAvailableServiceBindingWithId(availableServiceBindingId, db));
206         serviceBinding.setServiceDefinition((ServiceDefinitionImpl)ServiceDefinitionController.getController().getServiceDefinitionWithId(serviceDefinitionId, db));
207         serviceBinding.setSiteNodeVersion((SiteNodeVersionImpl)siteNodeVersion);
208
209         logger.info("createEntity: " + serviceBinding.getSiteNodeVersion().getSiteNodeVersionId());
210         
211         db.create(serviceBinding);
212             
213         RegistryController.getController().updateSiteNodeVersion(siteNodeVersion, db);
214
215         return serviceBinding;
216     }
217
218
219
220     public static ServiceBindingVO update(ServiceBindingVO serviceBindingVO, String JavaDoc qualifyerXML) throws ConstraintException, SystemException
221     {
222         logger.info("Updating a serviceBinding with the following...");
223
224         logger.info("name:" + serviceBindingVO.getName());
225         logger.info("bindingTypeId:" + serviceBindingVO.getBindingTypeId());
226         
227         Database db = CastorDatabaseService.getDatabase();
228         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
229         
230         ServiceBinding serviceBinding = null;
231         
232         beginTransaction(db);
233
234         try
235         {
236             serviceBinding = getServiceBindingWithId(serviceBindingVO.getServiceBindingId(), db);
237             serviceBinding.setPath(serviceBindingVO.getPath());
238             serviceBinding.getBindingQualifyers().clear();
239             Collection JavaDoc newQualifyers = QualifyerController.createQualifyers(qualifyerXML, serviceBinding);
240             serviceBinding.setBindingQualifyers(newQualifyers);
241             
242             RegistryController.getController().updateSiteNodeVersion(serviceBinding.getSiteNodeVersion(), db);
243
244             commitTransaction(db);
245         }
246         catch(Exception JavaDoc e)
247         {
248             logger.error("An error occurred so we should not complete the transaction:" + e, e);
249             e.printStackTrace();
250             rollbackTransaction(db);
251             throw new SystemException(e.getMessage());
252         }
253
254         return serviceBinding.getValueObject();
255     }
256
257
258     /**
259      * This method deletes all service bindings pointing to a content.
260      */

261
262     public static void deleteServiceBindingsReferencingContent(Content content, Database db) throws ConstraintException, SystemException, Exception JavaDoc
263     {
264         OQLQuery oql = db.getOQLQuery( "SELECT sb FROM org.infoglue.cms.entities.structure.impl.simple.ServiceBindingImpl sb WHERE sb.bindingQualifyers.name = $1 AND sb.bindingQualifyers.value = $2 ORDER BY sb.serviceBindingId");
265         oql.bind("contentId");
266         oql.bind(content.getContentId().toString());
267         
268         QueryResults results = oql.execute();
269         logger.info("Fetching entity in read/write mode");
270
271         while(results.hasMore())
272         {
273             ServiceBinding serviceBinding = (ServiceBindingImpl)results.next();
274             //logger.info("serviceBinding:" + serviceBinding.getServiceBindingId());
275
Collection JavaDoc qualifyers = serviceBinding.getBindingQualifyers();
276             Iterator JavaDoc qualifyersIterator = qualifyers.iterator();
277             while(qualifyersIterator.hasNext())
278             {
279                 Qualifyer qualifyer = (Qualifyer)qualifyersIterator.next();
280                 //logger.info("qualifyer:" + qualifyer.getName() + ":" + qualifyer.getValue() + " == " + qualifyer.getValue().equals(content.getContentId().toString()));
281
if(qualifyer.getName().equalsIgnoreCase("contentId") && qualifyer.getValue().equals(content.getContentId().toString()))
282                 {
283                     //db.remove(qualifyer);
284
qualifyersIterator.remove();
285                     //logger.info("Qualifyers:" + serviceBinding.getBindingQualifyers().size());
286
serviceBinding.getBindingQualifyers().remove(qualifyer);
287
288                     //logger.info("Qualifyers2:" + serviceBinding.getBindingQualifyers().size());
289
if(serviceBinding.getBindingQualifyers() == null || serviceBinding.getBindingQualifyers().size() == 0)
290                     {
291                         //logger.info("Removing service binding...");
292
db.remove(serviceBinding);
293                     }
294                 }
295             }
296             
297             SiteNodeVersion siteNodeVersion = serviceBinding.getSiteNodeVersion();
298             if(siteNodeVersion.getOwningSiteNode() == null)
299                 SiteNodeVersionController.getController().delete(siteNodeVersion, db);
300         }
301         
302         results.close();
303         oql.close();
304     }
305     
306     
307     /**
308      * This method deletes all service bindings pointing to a content.
309      */

310
311     public static void deleteServiceBindingsReferencingSiteNode(SiteNode siteNode, Database db) throws ConstraintException, SystemException, Exception JavaDoc
312     {
313         OQLQuery oql = db.getOQLQuery( "SELECT sb FROM org.infoglue.cms.entities.structure.impl.simple.ServiceBindingImpl sb WHERE sb.bindingQualifyers.name = $1 AND sb.bindingQualifyers.value = $2 ORDER BY sb.serviceBindingId");
314         oql.bind("siteNodeId");
315         oql.bind(siteNode.getSiteNodeId().toString());
316         
317         QueryResults results = oql.execute();
318         logger.info("Fetching entity in read/write mode");
319
320         while(results.hasMore())
321         {
322             ServiceBinding serviceBinding = (ServiceBindingImpl)results.next();
323             //logger.info("serviceBinding:" + serviceBinding.getServiceBindingId());
324
Collection JavaDoc qualifyers = serviceBinding.getBindingQualifyers();
325             Iterator JavaDoc qualifyersIterator = qualifyers.iterator();
326             while(qualifyersIterator.hasNext())
327             {
328                 Qualifyer qualifyer = (Qualifyer)qualifyersIterator.next();
329                 //logger.info("qualifyer:" + qualifyer.getName() + ":" + qualifyer.getValue() + " == " + qualifyer.getValue().equals(content.getContentId().toString()));
330
if(qualifyer.getName().equalsIgnoreCase("siteNodeId") && qualifyer.getValue().equals(siteNode.getSiteNodeId().toString()))
331                 {
332                     //db.remove(qualifyer);
333
qualifyersIterator.remove();
334                     //logger.info("Qualifyers:" + serviceBinding.getBindingQualifyers().size());
335
serviceBinding.getBindingQualifyers().remove(qualifyer);
336
337                     //logger.info("Qualifyers2:" + serviceBinding.getBindingQualifyers().size());
338
if(serviceBinding.getBindingQualifyers() == null || serviceBinding.getBindingQualifyers().size() == 0)
339                     {
340                         //logger.info("Removing service binding...");
341
db.remove(serviceBinding);
342                     }
343                 }
344             }
345         }
346         
347         results.close();
348         oql.close();
349     }
350     
351     /**
352      * This method deletes a service binding an all associated qualifyers.
353      */

354     
355     public static void delete(ServiceBindingVO serviceBindingVO) throws ConstraintException, SystemException
356     {
357         Database db = CastorDatabaseService.getDatabase();
358         beginTransaction(db);
359         try
360         {
361             ServiceBinding serviceBinding = ServiceBindingController.getServiceBindingWithId(serviceBindingVO.getServiceBindingId(), db);
362             //QualifyerController.deleteQualifyersForServiceBinding(serviceBinding, db);
363
//deleteEntity(ServiceBindingImpl.class, serviceBindingVO.getServiceBindingId(), db);
364
SiteNodeVersion siteNodeVersion = serviceBinding.getSiteNodeVersion();
365             
366             db.remove(serviceBinding);
367             
368             siteNodeVersion.getServiceBindings().remove(serviceBinding);
369             
370             RegistryController.getController().updateSiteNodeVersion(siteNodeVersion, db);
371
372             commitTransaction(db);
373         }
374         catch(Exception JavaDoc e)
375         {
376             e.printStackTrace();
377             rollbackTransaction(db);
378             throw new SystemException("An error occurred when we tried to remove a serviceBinding and it's qualifyers.");
379         }
380     }
381     
382     /**
383      * This method deletes a service binding an all associated qualifyers.
384      */

385     
386     public static void delete(ServiceBindingVO serviceBindingVO, Database db) throws ConstraintException, SystemException, Exception JavaDoc
387     {
388         ServiceBinding serviceBinding = ServiceBindingController.getServiceBindingWithId(serviceBindingVO.getServiceBindingId(), db);
389         
390         db.remove(serviceBinding);
391     
392         RegistryController.getController().updateSiteNodeVersion(serviceBinding.getSiteNodeVersion(), db);
393     }
394
395     
396     /**
397      * This method returns a list with QualifyerVO-objects which are available for the
398      * serviceBinding sent in
399      */

400     
401     public static List JavaDoc getQualifyerVOList(Integer JavaDoc serviceBindingId) throws ConstraintException, SystemException
402     {
403         Database db = CastorDatabaseService.getDatabase();
404         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
405
406         List JavaDoc qualifyerVOList = null;
407
408         beginTransaction(db);
409
410         try
411         {
412             ServiceBinding serviceBinding = getServiceBindingWithId(serviceBindingId, db);
413             Collection JavaDoc qualifyerList = serviceBinding.getBindingQualifyers();
414             qualifyerVOList = toVOList(qualifyerList);
415             
416             //If any of the validations or setMethods reported an error, we throw them up now before create.
417
ceb.throwIfNotEmpty();
418             
419             commitTransaction(db);
420         }
421         catch(ConstraintException ce)
422         {
423             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
424             rollbackTransaction(db);
425             throw ce;
426         }
427         catch(Exception JavaDoc e)
428         {
429             logger.error("An error occurred so we should not complete the transaction:" + e, e);
430             rollbackTransaction(db);
431             throw new SystemException(e.getMessage());
432         }
433
434         return qualifyerVOList;
435     }
436
437     /**
438      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
439      * is handling.
440      */

441
442     public BaseEntityVO getNewVO()
443     {
444         return new ServiceBindingVO();
445     }
446
447 }
448
Popular Tags