KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.StringReader JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.apache.xerces.parsers.DOMParser;
37 import org.exolab.castor.jdo.Database;
38 import org.exolab.castor.jdo.OQLQuery;
39 import org.exolab.castor.jdo.QueryResults;
40 import org.infoglue.cms.applications.common.VisualFormatter;
41 import org.infoglue.cms.entities.content.Content;
42 import org.infoglue.cms.entities.content.ContentVO;
43 import org.infoglue.cms.entities.content.ContentVersion;
44 import org.infoglue.cms.entities.content.ContentVersionVO;
45 import org.infoglue.cms.entities.content.DigitalAsset;
46 import org.infoglue.cms.entities.content.DigitalAssetVO;
47 import org.infoglue.cms.entities.content.impl.simple.ContentImpl;
48 import org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl;
49 import org.infoglue.cms.entities.kernel.BaseEntityVO;
50 import org.infoglue.cms.entities.management.ContentTypeDefinition;
51 import org.infoglue.cms.entities.management.Language;
52 import org.infoglue.cms.entities.management.RegistryVO;
53 import org.infoglue.cms.entities.management.impl.simple.LanguageImpl;
54 import org.infoglue.cms.entities.structure.SiteNode;
55 import org.infoglue.cms.entities.structure.SiteNodeVersion;
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.util.ConstraintExceptionBuffer;
61 import org.w3c.dom.CDATASection JavaDoc;
62 import org.w3c.dom.Document JavaDoc;
63 import org.w3c.dom.Node JavaDoc;
64 import org.w3c.dom.NodeList JavaDoc;
65 import org.xml.sax.InputSource JavaDoc;
66
67 /**
68  * @author Mattias Bogeblad
69  *
70  */

71
72 public class ContentVersionController extends BaseController
73 {
74     private final static Logger logger = Logger.getLogger(ContentVersionController.class.getName());
75
76     private static final ContentCategoryController contentCategoryController = ContentCategoryController.getController();
77     private final RegistryController registryController = RegistryController.getController();
78
79     /**
80      * Factory method to get object
81      */

82     
83     public static ContentVersionController getContentVersionController()
84     {
85         return new ContentVersionController();
86     }
87     
88     private static Map JavaDoc contentMap = Collections.synchronizedMap(new HashMap JavaDoc());
89
90     public Integer JavaDoc getContentIdForContentVersion(Integer JavaDoc contentVersionId) throws SystemException, Bug
91     {
92         Integer JavaDoc contentId = (Integer JavaDoc)contentMap.get(contentVersionId);
93         if(contentId == null)
94         {
95             ContentVersionVO ContentVersionVO = (ContentVersionVO) getVOWithId(ContentVersionImpl.class, contentVersionId);
96             contentId = ContentVersionVO.getContentId();
97             contentMap.put(contentVersionId, contentId);
98         }
99         
100         return contentId;
101     }
102
103     public Integer JavaDoc getContentIdForContentVersion(Integer JavaDoc contentVersionId, Database db) throws SystemException, Bug
104     {
105         Integer JavaDoc contentId = (Integer JavaDoc)contentMap.get(contentVersionId);
106         if(contentId == null)
107         {
108             System.out.println("Not cached the first time:" + contentVersionId);
109             ContentVersionVO ContentVersionVO = (ContentVersionVO) getVOWithId(ContentVersionImpl.class, contentVersionId, db);
110             contentId = ContentVersionVO.getContentId();
111             contentMap.put(contentVersionId, contentId);
112         }
113         
114         return contentId;
115     }
116
117     public ContentVersionVO getContentVersionVOWithId(Integer JavaDoc contentVersionId) throws SystemException, Bug
118     {
119         return (ContentVersionVO) getVOWithId(ContentVersionImpl.class, contentVersionId);
120     }
121
122     public ContentVersion getContentVersionWithId(Integer JavaDoc contentVersionId, Database db) throws SystemException, Bug
123     {
124         return (ContentVersion) getObjectWithId(ContentVersionImpl.class, contentVersionId, db);
125     }
126
127     public ContentVersion getReadOnlyContentVersionWithId(Integer JavaDoc contentVersionId, Database db) throws SystemException, Bug
128     {
129         return (ContentVersion) getObjectWithIdAsReadOnly(ContentVersionImpl.class, contentVersionId, db);
130     }
131
132     public List JavaDoc getContentVersionVOList() throws SystemException, Bug
133     {
134         return getAllVOObjects(ContentVersionImpl.class, "contentVersionId");
135     }
136
137     /**
138      * Recursive methods to get all contentVersions of a given state under the specified parent content.
139      */

140     
141     public List JavaDoc getContentVersionVOWithParentRecursive(Integer JavaDoc contentId, Integer JavaDoc stateId, boolean mustBeFirst) throws ConstraintException, SystemException
142     {
143         return getContentVersionVOWithParentRecursive(contentId, stateId, new ArrayList JavaDoc(), mustBeFirst);
144     }
145     
146     private List JavaDoc getContentVersionVOWithParentRecursive(Integer JavaDoc contentId, Integer JavaDoc stateId, List JavaDoc resultList, boolean mustBeFirst) throws ConstraintException, SystemException
147     {
148         // Get the versions of this content.
149
resultList.addAll(getLatestContentVersionVOWithParent(contentId, stateId, mustBeFirst));
150         // Get the children of this content and do the recursion
151
List JavaDoc childContentList = ContentController.getContentController().getContentChildrenVOList(contentId);
152         Iterator JavaDoc cit = childContentList.iterator();
153         while (cit.hasNext())
154         {
155             ContentVO contentVO = (ContentVO) cit.next();
156             getContentVersionVOWithParentRecursive(contentVO.getId(), stateId, resultList, mustBeFirst);
157         }
158     
159         return resultList;
160     }
161
162     
163     /**
164      * Recursive methods to get all contentVersions of a given state under the specified parent content.
165      */

166     
167     public List JavaDoc getContentVersionVOWithParentRecursiveAndRelated(Integer JavaDoc contentId, Integer JavaDoc stateId, boolean mustBeFirst) throws ConstraintException, SystemException
168     {
169         List JavaDoc contentVersionVOList = new ArrayList JavaDoc();
170         
171         Database db = CastorDatabaseService.getDatabase();
172
173         beginTransaction(db);
174
175         try
176         {
177             List JavaDoc contentVersionList = getContentVersionWithParentRecursiveAndRelated(contentId, stateId, new ArrayList JavaDoc(), new ArrayList JavaDoc(), db, mustBeFirst);
178             contentVersionVOList = toVOList(contentVersionList);
179             
180             commitTransaction(db);
181         }
182         catch(Exception JavaDoc e)
183         {
184             logger.error("An error occurred so we should not completes the transaction:" + e, e);
185             rollbackTransaction(db);
186             throw new SystemException(e.getMessage());
187         }
188         
189         return contentVersionVOList;
190     }
191     
192     private List JavaDoc getContentVersionWithParentRecursiveAndRelated(Integer JavaDoc contentId, Integer JavaDoc stateId, List JavaDoc resultList, List JavaDoc checkedContents, Database db, boolean mustBeFirst) throws ConstraintException, SystemException, Exception JavaDoc
193     {
194         checkedContents.add(contentId);
195         
196         // Get the versions of this content.
197
List JavaDoc contentVersions = getLatestContentVersionWithParent(contentId, stateId, db, mustBeFirst);
198         resultList.addAll(contentVersions);
199         
200         Iterator JavaDoc contentVersionsIterator = contentVersions.iterator();
201         while(contentVersionsIterator.hasNext())
202         {
203             ContentVersion contentVersion = (ContentVersion)contentVersionsIterator.next();
204             List JavaDoc relatedEntities = RegistryController.getController().getMatchingRegistryVOListForReferencingEntity(ContentVersion.class.getName(), contentVersion.getId().toString(), db);
205             Iterator JavaDoc relatedEntitiesIterator = relatedEntities.iterator();
206             
207             while(relatedEntitiesIterator.hasNext())
208             {
209                 RegistryVO registryVO = (RegistryVO)relatedEntitiesIterator.next();
210                 logger.info("registryVO:" + registryVO.getEntityName() + ":" + registryVO.getEntityId());
211                 if(registryVO.getEntityName().equals(Content.class.getName()) && !checkedContents.contains(new Integer JavaDoc(registryVO.getEntityId())))
212                 {
213                     List JavaDoc relatedContentVersions = getLatestContentVersionWithParent(new Integer JavaDoc(registryVO.getEntityId()), stateId, db, mustBeFirst);
214                     resultList.addAll(relatedContentVersions);
215                     checkedContents.add(new Integer JavaDoc(registryVO.getEntityId()));
216                 }
217             }
218         }
219         
220         
221         // Get the children of this content and do the recursion
222
List JavaDoc childContentList = ContentController.getContentController().getContentChildrenVOList(contentId);
223         Iterator JavaDoc cit = childContentList.iterator();
224         while (cit.hasNext())
225         {
226             ContentVO contentVO = (ContentVO) cit.next();
227             getContentVersionWithParentRecursiveAndRelated(contentVO.getId(), stateId, resultList, checkedContents, db, mustBeFirst);
228         }
229         
230         return resultList;
231     }
232
233     
234     public List JavaDoc getContentVersionVOWithParent(Integer JavaDoc contentId) throws SystemException, Bug
235     {
236         List JavaDoc resultList = new ArrayList JavaDoc();
237         Database db = CastorDatabaseService.getDatabase();
238         ContentVersionVO contentVersionVO = null;
239
240         beginTransaction(db);
241
242         try
243         {
244             List JavaDoc contentVersions = getContentVersionWithParent(contentId, db);
245             resultList = toVOList(contentVersions);
246             /*
247             OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 ORDER BY cv.contentVersionId desc");
248             oql.bind(contentId);
249             
250             QueryResults results = oql.execute(Database.ReadOnly);
251             
252             while (results.hasMore())
253             {
254                 ContentVersion contentVersion = (ContentVersion)results.next();
255                 logger.info("found one:" + contentVersion.getValueObject());
256                 contentVersionVO = contentVersion.getValueObject();
257                 resultList.add(contentVersionVO);
258             }
259             */

260             
261             commitTransaction(db);
262         }
263         catch(Exception JavaDoc e)
264         {
265             logger.error("An error occurred so we should not completes the transaction:" + e, e);
266             rollbackTransaction(db);
267             throw new SystemException(e.getMessage());
268         }
269         
270         return resultList;
271     }
272
273     public List JavaDoc getContentVersionWithParent(Integer JavaDoc contentId, Database db) throws SystemException, Bug, Exception JavaDoc
274     {
275         ArrayList JavaDoc resultList = new ArrayList JavaDoc();
276         ContentVersionVO contentVersionVO = null;
277
278         OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 ORDER BY cv.contentVersionId desc");
279         oql.bind(contentId);
280         
281         QueryResults results = oql.execute(Database.ReadOnly);
282         
283         while (results.hasMore())
284         {
285             ContentVersion contentVersion = (ContentVersion)results.next();
286             resultList.add(contentVersion);
287         }
288         
289         results.close();
290         oql.close();
291
292         return resultList;
293     }
294     
295     /**
296      * This method returns a list of active contentversions, and only one / language in the specified state
297      *
298      * @param contentId The content to look for versions in
299      * @param stateId The state of the versions
300      * @return A list of the latest versions matching the given state
301      * @throws SystemException
302      * @throws Bug
303      */

304
305     public List JavaDoc getLatestContentVersionVOWithParent(Integer JavaDoc contentId, Integer JavaDoc stateId, boolean mustBeFirst) throws SystemException, Bug
306     {
307         List JavaDoc resultList = new ArrayList JavaDoc();
308         
309         Database db = CastorDatabaseService.getDatabase();
310         
311         beginTransaction(db);
312
313         try
314         {
315             resultList = getLatestContentVersionWithParent(contentId, stateId, db, mustBeFirst);
316             resultList = toVOList(resultList);
317             
318             commitTransaction(db);
319         }
320         catch(Exception JavaDoc e)
321         {
322             logger.error("An error occurred so we should not completes the transaction:" + e, e);
323             rollbackTransaction(db);
324             throw new SystemException(e.getMessage());
325         }
326         
327         return resultList;
328     }
329     
330     /**
331      * This method returns a list of active contentversions, and only one / language in the specified state
332      *
333      * @param contentId The content to look for versions in
334      * @param stateId The state of the versions
335      * @return A list of the latest versions matching the given state
336      * @throws SystemException
337      * @throws Bug
338      */

339
340     public List JavaDoc getLatestContentVersionWithParent(Integer JavaDoc contentId, Integer JavaDoc stateId, Database db, boolean mustBeFirst) throws SystemException, Bug, Exception JavaDoc
341     {
342         ArrayList JavaDoc resultList = new ArrayList JavaDoc();
343         ArrayList JavaDoc langCheck = new ArrayList JavaDoc();
344         
345         OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 ORDER BY cv.contentVersionId desc");
346         oql.bind(contentId);
347         // oql.bind(stateId);
348

349         QueryResults results = oql.execute(Database.ReadOnly);
350         
351         // New improved
352
while (results.hasMore())
353         {
354             ContentVersion contentVersion = (ContentVersion)results.next();
355             logger.info("contentVersion:" + contentVersion.getValueObject().getContentName());
356             if(contentVersion.getIsActive().booleanValue())
357             {
358                 if ( (contentVersion.getStateId().compareTo(stateId)==0) && (!langCheck.contains(contentVersion.getLanguage().getLanguageId())))
359                 {
360                     logger.info("Added contentVersion:" + contentVersion.getValueObject().getContentName() + ":" + contentVersion.getId() + ":" + contentVersion.getIsActive() + ":" + contentVersion.getStateId());
361                     resultList.add(contentVersion);
362                     langCheck.add(contentVersion.getLanguage().getLanguageId());
363                 }
364                 
365                 if(mustBeFirst)
366                     langCheck.add(contentVersion.getLanguage().getLanguageId());
367             }
368         }
369         
370         results.close();
371         oql.close();
372
373         logger.info("getLatestContentVersionWithParent done...");
374         
375         return resultList;
376     }
377    
378     
379     /**
380      * This method returns the latest contentVersion there is for the given content if it is active and is the latest made.
381      */

382     
383     public List JavaDoc getLatestActiveContentVersionIfInState(Content content, Integer JavaDoc stateId, Database db) throws SystemException, Exception JavaDoc
384     {
385         List JavaDoc resultList = new ArrayList JavaDoc();
386         Map JavaDoc lastLanguageVersions = new HashMap JavaDoc();
387         Map JavaDoc languageVersions = new HashMap JavaDoc();
388         
389         Collection JavaDoc contentVersions = content.getContentVersions();
390         
391         Iterator JavaDoc versionIterator = contentVersions.iterator();
392         while(versionIterator.hasNext())
393         {
394             ContentVersion contentVersionCandidate = (ContentVersion)versionIterator.next();
395             
396             ContentVersion lastVersionInThatLanguage = (ContentVersion)lastLanguageVersions.get(contentVersionCandidate.getLanguage().getId());
397             if(lastVersionInThatLanguage == null || (lastVersionInThatLanguage.getId().intValue() < contentVersionCandidate.getId().intValue() && contentVersionCandidate.getIsActive().booleanValue()))
398                 lastLanguageVersions.put(contentVersionCandidate.getLanguage().getId(), contentVersionCandidate);
399             
400             if(contentVersionCandidate.getIsActive().booleanValue() && contentVersionCandidate.getStateId().intValue() == stateId.intValue())
401             {
402                 if(contentVersionCandidate.getOwningContent().getContentId().intValue() == content.getId().intValue())
403                 {
404                     ContentVersion versionInThatLanguage = (ContentVersion)languageVersions.get(contentVersionCandidate.getLanguage().getId());
405                     if(versionInThatLanguage == null || versionInThatLanguage.getContentVersionId().intValue() < contentVersionCandidate.getId().intValue())
406                     {
407                         languageVersions.put(contentVersionCandidate.getLanguage().getId(), contentVersionCandidate);
408                     }
409                 }
410             }
411         }
412
413         logger.info("Found languageVersions:" + languageVersions.size());
414         logger.info("Found lastLanguageVersions:" + lastLanguageVersions.size());
415         Iterator JavaDoc i = languageVersions.values().iterator();
416         while(i.hasNext())
417         {
418             ContentVersion contentVersion = (ContentVersion)i.next();
419             ContentVersion lastVersionInThatLanguage = (ContentVersion)lastLanguageVersions.get(contentVersion.getLanguage().getId());
420
421             logger.info("contentVersion:" + contentVersion.getId());
422             logger.info("lastVersionInThatLanguage:" + lastVersionInThatLanguage.getId());
423
424             if(contentVersion == lastVersionInThatLanguage)
425                 resultList.add(contentVersion);
426         }
427         
428         return resultList;
429     }
430     
431     
432     /**
433      * This method returns the latest active content version.
434      */

435     
436     public ContentVersionVO getLatestActiveContentVersionVO(Integer JavaDoc contentId, Integer JavaDoc languageId) throws SystemException, Bug
437     {
438         Database db = CastorDatabaseService.getDatabase();
439         ContentVersionVO contentVersionVO = null;
440
441         beginTransaction(db);
442
443         try
444         {
445             ContentVersion contentVersion = null;
446             
447             contentVersion = getLatestActiveContentVersion(contentId, languageId, db);
448             /*
449             Collection contentVersions = content.getContentVersions();
450             
451             Iterator i = contentVersions.iterator();
452             
453             while(i.hasNext())
454             {
455                 ContentVersion currentContentVersion = (ContentVersion)i.next();
456                 logger.info("found one candidate:" + currentContentVersion.getValueObject());
457                 if(contentVersion == null || (currentContentVersion.getId().intValue() > contentVersion.getId().intValue()))
458                 {
459                     if(currentContentVersion.getIsActive().booleanValue() && currentContentVersion.getLanguage().getId().intValue() == languageId.intValue())
460                         contentVersion = currentContentVersion;
461                 }
462             }
463             */

464             
465             if(contentVersion != null)
466                 contentVersionVO = contentVersion.getValueObject();
467             
468             commitTransaction(db);
469         }
470         catch(Exception JavaDoc e)
471         {
472             logger.error("An error occurred so we should not completes the transaction:" + e, e);
473             rollbackTransaction(db);
474             throw new SystemException(e.getMessage());
475         }
476         
477         return contentVersionVO;
478     }
479
480     /**
481      * This method returns the latest active content version.
482      */

483     
484     public ContentVersionVO getLatestActiveContentVersionVO(Integer JavaDoc contentId, Integer JavaDoc languageId, Database db) throws SystemException, Bug
485     {
486         ContentVersionVO contentVersionVO = null;
487
488         ContentVersion contentVersion = getLatestActiveContentVersion(contentId, languageId, db);
489             
490         if(contentVersion != null)
491             contentVersionVO = contentVersion.getValueObject();
492         
493         return contentVersionVO;
494     }
495
496     /**
497      * This method returns the latest active content version.
498      */

499     
500     public ContentVersion getLatestActiveContentVersion(Integer JavaDoc contentId, Integer JavaDoc languageId, Database db) throws SystemException, Bug
501     {
502         ContentVersion contentVersion = null;
503         
504         Content content = ContentController.getContentController().getContentWithId(contentId, db);
505         logger.info("contentId:" + contentId);
506         logger.info("languageId:" + languageId);
507         logger.info("content:" + content.getName());
508         Collection JavaDoc contentVersions = content.getContentVersions();
509         logger.info("contentVersions:" + contentVersions.size());
510         
511         Iterator JavaDoc i = contentVersions.iterator();
512         while(i.hasNext())
513         {
514             ContentVersion currentContentVersion = (ContentVersion)i.next();
515             logger.info("found one candidate:" + currentContentVersion.getValueObject());
516             if(contentVersion == null || (currentContentVersion.getId().intValue() > contentVersion.getId().intValue()))
517             {
518                 logger.info("currentContentVersion:" + currentContentVersion.getIsActive());
519                 logger.info("currentContentVersion:" + currentContentVersion.getLanguage().getId());
520                 if(currentContentVersion.getIsActive().booleanValue() && currentContentVersion.getLanguage().getId().intValue() == languageId.intValue())
521                     contentVersion = currentContentVersion;
522             }
523         }
524         
525         return contentVersion;
526     }
527     
528
529     public ContentVersionVO getLatestContentVersionVO(Integer JavaDoc contentId, Integer JavaDoc languageId) throws SystemException, Bug
530     {
531         Database db = CastorDatabaseService.getDatabase();
532         ContentVersionVO contentVersionVO = null;
533
534         beginTransaction(db);
535
536         try
537         {
538            
539             OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 ORDER BY cv.contentVersionId desc");
540             oql.bind(contentId);
541             oql.bind(languageId);
542             
543             QueryResults results = oql.execute(Database.ReadOnly);
544             
545             if (results.hasMore())
546             {
547                 ContentVersion contentVersion = (ContentVersion)results.next();
548                 logger.info("found one:" + contentVersion.getValueObject());
549                 contentVersionVO = contentVersion.getValueObject();
550             }
551             
552             results.close();
553             oql.close();
554
555             commitTransaction(db);
556         }
557         catch(Exception JavaDoc e)
558         {
559             logger.error("An error occurred so we should not completes the transaction:" + e, e);
560             rollbackTransaction(db);
561             throw new SystemException(e.getMessage());
562         }
563         
564         
565         return contentVersionVO;
566     }
567
568
569     public ContentVersion getContentVersionWithId(Integer JavaDoc contentVersionId) throws SystemException, Bug
570     {
571         Database db = CastorDatabaseService.getDatabase();
572         ContentVersion contentVersion = null;
573
574         beginTransaction(db);
575
576         try
577         {
578             contentVersion = getContentVersionWithId(contentVersionId, db);
579             
580             commitTransaction(db);
581         }
582         catch(Exception JavaDoc e)
583         {
584             logger.error("An error occurred so we should not completes the transaction:" + e, e);
585             rollbackTransaction(db);
586             throw new SystemException(e.getMessage());
587         }
588         
589         return contentVersion;
590     }
591
592
593     public ContentVersion getLatestContentVersion(Integer JavaDoc contentId, Integer JavaDoc languageId, Database db) throws SystemException, Bug, Exception JavaDoc
594     {
595         ContentVersion contentVersion = null;
596         
597         OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 ORDER BY cv.contentVersionId desc");
598         oql.bind(contentId);
599         oql.bind(languageId);
600         
601         QueryResults results = oql.execute(Database.ReadOnly);
602         
603         if (results.hasMore())
604         {
605             contentVersion = (ContentVersion)results.next();
606         }
607         
608         results.close();
609         oql.close();
610         
611         return contentVersion;
612     }
613
614     
615     /**
616      * This method created a new contentVersion in the database.
617      */

618     
619     public ContentVersionVO create(Integer JavaDoc contentId, Integer JavaDoc languageId, ContentVersionVO contentVersionVO, Integer JavaDoc oldContentVersionId) throws ConstraintException, SystemException
620     {
621         Database db = CastorDatabaseService.getDatabase();
622         ContentVersion contentVersion = null;
623
624         beginTransaction(db);
625         try
626         {
627             contentVersion = create(contentId, languageId, contentVersionVO, oldContentVersionId, db);
628             commitTransaction(db);
629         }
630         catch(Exception JavaDoc e)
631         {
632             logger.error("An error occurred so we should not completes the transaction:" + e, e);
633             rollbackTransaction(db);
634             throw new SystemException(e.getMessage());
635         }
636         
637         return contentVersion.getValueObject();
638     }
639
640     /**
641      * This method created a new contentVersion in the database. It also updates the owning content
642      * so it recognises the change.
643      */

644     
645     public ContentVersion create(Integer JavaDoc contentId, Integer JavaDoc languageId, ContentVersionVO contentVersionVO, Integer JavaDoc oldContentVersionId, Database db) throws ConstraintException, SystemException, Exception JavaDoc
646     {
647         Content content = ContentController.getContentController().getContentWithId(contentId, db);
648         Language language = LanguageController.getController().getLanguageWithId(languageId, db);
649         return create(content, language, contentVersionVO, oldContentVersionId, db);
650     }
651     
652     /**
653      * This method created a new contentVersion in the database. It also updates the owning content
654      * so it recognises the change.
655      */

656     
657     public ContentVersion create(Content content, Language language, ContentVersionVO contentVersionVO, Integer JavaDoc oldContentVersionId, Database db) throws ConstraintException, SystemException, Exception JavaDoc
658     {
659         ContentVersion contentVersion = new ContentVersionImpl();
660         contentVersion.setLanguage((LanguageImpl)language);
661         logger.info("Content:" + content.getContentId() + ":" + db.isPersistent(content));
662         contentVersion.setOwningContent((ContentImpl)content);
663         
664         contentVersion.setValueObject(contentVersionVO);
665         db.create(contentVersion);
666
667         content.getContentVersions().add(contentVersion);
668
669         if(oldContentVersionId != null && oldContentVersionId.intValue() != -1)
670             copyDigitalAssets(getContentVersionWithId(oldContentVersionId, db), contentVersion, db);
671             //contentVersion.setDigitalAssets(getContentVersionWithId(oldContentVersionId, db).getDigitalAssets());
672

673         return contentVersion;
674     }
675
676     /**
677      * This method deletes an contentversion and notifies the owning content.
678      */

679     
680     public void delete(ContentVersionVO contentVersionVO) throws ConstraintException, SystemException
681     {
682         Database db = CastorDatabaseService.getDatabase();
683         beginTransaction(db);
684         try
685         {
686             delete(contentVersionVO, db);
687             commitTransaction(db);
688         }
689         catch(Exception JavaDoc e)
690         {
691             logger.error("An error occurred so we should not completes the transaction:" + e, e);
692             rollbackTransaction(db);
693             throw new SystemException(e.getMessage());
694         }
695     }
696     
697     /**
698      * This method deletes an contentversion and notifies the owning content.
699      */

700     
701     public void delete(ContentVersionVO contentVersionVO, Database db) throws ConstraintException, SystemException, Exception JavaDoc
702     {
703         ContentVersion contentVersion = getContentVersionWithId(contentVersionVO.getContentVersionId(), db);
704         delete(contentVersion, db, false);
705     }
706
707     /**
708      * This method deletes an contentversion and notifies the owning content.
709      */

710     
711     public void delete(ContentVersion contentVersion, Database db) throws ConstraintException, SystemException, Exception JavaDoc
712     {
713         delete(contentVersion, db, false);
714     }
715     
716     /**
717      * This method deletes an contentversion and notifies the owning content.
718      */

719     
720     public void delete(ContentVersion contentVersion, Database db, boolean forceDelete) throws ConstraintException, SystemException, Exception JavaDoc
721     {
722         if (!forceDelete && contentVersion.getStateId().intValue() == ContentVersionVO.PUBLISHED_STATE.intValue() && contentVersion.getIsActive().booleanValue() == true)
723             throw new ConstraintException("ContentVersion.stateId", "3300");
724
725         contentCategoryController.deleteByContentVersion(contentVersion, db);
726
727         Content content = contentVersion.getOwningContent();
728
729         if(content != null)
730             content.getContentVersions().remove(contentVersion);
731
732         db.remove(contentVersion);
733     }
734
735
736
737     /**
738      * This method deletes all contentVersions for the content sent in.
739      * The contentVersion is related to digital assets but we don't remove the asset itself in case
740      * other versions or contents reference the same asset.
741      */

742     
743     public void deleteVersionsForContent(Content content, Database db) throws ConstraintException, SystemException, Bug, Exception JavaDoc
744     {
745         deleteVersionsForContent(content, db, false);
746     }
747     
748     /**
749      * This method deletes all contentVersions for the content sent in.
750      * The contentVersion is related to digital assets but we don't remove the asset itself in case
751      * other versions or contents reference the same asset.
752      */

753     
754     public void deleteVersionsForContent(Content content, Database db, boolean forceDelete) throws ConstraintException, SystemException, Bug, Exception JavaDoc
755     {
756         Collection JavaDoc contentVersions = Collections.synchronizedCollection(content.getContentVersions());
757         Iterator JavaDoc contentVersionIterator = contentVersions.iterator();
758             
759         while (contentVersionIterator.hasNext())
760         {
761             ContentVersion contentVersion = (ContentVersion)contentVersionIterator.next();
762             
763             Collection JavaDoc digitalAssetList = contentVersion.getDigitalAssets();
764             Iterator JavaDoc assets = digitalAssetList.iterator();
765             while (assets.hasNext())
766             {
767                 DigitalAsset digitalAsset = (DigitalAsset)assets.next();
768                 assets.remove();
769                 db.remove(digitalAsset);
770             }
771             
772             logger.info("Deleting contentVersion:" + contentVersion.getContentVersionId());
773             contentVersionIterator.remove();
774             delete(contentVersion, db, forceDelete);
775         }
776         content.setContentVersions(new ArrayList JavaDoc());
777     }
778
779     /**
780      * This method deletes a digitalAsset.
781      */

782     
783     public void deleteDigitalAsset(Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc assetKey) throws ConstraintException, SystemException
784     {
785         Database db = CastorDatabaseService.getDatabase();
786         beginTransaction(db);
787         try
788         {
789             ContentVersion contentVersion = this.getLatestActiveContentVersion(contentId, languageId, db);
790             
791             Collection JavaDoc digitalAssets = contentVersion.getDigitalAssets();
792             Iterator JavaDoc assetIterator = digitalAssets.iterator();
793             while(assetIterator.hasNext())
794             {
795                 DigitalAsset currentDigitalAsset = (DigitalAsset)assetIterator.next();
796                 if(currentDigitalAsset.getAssetKey().equals(assetKey))
797                 {
798                     assetIterator.remove();
799                     db.remove(currentDigitalAsset);
800                     break;
801                 }
802             }
803             
804             commitTransaction(db);
805         }
806         catch(Exception JavaDoc e)
807         {
808             logger.error("An error occurred so we should not completes the transaction:" + e, e);
809             rollbackTransaction(db);
810             throw new SystemException(e.getMessage());
811         }
812     }
813     
814     
815     /**
816      * This method updates the contentversion.
817      */

818     
819     public ContentVersionVO update(Integer JavaDoc contentId, Integer JavaDoc languageId, ContentVersionVO contentVersionVO) throws ConstraintException, SystemException
820     {
821         ContentVersionVO updatedContentVersionVO;
822         
823         Database db = CastorDatabaseService.getDatabase();
824
825         beginTransaction(db);
826         
827         try
828         {
829             Content content = ContentController.getContentController().getContentWithId(contentId, db);
830             ContentTypeDefinition contentTypeDefinition = content.getContentTypeDefinition();
831             ConstraintExceptionBuffer ceb = contentVersionVO.validateAdvanced(contentTypeDefinition.getValueObject());
832             ceb.throwIfNotEmpty();
833             
834             ContentVersion contentVersion = null;
835             
836             if(contentVersionVO.getId() == null)
837             {
838                 logger.info("Creating the entity because there was no version at all for: " + contentId + " " + languageId);
839                 contentVersion = create(contentId, languageId, contentVersionVO, null, db);
840             }
841             else
842             {
843                 contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(contentVersionVO.getId(), db);
844                 contentVersion.setValueObject(contentVersionVO);
845             }
846
847             registryController.updateContentVersion(contentVersion, db);
848
849             updatedContentVersionVO = contentVersion.getValueObject();
850             
851             commitTransaction(db);
852         }
853         catch(ConstraintException ce)
854         {
855             logger.error("Validation error:" + ce, ce);
856             rollbackTransaction(db);
857             throw ce;
858         }
859         catch(Exception JavaDoc e)
860         {
861             logger.error("An error occurred so we should not completes the transaction:" + e, e);
862             rollbackTransaction(db);
863             throw new SystemException(e.getMessage());
864         }
865         
866         return updatedContentVersionVO; //(ContentVersionVO) updateEntity(ContentVersionImpl.class, realContentVersionVO);
867
}
868
869
870     public List JavaDoc getPublishedActiveContentVersionVOList(Integer JavaDoc contentId) throws SystemException, Bug, Exception JavaDoc
871     {
872         List JavaDoc contentVersionVOList = new ArrayList JavaDoc();
873         
874         Database db = CastorDatabaseService.getDatabase();
875         beginTransaction(db);
876         try
877         {
878             OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.stateId = $2 AND cv.isActive = $3 ORDER BY cv.contentVersionId desc");
879             oql.bind(contentId);
880             oql.bind(ContentVersionVO.PUBLISHED_STATE);
881             oql.bind(true);
882             
883             QueryResults results = oql.execute(Database.ReadOnly);
884             
885             while (results.hasMore())
886             {
887                 ContentVersion contentVersion = (ContentVersion)results.next();
888                 contentVersionVOList.add(contentVersion.getValueObject());
889             }
890             
891             results.close();
892             oql.close();
893
894             commitTransaction(db);
895         }
896         catch(Exception JavaDoc e)
897         {
898             logger.error("An error occurred so we should not completes the transaction:" + e, e);
899             rollbackTransaction(db);
900             throw new SystemException(e.getMessage());
901         }
902             
903         return contentVersionVOList;
904     }
905
906     
907     public ContentVersion getLatestPublishedContentVersion(Integer JavaDoc contentId) throws SystemException, Bug, Exception JavaDoc
908     {
909         ContentVersion contentVersion = null;
910         
911         Database db = CastorDatabaseService.getDatabase();
912         beginTransaction(db);
913         try
914         {
915             OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.stateId = $2 AND cv.isActive = $3 ORDER BY cv.contentVersionId desc");
916             oql.bind(contentId);
917             oql.bind(ContentVersionVO.PUBLISHED_STATE);
918             oql.bind(true);
919             
920             QueryResults results = oql.execute(Database.ReadOnly);
921             
922             if (results.hasMore())
923             {
924                 contentVersion = (ContentVersion)results.next();
925             }
926
927             results.close();
928             oql.close();
929
930             commitTransaction(db);
931         }
932         catch(Exception JavaDoc e)
933         {
934             logger.error("An error occurred so we should not completes the transaction:" + e, e);
935             rollbackTransaction(db);
936             throw new SystemException(e.getMessage());
937         }
938             
939         return contentVersion;
940     }
941
942
943     public ContentVersion getLatestPublishedContentVersion(Integer JavaDoc contentId, Integer JavaDoc languageId) throws SystemException, Bug, Exception JavaDoc
944     {
945         ContentVersion contentVersion = null;
946         
947         Database db = CastorDatabaseService.getDatabase();
948         beginTransaction(db);
949         try
950         {
951             OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 AND cv.stateId = $3 AND cv.isActive = $4 ORDER BY cv.contentVersionId desc");
952             oql.bind(contentId);
953             oql.bind(languageId);
954             oql.bind(ContentVersionVO.PUBLISHED_STATE);
955             oql.bind(true);
956             
957             QueryResults results = oql.execute(Database.ReadOnly);
958             
959             if (results.hasMore())
960             {
961                 contentVersion = (ContentVersion)results.next();
962             }
963             
964             results.close();
965             oql.close();
966
967             commitTransaction(db);
968         }
969         catch(Exception JavaDoc e)
970         {
971             logger.error("An error occurred so we should not completes the transaction:" + e, e);
972             rollbackTransaction(db);
973             throw new SystemException(e.getMessage());
974         }
975             
976         return contentVersion;
977     }
978
979
980     public ContentVersion getLatestPublishedContentVersion(Integer JavaDoc contentId, Integer JavaDoc languageId, Database db) throws SystemException, Bug, Exception JavaDoc
981     {
982         ContentVersion contentVersion = null;
983         
984         OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 AND cv.stateId = $3 AND cv.isActive = $4 ORDER BY cv.contentVersionId desc");
985         oql.bind(contentId);
986         oql.bind(languageId);
987         oql.bind(ContentVersionVO.PUBLISHED_STATE);
988         oql.bind(true);
989         
990         QueryResults results = oql.execute();
991         this.logger.info("Fetching entity in read/write mode");
992
993         if (results.hasMore())
994         {
995             contentVersion = (ContentVersion)results.next();
996         }
997             
998         results.close();
999         oql.close();
1000
1001        return contentVersion;
1002    }
1003
1004
1005    /**
1006     * This method returns the version previous to the one sent in.
1007     */

1008    
1009    public ContentVersionVO getPreviousContentVersionVO(Integer JavaDoc contentId, Integer JavaDoc languageId, Integer JavaDoc contentVersionId) throws SystemException, Bug
1010    {
1011        Database db = CastorDatabaseService.getDatabase();
1012        ContentVersionVO contentVersionVO = null;
1013
1014        beginTransaction(db);
1015
1016        try
1017        {
1018            OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 AND cv.contentVersionId < $3 ORDER BY cv.contentVersionId desc");
1019            oql.bind(contentId);
1020            oql.bind(languageId);
1021            oql.bind(contentVersionId);
1022            
1023            QueryResults results = oql.execute(Database.ReadOnly);
1024            
1025            if (results.hasMore())
1026            {
1027                ContentVersion contentVersion = (ContentVersion)results.next();
1028                logger.info("found one:" + contentVersion.getValueObject());
1029                contentVersionVO = contentVersion.getValueObject();
1030            }
1031            
1032            results.close();
1033            oql.close();
1034
1035            commitTransaction(db);
1036        }
1037        catch(Exception JavaDoc e)
1038        {
1039            logger.error("An error occurred so we should not completes the transaction:" + e, e);
1040            rollbackTransaction(db);
1041            throw new SystemException(e.getMessage());
1042        }
1043        
1044        return contentVersionVO;
1045    }
1046
1047
1048    /**
1049     * This method returns the version previous to the one sent in.
1050     */

1051    
1052    public ContentVersionVO getPreviousActiveContentVersionVO(Integer JavaDoc contentId, Integer JavaDoc languageId, Integer JavaDoc contentVersionId, Database db) throws SystemException, Bug, Exception JavaDoc
1053    {
1054        ContentVersionVO contentVersionVO = null;
1055
1056        OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.owningContent.contentId = $1 AND cv.language.languageId = $2 AND cv.isActive = $3 AND cv.contentVersionId < $4 ORDER BY cv.contentVersionId desc");
1057        oql.bind(contentId);
1058        oql.bind(languageId);
1059        oql.bind(new Boolean JavaDoc(true));
1060        oql.bind(contentVersionId);
1061        
1062        QueryResults results = oql.execute(Database.ReadOnly);
1063        
1064        if (results.hasMore())
1065        {
1066            ContentVersion contentVersion = (ContentVersion)results.next();
1067            logger.info("found one:" + contentVersion.getValueObject());
1068            contentVersionVO = contentVersion.getValueObject();
1069        }
1070        
1071        results.close();
1072        oql.close();
1073
1074        return contentVersionVO;
1075    }
1076
1077
1078    /**
1079     * This method deletes the relation to a digital asset - not the asset itself.
1080     */

1081    public void deleteDigitalAssetRelation(Integer JavaDoc contentVersionId, Integer JavaDoc digitalAssetId) throws SystemException, Bug
1082    {
1083        Database db = CastorDatabaseService.getDatabase();
1084        beginTransaction(db);
1085
1086        try
1087        {
1088            ContentVersion contentVersion = getContentVersionWithId(contentVersionId, db);
1089            DigitalAsset digitalAsset = DigitalAssetController.getDigitalAssetWithId(digitalAssetId, db);
1090            contentVersion.getDigitalAssets().remove(digitalAsset);
1091            digitalAsset.getContentVersions().remove(contentVersion);
1092            commitTransaction(db);
1093        }
1094        catch(Exception JavaDoc e)
1095        {
1096            logger.error("An error occurred so we should not completes the transaction:" + e, e);
1097            rollbackTransaction(db);
1098            throw new SystemException(e.getMessage());
1099        }
1100    }
1101    
1102    
1103    /**
1104     * This method deletes the relation to a digital asset - not the asset itself.
1105     */

1106    public void deleteDigitalAssetRelation(Integer JavaDoc contentVersionId, DigitalAsset digitalAsset, Database db) throws SystemException, Bug
1107    {
1108        ContentVersion contentVersion = getContentVersionWithId(contentVersionId, db);
1109        contentVersion.getDigitalAssets().remove(digitalAsset);
1110        digitalAsset.getContentVersions().remove(contentVersion);
1111    }
1112    
1113    
1114    /**
1115     * This method assigns the same digital assets the old content-version has.
1116     * It's ofcourse important that noone deletes the digital asset itself for then it's lost to everyone.
1117     */

1118    
1119    public void copyDigitalAssets(ContentVersion originalContentVersion, ContentVersion newContentVersion, Database db) throws ConstraintException, SystemException, Exception JavaDoc
1120    {
1121        Collection JavaDoc digitalAssets = originalContentVersion.getDigitalAssets();
1122
1123        //List newDigitalAssets = new ArrayList();
1124
Iterator JavaDoc digitalAssetsIterator = digitalAssets.iterator();
1125        while(digitalAssetsIterator.hasNext())
1126        {
1127            DigitalAsset digitalAsset = (DigitalAsset)digitalAssetsIterator.next();
1128            logger.info("Copying digitalAssets " + digitalAsset.getAssetKey());
1129            DigitalAssetVO digitalAssetVO = digitalAsset.getValueObject();
1130            
1131            DigitalAssetController.create(digitalAssetVO, DigitalAssetController.getController().getAssetInputStream(digitalAsset), newContentVersion, db);
1132            //DigitalAssetController.create(digitalAssetVO, digitalAsset.getAssetBlob(), newContentVersion, db);
1133
logger.info("digitalAssets:" + digitalAssets.size());
1134        }
1135        //newContentVersion.setDigitalAssets(digitalAssets);
1136
}
1137
1138    
1139    /**
1140     * This method fetches a value from the xml that is the contentVersions Value. If the
1141     * contentVersioVO is null the contentVersion has not been created yet and no values are present.
1142     */

1143    public String JavaDoc getAttributeValue(Integer JavaDoc contentVersionId, String JavaDoc attributeName, boolean escapeHTML) throws SystemException
1144    {
1145        String JavaDoc value = "";
1146        ContentVersionVO contentVersionVO = getContentVersionVOWithId(contentVersionId);
1147        
1148        if(contentVersionVO != null)
1149        {
1150            try
1151            {
1152                logger.info("attributeName:" + attributeName);
1153                logger.info("VersionValue:" + contentVersionVO.getVersionValue());
1154                value = getAttributeValue(contentVersionVO, attributeName, escapeHTML);
1155            }
1156            catch (Exception JavaDoc e)
1157            {
1158                e.printStackTrace();
1159            }
1160        }
1161        //logger.info("value:" + value);
1162
return value;
1163    }
1164
1165    /**
1166     * Returns an attribute value from the ContentVersionVO
1167     *
1168     * @param contentVersionVO The version on which to find the value
1169     * @param attributeName THe name of the attribute whose value is wanted
1170     * @param escapeHTML A boolean indicating if the result should be escaped
1171     * @return The String vlaue of the attribute, or blank if it doe snot exist.
1172     */

1173    public String JavaDoc getAttributeValue(ContentVersionVO contentVersionVO, String JavaDoc attributeName, boolean escapeHTML)
1174    {
1175        String JavaDoc value = "";
1176        String JavaDoc xml = contentVersionVO.getVersionValue();
1177
1178        int startTagIndex = xml.indexOf("<" + attributeName + ">");
1179        int endTagIndex = xml.indexOf("]]></" + attributeName + ">");
1180
1181        if(startTagIndex > 0 && startTagIndex < xml.length() && endTagIndex > startTagIndex && endTagIndex < xml.length())
1182        {
1183            value = xml.substring(startTagIndex + attributeName.length() + 11, endTagIndex);
1184            if(escapeHTML)
1185                value = new VisualFormatter().escapeHTML(value);
1186        }
1187
1188        return value;
1189    }
1190
1191
1192    /**
1193     * This method fetches a value from the xml that is the contentVersions Value. If the
1194     * contentVersioVO is null the contentVersion has not been created yet and no values are present.
1195     */

1196     
1197    public void updateAttributeValue(Integer JavaDoc contentVersionId, String JavaDoc attributeName, String JavaDoc attributeValue, InfoGluePrincipal infogluePrincipal) throws SystemException, Bug
1198    {
1199        ContentVersionVO contentVersionVO = getContentVersionVOWithId(contentVersionId);
1200        
1201        if(contentVersionVO != null)
1202        {
1203            try
1204            {
1205                logger.info("attributeName:" + attributeName);
1206                logger.info("versionValue:" + contentVersionVO.getVersionValue());
1207                logger.info("attributeValue:" + attributeValue);
1208                InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(contentVersionVO.getVersionValue()));
1209                
1210                DOMParser parser = new DOMParser();
1211                parser.parse(inputSource);
1212                Document JavaDoc document = parser.getDocument();
1213                
1214                NodeList JavaDoc nl = document.getDocumentElement().getChildNodes();
1215                Node attributesNode = nl.item(0);
1216                
1217                boolean existed = false;
1218                nl = attributesNode.getChildNodes();
1219                for(int i=0; i<nl.getLength(); i++)
1220                {
1221                    Node n = nl.item(i);
1222                    if(n.getNodeName().equalsIgnoreCase(attributeName))
1223                    {
1224                        if(n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null)
1225                        {
1226                            n.getFirstChild().setNodeValue(attributeValue);
1227                            existed = true;
1228                            break;
1229                        }
1230                        else
1231                        {
1232                            CDATASection JavaDoc cdata = document.createCDATASection(attributeValue);
1233                            n.appendChild(cdata);
1234                            existed = true;
1235                            break;
1236                        }
1237                    }
1238                }
1239                
1240                if(existed == false)
1241                {
1242                    org.w3c.dom.Element JavaDoc attributeElement = document.createElement(attributeName);
1243                    attributesNode.appendChild(attributeElement);
1244                    CDATASection JavaDoc cdata = document.createCDATASection(attributeValue);
1245                    attributeElement.appendChild(cdata);
1246                }
1247                
1248                StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1249                org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
1250                logger.info("sb:" + sb);
1251                contentVersionVO.setVersionValue(sb.toString());
1252                contentVersionVO.setVersionModifier(infogluePrincipal.getName());
1253                update(contentVersionVO.getContentId(), contentVersionVO.getLanguageId(), contentVersionVO);
1254            }
1255            catch(Exception JavaDoc e)
1256            {
1257                e.printStackTrace();
1258            }
1259        }
1260    }
1261
1262    /**
1263     * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
1264     * is handling.
1265     */

1266
1267    public BaseEntityVO getNewVO()
1268    {
1269        return new ContentVersionVO();
1270    }
1271
1272    /**
1273     * Recursive methods to get all contentVersions of a given state under the specified parent content.
1274     */

1275    
1276    public void getContentAndAffectedItemsRecursive(Integer JavaDoc contentId, Integer JavaDoc stateId, List JavaDoc siteNodeVersionVOList, List JavaDoc contenteVersionVOList, boolean mustBeFirst, boolean includeMetaInfo) throws ConstraintException, SystemException
1277    {
1278        Database db = CastorDatabaseService.getDatabase();
1279
1280        beginTransaction(db);
1281
1282        try
1283        {
1284            Content content = ContentController.getContentController().getContentWithId(contentId, db);
1285
1286            getContentAndAffectedItemsRecursive(content, stateId, new ArrayList JavaDoc(), new ArrayList JavaDoc(), db, siteNodeVersionVOList, contenteVersionVOList, mustBeFirst, includeMetaInfo);
1287            
1288            commitTransaction(db);
1289        }
1290        catch(Exception JavaDoc e)
1291        {
1292            logger.error("An error occurred so we should not completes the transaction:" + e, e);
1293            rollbackTransaction(db);
1294            throw new SystemException(e.getMessage());
1295        }
1296    }
1297    
1298    private void getContentAndAffectedItemsRecursive(Content content, Integer JavaDoc stateId, List JavaDoc checkedSiteNodes, List JavaDoc checkedContents, Database db, List JavaDoc siteNodeVersionVOList, List JavaDoc contentVersionVOList, boolean mustBeFirst, boolean includeMetaInfo) throws ConstraintException, SystemException, Exception JavaDoc
1299    {
1300        checkedSiteNodes.add(content.getId());
1301        
1302        List JavaDoc contentVersions = getLatestContentVersionWithParent(content.getId(), stateId, db, mustBeFirst);
1303        
1304        Iterator JavaDoc contentVersionsIterator = contentVersions.iterator();
1305        while(contentVersionsIterator.hasNext())
1306        {
1307            ContentVersion contentVersion = (ContentVersion)contentVersionsIterator.next();
1308            contentVersionVOList.add(contentVersion.getValueObject());
1309            
1310            List JavaDoc relatedEntities = RegistryController.getController().getMatchingRegistryVOListForReferencingEntity(ContentVersion.class.getName(), contentVersion.getId().toString(), db);
1311            logger.info("relatedEntities:" + relatedEntities);
1312            Iterator JavaDoc relatedEntitiesIterator = relatedEntities.iterator();
1313            
1314            while(relatedEntitiesIterator.hasNext())
1315            {
1316                RegistryVO registryVO = (RegistryVO)relatedEntitiesIterator.next();
1317                logger.info("registryVO:" + registryVO.getEntityName() + ":" + registryVO.getEntityId());
1318                if(registryVO.getEntityName().equals(SiteNode.class.getName()) && !checkedSiteNodes.contains(new Integer JavaDoc(registryVO.getEntityId())))
1319                {
1320                    try
1321                    {
1322                        SiteNode relatedSiteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer JavaDoc(registryVO.getEntityId()), db);
1323                        SiteNodeVersion relatedSiteNodeVersion = SiteNodeVersionController.getController().getLatestActiveSiteNodeVersionIfInState(relatedSiteNode, stateId, db);
1324                        if(relatedSiteNodeVersion != null && content.getRepository().getId().intValue() == relatedSiteNodeVersion.getOwningSiteNode().getRepository().getId().intValue())
1325                        {
1326                            siteNodeVersionVOList.add(relatedSiteNodeVersion.getValueObject());
1327                        }
1328                    }
1329                    catch(Exception JavaDoc e)
1330                    {
1331                        logger.warn("The related siteNode with id:" + registryVO.getEntityId() + " could not be loaded.", e);
1332                    }
1333                    
1334                    checkedSiteNodes.add(new Integer JavaDoc(registryVO.getEntityId()));
1335                }
1336                else if(registryVO.getEntityName().equals(Content.class.getName()) && !checkedContents.contains(new Integer JavaDoc(registryVO.getEntityId())))
1337                {
1338                    try
1339                    {
1340                        Content relatedContent = ContentController.getContentController().getContentWithId(new Integer JavaDoc(registryVO.getEntityId()), db);
1341                        if(includeMetaInfo || (!includeMetaInfo && (relatedContent.getContentTypeDefinition() == null || !relatedContent.getContentTypeDefinition().getName().equalsIgnoreCase("Meta info"))))
1342                        {
1343                            List JavaDoc relatedContentVersions = ContentVersionController.getContentVersionController().getLatestActiveContentVersionIfInState(relatedContent, stateId, db);
1344                            logger.info("relatedContentVersions:" + relatedContentVersions.size());
1345                            
1346                            Iterator JavaDoc relatedContentVersionsIterator = relatedContentVersions.iterator();
1347                            while(relatedContentVersionsIterator.hasNext())
1348                            {
1349                                ContentVersion relatedContentVersion = (ContentVersion)relatedContentVersionsIterator.next();
1350                                if(relatedContentVersion != null && content.getRepository().getId().intValue() == relatedContentVersion.getOwningContent().getRepository().getId().intValue())
1351                                {
1352                                    contentVersionVOList.add(relatedContentVersion.getValueObject());
1353                                    logger.info("Added:" + relatedContentVersion.getId());
1354                                }
1355            
1356                            }
1357                        }
1358                    }
1359                    catch(Exception JavaDoc e)
1360                    {
1361                        logger.warn("The related content with id:" + registryVO.getEntityId() + " could not be loaded.", e);
1362                    }
1363                    
1364                    checkedContents.add(new Integer JavaDoc(registryVO.getEntityId()));
1365                }
1366            }
1367
1368        }
1369        
1370        // Get the children of this content and do the recursion
1371
Collection JavaDoc childContentList = content.getChildren();
1372        Iterator JavaDoc cit = childContentList.iterator();
1373        while (cit.hasNext())
1374        {
1375            Content citContent = (Content) cit.next();
1376            getContentAndAffectedItemsRecursive(citContent, stateId, checkedSiteNodes, checkedContents, db, siteNodeVersionVOList, contentVersionVOList, mustBeFirst, includeMetaInfo);
1377        }
1378        
1379    }
1380
1381
1382}
1383
Popular Tags