KickJava   Java API By Example, From Geeks To Geeks.

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


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.Language;
44 import org.infoglue.cms.entities.management.PropertiesCategoryVO;
45 import org.infoglue.cms.entities.management.RoleContentTypeDefinition;
46 import org.infoglue.cms.entities.management.RoleProperties;
47 import org.infoglue.cms.entities.management.RolePropertiesVO;
48 import org.infoglue.cms.entities.management.impl.simple.LanguageImpl;
49 import org.infoglue.cms.entities.management.impl.simple.RoleContentTypeDefinitionImpl;
50 import org.infoglue.cms.entities.management.impl.simple.RolePropertiesImpl;
51 import org.infoglue.cms.entities.structure.SiteNode;
52 import org.infoglue.cms.exception.Bug;
53 import org.infoglue.cms.exception.ConstraintException;
54 import org.infoglue.cms.exception.SystemException;
55 import org.infoglue.cms.util.ConstraintExceptionBuffer;
56 import org.infoglue.cms.util.dom.DOMBuilder;
57 import org.w3c.dom.CDATASection JavaDoc;
58 import org.w3c.dom.Document JavaDoc;
59 import org.w3c.dom.Node JavaDoc;
60 import org.w3c.dom.NodeList JavaDoc;
61 import org.xml.sax.InputSource JavaDoc;
62
63 /**
64  * This class is the controller for all handling of extranet roles properties.
65  */

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

74
75     public static RolePropertiesController getController()
76     {
77         return new RolePropertiesController();
78     }
79     
80     
81     public RoleProperties getRolePropertiesWithId(Integer JavaDoc rolePropertiesId, Database db) throws SystemException, Bug
82     {
83         return (RoleProperties) getObjectWithId(RolePropertiesImpl.class, rolePropertiesId, db);
84     }
85     
86     public RolePropertiesVO getRolePropertiesVOWithId(Integer JavaDoc rolePropertiesId) throws SystemException, Bug
87     {
88         return (RolePropertiesVO) getVOWithId(RolePropertiesImpl.class, rolePropertiesId);
89     }
90   
91     public List JavaDoc getRolePropertiesVOList() throws SystemException, Bug
92     {
93         return getAllVOObjects(RolePropertiesImpl.class, "rolePropertiesId");
94     }
95
96     
97     /**
98      * This method created a new RolePropertiesVO in the database.
99      */

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

128
129     public RoleProperties create(Integer JavaDoc languageId, Integer JavaDoc contentTypeDefinitionId, RolePropertiesVO rolePropertiesVO, Database db) throws ConstraintException, SystemException, Exception JavaDoc
130     {
131         Language language = LanguageController.getController().getLanguageWithId(languageId, db);
132         ContentTypeDefinition contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db);
133
134         RoleProperties roleProperties = new RolePropertiesImpl();
135         roleProperties.setLanguage((LanguageImpl)language);
136         roleProperties.setContentTypeDefinition((ContentTypeDefinition)contentTypeDefinition);
137     
138         roleProperties.setValueObject(rolePropertiesVO);
139         db.create(roleProperties);
140         
141         return roleProperties;
142     }
143     
144     /**
145      * This method updates an extranet role properties.
146      */

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

201
202     public List JavaDoc getRolePropertiesVOList(String JavaDoc roleName, Integer JavaDoc languageId) throws ConstraintException, SystemException
203     {
204         Database db = CastorDatabaseService.getDatabase();
205         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
206
207         List JavaDoc rolePropertiesVOList = new ArrayList JavaDoc();
208
209         beginTransaction(db);
210
211         try
212         {
213             List JavaDoc roleProperties = getRolePropertiesList(roleName, languageId, db, true);
214             rolePropertiesVOList = toVOList(roleProperties);
215             
216             //If any of the validations or setMethods reported an error, we throw them up now before create.
217
ceb.throwIfNotEmpty();
218             
219             commitTransaction(db);
220         }
221         catch(ConstraintException ce)
222         {
223             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
224             rollbackTransaction(db);
225             throw ce;
226         }
227         catch(Exception JavaDoc e)
228         {
229             logger.error("An error occurred so we should not complete the transaction:" + e, e);
230             rollbackTransaction(db);
231             throw new SystemException(e.getMessage());
232         }
233
234         return rolePropertiesVOList;
235     }
236
237     /**
238      * This method gets a list of roleProperties for a role
239      * The result is a list of propertiesblobs - each propertyblob is a list of actual properties.
240      */

241
242     public List JavaDoc getRolePropertiesList(String JavaDoc roleName, Integer JavaDoc languageId, Database db, boolean readOnly) throws ConstraintException, SystemException, Exception JavaDoc
243     {
244         List JavaDoc rolePropertiesList = new ArrayList JavaDoc();
245
246         OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.RolePropertiesImpl f WHERE f.roleName = $1 AND f.language = $2");
247         oql.bind(roleName);
248         oql.bind(languageId);
249
250         QueryResults results = null;
251         if(readOnly)
252         {
253             results = oql.execute(Database.ReadOnly);
254         }
255         else
256         {
257             logger.info("Fetching entity in read/write mode:" + roleName);
258             results = oql.execute();
259         }
260
261         while (results.hasMore())
262         {
263             RoleProperties roleProperties = (RoleProperties)results.next();
264             rolePropertiesList.add(roleProperties);
265         }
266
267         results.close();
268         oql.close();
269
270         return rolePropertiesList;
271     }
272     
273     public void delete(RolePropertiesVO rolePropertiesVO) throws ConstraintException, SystemException
274     {
275         deleteEntity(RolePropertiesImpl.class, rolePropertiesVO.getRolePropertiesId());
276     }
277
278     
279     /**
280      * This method should return a list of those digital assets the contentVersion has.
281      */

282         
283     public List JavaDoc getDigitalAssetVOList(Integer JavaDoc rolePropertiesId) throws SystemException, Bug
284     {
285         Database db = CastorDatabaseService.getDatabase();
286         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
287
288         List JavaDoc digitalAssetVOList = new ArrayList JavaDoc();
289
290         beginTransaction(db);
291
292         try
293         {
294             RoleProperties roleProperties = RolePropertiesController.getController().getRolePropertiesWithId(rolePropertiesId, db);
295             if(roleProperties != null)
296             {
297                 Collection JavaDoc digitalAssets = roleProperties.getDigitalAssets();
298                 digitalAssetVOList = toVOList(digitalAssets);
299             }
300                         
301             commitTransaction(db);
302         }
303         catch(Exception JavaDoc e)
304         {
305             logger.info("An error occurred when we tried to fetch the list of digitalAssets belonging to this roleProperties:" + e);
306             e.printStackTrace();
307             rollbackTransaction(db);
308             throw new SystemException(e.getMessage());
309         }
310         
311         return digitalAssetVOList;
312     }
313
314     
315     /**
316      * This method deletes the relation to a digital asset - not the asset itself.
317      */

318     public void deleteDigitalAssetRelation(Integer JavaDoc rolePropertiesId, DigitalAsset digitalAsset, Database db) throws SystemException, Bug
319     {
320         RoleProperties roleProperties = getRolePropertiesWithId(rolePropertiesId, db);
321         roleProperties.getDigitalAssets().remove(digitalAsset);
322         digitalAsset.getRoleProperties().remove(roleProperties);
323     }
324
325
326     /**
327      * This method fetches all content types available for this role.
328      */

329     
330     public List JavaDoc getContentTypeDefinitionVOList(String JavaDoc roleName) throws ConstraintException, SystemException
331     {
332         Database db = CastorDatabaseService.getDatabase();
333         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
334
335         List JavaDoc contentTypeDefinitionVOList = new ArrayList JavaDoc();
336
337         beginTransaction(db);
338
339         try
340         {
341             List JavaDoc roleContentTypeDefinitionList = getRoleContentTypeDefinitionList(roleName, db);
342             Iterator JavaDoc contentTypeDefinitionsIterator = roleContentTypeDefinitionList.iterator();
343             while(contentTypeDefinitionsIterator.hasNext())
344             {
345                 RoleContentTypeDefinition roleContentTypeDefinition = (RoleContentTypeDefinition)contentTypeDefinitionsIterator.next();
346                 contentTypeDefinitionVOList.add(roleContentTypeDefinition.getContentTypeDefinition().getValueObject());
347             }
348     
349             ceb.throwIfNotEmpty();
350     
351             commitTransaction(db);
352         }
353         catch(ConstraintException ce)
354         {
355             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
356             rollbackTransaction(db);
357             throw ce;
358         }
359         catch(Exception JavaDoc e)
360         {
361             logger.error("An error occurred so we should not complete the transaction:" + e, e);
362             rollbackTransaction(db);
363             throw new SystemException(e.getMessage());
364         }
365
366         return contentTypeDefinitionVOList;
367     }
368
369     /**
370      * This method fetches all role content types available for this role within a transaction.
371      */

372     
373     public List JavaDoc getRoleContentTypeDefinitionList(String JavaDoc roleName, Database db) throws ConstraintException, SystemException, Exception JavaDoc
374     {
375         List JavaDoc roleContentTypeDefinitionList = new ArrayList JavaDoc();
376
377         OQLQuery oql = db.getOQLQuery("SELECT f FROM org.infoglue.cms.entities.management.impl.simple.RoleContentTypeDefinitionImpl f WHERE f.roleName = $1");
378         oql.bind(roleName);
379
380         QueryResults results = oql.execute();
381         this.logger.info("Fetching entity in read/write mode");
382
383         while (results.hasMore())
384         {
385             RoleContentTypeDefinition roleContentTypeDefinition = (RoleContentTypeDefinition)results.next();
386             roleContentTypeDefinitionList.add(roleContentTypeDefinition);
387         }
388
389         results.close();
390         oql.close();
391
392         return roleContentTypeDefinitionList;
393     }
394     
395     /**
396      * This method fetches all content types available for this role.
397      */

398
399     public void updateContentTypeDefinitions(String JavaDoc roleName, String JavaDoc[] contentTypeDefinitionIds) throws ConstraintException, SystemException
400     {
401         Database db = CastorDatabaseService.getDatabase();
402         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
403
404         List JavaDoc contentTypeDefinitionVOList = new ArrayList JavaDoc();
405
406         beginTransaction(db);
407
408         try
409         {
410             List JavaDoc roleContentTypeDefinitionList = this.getRoleContentTypeDefinitionList(roleName, db);
411             Iterator JavaDoc contentTypeDefinitionsIterator = roleContentTypeDefinitionList.iterator();
412             while(contentTypeDefinitionsIterator.hasNext())
413             {
414                 RoleContentTypeDefinition roleContentTypeDefinition = (RoleContentTypeDefinition)contentTypeDefinitionsIterator.next();
415                 db.remove(roleContentTypeDefinition);
416             }
417             
418             for(int i=0; i<contentTypeDefinitionIds.length; i++)
419             {
420                 Integer JavaDoc contentTypeDefinitionId = new Integer JavaDoc(contentTypeDefinitionIds[i]);
421                 ContentTypeDefinition contentTypeDefinition = ContentTypeDefinitionController.getController().getContentTypeDefinitionWithId(contentTypeDefinitionId, db);
422                 RoleContentTypeDefinitionImpl roleContentTypeDefinitionImpl = new RoleContentTypeDefinitionImpl();
423                 roleContentTypeDefinitionImpl.setRoleName(roleName);
424                 roleContentTypeDefinitionImpl.setContentTypeDefinition(contentTypeDefinition);
425                 db.create(roleContentTypeDefinitionImpl);
426             }
427             
428             ceb.throwIfNotEmpty();
429
430             commitTransaction(db);
431         }
432         catch(ConstraintException ce)
433         {
434             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
435             rollbackTransaction(db);
436             throw ce;
437         }
438         catch(Exception JavaDoc e)
439         {
440             logger.error("An error occurred so we should not complete the transaction:" + e, e);
441             rollbackTransaction(db);
442             throw new SystemException(e.getMessage());
443         }
444     }
445     
446     
447     /**
448      * This method fetches a value from the xml that is the roleProperties Value. It then updates that
449      * single value and saves it back to the db.
450      */

451      
452     public void updateAttributeValue(Integer JavaDoc rolePropertiesId, String JavaDoc attributeName, String JavaDoc attributeValue) throws SystemException, Bug
453     {
454         RolePropertiesVO rolePropertiesVO = getRolePropertiesVOWithId(rolePropertiesId);
455         
456         if(rolePropertiesVO != null)
457         {
458             try
459             {
460                 logger.info("attributeName:" + attributeName);
461                 logger.info("versionValue:" + rolePropertiesVO.getValue());
462                 logger.info("attributeValue:" + attributeValue);
463                 InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(rolePropertiesVO.getValue()));
464                 
465                 DOMParser parser = new DOMParser();
466                 parser.parse(inputSource);
467                 Document JavaDoc document = parser.getDocument();
468                 
469                 NodeList JavaDoc nl = document.getDocumentElement().getChildNodes();
470                 Node attributesNode = nl.item(0);
471                 
472                 boolean existed = false;
473                 nl = attributesNode.getChildNodes();
474                 for(int i=0; i<nl.getLength(); i++)
475                 {
476                     Node n = nl.item(i);
477                     if(n.getNodeName().equalsIgnoreCase(attributeName))
478                     {
479                         if(n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null)
480                         {
481                             n.getFirstChild().setNodeValue(attributeValue);
482                             existed = true;
483                             break;
484                         }
485                         else
486                         {
487                             CDATASection JavaDoc cdata = document.createCDATASection(attributeValue);
488                             n.appendChild(cdata);
489                             existed = true;
490                             break;
491                         }
492                     }
493                 }
494                 
495                 if(existed == false)
496                 {
497                     org.w3c.dom.Element JavaDoc attributeElement = document.createElement(attributeName);
498                     attributesNode.appendChild(attributeElement);
499                     CDATASection JavaDoc cdata = document.createCDATASection(attributeValue);
500                     attributeElement.appendChild(cdata);
501                 }
502                 
503                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
504                 org.infoglue.cms.util.XMLHelper.serializeDom(document.getDocumentElement(), sb);
505                 logger.info("sb:" + sb);
506                 rolePropertiesVO.setValue(sb.toString());
507                 update(rolePropertiesVO.getLanguageId(), rolePropertiesVO.getContentTypeDefinitionId(), rolePropertiesVO);
508             }
509             catch(Exception JavaDoc e)
510             {
511                 e.printStackTrace();
512             }
513         }
514     }
515
516     
517     /**
518      * Returns the value of a Role Property
519      */

520
521     public String JavaDoc getAttributeValue(String JavaDoc roleName, Integer JavaDoc languageId, String JavaDoc attributeName) throws SystemException
522     {
523         String JavaDoc value = "";
524         
525         Database db = CastorDatabaseService.getDatabase();
526
527         beginTransaction(db);
528
529         try
530         {
531             List JavaDoc roleProperties = this.getRolePropertiesList(roleName, languageId, db, true);
532             Iterator JavaDoc iterator = roleProperties.iterator();
533             RoleProperties roleProperty = null;
534             while(iterator.hasNext())
535             {
536                 roleProperty = (RoleProperties)iterator.next();
537                 break;
538             }
539             
540             value = this.getAttributeValue(roleProperty.getValue(), attributeName, false);
541             
542             
543             commitTransaction(db);
544         }
545         catch(Exception JavaDoc e)
546         {
547             logger.error("An error occurred so we should not complete the transaction:" + e, e);
548             rollbackTransaction(db);
549             throw new SystemException(e.getMessage());
550         }
551         
552         return value;
553     }
554
555     
556     /**
557      * This method fetches a value from the xml that is the roleProperties Value.
558      */

559      
560     public String JavaDoc getAttributeValue(Integer JavaDoc rolePropertiesId, String JavaDoc attributeName, boolean escapeHTML) throws SystemException, Bug
561     {
562         String JavaDoc value = "";
563         
564         RolePropertiesVO rolePropertiesVO = getRolePropertiesVOWithId(rolePropertiesId);
565         
566         if(rolePropertiesVO != null)
567         {
568             value = getAttributeValue(rolePropertiesVO.getValue(), attributeName, escapeHTML);
569         }
570
571         return value;
572     }
573
574     /**
575      * This method fetches a value from the xml that is the roleProperties Value.
576      */

577      
578     public String JavaDoc getAttributeValue(String JavaDoc xml, String JavaDoc attributeName, boolean escapeHTML) throws SystemException, Bug
579     {
580         String JavaDoc value = "";
581         
582         try
583         {
584             InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(xml));
585             
586             DOMParser parser = new DOMParser();
587             parser.parse(inputSource);
588             Document JavaDoc document = parser.getDocument();
589             
590             NodeList JavaDoc nl = document.getDocumentElement().getChildNodes();
591             Node n = nl.item(0);
592             
593             nl = n.getChildNodes();
594             for(int i=0; i<nl.getLength(); i++)
595             {
596                 n = nl.item(i);
597                 if(n.getNodeName().equalsIgnoreCase(attributeName))
598                 {
599                     if(n.getFirstChild() != null && n.getFirstChild().getNodeValue() != null)
600                     {
601                         value = n.getFirstChild().getNodeValue();
602                         if(value != null && escapeHTML)
603                             value = new VisualFormatter().escapeHTML(value);
604
605                         break;
606                     }
607                 }
608             }
609         }
610         catch(Exception JavaDoc e)
611         {
612             e.printStackTrace();
613         }
614
615         return value;
616     }
617     
618     
619     /**
620      * Returns the related Contents
621      * @param rolePropertiesId
622      * @return
623      */

624
625     public List JavaDoc getRelatedContents(String JavaDoc roleName, Integer JavaDoc languageId, String JavaDoc attributeName) throws SystemException
626     {
627         Database db = CastorDatabaseService.getDatabase();
628
629         List JavaDoc relatedContentVOList = new ArrayList JavaDoc();
630
631         beginTransaction(db);
632
633         try
634         {
635             List JavaDoc roleProperties = this.getRolePropertiesList(roleName, languageId, db, true);
636             Iterator JavaDoc iterator = roleProperties.iterator();
637             RoleProperties roleProperty = null;
638             while(iterator.hasNext())
639             {
640                 roleProperty = (RoleProperties)iterator.next();
641                 break;
642             }
643             
644             String JavaDoc xml = this.getAttributeValue(roleProperty.getValue(), attributeName, false);
645             List JavaDoc contents = this.getRelatedContentsFromXML(db, xml);
646
647             relatedContentVOList = toVOList(contents);
648             
649             commitTransaction(db);
650         }
651         catch(Exception JavaDoc e)
652         {
653             logger.error("An error occurred so we should not complete the transaction:" + e, e);
654             rollbackTransaction(db);
655             throw new SystemException(e.getMessage());
656         }
657         
658         return relatedContentVOList;
659     }
660
661     /**
662      * Returns the related SiteNodes
663      * @param rolePropertiesId
664      * @return
665      */

666
667     public List JavaDoc getRelatedSiteNodes(String JavaDoc roleName, Integer JavaDoc languageId, String JavaDoc attributeName) throws SystemException
668     {
669         Database db = CastorDatabaseService.getDatabase();
670
671         List JavaDoc relatedSiteNodeVOList = new ArrayList JavaDoc();
672
673         beginTransaction(db);
674
675         try
676         {
677             List JavaDoc roleProperties = this.getRolePropertiesList(roleName, languageId, db, true);
678             Iterator JavaDoc iterator = roleProperties.iterator();
679             RoleProperties roleProperty = null;
680             while(iterator.hasNext())
681             {
682                 roleProperty = (RoleProperties)iterator.next();
683                 break;
684             }
685             
686             String JavaDoc xml = this.getAttributeValue(roleProperty.getValue(), attributeName, false);
687             List JavaDoc siteNodes = this.getRelatedSiteNodesFromXML(db, xml);
688
689             relatedSiteNodeVOList = toVOList(siteNodes);
690             
691             commitTransaction(db);
692         }
693         catch(Exception JavaDoc e)
694         {
695             logger.error("An error occurred so we should not complete the transaction:" + e, e);
696             rollbackTransaction(db);
697             throw new SystemException(e.getMessage());
698         }
699         
700         return relatedSiteNodeVOList;
701     }
702
703
704     /**
705      * Parses contents from an XML within a transaction
706      * @param qualifyerXML
707      * @return
708      */

709
710     private List JavaDoc getRelatedContentsFromXML(Database db, String JavaDoc qualifyerXML)
711     {
712         List JavaDoc contents = new ArrayList JavaDoc();
713         
714         if(qualifyerXML == null || qualifyerXML.length() == 0)
715             return contents;
716         
717         try
718         {
719             org.dom4j.Document document = new DOMBuilder().getDocument(qualifyerXML);
720             
721             String JavaDoc entity = document.getRootElement().attributeValue("entity");
722             
723             List JavaDoc children = document.getRootElement().elements();
724             Iterator JavaDoc i = children.iterator();
725             while(i.hasNext())
726             {
727                 Element child = (Element)i.next();
728                 String JavaDoc id = child.getStringValue();
729                 
730                 Content content = ContentController.getContentController().getContentWithId(new Integer JavaDoc(id), db);
731                 contents.add(content);
732             }
733         }
734         catch(Exception JavaDoc e)
735         {
736             e.printStackTrace();
737         }
738         
739         return contents;
740     }
741
742     /**
743      * Parses siteNodes from an XML within a transaction
744      * @param qualifyerXML
745      * @return
746      */

747
748     private List JavaDoc getRelatedSiteNodesFromXML(Database db, String JavaDoc qualifyerXML)
749     {
750         List JavaDoc siteNodes = new ArrayList JavaDoc();
751         
752         if(qualifyerXML == null || qualifyerXML.length() == 0)
753             return siteNodes;
754         
755         try
756         {
757             org.dom4j.Document document = new DOMBuilder().getDocument(qualifyerXML);
758             
759             String JavaDoc entity = document.getRootElement().attributeValue("entity");
760             
761             List JavaDoc children = document.getRootElement().elements();
762             Iterator JavaDoc i = children.iterator();
763             while(i.hasNext())
764             {
765                 Element child = (Element)i.next();
766                 String JavaDoc id = child.getStringValue();
767                 
768                 SiteNode siteNode = SiteNodeController.getController().getSiteNodeWithId(new Integer JavaDoc(id), db);
769                 siteNodes.add(siteNode);
770             }
771         }
772         catch(Exception JavaDoc e)
773         {
774             e.printStackTrace();
775         }
776         
777         return siteNodes;
778     }
779
780     
781     /**
782      * Returns all current Category relationships for th specified attribute name
783      * @param attribute
784      * @return
785      */

786     
787     public List JavaDoc getRelatedCategories(String JavaDoc roleName, Integer JavaDoc languageId, String JavaDoc attribute)
788     {
789         List JavaDoc relatedCategories = new ArrayList JavaDoc();
790         
791         try
792         {
793             List JavaDoc rolePropertiesVOList = this.getRolePropertiesVOList(roleName, languageId);
794             Iterator JavaDoc iterator = rolePropertiesVOList.iterator();
795             RolePropertiesVO rolePropertyVO = null;
796             while(iterator.hasNext())
797             {
798                 rolePropertyVO = (RolePropertiesVO)iterator.next();
799                 break;
800             }
801
802             if(rolePropertyVO != null && rolePropertyVO.getId() != null)
803             {
804                 List JavaDoc propertiesCategoryVOList = PropertiesCategoryController.getController().findByPropertiesAttribute(attribute, RoleProperties.class.getName(), rolePropertyVO.getId());
805                 Iterator JavaDoc propertiesCategoryVOListIterator = propertiesCategoryVOList.iterator();
806                 while(propertiesCategoryVOListIterator.hasNext())
807                 {
808                     PropertiesCategoryVO propertiesCategoryVO = (PropertiesCategoryVO)propertiesCategoryVOListIterator.next();
809                     relatedCategories.add(propertiesCategoryVO.getCategory());
810                 }
811             }
812         }
813         catch(Exception JavaDoc e)
814         {
815             logger.warn("We could not fetch the list of defined category keys: " + e.getMessage(), e);
816         }
817
818         return relatedCategories;
819     }
820     /**
821      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
822      * is handling.
823      */

824
825     public BaseEntityVO getNewVO()
826     {
827         return new RolePropertiesVO();
828     }
829
830 }
831  
Popular Tags