KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import org.apache.log4j.Logger;
34 import org.exolab.castor.jdo.Database;
35 import org.exolab.castor.jdo.OQLQuery;
36 import org.exolab.castor.jdo.QueryResults;
37 import org.infoglue.cms.applications.common.VisualFormatter;
38 import org.infoglue.cms.entities.content.Content;
39 import org.infoglue.cms.entities.content.ContentVO;
40 import org.infoglue.cms.entities.content.ContentVersionVO;
41 import org.infoglue.cms.entities.kernel.BaseEntityVO;
42 import org.infoglue.cms.entities.management.Language;
43 import org.infoglue.cms.entities.management.LanguageVO;
44 import org.infoglue.cms.entities.management.Repository;
45 import org.infoglue.cms.entities.management.ServiceDefinition;
46 import org.infoglue.cms.entities.management.ServiceDefinitionVO;
47 import org.infoglue.cms.entities.management.SiteNodeTypeDefinition;
48 import org.infoglue.cms.entities.management.impl.simple.RepositoryImpl;
49 import org.infoglue.cms.entities.management.impl.simple.SiteNodeTypeDefinitionImpl;
50 import org.infoglue.cms.entities.structure.ServiceBindingVO;
51 import org.infoglue.cms.entities.structure.SiteNode;
52 import org.infoglue.cms.entities.structure.SiteNodeVO;
53 import org.infoglue.cms.entities.structure.SiteNodeVersion;
54 import org.infoglue.cms.entities.structure.SiteNodeVersionVO;
55 import org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl;
56 import org.infoglue.cms.entities.structure.impl.simple.SmallSiteNodeImpl;
57 import org.infoglue.cms.exception.Bug;
58 import org.infoglue.cms.exception.ConstraintException;
59 import org.infoglue.cms.exception.SystemException;
60 import org.infoglue.cms.security.InfoGluePrincipal;
61 import org.infoglue.cms.util.ConstraintExceptionBuffer;
62
63 import com.opensymphony.module.propertyset.PropertySet;
64 import com.opensymphony.module.propertyset.PropertySetManager;
65
66 public class SiteNodeController extends BaseController
67 {
68     private final static Logger logger = Logger.getLogger(SiteNodeController.class.getName());
69
70     /**
71      * Factory method
72      */

73
74     public static SiteNodeController getController()
75     {
76         return new SiteNodeController();
77     }
78
79     /**
80      * This method gets the siteNodeVO with the given id
81      */

82      
83     public static SiteNodeVO getSiteNodeVOWithId(Integer JavaDoc siteNodeId) throws SystemException, Bug
84     {
85         return (SiteNodeVO) getVOWithId(SiteNodeImpl.class, siteNodeId);
86     }
87
88     /**
89      * This method gets the siteNodeVO with the given id
90      */

91      
92     public static SiteNodeVO getSiteNodeVOWithId(Integer JavaDoc siteNodeId, Database db) throws SystemException, Bug
93     {
94         return (SiteNodeVO) getVOWithId(SiteNodeImpl.class, siteNodeId, db);
95     }
96
97     /**
98      * This method gets the siteNodeVO with the given id
99      */

100      
101     public static SiteNodeVO getSmallSiteNodeVOWithId(Integer JavaDoc siteNodeId, Database db) throws SystemException, Bug
102     {
103         return (SiteNodeVO) getVOWithId(SmallSiteNodeImpl.class, siteNodeId, db);
104     }
105
106
107     public SiteNode getSiteNodeWithId(Integer JavaDoc siteNodeId, Database db) throws SystemException, Bug
108     {
109         return getSiteNodeWithId(siteNodeId, db, false);
110     }
111
112     public static SiteNode getSiteNodeWithId(Integer JavaDoc siteNodeId, Database db, boolean readOnly) throws SystemException, Bug
113     {
114         SiteNode siteNode = null;
115         try
116         {
117             if(readOnly)
118                 siteNode = (SiteNode)db.load(org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl.class, siteNodeId, Database.ReadOnly);
119             else
120             {
121                 logger.info("Loading " + siteNodeId + " in read/write mode.");
122                 siteNode = (SiteNode)db.load(org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl.class, siteNodeId);
123             }
124         }
125         catch(Exception JavaDoc e)
126         {
127             throw new SystemException("An error occurred when we tried to fetch the SiteNode. Reason:" + e.getMessage(), e);
128         }
129     
130         if(siteNode == null)
131         {
132             throw new Bug("The SiteNode with id [" + siteNodeId + "] was not found in SiteNodeHelper.getSiteNodeWithId. This should never happen.");
133         }
134     
135         return siteNode;
136     }
137     
138
139     /**
140      * This method deletes a siteNode and also erases all the children and all versions.
141      */

142         
143     public void delete(SiteNodeVO siteNodeVO) throws ConstraintException, SystemException
144     {
145         Database db = CastorDatabaseService.getDatabase();
146         beginTransaction(db);
147         try
148         {
149             delete(siteNodeVO, db);
150             
151             commitTransaction(db);
152         }
153         catch(ConstraintException ce)
154         {
155             logger.error("An error occurred so we should not complete the transaction:" + ce, ce);
156             rollbackTransaction(db);
157             throw ce;
158         }
159         catch(Exception JavaDoc e)
160         {
161             logger.error("An error occurred so we should not complete the transaction:" + e, e);
162             rollbackTransaction(db);
163             throw new SystemException(e.getMessage());
164         }
165     }
166
167     /**
168      * This method deletes a siteNode and also erases all the children and all versions.
169      */

170         
171     public void delete(SiteNodeVO siteNodeVO, Database db) throws ConstraintException, SystemException, Exception JavaDoc
172     {
173         SiteNode siteNode = getSiteNodeWithId(siteNodeVO.getSiteNodeId(), db);
174         SiteNode parent = siteNode.getParentSiteNode();
175         if(parent != null)
176         {
177             Iterator JavaDoc childSiteNodeIterator = parent.getChildSiteNodes().iterator();
178             while(childSiteNodeIterator.hasNext())
179             {
180                 SiteNode candidate = (SiteNode)childSiteNodeIterator.next();
181                 if(candidate.getId().equals(siteNodeVO.getSiteNodeId()))
182                     deleteRecursive(siteNode, childSiteNodeIterator, db);
183             }
184         }
185         else
186         {
187             deleteRecursive(siteNode, null, db);
188         }
189     }
190
191
192     /**
193      * Recursively deletes all siteNodes and their versions.
194      * This method is a mess as we had a problem with the lazy-loading and transactions.
195      * We have to begin and commit all the time...
196      */

197     
198     private static void deleteRecursive(SiteNode siteNode, Iterator JavaDoc parentIterator, Database db) throws ConstraintException, SystemException, Exception JavaDoc
199     {
200         List JavaDoc referenceBeanList = RegistryController.getController().getReferencingObjectsForSiteNode(siteNode.getId(), db);
201         if(referenceBeanList != null && referenceBeanList.size() > 0)
202             throw new ConstraintException("SiteNode.stateId", "3405");
203
204         Collection JavaDoc children = siteNode.getChildSiteNodes();
205         Iterator JavaDoc i = children.iterator();
206         while(i.hasNext())
207         {
208             SiteNode childSiteNode = (SiteNode)i.next();
209             deleteRecursive(childSiteNode, i, db);
210         }
211         siteNode.setChildSiteNodes(new ArrayList JavaDoc());
212         
213         if(getIsDeletable(siteNode))
214         {
215             SiteNodeVersionController.deleteVersionsForSiteNode(siteNode, db);
216             
217             ServiceBindingController.deleteServiceBindingsReferencingSiteNode(siteNode, db);
218
219             if(parentIterator != null)
220                 parentIterator.remove();
221             
222             db.remove(siteNode);
223         }
224         else
225         {
226             throw new ConstraintException("SiteNodeVersion.stateId", "3400");
227         }
228     }
229
230     /**
231      * This method returns true if the sitenode does not have any published siteNodeversions or
232      * are restricted in any other way.
233      */

234     
235     private static boolean getIsDeletable(SiteNode siteNode) throws SystemException
236     {
237         boolean isDeletable = true;
238     
239         Collection JavaDoc siteNodeVersions = siteNode.getSiteNodeVersions();
240         Iterator JavaDoc versionIterator = siteNodeVersions.iterator();
241         while (versionIterator.hasNext())
242         {
243             SiteNodeVersion siteNodeVersion = (SiteNodeVersion)versionIterator.next();
244             if(siteNodeVersion.getStateId().intValue() == SiteNodeVersionVO.PUBLISHED_STATE.intValue() && siteNodeVersion.getIsActive().booleanValue() == true)
245             {
246                 logger.warn("The siteNode had a published version so we cannot delete it..");
247                 isDeletable = false;
248                 break;
249             }
250         }
251             
252         return isDeletable;
253     }
254
255     
256     public SiteNodeVO create(Integer JavaDoc parentSiteNodeId, Integer JavaDoc siteNodeTypeDefinitionId, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc repositoryId, SiteNodeVO siteNodeVO) throws ConstraintException, SystemException
257     {
258         Database db = CastorDatabaseService.getDatabase();
259         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
260
261         SiteNode siteNode = null;
262
263         beginTransaction(db);
264
265         try
266         {
267             //Here you might want to add some validate functonality?
268
siteNode = create(db, parentSiteNodeId, siteNodeTypeDefinitionId, infoGluePrincipal, repositoryId, siteNodeVO);
269              
270             //If any of the validations or setMethods reported an error, we throw them up now before create.
271
ceb.throwIfNotEmpty();
272             
273             commitTransaction(db);
274         }
275         catch(ConstraintException ce)
276         {
277             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
278             //rollbackTransaction(db);
279
throw ce;
280         }
281         catch(Exception JavaDoc e)
282         {
283             logger.error("An error occurred so we should not complete the transaction:" + e, e);
284             //rollbackTransaction(db);
285
throw new SystemException(e.getMessage());
286         }
287
288         return siteNode.getValueObject();
289     }
290     
291     public SiteNode create(Database db, Integer JavaDoc parentSiteNodeId, Integer JavaDoc siteNodeTypeDefinitionId, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc repositoryId, SiteNodeVO siteNodeVO) throws SystemException, Exception JavaDoc
292     {
293         SiteNode siteNode = null;
294
295         logger.info("******************************************");
296         logger.info("parentSiteNode:" + parentSiteNodeId);
297         logger.info("siteNodeTypeDefinition:" + siteNodeTypeDefinitionId);
298         logger.info("repository:" + repositoryId);
299         logger.info("******************************************");
300         
301         //Fetch related entities here if they should be referenced
302

303         SiteNode parentSiteNode = null;
304         SiteNodeTypeDefinition siteNodeTypeDefinition = null;
305
306         if(parentSiteNodeId != null)
307         {
308             parentSiteNode = getSiteNodeWithId(parentSiteNodeId, db);
309             if(repositoryId == null)
310                 repositoryId = parentSiteNode.getRepository().getRepositoryId();
311         }
312         
313         if(siteNodeTypeDefinitionId != null)
314             siteNodeTypeDefinition = SiteNodeTypeDefinitionController.getController().getSiteNodeTypeDefinitionWithId(siteNodeTypeDefinitionId, db);
315
316         Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db);
317
318         
319         siteNode = new SiteNodeImpl();
320         siteNode.setValueObject(siteNodeVO);
321         siteNode.setParentSiteNode((SiteNodeImpl)parentSiteNode);
322         siteNode.setRepository((RepositoryImpl)repository);
323         siteNode.setSiteNodeTypeDefinition((SiteNodeTypeDefinitionImpl)siteNodeTypeDefinition);
324         siteNode.setCreator(infoGluePrincipal.getName());
325
326         db.create(siteNode);
327         
328         if(parentSiteNode != null)
329             parentSiteNode.getChildSiteNodes().add(siteNode);
330         
331         //commitTransaction(db);
332
//siteNode = (SiteNode) createEntity(siteNode, db);
333

334         //No siteNode is an island (humhum) so we also have to create an siteNodeVersion for it.
335
SiteNodeVersionController.createInitialSiteNodeVersion(db, siteNode, infoGluePrincipal);
336                     
337         return siteNode;
338     }
339
340     /**
341      * This method creates a new SiteNode and an siteNodeVersion. It does not commit the transaction however.
342      *
343      * @param db
344      * @param parentSiteNodeId
345      * @param siteNodeTypeDefinitionId
346      * @param userName
347      * @param repositoryId
348      * @param siteNodeVO
349      * @return
350      * @throws SystemException
351      */

352     
353     public SiteNode createNewSiteNode(Database db, Integer JavaDoc parentSiteNodeId, Integer JavaDoc siteNodeTypeDefinitionId, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc repositoryId, SiteNodeVO siteNodeVO) throws SystemException
354     {
355         SiteNode siteNode = null;
356
357         try
358         {
359             logger.info("******************************************");
360             logger.info("parentSiteNode:" + parentSiteNodeId);
361             logger.info("siteNodeTypeDefinition:" + siteNodeTypeDefinitionId);
362             logger.info("repository:" + repositoryId);
363             logger.info("******************************************");
364             
365             //Fetch related entities here if they should be referenced
366

367             SiteNode parentSiteNode = null;
368             SiteNodeTypeDefinition siteNodeTypeDefinition = null;
369
370             if(parentSiteNodeId != null)
371             {
372                 parentSiteNode = getSiteNodeWithId(parentSiteNodeId, db);
373                 if(repositoryId == null)
374                     repositoryId = parentSiteNode.getRepository().getRepositoryId();
375             }
376             
377             if(siteNodeTypeDefinitionId != null)
378                 siteNodeTypeDefinition = SiteNodeTypeDefinitionController.getController().getSiteNodeTypeDefinitionWithId(siteNodeTypeDefinitionId, db);
379             
380             Repository repository = RepositoryController.getController().getRepositoryWithId(repositoryId, db);
381
382             siteNode = new SiteNodeImpl();
383             siteNode.setValueObject(siteNodeVO);
384             siteNode.setParentSiteNode((SiteNodeImpl)parentSiteNode);
385             siteNode.setRepository((RepositoryImpl)repository);
386             siteNode.setSiteNodeTypeDefinition((SiteNodeTypeDefinitionImpl)siteNodeTypeDefinition);
387             siteNode.setCreator(infoGluePrincipal.getName());
388
389             //siteNode = (SiteNode) createEntity(siteNode, db);
390
db.create((SiteNode)siteNode);
391         
392             //No siteNode is an island (humhum) so we also have to create an siteNodeVersion for it.
393
SiteNodeVersion siteNodeVersion = SiteNodeVersionController.createInitialSiteNodeVersion(db, siteNode, infoGluePrincipal);
394         
395             List JavaDoc siteNodeVersions = new ArrayList JavaDoc();
396             siteNodeVersions.add(siteNodeVersion);
397             siteNode.setSiteNodeVersions(siteNodeVersions);
398         }
399         catch(Exception JavaDoc e)
400         {
401             throw new SystemException("An error occurred when we tried to create the SiteNode in the database. Reason:" + e.getMessage(), e);
402         }
403         
404         return siteNode;
405     }
406
407
408     /**
409      * This method returns the value-object of the parent of a specific siteNode.
410      */

411     
412     public static SiteNodeVO getParentSiteNode(Integer JavaDoc siteNodeId) throws SystemException, Bug
413     {
414         Database db = CastorDatabaseService.getDatabase();
415         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
416         SiteNodeVO parentSiteNodeVO = null;
417         
418         beginTransaction(db);
419
420         try
421         {
422             SiteNode parent = getParentSiteNode(siteNodeId, db);
423             if(parent != null)
424                 parentSiteNodeVO = parent.getValueObject();
425             
426             commitTransaction(db);
427         }
428         catch(Exception JavaDoc e)
429         {
430             logger.error("An error occurred so we should not complete the transaction:" + e, e);
431             rollbackTransaction(db);
432             throw new SystemException(e.getMessage());
433         }
434         return parentSiteNodeVO;
435     }
436     
437     /**
438      * This method returns the value-object of the parent of a specific siteNode.
439      */

440     
441     public static SiteNode getParentSiteNode(Integer JavaDoc siteNodeId, Database db) throws SystemException, Bug
442     {
443         SiteNode siteNode = (SiteNode) getObjectWithId(SiteNodeImpl.class, siteNodeId, db);
444         SiteNode parent = siteNode.getParentSiteNode();
445
446         return parent;
447     }
448     
449     /**
450      * This method returns a list of the children a siteNode has.
451      */

452     
453     public List JavaDoc getSiteNodeChildren(Integer JavaDoc parentSiteNodeId) throws ConstraintException, SystemException
454     {
455         Database db = CastorDatabaseService.getDatabase();
456         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
457
458         List JavaDoc childrenVOList = null;
459
460         beginTransaction(db);
461
462         try
463         {
464             SiteNode siteNode = getSiteNodeWithId(parentSiteNodeId, db);
465             Collection JavaDoc children = siteNode.getChildSiteNodes();
466             childrenVOList = SiteNodeController.toVOList(children);
467             
468             //If any of the validations or setMethods reported an error, we throw them up now before create.
469
ceb.throwIfNotEmpty();
470             
471             commitTransaction(db);
472         }
473         catch(ConstraintException ce)
474         {
475             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
476             rollbackTransaction(db);
477             throw ce;
478         }
479         catch(Exception JavaDoc e)
480         {
481             logger.error("An error occurred so we should not complete the transaction:" + e, e);
482             rollbackTransaction(db);
483             throw new SystemException(e.getMessage());
484         }
485         
486         return childrenVOList;
487     }
488     
489     
490     /**
491      * This method is sort of a sql-query-like method where you can send in arguments in form of a list
492      * of things that should match. The input is a Hashmap with a method and a List of HashMaps.
493      */

494     
495     public static List JavaDoc getSiteNodeVOList(HashMap JavaDoc argumentHashMap) throws SystemException, Bug
496     {
497         List JavaDoc siteNodes = null;
498         
499         String JavaDoc method = (String JavaDoc)argumentHashMap.get("method");
500         logger.info("method:" + method);
501         
502         if(method.equalsIgnoreCase("selectSiteNodeListOnIdList"))
503         {
504             siteNodes = new ArrayList JavaDoc();
505             List JavaDoc arguments = (List JavaDoc)argumentHashMap.get("arguments");
506             logger.info("Arguments:" + arguments.size());
507             Iterator JavaDoc argumentIterator = arguments.iterator();
508             while(argumentIterator.hasNext())
509             {
510                 HashMap JavaDoc argument = (HashMap JavaDoc)argumentIterator.next();
511                 logger.info("argument:" + argument.size());
512                  
513                 Iterator JavaDoc iterator = argument.keySet().iterator();
514                 while ( iterator.hasNext() )
515                    logger.info( " " + iterator.next() );
516
517
518                 Integer JavaDoc siteNodeId = new Integer JavaDoc((String JavaDoc)argument.get("siteNodeId"));
519                 logger.info("Getting the siteNode with Id:" + siteNodeId);
520                 siteNodes.add(getSiteNodeVOWithId(siteNodeId));
521             }
522         }
523         
524         return siteNodes;
525     }
526
527     /**
528      * This method is sort of a sql-query-like method where you can send in arguments in form of a list
529      * of things that should match. The input is a Hashmap with a method and a List of HashMaps.
530      */

531     
532     public static List JavaDoc getSiteNodeVOList(HashMap JavaDoc argumentHashMap, Database db) throws SystemException, Bug
533     {
534         List JavaDoc siteNodes = null;
535         
536         String JavaDoc method = (String JavaDoc)argumentHashMap.get("method");
537         logger.info("method:" + method);
538         
539         if(method.equalsIgnoreCase("selectSiteNodeListOnIdList"))
540         {
541             siteNodes = new ArrayList JavaDoc();
542             List JavaDoc arguments = (List JavaDoc)argumentHashMap.get("arguments");
543             logger.info("Arguments:" + arguments.size());
544             Iterator JavaDoc argumentIterator = arguments.iterator();
545             while(argumentIterator.hasNext())
546             {
547                 HashMap JavaDoc argument = (HashMap JavaDoc)argumentIterator.next();
548                 logger.info("argument:" + argument.size());
549                  
550                 Iterator JavaDoc iterator = argument.keySet().iterator();
551                 while ( iterator.hasNext() )
552                    logger.info( " " + iterator.next() );
553
554
555                 Integer JavaDoc siteNodeId = new Integer JavaDoc((String JavaDoc)argument.get("siteNodeId"));
556                 logger.info("Getting the siteNode with Id:" + siteNodeId);
557                 siteNodes.add(getSmallSiteNodeVOWithId(siteNodeId, db));
558             }
559         }
560         
561         return siteNodes;
562     }
563     /**
564      * This method fetches the root siteNode for a particular repository.
565      */

566             
567     public SiteNodeVO getRootSiteNodeVO(Integer JavaDoc repositoryId) throws ConstraintException, SystemException
568     {
569         Database db = CastorDatabaseService.getDatabase();
570         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
571
572         SiteNode siteNode = null;
573
574         beginTransaction(db);
575
576         try
577         {
578             siteNode = getRootSiteNode(repositoryId, db);
579             
580             commitTransaction(db);
581
582             //If any of the validations or setMethods reported an error, we throw them up now before create.
583
ceb.throwIfNotEmpty();
584         }
585         catch(Exception JavaDoc e)
586         {
587             logger.error("An error occurred so we should not complete the transaction:" + e, e);
588             rollbackTransaction(db);
589             throw new SystemException(e.getMessage());
590         }
591
592         return (siteNode == null) ? null : siteNode.getValueObject();
593     }
594
595     /**
596      * This method fetches the root siteNode for a particular repository within a certain transaction.
597      */

598             
599     public SiteNode getRootSiteNode(Integer JavaDoc repositoryId, Database db) throws ConstraintException, SystemException, Exception JavaDoc
600     {
601         SiteNode siteNode = null;
602         
603         OQLQuery oql = db.getOQLQuery( "SELECT s FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl s WHERE is_undefined(s.parentSiteNode) AND s.repository.repositoryId = $1");
604         oql.bind(repositoryId);
605         
606         QueryResults results = oql.execute();
607         this.logger.info("Fetching entity in read/write mode" + repositoryId);
608
609         if (results.hasMore())
610         {
611             siteNode = (SiteNode)results.next();
612         }
613
614         results.close();
615         oql.close();
616
617         return siteNode;
618     }
619
620
621     /**
622      * This method moves a siteNode after first making a couple of controls that the move is valid.
623      */

624     
625     public void moveSiteNode(SiteNodeVO siteNodeVO, Integer JavaDoc newParentSiteNodeId) throws ConstraintException, SystemException
626     {
627         Database db = CastorDatabaseService.getDatabase();
628         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
629
630         SiteNode siteNode = null;
631         SiteNode newParentSiteNode = null;
632         SiteNode oldParentSiteNode = null;
633         
634         beginTransaction(db);
635
636         try
637         {
638             //Validation that checks the entire object
639
siteNodeVO.validate();
640             
641             if(newParentSiteNodeId == null)
642             {
643                 logger.warn("You must specify the new parent-siteNode......");
644                 throw new ConstraintException("SiteNode.parentSiteNodeId", "3403");
645             }
646
647             if(siteNodeVO.getId().intValue() == newParentSiteNodeId.intValue())
648             {
649                 logger.warn("You cannot have the siteNode as it's own parent......");
650                 throw new ConstraintException("SiteNode.parentSiteNodeId", "3401");
651             }
652             
653             siteNode = getSiteNodeWithId(siteNodeVO.getSiteNodeId(), db);
654             oldParentSiteNode = siteNode.getParentSiteNode();
655             newParentSiteNode = getSiteNodeWithId(newParentSiteNodeId, db);
656             
657             if(oldParentSiteNode.getId().intValue() == newParentSiteNodeId.intValue())
658             {
659                 logger.warn("You cannot specify the same node as it originally was located in......");
660                 throw new ConstraintException("SiteNode.parentSiteNodeId", "3404");
661             }
662
663             SiteNode tempSiteNode = newParentSiteNode.getParentSiteNode();
664             while(tempSiteNode != null)
665             {
666                 if(tempSiteNode.getId().intValue() == siteNode.getId().intValue())
667                 {
668                     logger.warn("You cannot move the node to a child under it......");
669                     throw new ConstraintException("SiteNode.parentSiteNodeId", "3402");
670                 }
671                 tempSiteNode = tempSiteNode.getParentSiteNode();
672             }
673             
674             logger.info("Setting the new Parent siteNode:" + siteNode.getSiteNodeId() + " " + newParentSiteNode.getSiteNodeId());
675             siteNode.setParentSiteNode((SiteNodeImpl)newParentSiteNode);
676             
677             changeRepositoryRecursive(siteNode, newParentSiteNode.getRepository());
678             //siteNode.setRepository(newParentSiteNode.getRepository());
679
newParentSiteNode.getChildSiteNodes().add(siteNode);
680             oldParentSiteNode.getChildSiteNodes().remove(siteNode);
681             
682             //If any of the validations or setMethods reported an error, we throw them up now before create.
683
ceb.throwIfNotEmpty();
684             
685             commitTransaction(db);
686         }
687         catch(ConstraintException ce)
688         {
689             logger.warn("An error occurred so we should not complete the transaction:" + ce, ce);
690             rollbackTransaction(db);
691             throw ce;
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     }
701     
702     /**
703      * Recursively sets the sitenodes repositoryId.
704      * @param sitenode
705      * @param newRepository
706      */

707
708     private void changeRepositoryRecursive(SiteNode siteNode, Repository newRepository)
709     {
710         if(siteNode.getRepository().getId().intValue() != newRepository.getId().intValue())
711         {
712             siteNode.setRepository((RepositoryImpl)newRepository);
713             Iterator JavaDoc ChildSiteNodesIterator = siteNode.getChildSiteNodes().iterator();
714             while(ChildSiteNodesIterator.hasNext())
715             {
716                 SiteNode childSiteNode = (SiteNode)ChildSiteNodesIterator.next();
717                 changeRepositoryRecursive(childSiteNode, newRepository);
718             }
719         }
720     }
721     
722     
723     /**
724      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
725      * is handling.
726      */

727
728     public BaseEntityVO getNewVO()
729     {
730         return new SiteNodeVO();
731     }
732
733     /**
734      * This method returns a list of all siteNodes in a repository.
735      */

736
737     public List JavaDoc getRepositorySiteNodes(Integer JavaDoc repositoryId, Database db) throws SystemException, Exception JavaDoc
738     {
739         List JavaDoc siteNodes = new ArrayList JavaDoc();
740         
741         OQLQuery oql = db.getOQLQuery("SELECT sn FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl sn WHERE sn.repository.repositoryId = $1");
742         oql.bind(repositoryId);
743         
744         QueryResults results = oql.execute(Database.ReadOnly);
745         
746         while(results.hasMore())
747         {
748             SiteNode siteNode = (SiteNodeImpl)results.next();
749             siteNodes.add(siteNode);
750         }
751         
752         results.close();
753         oql.close();
754
755         return siteNodes;
756     }
757     
758     /**
759      * This method creates a meta info content for the new sitenode.
760      *
761      * @param db
762      * @param path
763      * @param newSiteNode
764      * @throws SystemException
765      * @throws Bug
766      * @throws Exception
767      * @throws ConstraintException
768      */

769     
770     public Content createSiteNodeMetaInfoContent(Database db, SiteNode newSiteNode, Integer JavaDoc repositoryId, InfoGluePrincipal principal, Integer JavaDoc pageTemplateContentId) throws SystemException, Bug, Exception JavaDoc, ConstraintException
771     {
772         Content content = null;
773         
774         String JavaDoc basePath = "Meta info folder";
775         String JavaDoc path = "";
776         
777         SiteNode parentSiteNode = newSiteNode.getParentSiteNode();
778         while(parentSiteNode != null)
779         {
780             path = "/" + parentSiteNode.getName() + path;
781             parentSiteNode = parentSiteNode.getParentSiteNode();
782         }
783         path = basePath + path;
784         
785         SiteNodeVersion siteNodeVersion = SiteNodeVersionController.getController().getLatestSiteNodeVersion(db, newSiteNode.getId(), false);
786         Language masterLanguage = LanguageController.getController().getMasterLanguage(db, repositoryId);
787        
788         ServiceDefinitionVO singleServiceDefinitionVO = null;
789         
790         Integer JavaDoc metaInfoContentTypeDefinitionId = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithName("Meta info", db).getId();
791         Integer JavaDoc availableServiceBindingId = AvailableServiceBindingController.getController().getAvailableServiceBindingWithName("Meta information", db, false).getId();
792
793         List JavaDoc serviceDefinitions = AvailableServiceBindingController.getController().getServiceDefinitionVOList(db, availableServiceBindingId);
794         if(serviceDefinitions == null || serviceDefinitions.size() == 0)
795         {
796             ServiceDefinition serviceDefinition = ServiceDefinitionController.getController().getServiceDefinitionWithName("Core content service", db, false);
797             String JavaDoc[] values = {serviceDefinition.getId().toString()};
798             AvailableServiceBindingController.getController().update(availableServiceBindingId, values, db);
799             singleServiceDefinitionVO = serviceDefinition.getValueObject();
800         }
801         else if(serviceDefinitions.size() == 1)
802         {
803             singleServiceDefinitionVO = (ServiceDefinitionVO)serviceDefinitions.get(0);
804         }
805         
806         ContentVO parentFolderContentVO = null;
807         
808         Content rootContent = ContentControllerProxy.getController().getRootContent(db, repositoryId, principal.getName(), true);
809         if(rootContent != null)
810         {
811             ContentVO parentFolderContent = ContentController.getContentController().getContentVOWithPath(repositoryId, path, true, principal, db);
812             
813             ContentVO contentVO = new ContentVO();
814             contentVO.setCreatorName(principal.getName());
815             contentVO.setIsBranch(new Boolean JavaDoc(false));
816             contentVO.setName(newSiteNode.getName() + " Metainfo");
817             contentVO.setRepositoryId(repositoryId);
818
819             content = ContentControllerProxy.getController().create(db, parentFolderContent.getId(), metaInfoContentTypeDefinitionId, repositoryId, contentVO);
820             
821             newSiteNode.setMetaInfoContentId(contentVO.getId());
822             
823             String JavaDoc componentStructure = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><components></components>";
824             if(pageTemplateContentId != null)
825             {
826                 Integer JavaDoc languageId = LanguageController.getController().getMasterLanguage(db, repositoryId).getId();
827                 ContentVersionVO contentVersionVO = ContentVersionController.getContentVersionController().getLatestActiveContentVersionVO(pageTemplateContentId, languageId, db);
828                 
829                 componentStructure = ContentVersionController.getContentVersionController().getAttributeValue(contentVersionVO, "ComponentStructure", false);
830             }
831             
832             //Create initial content version also... in masterlanguage
833
String JavaDoc versionValue = "<?xml version='1.0' encoding='UTF-8'?><article xmlns=\"x-schema:ArticleSchema.xml\"><attributes><Title><![CDATA[" + newSiteNode.getName() + "]]></Title><NavigationTitle><![CDATA[" + newSiteNode.getName() + "]]></NavigationTitle><NiceURIName><![CDATA[" + new VisualFormatter().replaceNonAscii(newSiteNode.getName(), '_') + "]]></NiceURIName><Description><![CDATA[" + newSiteNode.getName() + "]]></Description><MetaInfo><![CDATA[" + newSiteNode.getName() + "]]></MetaInfo><ComponentStructure><![CDATA[" + componentStructure + "]]></ComponentStructure></attributes></article>";
834             ContentVersionVO contentVersionVO = new ContentVersionVO();
835             contentVersionVO.setVersionComment("Autogenerated version");
836             contentVersionVO.setVersionModifier(principal.getName());
837             contentVersionVO.setVersionValue(versionValue);
838             ContentVersionController.getContentVersionController().create(contentVO.getId(), masterLanguage.getId(), contentVersionVO, null, db);
839
840             //Also created a version in the local master language for this part of the site if any
841
LanguageVO localMasterLanguageVO = getInitialLanguageVO(db, parentFolderContent.getId(), repositoryId);
842             if(localMasterLanguageVO.getId().intValue() != masterLanguage.getId().intValue())
843             {
844                 String JavaDoc versionValueLocalMaster = "<?xml version='1.0' encoding='UTF-8'?><article xmlns=\"x-schema:ArticleSchema.xml\"><attributes><Title><![CDATA[" + newSiteNode.getName() + "]]></Title><NavigationTitle><![CDATA[" + newSiteNode.getName() + "]]></NavigationTitle><NiceURIName><![CDATA[" + new VisualFormatter().replaceNonAscii(newSiteNode.getName(), '_') + "]]></NiceURIName><Description><![CDATA[" + newSiteNode.getName() + "]]></Description><MetaInfo><![CDATA[" + newSiteNode.getName() + "]]></MetaInfo><ComponentStructure><![CDATA[]]></ComponentStructure></attributes></article>";
845                 ContentVersionVO contentVersionVOLocalMaster = new ContentVersionVO();
846                 contentVersionVOLocalMaster.setVersionComment("Autogenerated version");
847                 contentVersionVOLocalMaster.setVersionModifier(principal.getName());
848                 contentVersionVOLocalMaster.setVersionValue(versionValueLocalMaster);
849                 ContentVersionController.getContentVersionController().create(contentVO.getId(), localMasterLanguageVO.getId(), contentVersionVOLocalMaster, null, db);
850             }
851             
852             ServiceBindingVO serviceBindingVO = new ServiceBindingVO();
853             serviceBindingVO.setName(newSiteNode.getName() + " Metainfo");
854             serviceBindingVO.setPath("/None specified/");
855         
856             String JavaDoc qualifyerXML = "<?xml version='1.0' encoding='UTF-8'?><qualifyer><contentId>" + contentVO.getId() + "</contentId></qualifyer>";
857         
858             ServiceBindingController.getController().create(db, serviceBindingVO, qualifyerXML, availableServiceBindingId, siteNodeVersion.getId(), singleServiceDefinitionVO.getId());
859         }
860
861         return content;
862     }
863
864     public LanguageVO getInitialLanguageVO(Database db, Integer JavaDoc contentId, Integer JavaDoc repositoryId) throws Exception JavaDoc
865     {
866         Map JavaDoc args = new HashMap JavaDoc();
867         args.put("globalKey", "infoglue");
868         PropertySet ps = PropertySetManager.getInstance("jdbc", args);
869
870         String JavaDoc initialLanguageId = ps.getString("content_" + contentId + "_initialLanguageId");
871         Content content = ContentController.getContentController().getContentWithId(contentId, db);
872         Content parentContent = content.getParentContent();
873         while((initialLanguageId == null || initialLanguageId.equalsIgnoreCase("-1")) && parentContent != null)
874         {
875             initialLanguageId = ps.getString("content_" + parentContent.getId() + "_initialLanguageId");
876             parentContent = parentContent.getParentContent();
877         }
878         
879         if(initialLanguageId != null && !initialLanguageId.equals("") && !initialLanguageId.equals("-1"))
880             return LanguageController.getController().getLanguageVOWithId(new Integer JavaDoc(initialLanguageId));
881         else
882             return LanguageController.getController().getMasterLanguage(repositoryId);
883     }
884
885     /**
886      * Recursive methods to get all sitenodes under the specific sitenode.
887      */

888     
889     public List JavaDoc getSiteNodeVOWithParentRecursive(Integer JavaDoc siteNodeId) throws ConstraintException, SystemException
890     {
891         return getSiteNodeVOWithParentRecursive(siteNodeId, new ArrayList JavaDoc());
892     }
893     
894     private List JavaDoc getSiteNodeVOWithParentRecursive(Integer JavaDoc siteNodeId, List JavaDoc resultList) throws ConstraintException, SystemException
895     {
896         // Get the versions of this content.
897
resultList.add(getSiteNodeVOWithId(siteNodeId));
898         
899         // Get the children of this content and do the recursion
900
List JavaDoc childSiteNodeList = SiteNodeController.getController().getSiteNodeChildren(siteNodeId);
901         Iterator JavaDoc cit = childSiteNodeList.iterator();
902         while (cit.hasNext())
903         {
904             SiteNodeVO siteNodeVO = (SiteNodeVO) cit.next();
905             getSiteNodeVOWithParentRecursive(siteNodeVO.getId(), resultList);
906         }
907     
908         return resultList;
909     }
910
911
912     public void setMetaInfoContentId(Integer JavaDoc siteNodeId, Integer JavaDoc metaInfoContentId) throws ConstraintException, SystemException
913     {
914         Database db = CastorDatabaseService.getDatabase();
915         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
916
917         beginTransaction(db);
918
919         try
920         {
921             setMetaInfoContentId(siteNodeId, metaInfoContentId, db);
922             
923             commitTransaction(db);
924         }
925         catch(Exception JavaDoc e)
926         {
927             logger.error("An error occurred so we should not complete the transaction:" + e, e);
928             rollbackTransaction(db);
929             throw new SystemException(e.getMessage());
930         }
931
932     }
933
934     public void setMetaInfoContentId(Integer JavaDoc siteNodeId, Integer JavaDoc metaInfoContentId, Database db) throws ConstraintException, SystemException
935     {
936         SiteNode siteNode = getSiteNodeWithId(siteNodeId, db);
937         siteNode.setMetaInfoContentId(metaInfoContentId);
938     }
939     
940     
941     public List JavaDoc getSiteNodeVOListWithoutMetaInfoContentId() throws ConstraintException, SystemException
942     {
943         List JavaDoc siteNodeVOList = new ArrayList JavaDoc();
944
945         Database db = CastorDatabaseService.getDatabase();
946         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
947
948         beginTransaction(db);
949
950         try
951         {
952             List JavaDoc siteNodes = getSiteNodesWithoutMetaInfoContentId(db);
953             siteNodeVOList = toVOList(siteNodes);
954             
955             commitTransaction(db);
956         }
957         catch(Exception JavaDoc e)
958         {
959             logger.error("An error occurred so we should not complete the transaction:" + e, e);
960             rollbackTransaction(db);
961             throw new SystemException(e.getMessage());
962         }
963         
964         return siteNodeVOList;
965     }
966
967     public List JavaDoc getSiteNodesWithoutMetaInfoContentId(Database db) throws ConstraintException, SystemException, Exception JavaDoc
968     {
969         List JavaDoc siteNodes = new ArrayList JavaDoc();
970
971         OQLQuery oql = db.getOQLQuery("SELECT sn FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl sn WHERE sn.metaInfoContentId = $1");
972         oql.bind(new Integer JavaDoc(-1));
973         
974         QueryResults results = oql.execute();
975         
976         while(results.hasMore())
977         {
978             SiteNode siteNode = (SiteNodeImpl)results.next();
979             siteNodes.add(siteNode);
980         }
981
982         results.close();
983         oql.close();
984
985         return siteNodes;
986     }
987
988 }
989  
990
Popular Tags