KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.apache.log4j.Logger;
34 import org.exolab.castor.jdo.Database;
35 import org.exolab.castor.jdo.OQLQuery;
36 import org.exolab.castor.jdo.QueryResults;
37 import org.infoglue.cms.applications.contenttool.wizards.actions.CreateContentWizardInfoBean;
38 import org.infoglue.cms.entities.content.Content;
39 import org.infoglue.cms.entities.content.ContentVO;
40 import org.infoglue.cms.entities.content.ContentVersion;
41 import org.infoglue.cms.entities.content.ContentVersionVO;
42 import org.infoglue.cms.entities.content.impl.simple.ContentImpl;
43 import org.infoglue.cms.entities.content.impl.simple.MediumContentImpl;
44 import org.infoglue.cms.entities.content.impl.simple.SmallContentImpl;
45 import org.infoglue.cms.entities.kernel.BaseEntityVO;
46 import org.infoglue.cms.entities.management.ContentTypeDefinition;
47 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO;
48 import org.infoglue.cms.entities.management.Repository;
49 import org.infoglue.cms.entities.management.RepositoryLanguage;
50 import org.infoglue.cms.entities.management.RepositoryVO;
51 import org.infoglue.cms.entities.management.ServiceDefinition;
52 import org.infoglue.cms.entities.management.impl.simple.ContentTypeDefinitionImpl;
53 import org.infoglue.cms.entities.management.impl.simple.RepositoryImpl;
54 import org.infoglue.cms.entities.structure.Qualifyer;
55 import org.infoglue.cms.entities.structure.ServiceBinding;
56 import org.infoglue.cms.exception.Bug;
57 import org.infoglue.cms.exception.ConstraintException;
58 import org.infoglue.cms.exception.SystemException;
59 import org.infoglue.cms.security.InfoGluePrincipal;
60 import org.infoglue.cms.services.BaseService;
61 import org.infoglue.cms.util.ConstraintExceptionBuffer;
62 import org.infoglue.deliver.util.CacheController;
63
64 import com.opensymphony.module.propertyset.PropertySet;
65 import com.opensymphony.module.propertyset.PropertySetManager;
66
67 /**
68  * @author Mattias Bogeblad
69  */

70
71 public class ContentController extends BaseController
72 {
73     private final static Logger logger = Logger.getLogger(ContentController.class.getName());
74
75     /**
76      * Factory method
77      */

78     
79     public static ContentController getContentController()
80     {
81         return new ContentController();
82     }
83
84     public ContentVO getContentVOWithId(Integer JavaDoc contentId) throws SystemException, Bug
85     {
86         return (ContentVO) getVOWithId(SmallContentImpl.class, contentId);
87     }
88
89     public ContentVO getContentVOWithId(Integer JavaDoc contentId, Database db) throws SystemException, Bug
90     {
91         return (ContentVO) getVOWithId(SmallContentImpl.class, contentId, db);
92     }
93
94     public ContentVO getSmallContentVOWithId(Integer JavaDoc contentId, Database db) throws SystemException, Bug
95     {
96         return (ContentVO) getVOWithId(SmallContentImpl.class, contentId, db);
97     }
98
99     public Content getContentWithId(Integer JavaDoc contentId, Database db) throws SystemException, Bug
100     {
101         return (Content) getObjectWithId(ContentImpl.class, contentId, db);
102     }
103
104     public Content getReadOnlyContentWithId(Integer JavaDoc contentId, Database db) throws SystemException, Bug
105     {
106         return (Content) getObjectWithIdAsReadOnly(ContentImpl.class, contentId, db);
107     }
108
109     
110     
111     public List JavaDoc getContentVOList() throws SystemException, Bug
112     {
113         return getAllVOObjects(ContentImpl.class, "contentId");
114     }
115     
116     /**
117      * This method finishes what the create content wizard initiated and resulted in.
118      */

119     
120     public ContentVO create(CreateContentWizardInfoBean createContentWizardInfoBean) throws ConstraintException, SystemException
121     {
122         Database db = CastorDatabaseService.getDatabase();
123         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
124
125         Content content = null;
126
127         beginTransaction(db);
128
129         try
130         {
131             content = create(db, createContentWizardInfoBean.getParentContentId(), createContentWizardInfoBean.getContentTypeDefinitionId(), createContentWizardInfoBean.getRepositoryId(), createContentWizardInfoBean.getContent().getValueObject());
132             
133             Iterator JavaDoc it = createContentWizardInfoBean.getContentVersions().keySet().iterator();
134             while (it.hasNext())
135             {
136                 Integer JavaDoc languageId = (Integer JavaDoc)it.next();
137                 logger.info("languageId:" + languageId);
138                 ContentVersionVO contentVersionVO = (ContentVersionVO)createContentWizardInfoBean.getContentVersions().get(languageId);
139                 ContentVersionController.getContentVersionController().create(content.getContentId(), languageId, contentVersionVO, null, db);
140             }
141             
142             //Bind if needed?
143

144             ceb.throwIfNotEmpty();
145             
146             commitTransaction(db);
147         }
148         catch(ConstraintException ce)
149         {
150             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
151             rollbackTransaction(db);
152             throw ce;
153         }
154         catch(Exception JavaDoc e)
155         {
156             logger.error("An error occurred so we should not complete the transaction:" + e, e);
157             rollbackTransaction(db);
158             throw new SystemException(e.getMessage());
159         }
160
161         return content.getValueObject();
162     }
163     
164     /**
165      * This method creates a new content-entity and references the entities it should know about.
166      * As castor is lousy at this in my opinion we also add the new entity to the surrounding entities.
167      */

168     
169     public ContentVO create(Integer JavaDoc parentContentId, Integer JavaDoc contentTypeDefinitionId, Integer JavaDoc repositoryId, ContentVO contentVO) throws ConstraintException, SystemException
170     {
171         Database db = CastorDatabaseService.getDatabase();
172         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
173
174         Content content = null;
175
176         beginTransaction(db);
177
178         try
179         {
180             content = create(db, parentContentId, contentTypeDefinitionId, repositoryId, contentVO);
181             ceb.throwIfNotEmpty();
182             
183             commitTransaction(db);
184         }
185         catch(ConstraintException ce)
186         {
187             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
188             rollbackTransaction(db);
189             throw ce;
190         }
191         catch(Exception JavaDoc e)
192         {
193             logger.error("An error occurred so we should not complete the transaction:" + e, e);
194             rollbackTransaction(db);
195             throw new SystemException(e.getMessage());
196         }
197
198         return content.getValueObject();
199     }
200
201     /**
202      * This method creates a new content-entity and references the entities it should know about.
203      * As castor is lousy at this in my opinion we also add the new entity to the surrounding entities.
204      */

205         
206     public Content create(Database db, Integer JavaDoc parentContentId, Integer JavaDoc contentTypeDefinitionId, Integer JavaDoc repositoryId, ContentVO contentVO) throws ConstraintException, SystemException, Exception JavaDoc
207     {
208         Content content = null;
209         
210         try
211         {
212             Content parentContent = null;
213             ContentTypeDefinition contentTypeDefinition = null;
214
215             if(parentContentId != null)
216             {
217                 parentContent = getContentWithId(parentContentId, db);
218                 
219                 if(repositoryId == null)
220                     repositoryId = parentContent.getRepository().getRepositoryId();
221             }
222             
223             if(contentTypeDefinitionId != null)
224                 contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db);
225
226             Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db);
227             
228             content = new ContentImpl();
229             content.setValueObject(contentVO);
230             content.setParentContent((ContentImpl)parentContent);
231             content.setRepository((RepositoryImpl)repository);
232             content.setContentTypeDefinition((ContentTypeDefinitionImpl)contentTypeDefinition);
233             
234             db.create(content);
235             
236             //Now we add the content to the knowledge of the related entities.
237
if(parentContent != null)
238             {
239                 parentContent.getChildren().add(content);
240                 parentContent.setIsBranch(new Boolean JavaDoc(true));
241             }
242             
243             //repository.getContents().add(content);
244
}
245         catch(Exception JavaDoc e)
246         {
247             logger.error("An error occurred so we should not complete the transaction:" + e, e);
248             e.printStackTrace();
249             //rollbackTransaction(db);
250
throw new SystemException(e.getMessage());
251         }
252         
253         return content;
254     }
255        
256
257     /**
258      * This method deletes a content and also erases all the children and all versions.
259      */

260         
261     public void delete(ContentVO contentVO) throws ConstraintException, SystemException
262     {
263         Database db = CastorDatabaseService.getDatabase();
264         beginTransaction(db);
265         try
266         {
267             delete(contentVO, db, false, false, false);
268             
269             commitTransaction(db);
270             
271         }
272         catch(ConstraintException ce)
273         {
274             logger.error("An error occurred so we should not complete the transaction:" + ce, ce);
275             rollbackTransaction(db);
276             throw ce;
277         }
278         catch(Exception JavaDoc e)
279         {
280             logger.error("An error occurred so we should not complete the transaction:" + e, e);
281             rollbackTransaction(db);
282             throw new SystemException(e.getMessage());
283         }
284
285     }
286     
287
288     /**
289      * This method deletes a content and also erases all the children and all versions.
290      */

291         
292     public void delete(ContentVO contentVO, Database db) throws ConstraintException, SystemException, Exception JavaDoc
293     {
294         delete(contentVO, db, false, false, false);
295     }
296     
297     /**
298      * This method deletes a content and also erases all the children and all versions.
299      */

300         
301     public void delete(ContentVO contentVO, Database db, boolean skipRelationCheck, boolean skipServiceBindings, boolean forceDelete) throws ConstraintException, SystemException, Exception JavaDoc
302     {
303         Content content = getContentWithId(contentVO.getContentId(), db);
304         Content parent = content.getParentContent();
305         if(parent != null)
306         {
307             Iterator JavaDoc childContentIterator = parent.getChildren().iterator();
308             while(childContentIterator.hasNext())
309             {
310                 Content candidate = (Content)childContentIterator.next();
311                 if(candidate.getId().equals(contentVO.getContentId()))
312                 {
313                     deleteRecursive(content, childContentIterator, db, skipRelationCheck, skipServiceBindings, forceDelete);
314                 }
315             }
316         }
317         else
318         {
319             deleteRecursive(content, null, db, skipRelationCheck, skipServiceBindings, forceDelete);
320         }
321     }
322
323     /**
324      * Recursively deletes all contents and their versions. Also updates related entities about the change.
325      */

326     
327     private static void deleteRecursive(Content content, Iterator JavaDoc parentIterator, Database db, boolean skipRelationCheck, boolean skipServiceBindings, boolean forceDelete) throws ConstraintException, SystemException, Exception JavaDoc
328     {
329         if(!skipRelationCheck)
330         {
331             List JavaDoc referenceBeanList = RegistryController.getController().getReferencingObjectsForContent(content.getId(), db);
332             if(referenceBeanList != null && referenceBeanList.size() > 0)
333                 throw new ConstraintException("ContentVersion.stateId", "3305");
334         }
335         
336         Collection JavaDoc children = content.getChildren();
337         Iterator JavaDoc childrenIterator = children.iterator();
338         while(childrenIterator.hasNext())
339         {
340             Content childContent = (Content)childrenIterator.next();
341             deleteRecursive(childContent, childrenIterator, db, skipRelationCheck, skipServiceBindings, forceDelete);
342         }
343         content.setChildren(new ArrayList JavaDoc());
344         
345         if(forceDelete || getIsDeletable(content))
346         {
347             ContentVersionController.getContentVersionController().deleteVersionsForContent(content, db, forceDelete);
348             
349             if(!skipServiceBindings)
350                 ServiceBindingController.deleteServiceBindingsReferencingContent(content, db);
351             
352             if(parentIterator != null)
353                 parentIterator.remove();
354             
355             db.remove(content);
356             
357             Map JavaDoc args = new HashMap JavaDoc();
358             args.put("globalKey", "infoglue");
359             PropertySet ps = PropertySetManager.getInstance("jdbc", args);
360
361             ps.remove( "content_" + content.getContentId() + "_allowedContentTypeNames");
362             ps.remove( "content_" + content.getContentId() + "_defaultContentTypeName");
363             ps.remove( "content_" + content.getContentId() + "_initialLanguageId");
364
365         }
366         else
367         {
368             throw new ConstraintException("ContentVersion.stateId", "3300");
369         }
370     }
371
372     
373     /**
374      * This method returns true if the content does not have any published contentversions or
375      * are restricted in any other way.
376      */

377     
378     private static boolean getIsDeletable(Content content)
379     {
380         boolean isDeletable = true;
381     
382         Collection JavaDoc contentVersions = content.getContentVersions();
383         Iterator JavaDoc versionIterator = contentVersions.iterator();
384         while (versionIterator.hasNext())
385         {
386             ContentVersion contentVersion = (ContentVersion)versionIterator.next();
387             if(contentVersion.getStateId().intValue() == ContentVersionVO.PUBLISHED_STATE.intValue() && contentVersion.getIsActive().booleanValue() == true)
388             {
389                 logger.info("The content had a published version so we cannot delete it..");
390                 isDeletable = false;
391                 break;
392             }
393         }
394             
395         return isDeletable;
396     }
397
398     
399     public ContentVO update(ContentVO contentVO) throws ConstraintException, SystemException
400     {
401         return update(contentVO, null);
402     }
403
404
405     public ContentVO update(ContentVO contentVO, Integer JavaDoc contentTypeDefinitionId) throws ConstraintException, SystemException
406     {
407         Database db = CastorDatabaseService.getDatabase();
408
409         Content content = null;
410
411         beginTransaction(db);
412
413         try
414         {
415             content = (Content)getObjectWithId(ContentImpl.class, contentVO.getId(), db);
416             content.setVO(contentVO);
417             
418             if(contentTypeDefinitionId != null)
419             {
420                 ContentTypeDefinition contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db);
421                 content.setContentTypeDefinition((ContentTypeDefinitionImpl)contentTypeDefinition);
422             }
423             
424             commitTransaction(db);
425         }
426         catch(Exception JavaDoc e)
427         {
428             logger.error("An error occurred so we should not complete the transaction:" + e, e);
429             rollbackTransaction(db);
430             throw new SystemException(e.getMessage());
431         }
432
433         return content.getValueObject();
434     }
435
436     public List JavaDoc getAvailableLanguagesForContentWithId(Integer JavaDoc contentId, Database db) throws ConstraintException, SystemException, Exception JavaDoc
437     {
438         List JavaDoc availableLanguageVOList = new ArrayList JavaDoc();
439         
440         Content content = getContentWithId(contentId, db);
441         if(content != null)
442         {
443             Repository repository = content.getRepository();
444             if(repository != null)
445             {
446                 List JavaDoc availableRepositoryLanguageList = RepositoryLanguageController.getController().getRepositoryLanguageListWithRepositoryId(repository.getId(), db);
447                 Iterator JavaDoc i = availableRepositoryLanguageList.iterator();
448                 while(i.hasNext())
449                 {
450                     RepositoryLanguage repositoryLanguage = (RepositoryLanguage)i.next();
451                     availableLanguageVOList.add(repositoryLanguage.getLanguage().getValueObject());
452                 }
453             }
454         }
455         
456         return availableLanguageVOList;
457     }
458 /*
459     public List getAvailableLanguagesForContentWithId(Integer contentId, Database db) throws ConstraintException, SystemException
460     {
461         List availableLanguageVOList = new ArrayList();
462         
463         Content content = getContentWithId(contentId, db);
464         if(content != null)
465         {
466             Repository repository = content.getRepository();
467             if(repository != null)
468             {
469                 Collection availableLanguages = repository.getRepositoryLanguages();
470                 Iterator i = availableLanguages.iterator();
471                 while(i.hasNext())
472                 {
473                     RepositoryLanguage repositoryLanguage = (RepositoryLanguage)i.next();
474                     
475                     int position = 0;
476                     Iterator availableLanguageVOListIterator = availableLanguageVOList.iterator();
477                     while(availableLanguageVOListIterator.hasNext())
478                     {
479                         LanguageVO availableLanguageVO = (LanguageVO)availableLanguageVOListIterator.next();
480                         if(repositoryLanguage.getLanguage().getValueObject().getId().intValue() < availableLanguageVO.getId().intValue())
481                             break;
482                         
483                         position++;
484                     }
485                     
486                     availableLanguageVOList.add(position, repositoryLanguage.getLanguage().getValueObject());
487                 }
488             }
489         }
490         
491         return availableLanguageVOList;
492     }
493 */

494     
495     /**
496      * This method returns the value-object of the parent of a specific content.
497      */

498     
499     public static ContentVO getParentContent(Integer JavaDoc contentId) throws SystemException, Bug
500     {
501         logger.info("Coming in with:" + contentId);
502         Database db = CastorDatabaseService.getDatabase();
503         ContentVO parentContentVO = null;
504         
505         beginTransaction(db);
506
507         try
508         {
509             Content content = (Content) getObjectWithId(ContentImpl.class, contentId, db);
510             logger.info("CONTENT:" + content.getName());
511             Content parent = content.getParentContent();
512             if(parent != null)
513                 parentContentVO = parent.getValueObject();
514             
515             commitTransaction(db);
516         }
517         catch(Exception JavaDoc e)
518         {
519             logger.error("An error occurred so we should not complete the transaction:" + e, e);
520             rollbackTransaction(db);
521             throw new SystemException(e.getMessage());
522         }
523         return parentContentVO;
524     }
525
526
527     public static void addChildContent(ContentVO parentVO, ContentVO childVO)
528         throws ConstraintException, SystemException
529     {
530
531         Database db = CastorDatabaseService.getDatabase();
532         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
533
534         beginTransaction(db);
535
536         try
537         {
538             Content parent = (Content) getObjectWithId(ContentImpl.class, parentVO.getContentId(), db);
539             Content child = (Content) getObjectWithId(ContentImpl.class, childVO.getContentId(), db);
540             parent.getChildren().add(child);
541
542             ceb.throwIfNotEmpty();
543             commitTransaction(db);
544         }
545         catch(ConstraintException ce)
546         {
547             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
548             rollbackTransaction(db);
549             throw ce;
550         }
551         catch(Exception JavaDoc e)
552         {
553             logger.error("An error occurred so we should not complete the transaction:" + e, e);
554             rollbackTransaction(db);
555             throw new SystemException(e.getMessage());
556         }
557         
558     }
559
560     public static void removeChildContent(ContentVO parentVO, ContentVO childVO)
561         throws ConstraintException, SystemException
562     {
563
564         Database db = CastorDatabaseService.getDatabase();
565         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
566
567         beginTransaction(db);
568
569         try
570         {
571             Content parent = (Content) getObjectWithId(ContentImpl.class, parentVO.getContentId(), db);
572             Content child = (Content) getObjectWithId(ContentImpl.class, childVO.getContentId(), db);
573             parent.getChildren().remove(child);
574
575             ceb.throwIfNotEmpty();
576             commitTransaction(db);
577         }
578         catch(ConstraintException ce)
579         {
580             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
581             rollbackTransaction(db);
582             throw ce;
583         }
584         catch(Exception JavaDoc e)
585         {
586             logger.error("An error occurred so we should not complete the transaction:" + e, e);
587             rollbackTransaction(db);
588             throw new SystemException(e.getMessage());
589         }
590         
591     }
592
593     
594
595     /**
596      * This method moves a content from one parent-content to another. First we check so no illegal actions are
597      * in process. For example the target folder must not be the item to be moved or a child to the item.
598      * Such actions would result in model-errors.
599      */

600         
601     public void moveContent(ContentVO contentVO, Integer JavaDoc newParentContentId) throws ConstraintException, SystemException
602     {
603         Database db = CastorDatabaseService.getDatabase();
604
605         beginTransaction(db);
606
607         try
608         {
609             moveContent(contentVO, newParentContentId, db);
610             
611             commitTransaction(db);
612         }
613         catch(Exception JavaDoc e)
614         {
615             logger.error("An error occurred so we should not complete the transaction:" + e, e);
616             rollbackTransaction(db);
617             throw new SystemException(e.getMessage());
618         }
619
620     }
621
622     /**
623      * This method moves a content from one parent-content to another. First we check so no illegal actions are
624      * in process. For example the target folder must not be the item to be moved or a child to the item.
625      * Such actions would result in model-errors.
626      */

627         
628     public void moveContent(ContentVO contentVO, Integer JavaDoc newParentContentId, Database db) throws ConstraintException, SystemException
629     {
630         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
631
632         Content content = null;
633         Content newParentContent = null;
634         Content oldParentContent = null;
635
636         //Validation that checks the entire object
637
contentVO.validate();
638         
639         if(newParentContentId == null)
640         {
641             logger.warn("You must specify the new parent-content......");
642             throw new ConstraintException("Content.parentContentId", "3303");
643         }
644
645         if(contentVO.getId().intValue() == newParentContentId.intValue())
646         {
647             logger.warn("You cannot have the content as it's own parent......");
648             throw new ConstraintException("Content.parentContentId", "3301");
649         }
650         
651         content = getContentWithId(contentVO.getContentId(), db);
652         oldParentContent = content.getParentContent();
653         newParentContent = getContentWithId(newParentContentId, db);
654                     
655         if(oldParentContent.getId().intValue() == newParentContentId.intValue())
656         {
657             logger.warn("You cannot specify the same folder as it originally was located in......");
658             throw new ConstraintException("Content.parentContentId", "3304");
659         }
660
661         Content tempContent = newParentContent.getParentContent();
662         while(tempContent != null)
663         {
664             if(tempContent.getId().intValue() == content.getId().intValue())
665             {
666                 logger.warn("You cannot move the content to a child under it......");
667                 throw new ConstraintException("Content.parentContentId", "3302");
668             }
669             tempContent = tempContent.getParentContent();
670         }
671         
672         oldParentContent.getChildren().remove(content);
673         content.setParentContent((ContentImpl)newParentContent);
674         
675         changeRepositoryRecursive(content, newParentContent.getRepository());
676         //content.setRepository(newParentContent.getRepository());
677
newParentContent.getChildren().add(content);
678         
679         //If any of the validations or setMethods reported an error, we throw them up now before create.
680
ceb.throwIfNotEmpty();
681     }
682
683     /**
684      * Recursively sets the contents repositoryId.
685      * @param content
686      * @param newRepository
687      */

688
689     private void changeRepositoryRecursive(Content content, Repository newRepository)
690     {
691         if(content.getRepository().getId().intValue() != newRepository.getId().intValue())
692         {
693             content.setRepository((RepositoryImpl)newRepository);
694             Iterator JavaDoc childContentsIterator = content.getChildren().iterator();
695             while(childContentsIterator.hasNext())
696             {
697                 Content childContent = (Content)childContentsIterator.next();
698                 changeRepositoryRecursive(childContent, newRepository);
699             }
700         }
701     }
702     
703     /**
704      * Returns all Contents having the specified ContentTypeDefintion.
705      */

706     
707     public List JavaDoc getContentVOWithContentTypeDefinition(String JavaDoc contentTypeDefinitionName) throws SystemException
708     {
709         Database db = CastorDatabaseService.getDatabase();
710         beginTransaction(db);
711         try
712         {
713             List JavaDoc result = getContentVOWithContentTypeDefinition(contentTypeDefinitionName, db);
714             commitTransaction(db);
715             return result;
716         }
717         catch(Exception JavaDoc e)
718         {
719             logger.error("An error occurred so we should not complete the transaction:" + e, e);
720             rollbackTransaction(db);
721             throw new SystemException(e.getMessage());
722         }
723     }
724     
725     /**
726      * Returns all Contents having the specified ContentTypeDefintion.
727      */

728     public List JavaDoc getContentVOWithContentTypeDefinition(String JavaDoc contentTypeDefinitionName, Database db) throws SystemException
729     {
730         HashMap JavaDoc arguments = new HashMap JavaDoc();
731         arguments.put("method", "selectListOnContentTypeName");
732
733         List JavaDoc argumentList = new ArrayList JavaDoc();
734         HashMap JavaDoc argument = new HashMap JavaDoc();
735         argument.put("contentTypeDefinitionName", contentTypeDefinitionName);
736         argumentList.add(argument);
737         arguments.put("arguments", argumentList);
738         try
739         {
740             return getContentVOList(arguments, db);
741         }
742         catch(SystemException e)
743         {
744             throw e;
745         }
746         catch(Exception JavaDoc e)
747         {
748             throw new SystemException(e.getMessage());
749         }
750     }
751     
752     /**
753      * This method is sort of a sql-query-like method where you can send in arguments in form of a list
754      * of things that should match. The input is a Hashmap with a method and a List of HashMaps.
755      */

756     
757     public List JavaDoc getContentVOList(HashMap JavaDoc argumentHashMap) throws SystemException, Bug
758     {
759         List JavaDoc contents = null;
760         
761         String JavaDoc method = (String JavaDoc)argumentHashMap.get("method");
762         logger.info("method:" + method);
763         
764         if(method.equalsIgnoreCase("selectContentListOnIdList"))
765         {
766             contents = new ArrayList JavaDoc();
767             List JavaDoc arguments = (List JavaDoc)argumentHashMap.get("arguments");
768             logger.info("Arguments:" + arguments.size());
769             Iterator JavaDoc argumentIterator = arguments.iterator();
770             while(argumentIterator.hasNext())
771             {
772                 HashMap JavaDoc argument = (HashMap JavaDoc)argumentIterator.next();
773                 Integer JavaDoc contentId = new Integer JavaDoc((String JavaDoc)argument.get("contentId"));
774                 logger.info("Getting the content with Id:" + contentId);
775                 contents.add(getContentVOWithId(contentId));
776             }
777         }
778         else if(method.equalsIgnoreCase("selectListOnContentTypeName"))
779         {
780             List JavaDoc arguments = (List JavaDoc)argumentHashMap.get("arguments");
781             logger.info("Arguments:" + arguments.size());
782             contents = getContentVOListByContentTypeNames(arguments);
783         }
784         return contents;
785     }
786     
787     /**
788      * This method is sort of a sql-query-like method where you can send in arguments in form of a list
789      * of things that should match. The input is a Hashmap with a method and a List of HashMaps.
790      */

791     
792     public List JavaDoc getContentVOList(HashMap JavaDoc argumentHashMap, Database db) throws SystemException, Exception JavaDoc
793     {
794         List JavaDoc contents = null;
795         
796         String JavaDoc method = (String JavaDoc)argumentHashMap.get("method");
797         logger.info("method:" + method);
798         
799         if(method.equalsIgnoreCase("selectContentListOnIdList"))
800         {
801             contents = new ArrayList JavaDoc();
802             List JavaDoc arguments = (List JavaDoc)argumentHashMap.get("arguments");
803             logger.info("Arguments:" + arguments.size());
804             Iterator JavaDoc argumentIterator = arguments.iterator();
805             while(argumentIterator.hasNext())
806             {
807                 HashMap JavaDoc argument = (HashMap JavaDoc)argumentIterator.next();
808                 Integer JavaDoc contentId = new Integer JavaDoc((String JavaDoc)argument.get("contentId"));
809                 logger.info("Getting the content with Id:" + contentId);
810                 contents.add(getSmallContentVOWithId(contentId, db));
811             }
812         }
813         else if(method.equalsIgnoreCase("selectListOnContentTypeName"))
814         {
815             List JavaDoc arguments = (List JavaDoc)argumentHashMap.get("arguments");
816             logger.info("Arguments:" + arguments.size());
817             contents = getContentVOListByContentTypeNames(arguments, db);
818         }
819         return contents;
820     }
821     
822
823     /**
824      * The input is a list of hashmaps.
825      */

826     
827     protected List JavaDoc getContentVOListByContentTypeNames(List JavaDoc arguments) throws SystemException, Bug
828     {
829         Database db = CastorDatabaseService.getDatabase();
830     
831         List JavaDoc contents = new ArrayList JavaDoc();
832         
833         beginTransaction(db);
834
835         try
836         {
837             Iterator JavaDoc i = arguments.iterator();
838             while(i.hasNext())
839             {
840                 HashMap JavaDoc argument = (HashMap JavaDoc)i.next();
841                 String JavaDoc contentTypeDefinitionName = (String JavaDoc)argument.get("contentTypeDefinitionName");
842                 
843                 //OQLQuery oql = db.getOQLQuery("CALL SQL SELECT c.contentId, c.name, c.publishDateTime, c.expireDateTime, c.isBranch, c.isProtected, c.creator, ctd.contentTypeDefinitionId, r.repositoryId FROM cmContent c, cmContentTypeDefinition ctd, cmRepository r where c.repositoryId = r.repositoryId AND c.contentTypeDefinitionId = ctd.contentTypeDefinitionId AND ctd.name = $1 AS org.infoglue.cms.entities.content.impl.simple.SmallContentImpl");
844
//OQLQuery oql = db.getOQLQuery("CALL SQL SELECT contentId, name FROM cmContent c, cmContentTypeDefinition ctd WHERE c.contentTypeDefinitionId = ctd.contentTypeDefinitionId AND ctd.name = $1 AS org.infoglue.cms.entities.content.impl.simple.ContentImpl");
845
OQLQuery oql = db.getOQLQuery("SELECT c FROM org.infoglue.cms.entities.content.impl.simple.MediumContentImpl c WHERE c.contentTypeDefinition.name = $1 ORDER BY c.contentId");
846                 oql.bind(contentTypeDefinitionName);
847                 
848                 QueryResults results = oql.execute(Database.ReadOnly);
849                 
850                 while(results.hasMore())
851                 {
852                     MediumContentImpl content = (MediumContentImpl)results.next();
853                     contents.add(content.getValueObject());
854                 }
855                 
856                 results.close();
857                 oql.close();
858             }
859             
860             commitTransaction(db);
861         }
862         catch(Exception JavaDoc e)
863         {
864             logger.error("An error occurred so we should not complete the transaction:" + e, e);
865             rollbackTransaction(db);
866             throw new SystemException(e.getMessage());
867         }
868         
869         return contents;
870     }
871     
872     
873     /**
874      * The input is a list of hashmaps.
875      */

876     
877     protected List JavaDoc getContentVOListByContentTypeNames(List JavaDoc arguments, Database db) throws SystemException, Exception JavaDoc
878     {
879         List JavaDoc contents = new ArrayList JavaDoc();
880
881         Iterator JavaDoc i = arguments.iterator();
882         while(i.hasNext())
883         {
884             HashMap JavaDoc argument = (HashMap JavaDoc)i.next();
885             String JavaDoc contentTypeDefinitionName = (String JavaDoc)argument.get("contentTypeDefinitionName");
886             //OQLQuery oql = db.getOQLQuery("CALL SQL SELECT c.contentId, c.name, c.publishDateTime, c.expireDateTime, c.isBranch, c.isProtected, c.creator, ctd.contentTypeDefinitionId, r.repositoryId FROM cmContent c, cmContentTypeDefinition ctd, cmRepository r where c.repositoryId = r.repositoryId AND c.contentTypeDefinitionId = ctd.contentTypeDefinitionId AND ctd.name = $1 AS org.infoglue.cms.entities.content.impl.simple.SmallContentImpl");
887
//OQLQuery oql = db.getOQLQuery("CALL SQL SELECT contentId, name FROM cmContent c, cmContentTypeDefinition ctd WHERE c.contentTypeDefinitionId = ctd.contentTypeDefinitionId AND ctd.name = $1 AS org.infoglue.cms.entities.content.impl.simple.ContentImpl");
888
OQLQuery oql = db.getOQLQuery("SELECT c FROM org.infoglue.cms.entities.content.impl.simple.MediumContentImpl c WHERE c.contentTypeDefinition.name = $1");
889             oql.bind(contentTypeDefinitionName);
890             
891             QueryResults results = oql.execute(Database.ReadOnly);
892             
893             while(results.hasMore())
894             {
895                 MediumContentImpl content = (MediumContentImpl)results.next();
896                 contents.add(content.getValueObject());
897             }
898             
899             results.close();
900             oql.close();
901         }
902         
903         return contents;
904     }
905
906     /**
907      * The input is a list of hashmaps.
908      */

909     /*
910     private static List getContentVOListByContentTypeNames(List arguments) throws SystemException, Bug
911     {
912         Database db = CastorDatabaseService.getDatabase();
913         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
914     
915         List contents = new ArrayList();
916         
917         beginTransaction(db);
918
919         try
920         {
921             Iterator i = arguments.iterator();
922             while(i.hasNext())
923             {
924                 HashMap argument = (HashMap)i.next();
925                 String contentTypeDefinitionName = (String)argument.get("contentTypeDefinitionName");
926                 
927                 OQLQuery oql = db.getOQLQuery("SELECT ctd FROM org.infoglue.cms.entities.management.impl.simple.ContentTypeDefinitionImpl ctd WHERE ctd.name = $1");
928                 oql.bind(contentTypeDefinitionName);
929                 
930                 QueryResults results = oql.execute(Database.ReadOnly);
931                 
932                 if (results.hasMore())
933                 {
934                     ContentTypeDefinition contentTypeDefinition = (ContentTypeDefinition)results.next();
935                     Collection contentList = contentTypeDefinition.getContents();
936                     contents = toVOList(contentList);
937                 }
938             }
939             
940             commitTransaction(db);
941         }
942         catch(Exception e)
943         {
944             logger.error("An error occurred so we should not complete the transaction:" + e, e);
945             rollbackTransaction(db);
946             throw new SystemException(e.getMessage());
947         }
948         
949         return contents;
950     }
951     */

952     
953     /**
954      * This method fetches the root content for a particular repository.
955      * If there is no such content we create one as all repositories need one to work.
956      */

957             
958     public ContentVO getRootContentVO(Integer JavaDoc repositoryId, String JavaDoc userName) throws ConstraintException, SystemException
959     {
960         Database db = CastorDatabaseService.getDatabase();
961         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
962
963         Content content = null;
964
965         beginTransaction(db);
966
967         try
968         {
969             logger.info("Fetching the root content for the repository " + repositoryId);
970             //OQLQuery oql = db.getOQLQuery( "SELECT c FROM org.infoglue.cms.entities.content.impl.simple.ContentImpl c WHERE is_undefined(c.parentContent) AND c.repository.repositoryId = $1");
971
OQLQuery oql = db.getOQLQuery( "SELECT c FROM org.infoglue.cms.entities.content.impl.simple.SmallContentImpl c WHERE is_undefined(c.parentContentId) AND c.repositoryId = $1");
972             oql.bind(repositoryId);
973             
974             QueryResults results = oql.execute(Database.ReadOnly);
975             if (results.hasMore())
976             {
977                 content = (Content)results.next();
978             }
979             else
980             {
981                 //None found - we create it and give it the name of the repository.
982
logger.info("Found no rootContent so we create a new....");
983                 ContentVO rootContentVO = new ContentVO();
984                 RepositoryVO repositoryVO = RepositoryController.getController().getRepositoryVOWithId(repositoryId);
985                 rootContentVO.setCreatorName(userName);
986                 rootContentVO.setName(repositoryVO.getName());
987                 rootContentVO.setIsBranch(new Boolean JavaDoc(true));
988                 content = create(db, null, null, repositoryId, rootContentVO);
989             }
990             
991             results.close();
992             oql.close();
993
994             //If any of the validations or setMethods reported an error, we throw them up now before create.
995
ceb.throwIfNotEmpty();
996             commitTransaction(db);
997             
998         }
999         catch(ConstraintException ce)
1000        {
1001            logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
1002            rollbackTransaction(db);
1003            throw ce;
1004        }
1005        catch(Exception JavaDoc e)
1006        {
1007            logger.error("An error occurred so we should not complete the transaction:" + e, e);
1008            rollbackTransaction(db);
1009            throw new SystemException(e.getMessage());
1010        }
1011
1012        return (content == null) ? null : content.getValueObject();
1013    }
1014
1015
1016    
1017    /**
1018     * This method fetches the root content for a particular repository.
1019     * If there is no such content we create one as all repositories need one to work.
1020     */

1021            
1022    public ContentVO getRootContentVO(Integer JavaDoc repositoryId, String JavaDoc userName, boolean createIfNonExisting) throws ConstraintException, SystemException
1023    {
1024        Database db = CastorDatabaseService.getDatabase();
1025        ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
1026
1027        Content content = null;
1028
1029        beginTransaction(db);
1030
1031        try
1032        {
1033            content = getRootContent(db, repositoryId, userName, createIfNonExisting);
1034            
1035            //If any of the validations or setMethods reported an error, we throw them up now before create.
1036
ceb.throwIfNotEmpty();
1037            commitTransaction(db);
1038            
1039        }
1040        catch(ConstraintException ce)
1041        {
1042            logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
1043            rollbackTransaction(db);
1044            throw ce;
1045        }
1046        catch(Exception JavaDoc e)
1047        {
1048            logger.error("An error occurred so we should not complete the transaction:" + e, e);
1049            rollbackTransaction(db);
1050            throw new SystemException(e.getMessage());
1051        }
1052
1053        return (content == null) ? null : content.getValueObject();
1054    }
1055    
1056    
1057    /**
1058     * This method fetches the root content for a particular repository within a transaction.
1059     * If there is no such content we create one as all repositories need one to work.
1060     */

1061            
1062    public Content getRootContent(Database db, Integer JavaDoc repositoryId, String JavaDoc userName, boolean createIfNonExisting) throws ConstraintException, SystemException, Exception JavaDoc
1063    {
1064        Content content = null;
1065
1066        logger.info("Fetching the root content for the repository " + repositoryId);
1067        OQLQuery oql = db.getOQLQuery( "SELECT c FROM org.infoglue.cms.entities.content.impl.simple.ContentImpl c WHERE is_undefined(c.parentContent) AND c.repository.repositoryId = $1");
1068        oql.bind(repositoryId);
1069            
1070        QueryResults results = oql.execute(Database.ReadOnly);
1071        if (results.hasMore())
1072        {
1073            content = (Content)results.next();
1074        }
1075        else
1076        {
1077            if(createIfNonExisting)
1078            {
1079                //None found - we create it and give it the name of the repository.
1080
logger.info("Found no rootContent so we create a new....");
1081                ContentVO rootContentVO = new ContentVO();
1082                RepositoryVO repositoryVO = RepositoryController.getController().getRepositoryVOWithId(repositoryId);
1083                rootContentVO.setCreatorName(userName);
1084                rootContentVO.setName(repositoryVO.getName());
1085                rootContentVO.setIsBranch(new Boolean JavaDoc(true));
1086                content = create(db, null, null, repositoryId, rootContentVO);
1087            }
1088        }
1089        
1090        results.close();
1091        oql.close();
1092        
1093        return content;
1094    }
1095
1096    
1097    /**
1098     * This method fetches the root content for a particular repository.
1099     * If there is no such content we create one as all repositories need one to work.
1100     */

1101            
1102    public Content getRootContent(Integer JavaDoc repositoryId, Database db) throws ConstraintException, SystemException, Exception JavaDoc
1103    {
1104        Content content = null;
1105
1106        OQLQuery oql = db.getOQLQuery( "SELECT c FROM org.infoglue.cms.entities.content.impl.simple.ContentImpl c WHERE is_undefined(c.parentContent) AND c.repository.repositoryId = $1");
1107        oql.bind(repositoryId);
1108            
1109        QueryResults results = oql.execute();
1110        this.logger.info("Fetching entity in read/write mode" + repositoryId);
1111
1112        if (results.hasMore())
1113        {
1114            content = (Content)results.next();
1115        }
1116
1117        results.close();
1118        oql.close();
1119
1120        return content;
1121    }
1122    
1123    /**
1124     * This method returns a list of the children a content has.
1125     */

1126    
1127    public List JavaDoc getContentChildrenVOList(Integer JavaDoc parentContentId) throws ConstraintException, SystemException
1128    {
1129        String JavaDoc key = "" + parentContentId;
1130        logger.info("key:" + key);
1131        List JavaDoc cachedChildContentVOList = (List JavaDoc)CacheController.getCachedObject("childContentCache", key);
1132        if(cachedChildContentVOList != null)
1133        {
1134            logger.info("There was an cached childContentVOList:" + cachedChildContentVOList.size());
1135            return cachedChildContentVOList;
1136        }
1137        
1138        Database db = CastorDatabaseService.getDatabase();
1139        ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
1140
1141        List JavaDoc childrenVOList = null;
1142
1143        beginTransaction(db);
1144
1145        try
1146        {
1147            Content content = getContentWithId(parentContentId, db);
1148            Collection JavaDoc children = content.getChildren();
1149            childrenVOList = ContentController.toVOList(children);
1150            
1151            //If any of the validations or setMethods reported an error, we throw them up now before create.
1152
ceb.throwIfNotEmpty();
1153            
1154            commitTransaction(db);
1155        }
1156        catch(ConstraintException ce)
1157        {
1158            logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
1159            rollbackTransaction(db);
1160            throw ce;
1161        }
1162        catch(Exception JavaDoc e)
1163        {
1164            logger.error("An error occurred so we should not complete the transaction:" + e, e);
1165            rollbackTransaction(db);
1166            throw new SystemException(e.getMessage());
1167        }
1168        
1169        CacheController.cacheObject("childContentCache", key, childrenVOList);
1170        
1171        return childrenVOList;
1172    }
1173    
1174    /**
1175     * This method returns the contentTypeDefinitionVO which is associated with this content.
1176     */

1177    
1178    public ContentTypeDefinitionVO getContentTypeDefinition(Integer JavaDoc contentId) throws ConstraintException, SystemException
1179    {
1180        Database db = CastorDatabaseService.getDatabase();
1181        ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
1182        
1183        ContentTypeDefinitionVO contentTypeDefinitionVO = null;
1184        
1185        beginTransaction(db);
1186
1187        try
1188        {
1189            Content content = getContentWithId(contentId, db);
1190            if(content != null && content.getContentTypeDefinition() != null)
1191                contentTypeDefinitionVO = content.getContentTypeDefinition().getValueObject();
1192    
1193            //If any of the validations or setMethods reported an error, we throw them up now before create.
1194
ceb.throwIfNotEmpty();
1195            
1196            commitTransaction(db);
1197        }
1198        catch(ConstraintException ce)
1199        {
1200            logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
1201            rollbackTransaction(db);
1202            throw ce;
1203        }
1204        catch(Exception JavaDoc e)
1205        {
1206            logger.error("An error occurred so we should not complete the transaction:" + e, e);
1207            rollbackTransaction(db);
1208            throw new SystemException(e.getMessage());
1209        }
1210        
1211        return contentTypeDefinitionVO;
1212    }
1213
1214    /**
1215     * This method reurns a list of available languages for this content.
1216     */

1217    
1218    public List JavaDoc getRepositoryLanguages(Integer JavaDoc contentId) throws ConstraintException, SystemException
1219    {
1220        Database db = CastorDatabaseService.getDatabase();
1221        ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
1222
1223        List JavaDoc languages = null;
1224        
1225        beginTransaction(db);
1226
1227        try
1228        {
1229            languages = getAvailableLanguagesForContentWithId(contentId, db);
1230            
1231            //If any of the validations or setMethods reported an error, we throw them up now before create.
1232
ceb.throwIfNotEmpty();
1233            
1234            commitTransaction(db);
1235        }
1236        catch(ConstraintException ce)
1237        {
1238            logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
1239            rollbackTransaction(db);
1240            throw ce;
1241        }
1242        catch(Exception JavaDoc e)
1243        {
1244            logger.error("An error occurred so we should not complete the transaction:" + e, e);
1245            rollbackTransaction(db);
1246            throw new SystemException(e.getMessage());
1247        }
1248
1249        return languages;
1250    }
1251
1252    
1253    /**
1254     * This method returns the bound contents based on a servicebinding.
1255     */

1256    
1257    public static List JavaDoc getBoundContents(Integer JavaDoc serviceBindingId) throws SystemException, Exception JavaDoc
1258    {
1259        List JavaDoc result = new ArrayList JavaDoc();
1260        
1261        Database db = CastorDatabaseService.getDatabase();
1262
1263        beginTransaction(db);
1264        
1265        try
1266        {
1267            result = getBoundContents(db, serviceBindingId);
1268            
1269            commitTransaction(db);
1270        }
1271        catch(Exception JavaDoc e)
1272        {
1273            logger.error("An error occurred so we should not complete the transaction:" + e, e);
1274            rollbackTransaction(db);
1275            throw new SystemException(e.getMessage());
1276        }
1277        
1278        return result;
1279    }
1280
1281
1282    /**
1283     * This method returns the bound contents based on a servicebinding.
1284     */

1285    
1286    public static List JavaDoc getBoundContents(Database db, Integer JavaDoc serviceBindingId) throws SystemException, Exception JavaDoc
1287    {
1288        List JavaDoc result = new ArrayList JavaDoc();
1289        
1290        ServiceBinding serviceBinding = ServiceBindingController.getServiceBindingWithId(serviceBindingId, db);
1291        
1292        if(serviceBinding != null)
1293        {
1294            ServiceDefinition serviceDefinition = serviceBinding.getServiceDefinition();
1295            if(serviceDefinition != null)
1296            {
1297                String JavaDoc serviceClassName = serviceDefinition.getClassName();
1298                BaseService service = (BaseService)Class.forName(serviceClassName).newInstance();
1299                 
1300                HashMap JavaDoc arguments = new HashMap JavaDoc();
1301                arguments.put("method", "selectContentListOnIdList");
1302                    
1303                List JavaDoc qualifyerList = new ArrayList JavaDoc();
1304                Collection JavaDoc qualifyers = serviceBinding.getBindingQualifyers();
1305
1306                qualifyers = sortQualifyers(qualifyers);
1307
1308                Iterator JavaDoc iterator = qualifyers.iterator();
1309                while(iterator.hasNext())
1310                {
1311                    Qualifyer qualifyer = (Qualifyer)iterator.next();
1312                    HashMap JavaDoc argument = new HashMap JavaDoc();
1313                    argument.put(qualifyer.getName(), qualifyer.getValue());
1314                    qualifyerList.add(argument);
1315                }
1316                arguments.put("arguments", qualifyerList);
1317                
1318                List JavaDoc contents = service.selectMatchingEntities(arguments);
1319                
1320                if(contents != null)
1321                {
1322                    Iterator JavaDoc i = contents.iterator();
1323                    while(i.hasNext())
1324                    {
1325                        ContentVO candidate = (ContentVO)i.next();
1326                        result.add(candidate);
1327                    }
1328                }
1329            }
1330        }
1331                    
1332        return result;
1333    }
1334
1335    
1336    public static List JavaDoc getInTransactionBoundContents(Database db, Integer JavaDoc serviceBindingId) throws SystemException, Exception JavaDoc
1337    {
1338        List JavaDoc result = new ArrayList JavaDoc();
1339        
1340        ServiceBinding serviceBinding = ServiceBindingController.getServiceBindingWithId(serviceBindingId, db);
1341        
1342        if(serviceBinding != null)
1343        {
1344            ServiceDefinition serviceDefinition = serviceBinding.getServiceDefinition();
1345            if(serviceDefinition != null)
1346            {
1347                String JavaDoc serviceClassName = serviceDefinition.getClassName();
1348                BaseService service = (BaseService)Class.forName(serviceClassName).newInstance();
1349                 
1350                HashMap JavaDoc arguments = new HashMap JavaDoc();
1351                arguments.put("method", "selectContentListOnIdList");
1352                    
1353                List JavaDoc qualifyerList = new ArrayList JavaDoc();
1354                Collection JavaDoc qualifyers = serviceBinding.getBindingQualifyers();
1355
1356                qualifyers = sortQualifyers(qualifyers);
1357
1358                Iterator JavaDoc iterator = qualifyers.iterator();
1359                while(iterator.hasNext())
1360                {
1361                    Qualifyer qualifyer = (Qualifyer)iterator.next();
1362                    HashMap JavaDoc argument = new HashMap JavaDoc();
1363                    argument.put(qualifyer.getName(), qualifyer.getValue());
1364                    qualifyerList.add(argument);
1365                }
1366                arguments.put("arguments", qualifyerList);
1367                
1368                List JavaDoc contents = service.selectMatchingEntities(arguments, db);
1369                
1370                if(contents != null)
1371                {
1372                    Iterator JavaDoc i = contents.iterator();
1373                    while(i.hasNext())
1374                    {
1375                        ContentVO candidate = (ContentVO)i.next();
1376                        result.add(candidate);
1377                    }
1378                }
1379            }
1380        }
1381                    
1382        return result;
1383    }
1384
1385
1386    /**
1387     * This method just sorts the list of qualifyers on sortOrder.
1388     */

1389    
1390    private static List JavaDoc sortQualifyers(Collection JavaDoc qualifyers)
1391    {
1392        List JavaDoc sortedQualifyers = new ArrayList JavaDoc();
1393
1394        try
1395        {
1396            Iterator JavaDoc iterator = qualifyers.iterator();
1397            while(iterator.hasNext())
1398            {
1399                Qualifyer qualifyer = (Qualifyer)iterator.next();
1400                int index = 0;
1401                Iterator JavaDoc sortedListIterator = sortedQualifyers.iterator();
1402                while(sortedListIterator.hasNext())
1403                {
1404                    Qualifyer sortedQualifyer = (Qualifyer)sortedListIterator.next();
1405                    if(sortedQualifyer.getSortOrder().intValue() > qualifyer.getSortOrder().intValue())
1406                    {
1407                        break;
1408                    }
1409                    index++;
1410                }
1411                sortedQualifyers.add(index, qualifyer);
1412                                    
1413            }
1414        }
1415        catch(Exception JavaDoc e)
1416        {
1417            logger.warn("The sorting of qualifyers failed:" + e.getMessage(), e);
1418        }
1419            
1420        return sortedQualifyers;
1421    }
1422 
1423    /**
1424     * This method returns the contents belonging to a certain repository.
1425     */

1426    
1427    public List JavaDoc getRepositoryContents(Integer JavaDoc repositoryId, Database db) throws SystemException, Exception JavaDoc
1428    {
1429        List JavaDoc contents = new ArrayList JavaDoc();
1430        
1431        OQLQuery oql = db.getOQLQuery("SELECT c FROM org.infoglue.cms.entities.content.impl.simple.MediumContentImpl c WHERE c.repositoryId = $1");
1432        oql.bind(repositoryId);
1433        
1434        QueryResults results = oql.execute(Database.ReadOnly);
1435        
1436        while(results.hasMore())
1437        {
1438            MediumContentImpl content = (MediumContentImpl)results.next();
1439            contents.add(content);
1440        }
1441
1442        results.close();
1443        oql.close();
1444
1445        return contents;
1446    }
1447    
1448    /**
1449     * Returns the content belonging to the specified repository and with the specified path.
1450     * Note! If a folder contains more than one child with a requested name, then one of the children
1451     * will be used (non-deterministic).
1452     *
1453     * Example:
1454     * If we have the following repository (id=100):
1455     * <root id="1">
1456     * <news id="2">
1457     * <imported id="3">
1458     * <calendar id="4">
1459     * then:
1460     * getContentVOWithPath(100, "", true, db) => returns content "1"
1461     * getContentVOWithPath(100, "news", true, db) => returns content "2"
1462     * getContentVOWithPath(100, "news/imported", true, db) => returns content "3"
1463     * getContentVOWithPath(100, "news/other", true, db) => will create a new content with the name "other" with content "2" as parent
1464     * getContentVOWithPath(100, "news/other", false, db) => will throw an exception
1465     *
1466     * @param repositoryId the repository identifier
1467     * @param path the path of the content starting from the root of the repository
1468     * @param forceFolders if true then non-existing folders will be created; otherwise an exception will be thrown
1469     * @param db the database to use
1470     */

1471    public ContentVO getContentVOWithPath(Integer JavaDoc repositoryId, String JavaDoc path, boolean forceFolders, InfoGluePrincipal creator, Database db) throws SystemException, Exception JavaDoc
1472    {
1473        Content content = getRootContent(repositoryId, db);
1474        final String JavaDoc paths[] = path.split("/");
1475        for(int i=0; i<paths.length; ++i) {
1476            final String JavaDoc name = paths[i];
1477            final Content childContent = getChildWithName(content, name);
1478            if(childContent != null)
1479                content = childContent;
1480            else if(childContent == null && !forceFolders)
1481                throw new SystemException("There exists no content with the path [" + path + "].");
1482            else
1483            {
1484                logger.info(" CREATE " + name);
1485                ContentVO contentVO = new ContentVO();
1486                contentVO.setIsBranch(Boolean.TRUE);
1487                contentVO.setCreatorName(creator.getName());
1488                contentVO.setName(name);
1489                content = create(db, content.getId(), null, repositoryId, contentVO);
1490            }
1491        }
1492        return content.getValueObject();
1493    }
1494    
1495    /**
1496     *
1497     */

1498    private Content getChildWithName(Content content, String JavaDoc name)
1499    {
1500        for(Iterator JavaDoc i=content.getChildren().iterator(); i.hasNext(); )
1501        {
1502            final Content childContent = (Content) i.next();
1503            if(childContent.getName().equals(name))
1504                return childContent;
1505        }
1506        return null;
1507    }
1508    
1509    
1510    /**
1511     * Recursive methods to get all contentVersions of a given state under the specified parent content.
1512     */

1513    
1514    public List JavaDoc getContentVOWithParentRecursive(Integer JavaDoc contentId) throws ConstraintException, SystemException
1515    {
1516        return getContentVOWithParentRecursive(contentId, new ArrayList JavaDoc());
1517    }
1518    
1519    private List JavaDoc getContentVOWithParentRecursive(Integer JavaDoc contentId, List JavaDoc resultList) throws ConstraintException, SystemException
1520    {
1521        // Get the versions of this content.
1522
resultList.add(getContentVOWithId(contentId));
1523        
1524        // Get the children of this content and do the recursion
1525
List JavaDoc childContentList = ContentController.getContentController().getContentChildrenVOList(contentId);
1526        Iterator JavaDoc cit = childContentList.iterator();
1527        while (cit.hasNext())
1528        {
1529            ContentVO contentVO = (ContentVO) cit.next();
1530            getContentVOWithParentRecursive(contentVO.getId(), resultList);
1531        }
1532    
1533        return resultList;
1534    }
1535
1536    /**
1537     * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
1538     * is handling.
1539     */

1540
1541    public BaseEntityVO getNewVO()
1542    {
1543        return new ContentVO();
1544    }
1545
1546 
1547}
1548
Popular Tags