KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.apache.xerces.parsers.DOMParser;
34 import org.dom4j.Element;
35 import org.exolab.castor.jdo.Database;
36 import org.exolab.castor.jdo.OQLQuery;
37 import org.exolab.castor.jdo.QueryResults;
38 import org.infoglue.cms.applications.common.VisualFormatter;
39 import org.infoglue.cms.entities.content.Content;
40 import org.infoglue.cms.entities.content.DigitalAsset;
41 import org.infoglue.cms.entities.kernel.BaseEntityVO;
42 import org.infoglue.cms.entities.management.ContentTypeDefinition;
43 import org.infoglue.cms.entities.management.GroupContentTypeDefinition;
44 import org.infoglue.cms.entities.management.GroupProperties;
45 import org.infoglue.cms.entities.management.GroupPropertiesVO;
46 import org.infoglue.cms.entities.management.Language;
47 import org.infoglue.cms.entities.management.PropertiesCategory;
48 import org.infoglue.cms.entities.management.PropertiesCategoryVO;
49 import org.infoglue.cms.entities.management.impl.simple.GroupContentTypeDefinitionImpl;
50 import org.infoglue.cms.entities.management.impl.simple.GroupPropertiesImpl;
51 import org.infoglue.cms.entities.management.impl.simple.LanguageImpl;
52 import org.infoglue.cms.entities.structure.SiteNode;
53 import org.infoglue.cms.exception.Bug;
54 import org.infoglue.cms.exception.ConstraintException;
55 import org.infoglue.cms.exception.SystemException;
56 import org.infoglue.cms.util.ConstraintExceptionBuffer;
57 import org.infoglue.cms.util.dom.DOMBuilder;
58 import org.infoglue.deliver.util.CacheController;
59 import org.w3c.dom.CDATASection JavaDoc;
60 import org.w3c.dom.Document JavaDoc;
61 import org.w3c.dom.Node JavaDoc;
62 import org.w3c.dom.NodeList JavaDoc;
63 import org.xml.sax.InputSource JavaDoc;
64
65 /**
66  * This class is the controller for all handling of extranet groups properties.
67  */

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

76
77     public static GroupPropertiesController getController()
78     {
79         return new GroupPropertiesController();
80     }
81     
82     
83     public GroupProperties getGroupPropertiesWithId(Integer JavaDoc groupPropertiesId, Database db) throws SystemException, Bug
84     {
85         return (GroupProperties) getObjectWithId(GroupPropertiesImpl.class, groupPropertiesId, db);
86     }
87     
88     public GroupPropertiesVO getGroupPropertiesVOWithId(Integer JavaDoc groupPropertiesId) throws SystemException, Bug
89     {
90         return (GroupPropertiesVO) getVOWithId(GroupPropertiesImpl.class, groupPropertiesId);
91     }
92   
93     public List JavaDoc getGroupPropertiesVOList() throws SystemException, Bug
94     {
95         return getAllVOObjects(GroupPropertiesImpl.class, "groupPropertiesId");
96     }
97
98     
99     /**
100      * This method created a new GroupPropertiesVO in the database.
101      */

102
103     public GroupPropertiesVO create(Integer JavaDoc languageId, Integer JavaDoc contentTypeDefinitionId, GroupPropertiesVO groupPropertiesVO) throws ConstraintException, SystemException
104     {
105         Database db = CastorDatabaseService.getDatabase();
106         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
107
108         GroupProperties groupProperties = null;
109
110         beginTransaction(db);
111         try
112         {
113             groupProperties = create(languageId, contentTypeDefinitionId, groupPropertiesVO, db);
114             commitTransaction(db);
115         }
116         catch(Exception JavaDoc e)
117         {
118             logger.error("An error occurred so we should not completes the transaction:" + e, e);
119             rollbackTransaction(db);
120             throw new SystemException(e.getMessage());
121         }
122     
123         return groupProperties.getValueObject();
124     }
125
126     /**
127      * This method created a new GroupPropertiesVO in the database. It also updates the extranetgroup
128      * so it recognises the change.
129      */

130
131     public GroupProperties create(Integer JavaDoc languageId, Integer JavaDoc contentTypeDefinitionId, GroupPropertiesVO groupPropertiesVO, Database db) throws ConstraintException, SystemException, Exception JavaDoc
132     {
133         Language language = LanguageController.getController().getLanguageWithId(languageId, db);
134         ContentTypeDefinition contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db);
135
136         GroupProperties groupProperties = new GroupPropertiesImpl();
137         groupProperties.setLanguage((LanguageImpl)language);
138         groupProperties.setContentTypeDefinition((ContentTypeDefinition)contentTypeDefinition);
139     
140         groupProperties.setValueObject(groupPropertiesVO);
141         db.create(groupProperties);
142         
143         return groupProperties;
144     }
145     
146     /**
147      * This method updates an extranet group properties.
148      */

149
150     public GroupPropertiesVO update(Integer JavaDoc languageId, Integer JavaDoc contentTypeDefinitionId, GroupPropertiesVO groupPropertiesVO) throws ConstraintException, SystemException
151     {
152         GroupPropertiesVO realGroupPropertiesVO = groupPropertiesVO;
153         
154         if(groupPropertiesVO.getId() == null)
155         {
156             logger.info("Creating the entity because there was no version at all for: " + contentTypeDefinitionId + " " + languageId);
157             realGroupPropertiesVO = create(languageId, contentTypeDefinitionId, groupPropertiesVO);
158         }
159
160         return (GroupPropertiesVO) updateEntity(GroupPropertiesImpl.class, (BaseEntityVO) realGroupPropertiesVO);
161     }
162
163     public GroupPropertiesVO update(GroupPropertiesVO groupPropertiesVO, String JavaDoc[] extranetUsers) throws ConstraintException, SystemException
164     {
165         Database db = CastorDatabaseService.getDatabase();
166         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
167
168         GroupProperties groupProperties = null;
169
170         beginTransaction(db);
171
172         try
173         {
174             //add validation here if needed
175
groupProperties = getGroupPropertiesWithId(groupPropertiesVO.getGroupPropertiesId(), db);
176             groupProperties.setValueObject(groupPropertiesVO);
177
178             //If any of the validations or setMethods reported an error, we throw them up now before create.
179
ceb.throwIfNotEmpty();
180             
181             commitTransaction(db);
182         }
183         catch(ConstraintException ce)
184         {
185             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
186             rollbackTransaction(db);
187             throw ce;
188         }
189         catch(Exception JavaDoc e)
190         {
191             logger.error("An error occurred so we should not complete the transaction:" + e, e);
192             rollbackTransaction(db);
193             throw new SystemException(e.getMessage());
194         }
195
196         return groupProperties.getValueObject();
197     }
198
199     
200     /**
201      * This method gets a list of groupProperties for a group
202      * The result is a list of propertiesblobs - each propertyblob is a list of actual properties.
203      */

204
205     public List JavaDoc getGroupPropertiesVOList(String JavaDoc groupName, Integer JavaDoc languageId) throws ConstraintException, SystemException
206     {
207         List JavaDoc groupPropertiesVOList = new ArrayList JavaDoc();
208         
209         Database db = CastorDatabaseService.getDatabase();
210         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
211
212         beginTransaction(db);
213
214         try
215         {
216             groupPropertiesVOList = getGroupPropertiesVOList(groupName, languageId, db);
217             
218             commitTransaction(db);
219         }
220         catch(Exception JavaDoc e)
221         {
222             logger.error("An error occurred so we should not complete the transaction:" + e, e);
223             rollbackTransaction(db);
224             throw new SystemException(e.getMessage());
225         }
226         
227         return groupPropertiesVOList;
228     }
229
230     
231     /**
232      * This method gets a list of groupProperties for a group
233      * The result is a list of propertiesblobs - each propertyblob is a list of actual properties.
234      */

235
236     public List JavaDoc getGroupPropertiesVOList(String JavaDoc groupName, Integer JavaDoc languageId, Database db) throws ConstraintException, Exception JavaDoc
237     {
238         List JavaDoc groupPropertiesVOList = new ArrayList JavaDoc();
239         
240         String JavaDoc cacheKey = "" + groupName + "_" + languageId;
241         logger.info("cacheKey:" + cacheKey);
242         groupPropertiesVOList = (List JavaDoc)CacheController.getCachedObject("groupPropertiesCache", cacheKey);
243         if(groupPropertiesVOList != null)
244         {
245             logger.info("There was an cached groupPropertiesVOList:" + groupPropertiesVOList.size());
246         }
247         else
248         {
249             List JavaDoc groupPropertiesList = getGroupPropertiesList(groupName, languageId, db, true);
250             if(groupPropertiesList != null)
251             {
252                 groupPropertiesVOList = toVOList(groupPropertiesList);
253                 CacheController.cacheObject("groupPropertiesCache", cacheKey, groupPropertiesVOList);
254             }
255
256         }
257         
258         return groupPropertiesVOList;
259     }
260
261     /**
262      * This method gets a list of groupProperties for a group
263      * The result is a list of propertiesblobs - each propertyblob is a list of actual properties.
264      */

265
266     public List JavaDoc getGroupPropertiesList(String JavaDoc groupName, Integer JavaDoc languageId, Database db, boolean readOnly) throws ConstraintException, SystemException, Exception JavaDoc
267     {
268         List JavaDoc groupPropertiesList = new ArrayList JavaDoc();
269
270         OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.GroupPropertiesImpl f WHERE f.groupName = $1 AND f.language = $2");
271         oql.bind(groupName);
272         oql.bind(languageId);
273
274         QueryResults results;
275         if(readOnly)
276         {
277             results = oql.execute(Database.ReadOnly);
278         }
279         else
280         {
281             this.logger.info("Fetching groupPropertiesList in read/write mode");
282             results = oql.execute();
283         }
284
285         while (results.hasMore())
286         {
287             GroupProperties groupProperties = (GroupProperties)results.next();
288             groupPropertiesList.add(groupProperties);
289         }
290
291         results.close();
292         oql.close();
293
294         return groupPropertiesList;
295     }
296
297     public void delete(GroupPropertiesVO groupPropertiesVO) throws ConstraintException, SystemException
298     {
299         deleteEntity(GroupPropertiesImpl.class, groupPropertiesVO.getGroupPropertiesId());
300     }
301
302     
303     /**
304      * This method should return a list of those digital assets the contentVersion has.
305      */

306         
307     public List JavaDoc getDigitalAssetVOList(Integer JavaDoc groupPropertiesId) throws SystemException, Bug
308     {
309         Database db = CastorDatabaseService.getDatabase();
310         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
311
312         List JavaDoc digitalAssetVOList = new ArrayList JavaDoc();
313
314         beginTransaction(db);
315
316         try
317         {
318             GroupProperties groupProperties = GroupPropertiesController.getController().getGroupPropertiesWithId(groupPropertiesId, db);
319             if(groupProperties != null)
320             {
321                 Collection JavaDoc digitalAssets = groupProperties.getDigitalAssets();
322                 digitalAssetVOList = toVOList(digitalAssets);
323             }
324                         
325             commitTransaction(db);
326         }
327         catch(Exception JavaDoc e)
328         {
329             logger.info("An error occurred when we tried to fetch the list of digitalAssets belonging to this groupProperties:" + e);
330             e.printStackTrace();
331             rollbackTransaction(db);
332             throw new SystemException(e.getMessage());
333         }
334         
335         return digitalAssetVOList;
336     }
337
338     
339     /**
340      * This method deletes the relation to a digital asset - not the asset itself.
341      */

342     public void deleteDigitalAssetRelation(Integer JavaDoc groupPropertiesId, DigitalAsset digitalAsset, Database db) throws SystemException, Bug
343     {
344         GroupProperties groupProperties = getGroupPropertiesWithId(groupPropertiesId, db);
345         groupProperties.getDigitalAssets().remove(digitalAsset);
346         digitalAsset.getGroupProperties().remove(groupProperties);
347     }
348
349
350     /**
351      * This method fetches all content types available for this group.
352      */

353     
354     public List JavaDoc getContentTypeDefinitionVOList(String JavaDoc groupName) throws ConstraintException, SystemException
355     {
356         Database db = CastorDatabaseService.getDatabase();
357         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
358
359         List JavaDoc contentTypeDefinitionVOList = new ArrayList JavaDoc();
360
361         beginTransaction(db);
362
363         try
364         {
365             List JavaDoc groupContentTypeDefinitionList = getGroupContentTypeDefinitionList(groupName, db);
366             Iterator JavaDoc contentTypeDefinitionsIterator = groupContentTypeDefinitionList.iterator();
367             while(contentTypeDefinitionsIterator.hasNext())
368             {
369                 GroupContentTypeDefinition groupContentTypeDefinition = (GroupContentTypeDefinition)contentTypeDefinitionsIterator.next();
370                 contentTypeDefinitionVOList.add(groupContentTypeDefinition.getContentTypeDefinition().getValueObject());
371             }
372     
373             ceb.throwIfNotEmpty();
374     
375             commitTransaction(db);
376         }
377         catch(ConstraintException ce)
378         {
379             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
380             rollbackTransaction(db);
381             throw ce;
382         }
383         catch(Exception JavaDoc e)
384         {
385             logger.error("An error occurred so we should not complete the transaction:" + e, e);
386             rollbackTransaction(db);
387             throw new SystemException(e.getMessage());
388         }
389
390         return contentTypeDefinitionVOList;
391     }
392
393     /**
394      * This method fetches all group content types available for this group within a transaction.
395      */

396     
397     public List JavaDoc getGroupContentTypeDefinitionList(String JavaDoc groupName, Database db) throws ConstraintException, SystemException, Exception JavaDoc
398     {
399         List JavaDoc groupContentTypeDefinitionList = new ArrayList JavaDoc();
400
401         OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.GroupContentTypeDefinitionImpl f WHERE f.groupName = $1");
402         oql.bind(groupName);
403
404         QueryResults results = oql.execute();
405         this.logger.info("Fetching groupContentTypeDefinitionList in read/write mode");
406
407         while (results.hasMore())
408         {
409             GroupContentTypeDefinition groupContentTypeDefinition = (GroupContentTypeDefinition)results.next();
410             groupContentTypeDefinitionList.add(groupContentTypeDefinition);
411         }
412
413         results.close();
414         oql.close();
415
416         return groupContentTypeDefinitionList;
417     }
418     
419     /**
420      * This method fetches all content types available for this group.
421      */

422
423     public void updateContentTypeDefinitions(String JavaDoc groupName, String JavaDoc[] contentTypeDefinitionIds) throws ConstraintException, SystemException
424     {
425         Database db = CastorDatabaseService.getDatabase();
426         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
427
428         List JavaDoc contentTypeDefinitionVOList = new ArrayList JavaDoc();
429
430         beginTransaction(db);
431
432         try
433         {
434             List JavaDoc groupContentTypeDefinitionList = this.getGroupContentTypeDefinitionList(groupName, db);
435             Iterator JavaDoc contentTypeDefinitionsIterator = groupContentTypeDefinitionList.iterator();
436             while(contentTypeDefinitionsIterator.hasNext())
437             {
438                 GroupContentTypeDefinition groupContentTypeDefinition = (GroupContentTypeDefinition)contentTypeDefinitionsIterator.next();
439                 db.remove(groupContentTypeDefinition);
440             }
441             
442             for(int i=0; i<contentTypeDefinitionIds.length; i++)
443             {
444                 Integer JavaDoc contentTypeDefinitionId = new Integer JavaDoc(contentTypeDefinitionIds[i]);
445                 ContentTypeDefinition contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db);
446                 GroupContentTypeDefinitionImpl groupContentTypeDefinitionImpl = new GroupContentTypeDefinitionImpl();
447                 groupContentTypeDefinitionImpl.setGroupName(groupName);
448                 groupContentTypeDefinitionImpl.setContentTypeDefinition(contentTypeDefinition);
449                 db.create(groupContentTypeDefinitionImpl);
450             }
451             
452             ceb.throwIfNotEmpty();
453
454             commitTransaction(db);
455         }
456         catch(ConstraintException ce)
457         {
458             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
459             rollbackTransaction(db);
460             throw ce;
461         }
462         catch(Exception JavaDoc e)
463         {
464             logger.error("An error occurred so we should not complete the transaction:" + e, e);
465             rollbackTransaction(db);
466             throw new SystemException(e.getMessage());
467         }
468     }
469     
470     
471     /**
472      * This method fetches a value from the xml that is the groupProperties Value. It then updates that
473      * single value and saves it back to the db.
474      */

475      
476     public void updateAttributeValue(Integer JavaDoc groupPropertiesId, String JavaDoc attributeName, String JavaDoc attributeValue) throws SystemException, Bug
477     {
478         GroupPropertiesVO groupPropertiesVO = getGroupPropertiesVOWithId(groupPropertiesId);
479         
480         if(groupPropertiesVO != null)
481         {
482             try
483             {
484                 logger.info("attributeName:" + attributeName);
485                 logger.info("versionValue:" + groupPropertiesVO.getValue());
486                 logger.info("attributeValue:" + attributeValue);
487                 InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(groupPropertiesVO.getValue()));
488                 
489                 DOMParser parser = new DOMParser();
490                 parser.parse(inputSource);
491                 Document JavaDoc document = parser.getDocument();
492                 
493                 NodeList JavaDoc nl = document.getDocumentElement().getChildNodes();
494                 Node attributesNode = nl.item(0);
495                 
496                 boolean existed = false;
497                 nl = attributesNode.getChildNodes();
498                 for(int i=0; i<nl.getLength(); i++)
499                 {
500                     Node n = nl.item(i);
501                     if(n.getNodeName().equalsIgnoreCase(attributeName))
502                     {
503                         if(n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null)
504                         {
505                             n.getFirstChild().setNodeValue(attributeValue);
506                             existed = true;
507                             break;
508                         }
509                         else
510                         {
511                             CDATASection JavaDoc cdata = document.createCDATASection(attributeValue);
512                             n.appendChild(cdata);
513                             existed = true;
514                             break;
515                         }
516                     }
517                 }
518                 
519                 if(existed == false)
520                 {
521                     org.w3c.dom.Element JavaDoc attributeElement = document.createElement(attributeName);
522                     attributesNode.appendChild(attributeElement);
523                     CDATASection JavaDoc cdata = document.createCDATASection(attributeValue);
524                     attributeElement.appendChild(cdata);
525                 }
526                 
527                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
528                 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
529                 logger.info("sb:" + sb);
530                 groupPropertiesVO.setValue(sb.toString());
531                 update(groupPropertiesVO.getLanguageId(), groupPropertiesVO.getContentTypeDefinitionId(), groupPropertiesVO);
532             }
533             catch(Exception JavaDoc e)
534             {
535                 e.printStackTrace();
536             }
537         }
538     }
539
540     
541     /**
542      * Returns the value of a Group Property
543      */

544
545     public String JavaDoc getAttributeValue(String JavaDoc groupName, Integer JavaDoc languageId, String JavaDoc attributeName) throws SystemException
546     {
547         String JavaDoc value = "";
548         
549         Database db = CastorDatabaseService.getDatabase();
550
551         beginTransaction(db);
552
553         try
554         {
555             value = getAttributeValue(groupName, languageId, attributeName, db);
556             
557             commitTransaction(db);
558         }
559         catch(Exception JavaDoc e)
560         {
561             logger.error("An error occurred so we should not complete the transaction:" + e, e);
562             rollbackTransaction(db);
563             throw new SystemException(e.getMessage());
564         }
565         
566         return value;
567     }
568
569
570     /**
571      * Returns the value of a Group Property
572      */

573
574     public String JavaDoc getAttributeValue(String JavaDoc groupName, Integer JavaDoc languageId, String JavaDoc attributeName, Database db) throws SystemException, Exception JavaDoc
575     {
576         String JavaDoc value = "";
577         
578         List JavaDoc groupPropertiesVO = this.getGroupPropertiesVOList(groupName, languageId, db);
579         Iterator JavaDoc iterator = groupPropertiesVO.iterator();
580         GroupPropertiesVO groupPropertyVO = null;
581         while(iterator.hasNext())
582         {
583             groupPropertyVO = (GroupPropertiesVO)iterator.next();
584             break;
585         }
586
587         if(groupPropertyVO != null)
588         {
589             value = this.getAttributeValue(groupPropertyVO.getValue(), attributeName, false);
590         }
591         
592         return value;
593     }
594
595     /**
596      * This method fetches a value from the xml that is the groupProperties Value.
597      */

598      
599     public String JavaDoc getAttributeValue(Integer JavaDoc groupPropertiesId, String JavaDoc attributeName, boolean escapeHTML) throws SystemException, Bug
600     {
601         String JavaDoc value = "";
602         
603         GroupPropertiesVO groupPropertiesVO = getGroupPropertiesVOWithId(groupPropertiesId);
604         
605         if(groupPropertiesVO != null)
606         {
607             value = getAttributeValue(groupPropertiesVO.getValue(), attributeName, escapeHTML);
608         }
609
610         return value;
611     }
612
613     
614     /**
615      * This method fetches a value from the xml that is the groupProperties Value.
616      */

617      
618     public String JavaDoc getAttributeValue(String JavaDoc xml, String JavaDoc attributeName, boolean escapeHTML) throws SystemException, Bug
619     {
620         String JavaDoc value = "";
621         
622         try
623         {
624             InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(xml));
625             
626             DOMParser parser = new DOMParser();
627             parser.parse(inputSource);
628             Document JavaDoc document = parser.getDocument();
629             
630             NodeList JavaDoc nl = document.getDocumentElement().getChildNodes();
631             Node n = nl.item(0);
632             
633             nl = n.getChildNodes();
634             for(int i=0; i<nl.getLength(); i++)
635             {
636                 n = nl.item(i);
637                 if(n.getNodeName().equalsIgnoreCase(attributeName))
638                 {
639                     if(n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null)
640                     {
641                         value = n.getFirstChild().getNodeValue();
642                         if(value != null && escapeHTML)
643                             value = new VisualFormatter().escapeHTML(value);
644
645                         break;
646                     }
647                 }
648             }
649         }
650         catch(Exception JavaDoc e)
651         {
652             e.printStackTrace();
653         }
654
655         return value;
656     }
657
658     
659     /**
660      * Returns the related Contents
661      * @param groupPropertiesId
662      * @return
663      */

664
665     public List JavaDoc getRelatedContents(String JavaDoc groupName, Integer JavaDoc languageId, String JavaDoc attributeName) throws SystemException
666     {
667         Database db = CastorDatabaseService.getDatabase();
668
669         List JavaDoc relatedContentVOList = new ArrayList JavaDoc();
670
671         beginTransaction(db);
672
673         try
674         {
675             List JavaDoc relatedContents = getRelatedContents(groupName, languageId, attributeName, db);
676             if(relatedContents != null)
677                 relatedContentVOList = toVOList(relatedContents);
678             
679             commitTransaction(db);
680         }
681         catch(Exception JavaDoc e)
682         {
683             logger.error("An error occurred so we should not complete the transaction:" + e, e);
684             rollbackTransaction(db);
685             throw new SystemException(e.getMessage());
686         }
687         
688         return relatedContentVOList;
689     }
690
691     /**
692      * Returns the related Contents
693      * @param groupPropertiesId
694      * @return
695      */

696
697     public List JavaDoc getReadOnlyRelatedContents(String JavaDoc groupName, Integer JavaDoc languageId, String JavaDoc attributeName, Database db) throws SystemException, Exception JavaDoc
698     {
699         List JavaDoc relatedContentList = new ArrayList JavaDoc();
700
701         List JavaDoc groupPropertiesVO = this.getGroupPropertiesVOList(groupName, languageId, db);
702         Iterator JavaDoc iterator = groupPropertiesVO.iterator();
703         GroupPropertiesVO groupPropertyVO = null;
704         while(iterator.hasNext())
705         {
706             groupPropertyVO = (GroupPropertiesVO)iterator.next();
707             break;
708         }
709
710         if(groupPropertyVO != null)
711         {
712             String JavaDoc xml = this.getAttributeValue(groupPropertyVO.getValue(), attributeName, false);
713             relatedContentList = this.getReadOnlyRelatedContentsFromXML(db, xml);
714         }
715         
716         return relatedContentList;
717     }
718
719     /**
720      * Returns the related Contents
721      * @param groupPropertiesId
722      * @return
723      */

724
725     public List JavaDoc getRelatedContents(String JavaDoc groupName, Integer JavaDoc languageId, String JavaDoc attributeName, Database db) throws SystemException, Exception JavaDoc
726     {
727         List JavaDoc relatedContentList = new ArrayList JavaDoc();
728
729         List JavaDoc groupPropertiesVO = this.getGroupPropertiesVOList(groupName, languageId, db);
730         Iterator JavaDoc iterator = groupPropertiesVO.iterator();
731         GroupPropertiesVO groupPropertyVO = null;
732         while(iterator.hasNext())
733         {
734             groupPropertyVO = (GroupPropertiesVO)iterator.next();
735             break;
736         }
737
738         if(groupPropertyVO != null)
739         {
740             String JavaDoc xml = this.getAttributeValue(groupPropertyVO.getValue(), attributeName, false);
741             relatedContentList = this.getRelatedContentsFromXML(db, xml);
742         }
743         
744         return relatedContentList;
745     }
746
747     /**
748      * Returns the related SiteNodes
749      * @param groupPropertiesId
750      * @return
751      */

752
753     public List JavaDoc getRelatedSiteNodes(String JavaDoc groupName, Integer JavaDoc languageId, String JavaDoc attributeName) throws SystemException
754     {
755         Database db = CastorDatabaseService.getDatabase();
756
757         List JavaDoc relatedSiteNodeVOList = new ArrayList JavaDoc();
758
759         beginTransaction(db);
760
761         try
762         {
763             List JavaDoc relatedSiteNodes = getRelatedSiteNodes(groupName, languageId, attributeName, db);
764             if(relatedSiteNodes != null)
765                 relatedSiteNodeVOList = toVOList(relatedSiteNodes);
766             
767             commitTransaction(db);
768         }
769         catch(Exception JavaDoc e)
770         {
771             logger.error("An error occurred so we should not complete the transaction:" + e, e);
772             rollbackTransaction(db);
773             throw new SystemException(e.getMessage());
774         }
775         
776         return relatedSiteNodeVOList;
777     }
778
779
780     /**
781      * Returns the related SiteNodes
782      * @param groupPropertiesId
783      * @return
784      */

785
786     public List JavaDoc getRelatedSiteNodes(String JavaDoc groupName, Integer JavaDoc languageId, String JavaDoc attributeName, Database db) throws SystemException, Exception JavaDoc
787     {
788         List JavaDoc relatedSiteNodeList = new ArrayList JavaDoc();
789
790         List JavaDoc groupProperties = this.getGroupPropertiesVOList(groupName, languageId, db);
791         Iterator JavaDoc iterator = groupProperties.iterator();
792         GroupPropertiesVO groupPropertyVO = null;
793         while(iterator.hasNext())
794         {
795             groupPropertyVO = (GroupPropertiesVO)iterator.next();
796             break;
797         }
798         
799         if(groupPropertyVO != null)
800         {
801             String JavaDoc xml = this.getAttributeValue(groupPropertyVO.getValue(), attributeName, false);
802             relatedSiteNodeList = this.getRelatedSiteNodesFromXML(db, xml);
803         }
804
805         return relatedSiteNodeList;
806     }
807
808     /**
809      * Returns the related SiteNodes
810      * @param groupPropertiesId
811      * @return
812      */

813
814     public List JavaDoc getReadOnlyRelatedSiteNodes(String JavaDoc groupName, Integer JavaDoc languageId, String JavaDoc attributeName, Database db) throws SystemException, Exception JavaDoc
815     {
816         List JavaDoc relatedSiteNodeList = new ArrayList JavaDoc();
817
818         List JavaDoc groupProperties = this.getGroupPropertiesVOList(groupName, languageId, db);
819         Iterator JavaDoc iterator = groupProperties.iterator();
820         GroupPropertiesVO groupPropertyVO = null;
821         while(iterator.hasNext())
822         {
823             groupPropertyVO = (GroupPropertiesVO)iterator.next();
824             break;
825         }
826         
827         if(groupPropertyVO != null)
828         {
829             String JavaDoc xml = this.getAttributeValue(groupPropertyVO.getValue(), attributeName, false);
830             relatedSiteNodeList = this.getReadOnlyRelatedSiteNodesFromXML(db, xml);
831         }
832
833         return relatedSiteNodeList;
834     }
835
836     /**
837      * Parses contents from an XML within a transaction
838      * @param qualifyerXML
839      * @return
840      */

841
842     private List JavaDoc getRelatedContentsFromXML(Database db, String JavaDoc qualifyerXML)
843     {
844         List JavaDoc contents = new ArrayList JavaDoc();
845         
846         if(qualifyerXML == null || qualifyerXML.length() == 0)
847             return contents;
848         
849         try
850         {
851             org.dom4j.Document document = new DOMBuilder().getDocument(qualifyerXML);
852             
853             String JavaDoc entity = document.getRootElement().attributeValue("entity");
854             
855             List JavaDoc children = document.getRootElement().elements();
856             Iterator JavaDoc i = children.iterator();
857             while(i.hasNext())
858             {
859                 Element child = (Element)i.next();
860                 String JavaDoc id = child.getStringValue();
861                 
862                 Content content = ContentController.getContentController().getContentWithId(new Integer JavaDoc(id), db);
863                 contents.add(content);
864             }
865         }
866         catch(Exception JavaDoc e)
867         {
868             e.printStackTrace();
869         }
870         
871         return contents;
872     }
873
874
875     /**
876      * Parses contents from an XML within a transaction
877      * @param qualifyerXML
878      * @return
879      */

880
881     private List JavaDoc getReadOnlyRelatedContentsFromXML(Database db, String JavaDoc qualifyerXML)
882     {
883         List JavaDoc contents = new ArrayList JavaDoc();
884         
885         if(qualifyerXML == null || qualifyerXML.length() == 0)
886             return contents;
887         
888         try
889         {
890             org.dom4j.Document document = new DOMBuilder().getDocument(qualifyerXML);
891             
892             String JavaDoc entity = document.getRootElement().attributeValue("entity");
893             
894             List JavaDoc children = document.getRootElement().elements();
895             Iterator JavaDoc i = children.iterator();
896             while(i.hasNext())
897             {
898                 Element child = (Element)i.next();
899                 String JavaDoc id = child.getStringValue();
900                 
901                 Content content = ContentController.getContentController().getReadOnlyContentWithId(new Integer JavaDoc(id), db);
902                 contents.add(content);
903             }
904         }
905         catch(Exception JavaDoc e)
906         {
907             e.printStackTrace();
908         }
909         
910         return contents;
911     }
912
913     /**
914      * Parses siteNodes from an XML within a transaction
915      * @param qualifyerXML
916      * @return
917      */

918
919     private List JavaDoc getRelatedSiteNodesFromXML(Database db, String JavaDoc qualifyerXML)
920     {
921         List JavaDoc siteNodes = new ArrayList JavaDoc();
922         
923         if(qualifyerXML == null || qualifyerXML.length() == 0)
924             return siteNodes;
925         
926         try
927         {
928             org.dom4j.Document document = new DOMBuilder().getDocument(qualifyerXML);
929             
930             String JavaDoc entity = document.getRootElement().attributeValue("entity");
931             
932             List JavaDoc children = document.getRootElement().elements();
933             Iterator JavaDoc i = children.iterator();
934             while(i.hasNext())
935             {
936                 Element child = (Element)i.next();
937                 String JavaDoc id = child.getStringValue();
938                 
939                 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer JavaDoc(id), db);
940                 siteNodes.add(siteNode);
941             }
942         }
943         catch(Exception JavaDoc e)
944         {
945             e.printStackTrace();
946         }
947         
948         return siteNodes;
949     }
950
951     /**
952      * Parses siteNodes from an XML within a transaction
953      * @param qualifyerXML
954      * @return
955      */

956
957     private List JavaDoc getReadOnlyRelatedSiteNodesFromXML(Database db, String JavaDoc qualifyerXML)
958     {
959         List JavaDoc siteNodes = new ArrayList JavaDoc();
960         
961         if(qualifyerXML == null || qualifyerXML.length() == 0)
962             return siteNodes;
963         
964         try
965         {
966             org.dom4j.Document document = new DOMBuilder().getDocument(qualifyerXML);
967             
968             String JavaDoc entity = document.getRootElement().attributeValue("entity");
969             
970             List JavaDoc children = document.getRootElement().elements();
971             Iterator JavaDoc i = children.iterator();
972             while(i.hasNext())
973             {
974                 Element child = (Element)i.next();
975                 String JavaDoc id = child.getStringValue();
976                 
977                 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer JavaDoc(id), db, true);
978                 siteNodes.add(siteNode);
979             }
980         }
981         catch(Exception JavaDoc e)
982         {
983             e.printStackTrace();
984         }
985         
986         return siteNodes;
987     }
988     
989     /**
990      * Returns all current Category relationships for th specified attribute name
991      * @param attribute
992      * @return
993      */

994     
995     public List JavaDoc getRelatedCategories(String JavaDoc groupName, Integer JavaDoc languageId, String JavaDoc attribute)
996     {
997         List JavaDoc relatedCategories = new ArrayList JavaDoc();
998         
999         try
1000        {
1001            List JavaDoc groupPropertiesVOList = this.getGroupPropertiesVOList(groupName, languageId);
1002            Iterator JavaDoc iterator = groupPropertiesVOList.iterator();
1003            GroupPropertiesVO groupPropertyVO = null;
1004            while(iterator.hasNext())
1005            {
1006                groupPropertyVO = (GroupPropertiesVO)iterator.next();
1007                break;
1008            }
1009
1010            if(groupPropertyVO != null && groupPropertyVO.getId() != null)
1011            {
1012                List JavaDoc propertiesCategoryVOList = PropertiesCategoryController.getController().findByPropertiesAttribute(attribute, GroupProperties.class.getName(), groupPropertyVO.getId());
1013                Iterator JavaDoc propertiesCategoryVOListIterator = propertiesCategoryVOList.iterator();
1014                while(propertiesCategoryVOListIterator.hasNext())
1015                {
1016                    PropertiesCategoryVO propertiesCategoryVO = (PropertiesCategoryVO)propertiesCategoryVOListIterator.next();
1017                    relatedCategories.add(propertiesCategoryVO.getCategory());
1018                }
1019            }
1020        }
1021        catch(Exception JavaDoc e)
1022        {
1023            logger.warn("We could not fetch the list of defined category keys: " + e.getMessage(), e);
1024        }
1025
1026        return relatedCategories;
1027    }
1028    
1029    
1030    /**
1031     * Returns all current Category relationships for th specified attribute name
1032     * @param attribute
1033     * @return
1034     */

1035    
1036    public List JavaDoc getRelatedCategoriesVOList(String JavaDoc groupName, Integer JavaDoc languageId, String JavaDoc attribute, Database db) throws SystemException, Exception JavaDoc
1037    {
1038        List JavaDoc relatedCategoriesVOList = new ArrayList JavaDoc();
1039        
1040        
1041        String JavaDoc cacheKey = "" + groupName + "_" + languageId + "_" + attribute;
1042        logger.info("cacheKey:" + cacheKey);
1043        relatedCategoriesVOList = (List JavaDoc)CacheController.getCachedObject("relatedCategoriesCache", cacheKey);
1044        if(relatedCategoriesVOList != null)
1045        {
1046            logger.info("There was an cached groupPropertiesVOList:" + relatedCategoriesVOList.size());
1047        }
1048        else
1049        {
1050            List JavaDoc relatedCategories = getRelatedCategories(groupName, languageId, attribute, db);
1051            if(relatedCategories != null)
1052            {
1053                relatedCategoriesVOList = toVOList(relatedCategories);
1054                CacheController.cacheObject("relatedCategoriesCache", cacheKey, relatedCategoriesVOList);
1055            }
1056        }
1057
1058        return relatedCategoriesVOList;
1059    }
1060
1061    /**
1062     * Returns all current Category relationships for th specified attribute name
1063     * @param attribute
1064     * @return
1065     */

1066    
1067    public List JavaDoc getRelatedCategories(String JavaDoc groupName, Integer JavaDoc languageId, String JavaDoc attribute, Database db)
1068    {
1069        List JavaDoc relatedCategories = new ArrayList JavaDoc();
1070        
1071        try
1072        {
1073            List JavaDoc groupPropertiesVOList = this.getGroupPropertiesVOList(groupName, languageId, db);
1074            Iterator JavaDoc iterator = groupPropertiesVOList.iterator();
1075            GroupPropertiesVO groupPropertyVO = null;
1076            while(iterator.hasNext())
1077            {
1078                groupPropertyVO = (GroupPropertiesVO)iterator.next();
1079                break;
1080            }
1081
1082            if(groupPropertyVO != null && groupPropertyVO.getId() != null)
1083            {
1084                List JavaDoc propertiesCategoryList = PropertiesCategoryController.getController().findByPropertiesAttribute(attribute, GroupProperties.class.getName(), groupPropertyVO.getId(), db);
1085
1086                Iterator JavaDoc propertiesCategoryListIterator = propertiesCategoryList.iterator();
1087                while(propertiesCategoryListIterator.hasNext())
1088                {
1089                    PropertiesCategory propertiesCategory = (PropertiesCategory)propertiesCategoryListIterator.next();
1090                    relatedCategories.add(propertiesCategory.getCategory());
1091                }
1092            }
1093        }
1094        catch(Exception JavaDoc e)
1095        {
1096            logger.warn("We could not fetch the list of defined category keys: " + e.getMessage(), e);
1097        }
1098
1099        return relatedCategories;
1100    }
1101
1102    /**
1103     * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
1104     * is handling.
1105     */

1106
1107    public BaseEntityVO getNewVO()
1108    {
1109        return new GroupPropertiesVO();
1110    }
1111
1112}
1113 
Popular Tags