KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > controllers > kernel > impl > simple > NodeDeliveryController


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.deliver.controllers.kernel.impl.simple;
25
26 import java.net.URLEncoder JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.http.HttpSession JavaDoc;
38
39 import org.apache.log4j.Logger;
40 import org.exolab.castor.jdo.Database;
41 import org.exolab.castor.jdo.OQLQuery;
42 import org.exolab.castor.jdo.QueryResults;
43 import org.infoglue.cms.controllers.kernel.impl.simple.CastorDatabaseService;
44 import org.infoglue.cms.entities.content.Content;
45 import org.infoglue.cms.entities.content.ContentVO;
46 import org.infoglue.cms.entities.content.impl.simple.ContentImpl;
47 import org.infoglue.cms.entities.management.AvailableServiceBinding;
48 import org.infoglue.cms.entities.management.AvailableServiceBindingVO;
49 import org.infoglue.cms.entities.management.LanguageVO;
50 import org.infoglue.cms.entities.management.RepositoryVO;
51 import org.infoglue.cms.entities.management.ServiceDefinitionVO;
52 import org.infoglue.cms.entities.management.SiteNodeTypeDefinition;
53 import org.infoglue.cms.entities.structure.Qualifyer;
54 import org.infoglue.cms.entities.structure.ServiceBinding;
55 import org.infoglue.cms.entities.structure.SiteNode;
56 import org.infoglue.cms.entities.structure.SiteNodeVO;
57 import org.infoglue.cms.entities.structure.SiteNodeVersion;
58 import org.infoglue.cms.entities.structure.SiteNodeVersionVO;
59 import org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl;
60 import org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl;
61 import org.infoglue.cms.entities.structure.impl.simple.SmallSiteNodeImpl;
62 import org.infoglue.cms.exception.Bug;
63 import org.infoglue.cms.exception.SystemException;
64 import org.infoglue.cms.security.InfoGluePrincipal;
65 import org.infoglue.cms.services.BaseService;
66 import org.infoglue.cms.util.CmsPropertyHandler;
67 import org.infoglue.cms.util.ConstraintExceptionBuffer;
68 import org.infoglue.deliver.applications.databeans.DeliveryContext;
69 import org.infoglue.deliver.applications.filters.URIMapperCache;
70 import org.infoglue.deliver.applications.filters.ViewPageFilter;
71 import org.infoglue.deliver.controllers.kernel.URLComposer;
72 import org.infoglue.deliver.util.CacheController;
73 import org.infoglue.deliver.util.HttpHelper;
74 import org.infoglue.deliver.util.NullObject;
75
76
77 public class NodeDeliveryController extends BaseDeliveryController
78 {
79     private final static Logger logger = Logger.getLogger(NodeDeliveryController.class.getName());
80
81     private URLComposer urlComposer = null;
82
83     protected static final String JavaDoc META_INFO_BINDING_NAME = "Meta information";
84     protected static final String JavaDoc NAV_TITLE_ATTRIBUTE_NAME = "NavigationTitle";
85
86     private static final boolean USE_INHERITANCE = true;
87     private static final boolean DO_NOT_USE_INHERITANCE = false;
88     
89     protected static final Integer JavaDoc NO = new Integer JavaDoc(0);
90     protected static final Integer JavaDoc YES = new Integer JavaDoc(1);
91     protected static final Integer JavaDoc INHERITED = new Integer JavaDoc(2);
92
93     private Integer JavaDoc siteNodeId = null;
94     private Integer JavaDoc languageId = null;
95     private Integer JavaDoc contentId = null;
96     
97     /**
98      * Private constructor to enforce factory-use
99      */

100     
101     private NodeDeliveryController(Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId) throws SystemException, Exception JavaDoc
102     {
103         this.siteNodeId = siteNodeId;
104         this.languageId = languageId;
105         this.contentId = contentId;
106         this.urlComposer = URLComposer.getURLComposer();
107     }
108     
109     /**
110      * Factory method
111      */

112     
113     public static NodeDeliveryController getNodeDeliveryController(Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId) throws SystemException, Exception JavaDoc
114     {
115         return new NodeDeliveryController(siteNodeId, languageId, contentId);
116     }
117
118     /**
119      * Factory method
120      */

121     
122     public static NodeDeliveryController getNodeDeliveryController(DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
123     {
124         return new NodeDeliveryController(deliveryContext.getSiteNodeId(), deliveryContext.getLanguageId(), deliveryContext.getContentId());
125     }
126
127     /**
128      * This method returns which mode the delivery-engine is running in.
129      * The mode is important to be able to show working, preview and published data separate.
130      */

131     
132     private Integer JavaDoc getOperatingMode()
133     {
134         Integer JavaDoc operatingMode = new Integer JavaDoc(0); //Default is working
135
try
136         {
137             operatingMode = new Integer JavaDoc(CmsPropertyHandler.getOperatingMode());
138             //logger.info("Operating mode is:" + operatingMode);
139
}
140         catch(Exception JavaDoc e)
141         {
142             logger.warn("We could not get the operating mode from the propertyFile:" + e.getMessage(), e);
143         }
144         return operatingMode;
145     }
146     
147     /**
148      * This method gets the appropriate siteNodeVersion
149      */

150
151     public SiteNodeVersionVO getActiveSiteNodeVersionVO(Database db, Integer JavaDoc siteNodeId) throws Exception JavaDoc
152     {
153         SiteNodeVersionVO siteNodeVersionVO = null;
154         
155         SiteNodeVersion siteNodeVersion = getActiveSiteNodeVersion(siteNodeId, db);
156         if(siteNodeVersion != null)
157             siteNodeVersionVO = siteNodeVersion.getValueObject();
158         
159         return siteNodeVersionVO;
160     }
161     
162     /**
163      * This method gets the appropriate siteNodeVersion
164      */

165     
166     public SiteNodeVersion getActiveSiteNodeVersion(Integer JavaDoc siteNodeId, Database db) throws Exception JavaDoc
167     {
168         SiteNodeVersion siteNodeVersion = null;
169         
170         SiteNode siteNode = (SiteNode)this.getObjectWithId(SiteNodeImpl.class, siteNodeId, db);
171         logger.info("Loaded siteNode " + siteNode.getName());
172         Collection JavaDoc siteNodeVersions = siteNode.getSiteNodeVersions();
173         logger.info("Loaded versions " + siteNodeVersions.size());
174         
175         Iterator JavaDoc versionIterator = siteNodeVersions.iterator();
176         while(versionIterator.hasNext())
177         {
178             SiteNodeVersion siteNodeVersionCandidate = (SiteNodeVersion)versionIterator.next();
179             logger.info("SiteNodeVersionCandidate " + siteNodeVersionCandidate.getId());
180             if(siteNodeVersionCandidate.getIsActive().booleanValue() && siteNodeVersionCandidate.getStateId().intValue() >= getOperatingMode().intValue())
181             {
182                 if(siteNodeVersionCandidate.getOwningSiteNode().getSiteNodeId().intValue() == siteNodeId.intValue())
183                 {
184                     if(siteNodeVersion == null || siteNodeVersion.getSiteNodeVersionId().intValue() < siteNodeVersionCandidate.getId().intValue())
185                     {
186                         siteNodeVersion = siteNodeVersionCandidate;
187                     }
188                 }
189             }
190         }
191         
192         return siteNodeVersion;
193     }
194     
195     /**
196      * This method checks if there is a serviceBinding with the name on this or any parent node.
197      */

198     
199     public ServiceDefinitionVO getInheritedServiceDefinition(List JavaDoc qualifyerList, Integer JavaDoc siteNodeId, AvailableServiceBindingVO availableServiceBindingVO, Database db, boolean inheritParentBindings) throws SystemException, Exception JavaDoc
200     {
201         logger.info("Trying to find binding " + availableServiceBindingVO.getName() + " on siteNodeId:" + siteNodeId);
202         ServiceDefinitionVO serviceDefinitionVO = null;
203
204         SiteNode siteNode = (SiteNode)this.getObjectWithId(SmallSiteNodeImpl.class, siteNodeId, db);
205         logger.info("Loaded siteNode " + siteNode.getName());
206
207         serviceDefinitionVO = getServiceDefinitionVO(qualifyerList, siteNode, availableServiceBindingVO, db);
208         logger.info("Loaded serviceDefinitionVO " + serviceDefinitionVO);
209         
210         if(serviceDefinitionVO == null)
211         {
212             //We check if the available service definition state that this is a inheritable binding
213
//AvailableServiceBinding availableServiceBinding = getAvailableServiceBindingRecursive(siteNodeVersion.getOwningSiteNode(), availableServiceBindingName, inheritParentBindings);
214
if(availableServiceBindingVO != null && availableServiceBindingVO.getIsInheritable().booleanValue() && inheritParentBindings)
215             {
216                 logger.info("No binding found - lets try the parent.");
217                 SiteNode parent = siteNode.getParentSiteNode();
218                 if(parent != null)
219                     serviceDefinitionVO = getInheritedServiceDefinition(qualifyerList, parent.getSiteNodeId(), availableServiceBindingVO, db, inheritParentBindings);
220             }
221         }
222         
223         //try{ throw new Exception("APA"); } catch(Exception e){e.printStackTrace();}
224

225         logger.info("Loaded serviceDefinitionVO end...");
226         
227         return serviceDefinitionVO;
228     }
229     
230     private ServiceDefinitionVO getServiceDefinitionVO(List JavaDoc qualifyerList, SiteNode siteNode, AvailableServiceBindingVO availableServiceBindingVO, Database db) throws Exception JavaDoc
231     {
232         ServiceDefinitionVO serviceDefinitionVO = null;
233         //ServiceBinding serviceBinding = null;
234

235         String JavaDoc key = "" + siteNode.getSiteNodeId() + "_" + availableServiceBindingVO.getId();
236         logger.info("key:" + key);
237         Object JavaDoc object = CacheController.getCachedObject("serviceDefinitionCache", key);
238         Object JavaDoc object2 = CacheController.getCachedObject("qualifyerListCache", key);
239         if(object != null && object2 != null)
240         {
241             logger.info("There was an cached ServiceDefinitionVO:" + object);
242             if(object instanceof ServiceDefinitionVO)
243                 serviceDefinitionVO = (ServiceDefinitionVO)object;
244             if(object2 instanceof List JavaDoc)
245                 qualifyerList.addAll((List JavaDoc)object2);
246         }
247         else
248         {
249             OQLQuery oql = db.getOQLQuery( "SELECT sb FROM org.infoglue.cms.entities.structure.impl.simple.ServiceBindingImpl sb WHERE sb.siteNodeVersion.owningSiteNode = $1 AND sb.availableServiceBinding = $2 AND sb.siteNodeVersion.isActive = $3 AND sb.siteNodeVersion.stateId >= $4 order by sb.siteNodeVersion.siteNodeVersionId DESC");
250             oql.bind(siteNode);
251             oql.bind(availableServiceBindingVO.getId());
252             oql.bind(new Boolean JavaDoc(true));
253             oql.bind(getOperatingMode());
254             
255             QueryResults results = oql.execute(Database.ReadOnly);
256
257             if (results.hasMore())
258             {
259                 ServiceBinding serviceBinding = (ServiceBinding)results.next();
260                 SiteNodeVersionVO latestSiteNodeVersionVO = getLatestActiveSiteNodeVersionVO(db, siteNode.getId());
261                 logger.info("serviceBinding sitenodeVersion:" + serviceBinding.getSiteNodeVersion().getId() + ":" + latestSiteNodeVersionVO.getId());
262                 if(serviceBinding.getSiteNodeVersion().getId().equals(latestSiteNodeVersionVO.getId()))
263                 {
264                     serviceDefinitionVO = serviceBinding.getServiceDefinition().getValueObject();
265                     Collection JavaDoc qualifyers = serviceBinding.getBindingQualifyers();
266                     
267                     qualifyers = sortQualifyers(qualifyers);
268                     
269                     Iterator JavaDoc iterator = qualifyers.iterator();
270                     while(iterator.hasNext())
271                     {
272                         Qualifyer qualifyer = (Qualifyer)iterator.next();
273                         HashMap JavaDoc argument = new HashMap JavaDoc();
274                         argument.put(qualifyer.getName(), qualifyer.getValue());
275                         qualifyerList.add(argument);
276                     }
277                     
278                     object = serviceDefinitionVO;
279                     object2 = qualifyerList;
280                 }
281             }
282             else
283             {
284                 object = new NullObject();
285                 object2 = new NullObject();
286             }
287             
288             
289             results.close();
290             oql.close();
291
292             CacheController.cacheObject("serviceDefinitionCache", key, object);
293             CacheController.cacheObject("qualifyerListCache", key, object2);
294
295         }
296             
297             
298         /*
299         Collection serviceBindings = siteNodeVersion.getServiceBindings();
300         Iterator serviceBindingIterator = serviceBindings.iterator();
301         while(serviceBindingIterator.hasNext())
302         {
303             ServiceBinding serviceBindingCandidate = (ServiceBinding)serviceBindingIterator.next();
304             //logger.warn("siteNodeVersion " + siteNodeVersion.getId());
305             if(serviceBindingCandidate.getAvailableServiceBinding().getAvailableServiceBindingId().intValue() == availableServiceBindingVO.getId().intValue())
306             //if(serviceBindingCandidate.getValueObject().getAvailableServiceBindingId().intValue() == availableServiceBindingVO.getId().intValue())
307             {
308                 serviceBinding = serviceBindingCandidate;
309             }
310         }
311         */

312         
313         return serviceDefinitionVO;
314     }
315     
316     /**
317      * This method fetches an available service binding as long as there is one associated with this site nodes type definition or any
318      * of the parent site node type definitions.
319      */

320     
321     private AvailableServiceBinding getAvailableServiceBindingRecursive(SiteNode siteNode, String JavaDoc availableServiceBindingName, boolean inheritParentBindings)
322     {
323         if(siteNode == null || availableServiceBindingName == null)
324             return null;
325         
326         AvailableServiceBinding availableServiceBinding = null;
327         
328         SiteNodeTypeDefinition siteNodeTypeDefinition = siteNode.getSiteNodeTypeDefinition();
329         if(siteNodeTypeDefinition != null)
330         {
331             Collection JavaDoc availableServiceBindings = siteNodeTypeDefinition.getAvailableServiceBindings();
332             
333             Iterator JavaDoc availableServiceBindingsIterator = availableServiceBindings.iterator();
334             while(availableServiceBindingsIterator.hasNext())
335             {
336                 AvailableServiceBinding currentAvailableServiceBinding = (AvailableServiceBinding)availableServiceBindingsIterator.next();
337                 if(currentAvailableServiceBinding.getName().equalsIgnoreCase(availableServiceBindingName))
338                 {
339                     availableServiceBinding = currentAvailableServiceBinding;
340                 }
341             }
342         }
343                 
344         if(availableServiceBinding == null)
345             availableServiceBinding = getAvailableServiceBindingRecursive(siteNode.getParentSiteNode(), availableServiceBindingName, inheritParentBindings);
346             
347         return availableServiceBinding;
348     }
349
350
351     /**
352      * This method returns the SiteNodeVO that is sent in.
353      */

354     
355     public SiteNode getSiteNode(Database db, Integer JavaDoc siteNodeId) throws SystemException
356     {
357         SiteNode siteNode = null;
358         
359         siteNode = (SiteNode)getObjectWithId(SiteNodeImpl.class, siteNodeId, db);
360
361         return siteNode;
362     }
363
364     /**
365      * This method returns the latest sitenodeVersion there is for the given siteNode.
366      */

367     
368     public SiteNodeVersionVO getLatestActiveSiteNodeVersionVO(Database db, Integer JavaDoc siteNodeId) throws SystemException, Exception JavaDoc
369     {
370         String JavaDoc key = "" + siteNodeId;
371         logger.info("key:" + key);
372         SiteNodeVersionVO siteNodeVersionVO = (SiteNodeVersionVO)CacheController.getCachedObject("latestSiteNodeVersionCache", key);
373         if(siteNodeVersionVO != null)
374         {
375             if(logger.isInfoEnabled())
376                 logger.info("There was an cached siteNodeVersionVO:" + siteNodeVersionVO);
377         }
378         else
379         {
380             SiteNodeVersion siteNodeVersion = getLatestActiveSiteNodeVersion(siteNodeId, db);
381             if(siteNodeVersion != null)
382                 siteNodeVersionVO = siteNodeVersion.getValueObject();
383             
384             CacheController.cacheObject("latestSiteNodeVersionCache", key, siteNodeVersionVO);
385         }
386                 
387         return siteNodeVersionVO;
388     }
389
390     /**
391      * This method returns the latest sitenodeVersion there is for the given siteNode and stores it in a cache special to the deliver page cache.
392      */

393     
394     public SiteNodeVersionVO getLatestActiveSiteNodeVersionVOForPageCache(Database db, Integer JavaDoc siteNodeId) throws SystemException, Exception JavaDoc
395     {
396         String JavaDoc key = "" + siteNodeId;
397         logger.info("key:" + key);
398         SiteNodeVersionVO siteNodeVersionVO = (SiteNodeVersionVO)CacheController.getCachedObject("pageCacheLatestSiteNodeVersions", key);
399         if(siteNodeVersionVO != null)
400         {
401             if(logger.isInfoEnabled())
402                 logger.info("There was an cached siteNodeVersionVO:" + siteNodeVersionVO);
403         }
404         else
405         {
406             SiteNodeVersion siteNodeVersion = getLatestActiveSiteNodeVersion(siteNodeId, db);
407             if(siteNodeVersion != null)
408                 siteNodeVersionVO = siteNodeVersion.getValueObject();
409             
410             CacheController.cacheObject("pageCacheLatestSiteNodeVersions", key, siteNodeVersionVO);
411         }
412                 
413         return siteNodeVersionVO;
414     }
415
416
417     /**
418      * This method returns the latest sitenodeVersion there is for the given siteNode.
419      */

420     /*
421     public SiteNodeVersion getLatestActiveSiteNodeVersion(Integer siteNodeId, Database db) throws SystemException, Exception
422     {
423         SiteNodeVersion siteNodeVersion = null;
424         
425         logger.info("Loading siteNode " + siteNodeId);
426         SiteNode siteNode = (SiteNode)this.getObjectWithId(SiteNodeImpl.class, siteNodeId, db);
427         logger.info("siteNode " + siteNode.getName());
428         Collection siteNodeVersions = siteNode.getSiteNodeVersions();
429         logger.info("siteNodeVersions " + siteNodeVersions);
430         
431         Iterator versionIterator = siteNodeVersions.iterator();
432         while(versionIterator.hasNext())
433         {
434             SiteNodeVersion siteNodeVersionCandidate = (SiteNodeVersion)versionIterator.next();
435             logger.info("SiteNodeVersionCandidate " + siteNodeVersionCandidate.getId());
436             if(siteNodeVersionCandidate.getIsActive().booleanValue() && siteNodeVersionCandidate.getStateId().intValue() >= getOperatingMode().intValue())
437             {
438                 if(siteNodeVersionCandidate.getOwningSiteNode().getSiteNodeId().intValue() == siteNodeId.intValue())
439                 {
440                     if(siteNodeVersion == null || siteNodeVersion.getSiteNodeVersionId().intValue() < siteNodeVersionCandidate.getId().intValue())
441                     {
442                         siteNodeVersion = siteNodeVersionCandidate;
443                     }
444                 }
445             }
446         }
447         
448         return siteNodeVersion;
449     }
450     */

451     
452     /**
453      * This method returns the latest sitenodeVersion there is for the given siteNode.
454      */

455     
456     public SiteNodeVersion getLatestActiveSiteNodeVersion(Integer JavaDoc siteNodeId, Database db) throws SystemException, Exception JavaDoc
457     {
458         SiteNodeVersion siteNodeVersion = null;
459         
460         String JavaDoc versionKey = "" + siteNodeId + "_" + getOperatingMode() + "_siteNodeVersionId";
461         //System.out.println("versionKey:" + versionKey);
462

463         
464         Integer JavaDoc siteNodeVersionId = (Integer JavaDoc)CacheController.getCachedObject("latestSiteNodeVersionCache", versionKey);
465         if(siteNodeVersionId != null)
466         {
467             logger.info("There was a cached sitenode version id:" + siteNodeVersionId);
468             //System.out.println("There was a cached content version id:" + contentVersionId);
469
siteNodeVersion = (SiteNodeVersion)getObjectWithId(SiteNodeVersionImpl.class, siteNodeVersionId, db);
470             //System.out.println("Loaded the version from cache instead of querying it:" + contentVersionId);
471
//logger.info("contentVersion read");
472
}
473         else
474         {
475             OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeVersionImpl cv WHERE cv.owningSiteNode.siteNodeId = $1 AND cv.stateId >= $2 AND cv.isActive = $3 ORDER BY cv.siteNodeVersionId desc");
476             oql.bind(siteNodeId);
477             oql.bind(getOperatingMode());
478             oql.bind(true);
479             
480             QueryResults results = oql.execute(Database.ReadOnly);
481             
482             if (results.hasMore())
483             {
484                 siteNodeVersion = (SiteNodeVersion)results.next();
485                 CacheController.cacheObject("latestSiteNodeVersionCache", versionKey, siteNodeVersion.getId());
486             }
487         
488             results.close();
489             oql.close();
490         }
491         
492         //if(contentVersion != null)
493
// deliveryContext.addUsedContentVersion("contentVersion_" + contentVersion.getId());
494

495         return siteNodeVersion;
496     }
497     
498
499     /**
500      * This method returns the SiteNodeVO that is the parent to the one sent in.
501      */

502     
503     public SiteNodeVO getParentSiteNode(Database db, Integer JavaDoc siteNodeId) throws SystemException
504     {
505         String JavaDoc key = "" + siteNodeId;
506         logger.info("key getParentSiteNode:" + key);
507         Object JavaDoc object = CacheController.getCachedObject("parentSiteNodeCache", key);
508         SiteNodeVO parentSiteNodeVO = null;
509         logger.info("object:" + object);
510         if(object instanceof NullObject)
511         {
512             logger.info("There was an cached parentSiteNodeVO but it was null:" + object);
513         }
514         else if(object != null)
515         {
516             logger.info("There was an cached parentSiteNodeVO:" + parentSiteNodeVO);
517             parentSiteNodeVO = (SiteNodeVO)object;
518         }
519         else
520         {
521             logger.info("There was no cached parentSiteNodeVO:" + parentSiteNodeVO);
522             
523             SiteNode siteNode = (SiteNode)getObjectWithId(SmallSiteNodeImpl.class, siteNodeId, db);
524             SiteNode parentSiteNode = siteNode.getParentSiteNode();
525             if(parentSiteNode != null)
526             {
527                 parentSiteNodeVO = parentSiteNode.getValueObject();
528                 CacheController.cacheObject("parentSiteNodeCache", key, parentSiteNodeVO);
529                 logger.info("Caching parentSiteNodeVO:" + parentSiteNodeVO);
530             }
531             else
532             {
533                 logger.info("Caching parentSiteNodeVO: NullObject");
534                 CacheController.cacheObject("parentSiteNodeCache", key, new NullObject());
535             }
536             
537             
538         }
539         
540         return parentSiteNodeVO;
541     }
542
543     /**
544      * This method returns the SiteNodeVO that is the parent to the one sent in.
545      */

546     
547     public SiteNodeVO getParentSiteNodeForPageCache(Database db, Integer JavaDoc siteNodeId) throws SystemException
548     {
549         String JavaDoc key = "" + siteNodeId;
550         logger.info("key getParentSiteNode:" + key);
551         Object JavaDoc object = CacheController.getCachedObject("pageCacheParentSiteNodeCache", key);
552         SiteNodeVO parentSiteNodeVO = null;
553         logger.info("object:" + object);
554         if(object instanceof NullObject)
555         {
556             logger.info("There was an cached parentSiteNodeVO but it was null:" + object);
557         }
558         else if(object != null)
559         {
560             logger.info("There was an cached parentSiteNodeVO:" + parentSiteNodeVO);
561             parentSiteNodeVO = (SiteNodeVO)object;
562         }
563         else
564         {
565             logger.info("There was no cached parentSiteNodeVO:" + parentSiteNodeVO);
566             
567             SiteNode siteNode = (SiteNode)getObjectWithId(SmallSiteNodeImpl.class, siteNodeId, db);
568             SiteNode parentSiteNode = siteNode.getParentSiteNode();
569             if(parentSiteNode != null)
570             {
571                 parentSiteNodeVO = parentSiteNode.getValueObject();
572                 CacheController.cacheObject("pageCacheParentSiteNodeCache", key, parentSiteNodeVO);
573                 logger.info("Caching parentSiteNodeVO:" + parentSiteNodeVO);
574             }
575             else
576             {
577                 logger.info("Caching parentSiteNodeVO: NullObject");
578                 CacheController.cacheObject("pageCacheParentSiteNodeCache", key, new NullObject());
579             }
580             
581             
582         }
583         
584         return parentSiteNodeVO;
585     }
586
587
588     /**
589      * This method returns true if the if the page in question (ie sitenode) has page-caching disabled.
590      * This is essential to turn off when you have a dynamic page like an external application or searchresult.
591      */

592     
593     public boolean getIsPageCacheDisabled(Database db, Integer JavaDoc siteNodeId)
594     {
595         boolean isPageCacheDisabled = false;
596         
597         try
598         {
599             SiteNodeVersionVO latestSiteNodeVersionVO = getLatestActiveSiteNodeVersionVO(db, siteNodeId);
600             if(latestSiteNodeVersionVO != null && latestSiteNodeVersionVO.getDisablePageCache() != null)
601             {
602                 if(latestSiteNodeVersionVO.getDisablePageCache().intValue() == NO.intValue())
603                     isPageCacheDisabled = false;
604                 else if(latestSiteNodeVersionVO.getDisablePageCache().intValue() == YES.intValue())
605                     isPageCacheDisabled = true;
606                 else if(latestSiteNodeVersionVO.getDisablePageCache().intValue() == INHERITED.intValue())
607                 {
608                     SiteNodeVO parentSiteNode = this.getParentSiteNode(db, siteNodeId);
609                     if(parentSiteNode != null)
610                         isPageCacheDisabled = getIsPageCacheDisabled(db, parentSiteNode.getSiteNodeId());
611                 }
612             }
613
614         }
615         catch(Exception JavaDoc e)
616         {
617             logger.warn("An error occurred trying to get if the siteNodeVersion has disabled pageCache:" + e.getMessage(), e);
618         }
619                 
620         return isPageCacheDisabled;
621     }
622
623     /**
624      * This method returns the pageCacheKey for the page.
625      */

626     
627     public String JavaDoc getPageCacheKey(Database db, HttpSession JavaDoc session, HttpServletRequest JavaDoc request, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId, String JavaDoc userAgent, String JavaDoc queryString, String JavaDoc extra)
628     {
629         return getPageCacheKey(db, session, request, siteNodeId, languageId, contentId, userAgent, queryString, extra, true);
630     }
631     
632     /**
633      * This method returns the pageCacheKey for the page.
634      */

635     
636     public String JavaDoc getPageCacheKey(Database db, HttpSession JavaDoc session, HttpServletRequest JavaDoc request, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId, String JavaDoc userAgent, String JavaDoc queryString, String JavaDoc extra, boolean includeOriginalRequestURL)
637     {
638         String JavaDoc pageKey = CacheController.getPageCacheKey(session, request, siteNodeId, languageId, contentId, userAgent, queryString, extra);
639         try
640         {
641             
642             SiteNodeVersionVO latestSiteNodeVersionVO = getLatestActiveSiteNodeVersionVOForPageCache(db, siteNodeId);
643             Integer JavaDoc currentSiteNodeId = siteNodeId;
644             
645             while(latestSiteNodeVersionVO == null || latestSiteNodeVersionVO.getPageCacheKey() == null || latestSiteNodeVersionVO.getPageCacheKey().length() == 0 || latestSiteNodeVersionVO.getPageCacheKey().equalsIgnoreCase("default"))
646             {
647                 if(currentSiteNodeId == null)
648                     break;
649                 
650                 SiteNodeVO parentSiteNodeVO = getParentSiteNode(db, currentSiteNodeId);
651                 if(parentSiteNodeVO != null)
652                 {
653                     latestSiteNodeVersionVO = getLatestActiveSiteNodeVersionVOForPageCache(db, parentSiteNodeVO.getId());
654                     if(latestSiteNodeVersionVO != null)
655                         currentSiteNodeId = latestSiteNodeVersionVO.getSiteNodeId();
656                     else
657                         currentSiteNodeId = null;
658                 }
659                 else
660                     break;
661             }
662             
663             if(latestSiteNodeVersionVO != null && latestSiteNodeVersionVO.getPageCacheKey() != null && latestSiteNodeVersionVO.getPageCacheKey().length() > 0 && !latestSiteNodeVersionVO.getPageCacheKey().equalsIgnoreCase("default"))
664             {
665                 String JavaDoc originalRequestURL = request.getParameter("originalRequestURL");
666                 if(originalRequestURL == null || originalRequestURL.length() == 0)
667                     originalRequestURL = request.getRequestURL().toString();
668
669                 pageKey = originalRequestURL + "_" + latestSiteNodeVersionVO.getPageCacheKey();
670                 pageKey = pageKey.replaceAll("\\$siteNodeId", "" + siteNodeId);
671                 pageKey = pageKey.replaceAll("\\$languageId", "" + languageId);
672                 pageKey = pageKey.replaceAll("\\$contentId", "" + contentId);
673                 pageKey = pageKey.replaceAll("\\$useragent", "" + userAgent);
674                 pageKey = pageKey.replaceAll("\\$queryString", "" + queryString);
675             
676                 int sessionAttributeStartIndex = pageKey.indexOf("$session.");
677                 while(sessionAttributeStartIndex > -1)
678                 {
679                     int sessionAttributeEndIndex = pageKey.indexOf("_", sessionAttributeStartIndex);
680                     String JavaDoc sessionAttribute = null;
681                     if(sessionAttributeEndIndex > -1)
682                         sessionAttribute = pageKey.substring(sessionAttributeStartIndex + 9, sessionAttributeEndIndex);
683                     else
684                         sessionAttribute = pageKey.substring(sessionAttributeStartIndex + 9);
685
686                     pageKey = pageKey.replaceAll("\\$session." + sessionAttribute, "" + session.getAttribute(sessionAttribute));
687                 
688                     sessionAttributeStartIndex = pageKey.indexOf("$session.");
689                 }
690
691                 int cookieAttributeStartIndex = pageKey.indexOf("$cookie.");
692                 while(cookieAttributeStartIndex > -1)
693                 {
694                     int cookieAttributeEndIndex = pageKey.indexOf("_", cookieAttributeStartIndex);
695                     String JavaDoc cookieAttribute = null;
696                     if(cookieAttributeEndIndex > -1)
697                         cookieAttribute = pageKey.substring(cookieAttributeStartIndex + 8, cookieAttributeEndIndex);
698                     else
699                         cookieAttribute = pageKey.substring(cookieAttributeStartIndex + 8);
700
701                     HttpHelper httpHelper = new HttpHelper();
702                     pageKey = pageKey.replaceAll("\\$cookie." + cookieAttribute, "" + httpHelper.getCookie(request, cookieAttribute));
703                 
704                     cookieAttributeStartIndex = pageKey.indexOf("$cookie.");
705                 }
706
707             }
708
709         }
710         catch(Exception JavaDoc e)
711         {
712             logger.warn("An error occurred trying to get if the siteNodeVersion had a different pageCacheKey:" + e.getMessage(), e);
713         }
714
715         return pageKey;
716     }
717
718     /**
719      * This method returns true if the if the page in question (ie sitenode) has editOnSight disabled.
720      */

721     
722     public boolean getIsEditOnSightDisabled(Database db, Integer JavaDoc siteNodeId)
723     {
724         logger.info("getIsEditOnSightDisabled:" + siteNodeId);
725         
726         boolean isEditOnSightDisabled = false;
727         
728         try
729         {
730             SiteNodeVersionVO latestSiteNodeVersionVO = getLatestActiveSiteNodeVersionVO(db, siteNodeId);
731             if(latestSiteNodeVersionVO != null && latestSiteNodeVersionVO.getDisableEditOnSight() != null)
732             {
733                 if(latestSiteNodeVersionVO.getDisableEditOnSight().intValue() == NO.intValue())
734                     isEditOnSightDisabled = false;
735                 else if(latestSiteNodeVersionVO.getDisableEditOnSight().intValue() == YES.intValue())
736                     isEditOnSightDisabled = true;
737                 else if(latestSiteNodeVersionVO.getDisableEditOnSight().intValue() == INHERITED.intValue())
738                 {
739                     SiteNodeVO parentSiteNode = this.getParentSiteNode(db, siteNodeId);
740                     if(parentSiteNode != null)
741                         isEditOnSightDisabled = getIsEditOnSightDisabled(db, parentSiteNode.getSiteNodeId());
742                 }
743             }
744
745         }
746         catch(Exception JavaDoc e)
747         {
748             logger.warn("An error occurred trying to get if the siteNodeVersion has disabled pageCache:" + e.getMessage(), e);
749         }
750             
751         logger.info("getIsEditOnSightDisabled:" + isEditOnSightDisabled);
752         
753         return isEditOnSightDisabled;
754     }
755     
756     /**
757      * This method returns true if the if the page in question (ie sitenode) is protected byt the exctranet fnctionality.
758      * This is essential to turn off when you have a dynamic page like an external application or searchresult.
759      */

760     
761     public boolean getIsPageProtected(Database db, Integer JavaDoc siteNodeId)
762     {
763         boolean isPageProtected = false;
764         
765         try
766         {
767             SiteNodeVersionVO latestSiteNodeVersionVO = getLatestActiveSiteNodeVersionVO(db, siteNodeId);
768             if(latestSiteNodeVersionVO != null && latestSiteNodeVersionVO.getIsProtected() != null)
769             {
770                 if(latestSiteNodeVersionVO.getIsProtected().intValue() == NO.intValue())
771                     isPageProtected = false;
772                 else if(latestSiteNodeVersionVO.getIsProtected().intValue() == YES.intValue())
773                     isPageProtected = true;
774                 else if(latestSiteNodeVersionVO.getIsProtected().intValue() == INHERITED.intValue())
775                 {
776                     SiteNodeVO parentSiteNode = this.getParentSiteNode(db, siteNodeId);
777                     if(parentSiteNode != null)
778                         isPageProtected = getIsPageProtected(db, parentSiteNode.getSiteNodeId());
779                 }
780             }
781
782         }
783         catch(Exception JavaDoc e)
784         {
785             logger.warn("An error occurred trying to get if the siteNodeVersion has disabled pageCache:" + e.getMessage(), e);
786         }
787                 
788         return isPageProtected;
789     }
790     
791     
792     /**
793      * This method returns the id of the siteNodeVersion that is protected if any.
794      */

795     
796     public Integer JavaDoc getProtectedSiteNodeVersionId(Database db, Integer JavaDoc siteNodeId)
797     {
798         Integer JavaDoc protectedSiteNodeVersionId = null;
799         
800         try
801         {
802             SiteNodeVersionVO siteNodeVersionVO = getLatestActiveSiteNodeVersionVO(db, siteNodeId);
803             logger.info("siteNodeId:" + siteNodeId);
804             if(siteNodeVersionVO != null && siteNodeVersionVO.getIsProtected() != null)
805             {
806                 logger.info("siteNodeVersionVO:" + siteNodeVersionVO.getId() + ":" + siteNodeVersionVO.getIsProtected());
807                 if(siteNodeVersionVO.getIsProtected().intValue() == NO.intValue())
808                     protectedSiteNodeVersionId = null;
809                 else if(siteNodeVersionVO.getIsProtected().intValue() == YES.intValue())
810                     protectedSiteNodeVersionId = siteNodeVersionVO.getId();
811                 else if(siteNodeVersionVO.getIsProtected().intValue() == INHERITED.intValue())
812                 {
813                     SiteNodeVO parentSiteNode = this.getParentSiteNode(db, siteNodeId);
814                     if(parentSiteNode != null)
815                         protectedSiteNodeVersionId = getProtectedSiteNodeVersionId(db, parentSiteNode.getSiteNodeId());
816                 }
817             }
818
819         }
820         catch(Exception JavaDoc e)
821         {
822             logger.warn("An error occurred trying to get if the siteNodeVersion has disabled pageCache:" + e.getMessage(), e);
823         }
824                 
825         return protectedSiteNodeVersionId;
826     }
827
828
829     /**
830      * This method returns the id of the siteNodeVersion that is protected if any.
831      */

832     
833     public Integer JavaDoc getProtectedSiteNodeVersionIdForPageCache(Database db, Integer JavaDoc siteNodeId)
834     {
835         Integer JavaDoc protectedSiteNodeVersionId = null;
836         
837         try
838         {
839             SiteNodeVersionVO siteNodeVersionVO = this.getLatestActiveSiteNodeVersionVOForPageCache(db, siteNodeId);
840             logger.info("siteNodeId:" + siteNodeId);
841             if(siteNodeVersionVO != null && siteNodeVersionVO.getIsProtected() != null)
842             {
843                 logger.info("siteNodeVersionVO:" + siteNodeVersionVO.getId() + ":" + siteNodeVersionVO.getIsProtected());
844                 if(siteNodeVersionVO.getIsProtected().intValue() == NO.intValue())
845                     protectedSiteNodeVersionId = null;
846                 else if(siteNodeVersionVO.getIsProtected().intValue() == YES.intValue())
847                     protectedSiteNodeVersionId = siteNodeVersionVO.getId();
848                 else if(siteNodeVersionVO.getIsProtected().intValue() == INHERITED.intValue())
849                 {
850                     SiteNodeVO parentSiteNode = this.getParentSiteNodeForPageCache(db, siteNodeId);
851                     if(parentSiteNode != null)
852                         protectedSiteNodeVersionId = getProtectedSiteNodeVersionIdForPageCache(db, parentSiteNode.getSiteNodeId());
853                 }
854             }
855
856         }
857         catch(Exception JavaDoc e)
858         {
859             logger.warn("An error occurred trying to get if the siteNodeVersion has disabled pageCache:" + e.getMessage(), e);
860         }
861                 
862         return protectedSiteNodeVersionId;
863     }
864
865     
866     /**
867      * This method returns the id of the siteNodeVersion that has disabled languages if any.
868      */

869     
870     public Integer JavaDoc getDisabledLanguagesSiteNodeVersionId(Database db, Integer JavaDoc siteNodeId)
871     {
872         Integer JavaDoc disabledLanguagesSiteNodeVersionId = null;
873         
874         try
875         {
876             SiteNodeVersionVO siteNodeVersionVO = this.getLatestActiveSiteNodeVersionVOForPageCache(db, siteNodeId);
877             logger.info("siteNodeId:" + siteNodeId);
878             if(siteNodeVersionVO != null && siteNodeVersionVO.getDisableLanguages() != null)
879             {
880                 logger.info("siteNodeVersionVO:" + siteNodeVersionVO.getId() + ":" + siteNodeVersionVO.getDisableLanguages());
881                 if(siteNodeVersionVO.getDisableLanguages().intValue() == NO.intValue())
882                     disabledLanguagesSiteNodeVersionId = null;
883                 else if(siteNodeVersionVO.getDisableLanguages().intValue() == YES.intValue())
884                     disabledLanguagesSiteNodeVersionId = siteNodeVersionVO.getId();
885                 else if(siteNodeVersionVO.getDisableLanguages().intValue() == INHERITED.intValue())
886                 {
887                     SiteNodeVO parentSiteNode = this.getParentSiteNodeForPageCache(db, siteNodeId);
888                     if(parentSiteNode != null)
889                         disabledLanguagesSiteNodeVersionId = getDisabledLanguagesSiteNodeVersionId(db, parentSiteNode.getSiteNodeId());
890                 }
891             }
892
893         }
894         catch(Exception JavaDoc e)
895         {
896             logger.warn("An error occurred trying to get if the siteNodeVersion has disabled languages:" + e.getMessage(), e);
897         }
898                 
899         return disabledLanguagesSiteNodeVersionId;
900     }
901
902     /**
903      * This method return a single content bound.
904      */

905     
906     public ContentVO getBoundContent(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, boolean useLanguageFallback, String JavaDoc availableServiceBindingName, boolean inheritParentBindings, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
907     {
908         List JavaDoc contents = getBoundContents(db, infoGluePrincipal, siteNodeId, languageId, useLanguageFallback, availableServiceBindingName, inheritParentBindings, false, deliveryContext);
909         return (contents != null && contents.size() > 0) ? (ContentVO)contents.get(0) : null;
910     }
911
912
913     /**
914      * This method return a single content bound.
915      */

916     
917     public ContentVO getBoundContent(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, boolean useLanguageFallback, String JavaDoc availableServiceBindingName, boolean inheritParentBindings, boolean includeFolders, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
918     {
919         List JavaDoc contents = getBoundContents(db, infoGluePrincipal, siteNodeId, languageId, useLanguageFallback, availableServiceBindingName, inheritParentBindings, includeFolders, deliveryContext);
920         return (contents != null && contents.size() > 0) ? (ContentVO)contents.get(0) : null;
921     }
922
923     /**
924      * This method return a single content bound.
925      */

926     
927     public ContentVO getBoundContent(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, boolean useLanguageFallback, String JavaDoc availableServiceBindingName, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
928     {
929         logger.info("siteNodeId:" + siteNodeId);
930         logger.info("availableServiceBindingName:" + availableServiceBindingName);
931         List JavaDoc contents = getBoundContents(db, infoGluePrincipal, siteNodeId, languageId, useLanguageFallback, availableServiceBindingName, USE_INHERITANCE, true, deliveryContext);
932         return (contents != null && contents.size() > 0) ? (ContentVO)contents.get(0) : null;
933     }
934     
935     /**
936      * This method return a single content bound.
937      */

938     
939     public ContentVO getBoundContent(InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, boolean useLanguageFallback, String JavaDoc availableServiceBindingName, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
940     {
941         List JavaDoc contents = null;
942         
943         Database db = CastorDatabaseService.getDatabase();
944         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
945
946         beginTransaction(db);
947
948         try
949         {
950             contents = getBoundContents(db, infoGluePrincipal, siteNodeId, languageId, useLanguageFallback, availableServiceBindingName, USE_INHERITANCE, true, deliveryContext);
951         
952             closeTransaction(db);
953         }
954         catch(Exception JavaDoc e)
955         {
956             logger.error("An error occurred so we should not complete the transaction:" + e, e);
957             rollbackTransaction(db);
958             throw new SystemException(e.getMessage());
959         }
960
961         return (contents != null && contents.size() > 0) ? (ContentVO)contents.get(0) : null;
962     }
963
964
965     /**
966      * This method returns a list of contents bound to the named availableServiceBinding.
967      */

968     
969     public List JavaDoc getBoundContents(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, boolean useLanguageFallback, String JavaDoc availableServiceBindingName, boolean inheritParentBindings, boolean includeFolders, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
970     {
971         String JavaDoc boundContentsKey = "" + infoGluePrincipal.getName() + "_" + siteNodeId + "_" + languageId + "_" + useLanguageFallback + "_" + includeFolders + "_" + availableServiceBindingName + "_" + USE_INHERITANCE;
972         //String boundContentsKey = "" + siteNodeId + "_" + availableServiceBindingName + "_" + USE_INHERITANCE;
973
logger.info("boundContentsKey:" + boundContentsKey);
974         List JavaDoc boundContentVOList = (List JavaDoc)CacheController.getCachedObject("boundContentCache", boundContentsKey);
975         if(boundContentVOList != null)
976         {
977             logger.info("There was an cached content boundContentVOList:" + boundContentVOList.size());
978         }
979         else
980         {
981             boundContentVOList = new ArrayList JavaDoc();
982             
983             logger.info("Coming in with:" + siteNodeId + " and " + availableServiceBindingName);
984
985             Integer JavaDoc metaInfoContentId = null;
986             if(availableServiceBindingName.equalsIgnoreCase("Meta information"))
987             {
988                 SiteNode siteNode = getSiteNode(db, siteNodeId);
989                 metaInfoContentId = siteNode.getMetaInfoContentId();
990             }
991             
992             if(metaInfoContentId != null && metaInfoContentId.intValue() > -1)
993             {
994                 ContentVO contentVO = ContentDeliveryController.getContentDeliveryController().getContentVO(db, metaInfoContentId, deliveryContext);
995                 boundContentVOList.add(contentVO);
996             }
997             else
998             {
999                 AvailableServiceBindingVO availableServiceBindingVO = AvailableServiceBindingDeliveryController.getAvailableServiceBindingDeliveryController().getAvailableServiceBindingVO(availableServiceBindingName, db);
1000            
1001                List JavaDoc qualifyerList = new ArrayList JavaDoc();
1002                ServiceDefinitionVO serviceDefinitionVO = getInheritedServiceDefinition(qualifyerList, siteNodeId, availableServiceBindingVO, db, inheritParentBindings);
1003                if(serviceDefinitionVO != null)
1004                {
1005                    String JavaDoc serviceClassName = serviceDefinitionVO.getClassName();
1006                    BaseService service = (BaseService)Class.forName(serviceClassName).newInstance();
1007                 
1008                    HashMap JavaDoc arguments = new HashMap JavaDoc();
1009                    arguments.put("method", "selectContentListOnIdList");
1010                    
1011                    arguments.put("arguments", qualifyerList);
1012                    
1013                    List JavaDoc contents = service.selectMatchingEntities(arguments, db);
1014                    
1015                    logger.info("Found bound contents:" + contents.size());
1016                    if(contents != null)
1017                    {
1018                        Iterator JavaDoc i = contents.iterator();
1019                        while(i.hasNext())
1020                        {
1021                            ContentVO candidate = (ContentVO)i.next();
1022                            logger.info("candidate:" + candidate.getName());
1023                            //Checking to see that now is between the contents publish and expire-date.
1024
//if(ContentDeliveryController.getContentDeliveryController().isValidContent(candidate.getId(), languageId, useLanguageFallback, infoGluePrincipal))
1025
// boundContentVOList.add(candidate);
1026
Content candidateContent = (Content)getObjectWithId(ContentImpl.class, candidate.getId(), db);
1027                            
1028                            logger.info("candidateContent:" + candidateContent.getName());
1029                            if(ContentDeliveryController.getContentDeliveryController().isValidContent(infoGluePrincipal, candidateContent, languageId, useLanguageFallback, includeFolders, db, deliveryContext))
1030                            {
1031                                deliveryContext.addUsedContent("content_" + candidate.getId());
1032    
1033                                boundContentVOList.add(candidate);
1034                            }
1035                        }
1036                    }
1037                }
1038            }
1039            
1040            CacheController.cacheObject("boundContentCache", boundContentsKey, boundContentVOList);
1041        }
1042        
1043        return boundContentVOList;
1044
1045    }
1046
1047    
1048
1049    /**
1050     * This method returns a list of children to the bound content with the named availableServiceBindingName.
1051     * The collection of contents are also sorted on given arguments.
1052     */

1053    
1054    public List JavaDoc getBoundFolderContents(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, String JavaDoc availableServiceBindingName, boolean searchRecursive, Integer JavaDoc maximumNumberOfLevels, String JavaDoc sortAttribute, String JavaDoc sortOrder, boolean useLanguageFallback, boolean includeFolders, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1055    {
1056        List JavaDoc folderContents = new ArrayList JavaDoc();
1057        
1058        deliveryContext.addUsedContent("selectiveCacheUpdateNonApplicable");
1059        
1060        logger.info("Coming in with:" + siteNodeId + " and " + availableServiceBindingName + " and " + searchRecursive + " and " + maximumNumberOfLevels + " and " + sortAttribute + " and " + sortOrder);
1061        
1062        ContentVO contentVO = getBoundContent(db, infoGluePrincipal, siteNodeId, languageId, useLanguageFallback, availableServiceBindingName, includeFolders, deliveryContext);
1063        logger.info("contentVO:" + contentVO);
1064        
1065        if(contentVO != null)
1066        {
1067            folderContents = ContentDeliveryController.getContentDeliveryController().getSortedChildContents(infoGluePrincipal, languageId, contentVO.getContentId(), siteNodeId, db, searchRecursive, maximumNumberOfLevels, sortAttribute, sortOrder, useLanguageFallback, includeFolders, deliveryContext);
1068        }
1069        
1070        return folderContents;
1071    }
1072
1073
1074    /**
1075     * This method returns a list of children to the bound content with the named availableServiceBindingName.
1076     * The collection of contents are also sorted on given arguments.
1077     */

1078    
1079    public List JavaDoc getBoundFolderContents(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc contentId, Integer JavaDoc languageId, boolean searchRecursive, Integer JavaDoc maximumNumberOfLevels, String JavaDoc sortAttribute, String JavaDoc sortOrder, boolean useLanguageFallback, boolean includeFolders, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1080    {
1081        List JavaDoc folderContents = new ArrayList JavaDoc();
1082                
1083        folderContents = ContentDeliveryController.getContentDeliveryController().getSortedChildContents(infoGluePrincipal, languageId, contentId, siteNodeId, db, searchRecursive, maximumNumberOfLevels, sortAttribute, sortOrder, useLanguageFallback, includeFolders, deliveryContext);
1084        
1085        return folderContents;
1086    }
1087
1088
1089    /**
1090     * This method return a single siteNode bound.
1091     */

1092    
1093    public SiteNodeVO getBoundSiteNode(Database db, Integer JavaDoc siteNodeId, String JavaDoc availableServiceBindingName) throws SystemException, Exception JavaDoc
1094    {
1095        List JavaDoc siteNodes = getBoundSiteNodes(db, siteNodeId, availableServiceBindingName);
1096        return (siteNodes != null && siteNodes.size() > 0) ? (SiteNodeVO)siteNodes.get(0) : null;
1097    }
1098    
1099
1100    /**
1101     * This method return a single siteNode bound.
1102     */

1103    
1104    public SiteNodeVO getBoundSiteNode(Database db, Integer JavaDoc siteNodeId, String JavaDoc availableServiceBindingName, int position) throws SystemException, Exception JavaDoc
1105    {
1106        List JavaDoc siteNodes = getBoundSiteNodes(db, siteNodeId, availableServiceBindingName);
1107        return (siteNodes != null && siteNodes.size() > position) ? (SiteNodeVO)siteNodes.get(position) : null;
1108    }
1109
1110
1111    /**
1112     * This method should be rewritten later....
1113     * The concept is to fetch the bound siteNode
1114     */

1115    
1116    public List JavaDoc getBoundSiteNodes(Database db, Integer JavaDoc siteNodeId, String JavaDoc availableServiceBindingName) throws SystemException, Exception JavaDoc
1117    {
1118        String JavaDoc boundSiteNodesKey = "" + siteNodeId + "_" + availableServiceBindingName + "_" + USE_INHERITANCE;
1119        logger.info("boundSiteNodesKey:" + boundSiteNodesKey);
1120        List JavaDoc boundSiteNodeVOList = (List JavaDoc)CacheController.getCachedObject("boundSiteNodeCache", boundSiteNodesKey);
1121        if(boundSiteNodeVOList != null)
1122        {
1123            logger.info("There was an cached content boundSiteNodeVOList:" + boundSiteNodeVOList.size());
1124        }
1125        else
1126        {
1127            boundSiteNodeVOList = new ArrayList JavaDoc();
1128
1129            AvailableServiceBindingVO availableServiceBindingVO = AvailableServiceBindingDeliveryController.getAvailableServiceBindingDeliveryController().getAvailableServiceBindingVO(availableServiceBindingName, db);
1130
1131            List JavaDoc qualifyerList = new ArrayList JavaDoc();
1132            ServiceDefinitionVO serviceDefinitionVO = getInheritedServiceDefinition(qualifyerList, siteNodeId, availableServiceBindingVO, db, USE_INHERITANCE);
1133            
1134            if(serviceDefinitionVO != null)
1135            {
1136                String JavaDoc serviceClassName = serviceDefinitionVO.getClassName();
1137                BaseService service = (BaseService)Class.forName(serviceClassName).newInstance();
1138                
1139                HashMap JavaDoc arguments = new HashMap JavaDoc();
1140                arguments.put("method", "selectSiteNodeListOnIdList");
1141            
1142                arguments.put("arguments", qualifyerList);
1143            
1144                List JavaDoc siteNodes = service.selectMatchingEntities(arguments, db);
1145            
1146                logger.info("Found bound siteNodes:" + siteNodes.size());
1147                if(siteNodes != null)
1148                {
1149                    Iterator JavaDoc i = siteNodes.iterator();
1150                    while(i.hasNext())
1151                    {
1152                        SiteNodeVO candidate = (SiteNodeVO)i.next();
1153                        logger.info("candidate:" + candidate.getId());
1154                        //Checking to see that now is between the contents publish and expire-date.
1155
if(isValidSiteNode(db, candidate.getId()))
1156                            boundSiteNodeVOList.add(candidate);
1157                    }
1158                }
1159            }
1160                                            
1161            CacheController.cacheObject("boundSiteNodeCache", boundSiteNodesKey, boundSiteNodeVOList);
1162        }
1163        
1164        return boundSiteNodeVOList;
1165    }
1166
1167
1168    
1169    /**
1170     * This method returns a url to the given page. The url is composed of siteNode, language and content
1171     */

1172
1173    public String JavaDoc getPageUrl(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId, DeliveryContext deliveryContext) throws SystemException
1174    {
1175        String JavaDoc pageUrl = "";
1176
1177        pageUrl = urlComposer.composePageUrl(db, infoGluePrincipal, siteNodeId, languageId, contentId, deliveryContext);
1178        
1179        return pageUrl;
1180    }
1181
1182
1183    public String JavaDoc getPageUrlAfterLanguageChange(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId, DeliveryContext deliveryContext) throws SystemException
1184    {
1185        /*
1186        SiteNode siteNode = getSiteNode(db, siteNodeId);
1187        String dnsName = CmsPropertyHandler.getWebServerAddress();
1188        if(siteNode != null && siteNode.getRepository().getDnsName() != null && !siteNode.getRepository().getDnsName().equals(""))
1189            dnsName = siteNode.getRepository().getDnsName();
1190        */

1191        return urlComposer.composePageUrlAfterLanguageChange(db, infoGluePrincipal, siteNodeId, languageId, contentId, deliveryContext);
1192    }
1193    
1194    public String JavaDoc getPageAsDigitalAssetUrl(Database database, InfoGluePrincipal principal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId, DeliveryContext context, String JavaDoc fileSuffix) throws SystemException
1195    {
1196        String JavaDoc pageAsDigitalAssetUrl = null;
1197        
1198        boolean fullUrl = context.getUseFullUrl();
1199        context.setUseFullUrl(true);
1200        
1201        boolean disableNiceUri = context.getDisableNiceUri();
1202        context.setDisableNiceUri(true);
1203        
1204        try
1205        {
1206            //String pageCacheKey = getPageCacheKey(database, context.getHttpServletRequest().getSession(), context.getHttpServletRequest(), siteNodeId, languageId, contentId, context.getHttpServletRequest().getHeader("User-Agent"), "", "", false);
1207
//System.out.println("pageCacheKey:" + pageCacheKey);
1208

1209            String JavaDoc pageContent = null;
1210            String JavaDoc fileName = null;
1211            
1212            int i = 0;
1213            String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
1214            while(filePath != null)
1215            {
1216                try
1217                {
1218                    if(pageContent == null)
1219                    {
1220                        String JavaDoc pageUrl = getPageUrl(database, principal, siteNodeId, languageId, contentId, context);
1221                        logger.info("pageUrl:" + pageUrl);
1222                        pageUrl = pageUrl.replaceAll("&amp;", "&");
1223                        if(pageUrl.indexOf("&") > -1)
1224                        {
1225                            pageUrl = pageUrl + "&includeUsedEntities=true";
1226                        }
1227                        else
1228                        {
1229                            pageUrl = pageUrl + "?includeUsedEntities=true";
1230                        }
1231                        logger.info("pageUrl:" + pageUrl);
1232                        
1233                        Map JavaDoc headers = new HashMap JavaDoc();
1234
1235                        Enumeration JavaDoc headersEnumeration = context.getHttpServletRequest().getHeaderNames();
1236                        while(headersEnumeration.hasMoreElements())
1237                        {
1238                            String JavaDoc headerName = (String JavaDoc)headersEnumeration.nextElement();
1239                            String JavaDoc headerValue = (String JavaDoc)context.getHttpServletRequest().getHeader(headerName);
1240                            logger.info(headerName + "=" + headerValue);
1241                            headers.put(headerName, headerValue);
1242                        }
1243                        
1244                        headers.put("User-Agent", context.getHttpServletRequest().getHeader("User-Agent") + ";Java");
1245                        
1246                        HttpHelper helper = new HttpHelper();
1247                        pageContent = helper.getUrlContent(pageUrl, headers);
1248                        logger.info("pageContent:" + pageContent);
1249
1250                        int usedEntitiesIndex = pageContent.indexOf("<usedEntities>");
1251                        if(usedEntitiesIndex > -1)
1252                        {
1253                            int usedEntitiesEndIndex = pageContent.indexOf("</usedEntities>");
1254                            String JavaDoc usedEntities = pageContent.substring(usedEntitiesIndex + 14, usedEntitiesEndIndex);
1255                            String JavaDoc[] usedEntitiesArray = usedEntities.split(",");
1256                            for(int j=0; j < usedEntitiesArray.length; j++)
1257                            {
1258                                String JavaDoc entity = usedEntitiesArray[j];
1259                                logger.info("entity:" + entity);
1260
1261                                if(entity.indexOf("content_") > -1)
1262                                    context.addUsedContent(entity.substring(8));
1263                                else if(entity.indexOf("contentVersion_") > -1)
1264                                    context.addUsedContentVersion(entity.substring(15));
1265                                else if(entity.indexOf("siteNode_") > -1)
1266                                    context.addUsedSiteNode(entity.substring(9));
1267                                else if(entity.indexOf("siteNodeVersion_") > -1)
1268                                    context.addUsedSiteNodeVersion(entity.substring(16));
1269                            }
1270                        }
1271                        fileName = "" + pageContent.hashCode() + (fileSuffix == null ? "" : "." + fileSuffix);
1272                        logger.info("fileName:" + fileName);
1273                    }
1274
1275                    DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpAttributeToFile(pageContent, fileName, filePath);
1276                }
1277                catch(Exception JavaDoc e)
1278                {
1279                    logger.warn("An file could not be written:" + e.getMessage(), e);
1280                }
1281                
1282                i++;
1283                filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
1284            }
1285
1286            SiteNode siteNode = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId).getSiteNode(database, siteNodeId);
1287            String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
1288            if(siteNode != null && siteNode.getRepository().getDnsName() != null && !siteNode.getRepository().getDnsName().equals(""))
1289                dnsName = siteNode.getRepository().getDnsName();
1290
1291            pageAsDigitalAssetUrl = urlComposer.composeDigitalAssetUrl(dnsName, fileName, context);
1292        }
1293        catch(Exception JavaDoc e)
1294        {
1295            logger.warn("An error occurred when we fetched the url which we wanted to persist as an digital asset: " + e.getMessage(), e);
1296            throw new SystemException("An error occurred when we fetched the url which we wanted to persist as an digital asset: " + e.getMessage(), e);
1297        }
1298        
1299        context.setDisableNiceUri(disableNiceUri);
1300        context.setUseFullUrl(fullUrl);
1301
1302        return pageAsDigitalAssetUrl;
1303    }
1304
1305    /**
1306     * This method constructs a string representing the path to the page with respect to where in the
1307     * structure the page is. It also takes the page title into consideration. It is done by recursively going
1308     * up in the structure until the root is reached. On each node we collect the pageTitle.
1309     */

1310
1311    public String JavaDoc getPagePath(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId, String JavaDoc bindingName, String JavaDoc attributeName, boolean useLanguageFallBack, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1312    {
1313        String JavaDoc pagePath = "/";
1314        
1315        SiteNodeVO parentSiteNode = this.getParentSiteNode(db, siteNodeId);
1316        if(parentSiteNode != null)
1317        {
1318            pagePath = getPagePath(db, infoGluePrincipal, parentSiteNode.getId(), languageId, null, bindingName, attributeName, useLanguageFallBack, deliveryContext) + "/";
1319        }
1320        
1321        pagePath += this.getPageNavigationTitle(db, infoGluePrincipal, siteNodeId, languageId, contentId, bindingName, attributeName, useLanguageFallBack, deliveryContext, false);
1322        pagePath = pagePath.replaceAll(" ", "_");
1323        
1324        return pagePath;
1325    }
1326
1327
1328    /**
1329     * This method returns a url to the delivery engine
1330     */

1331    public String JavaDoc getPageBaseUrl(Database db) throws SystemException
1332    {
1333        String JavaDoc pageUrl = "";
1334        
1335        SiteNode siteNode = this.getSiteNode(db, this.siteNodeId);
1336        String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
1337        if(siteNode != null && siteNode.getRepository().getDnsName() != null && !siteNode.getRepository().getDnsName().equals(""))
1338            dnsName = siteNode.getRepository().getDnsName();
1339                        
1340        pageUrl = urlComposer.composePageBaseUrl(dnsName);
1341        
1342        return pageUrl;
1343    }
1344
1345
1346    /**
1347     * This method returns the navigation-title to the given page.
1348     * The title is based on the content sent in firstly, secondly the siteNode.
1349     * The actual text is fetched from either the content or the metacontent bound to the sitenode.
1350     */

1351    public String JavaDoc getPageNavigationTitle(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId, String JavaDoc metaBindingName, String JavaDoc attributeName, boolean useLanguageFallback, DeliveryContext deliveryContext, boolean escapeHTML) throws SystemException, Exception JavaDoc
1352    {
1353        String JavaDoc navTitle = "";
1354        
1355        if(contentId == null || contentId.intValue() == -1)
1356        {
1357            ContentVO content = getBoundContent(db, infoGluePrincipal, siteNodeId, languageId, useLanguageFallback, metaBindingName, deliveryContext);
1358            if(content != null)
1359                navTitle = ContentDeliveryController.getContentDeliveryController().getContentAttribute(db, content.getContentId(), languageId, attributeName, siteNodeId, useLanguageFallback, deliveryContext, infoGluePrincipal, escapeHTML);
1360        }
1361        else
1362        {
1363            navTitle = ContentDeliveryController.getContentDeliveryController().getContentAttribute(db, contentId, languageId, attributeName, siteNodeId, useLanguageFallback, deliveryContext, infoGluePrincipal, escapeHTML);
1364        }
1365        
1366        return navTitle;
1367    }
1368
1369    
1370    public Integer JavaDoc getSiteNodeId(Database db, InfoGluePrincipal infogluePrincipal, Integer JavaDoc repositoryId, String JavaDoc path, String JavaDoc attributeName, Integer JavaDoc parentSiteNodeId, Integer JavaDoc languageId, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1371    {
1372        /*
1373        logger.info("repositoryId:" + repositoryId);
1374        logger.info("navigationTitle:" + navigationTitle);
1375        logger.info("parentSiteNodeId:" + parentSiteNodeId);
1376        logger.info("languageId:" + languageId);
1377        */

1378        if (repositoryId == null || repositoryId.intValue() == -1)
1379        {
1380            repositoryId = RepositoryDeliveryController.getRepositoryDeliveryController().getMasterRepository(db).getRepositoryId();
1381            logger.info("RepositoryId not specifed - Resolved master repository to "+repositoryId);
1382        }
1383        
1384        if (repositoryId == null)
1385            throw new SystemException("No repository given and unable to resolve master repository");
1386
1387        List JavaDoc languages = LanguageDeliveryController.getLanguageDeliveryController().getAvailableLanguagesForRepository(db, repositoryId);
1388
1389        List JavaDoc siteNodes = new ArrayList JavaDoc();
1390        
1391        if(parentSiteNodeId == null || parentSiteNodeId.intValue() == -1)
1392        {
1393            SiteNodeVO rootSiteNodeVO = this.getRootSiteNode(db, repositoryId);
1394            siteNodes.add(rootSiteNodeVO);
1395        }
1396        else
1397        {
1398            siteNodes = this.getChildSiteNodes(db, parentSiteNodeId);
1399        }
1400        
1401        //System.out.println("siteNodes:" + siteNodes.size());
1402

1403        Iterator JavaDoc siteNodeIterator = siteNodes.iterator();
1404        while (siteNodeIterator.hasNext())
1405        {
1406            SiteNodeVO siteNodeVO = (SiteNodeVO)siteNodeIterator.next();
1407            
1408            if (path == null || path.length() == 0)
1409            {
1410                logger.info("Returning siteNode:" + siteNodeVO.getName());
1411                return siteNodeVO.getId();
1412            }
1413            
1414            logger.info("Continued with siteNode: " + siteNodeVO.getName());
1415            //System.out.println("Continued with siteNode: " + siteNodeVO.getName());
1416

1417            if(siteNodeVO.getMetaInfoContentId() == null)
1418                throw new SystemException("The site node " + siteNodeVO.getName() + "(" + siteNodeVO.getId() + ") had no meta info. Fix this by editing the site node. Should never happen.");
1419                 
1420            ContentVO content = ContentDeliveryController.getContentDeliveryController().getContentVO(db, siteNodeVO.getMetaInfoContentId(), deliveryContext);
1421            if(content != null)
1422            {
1423                //logger.info("Content "+content.getContentId());
1424
String JavaDoc pathCandidate = null;
1425                for (int i=0;i<languages.size();i++)
1426                {
1427                    LanguageVO language = (LanguageVO) languages.get(i);
1428                    //logger.info("Language : "+language.getLanguageCode());
1429

1430                    if(attributeName.equals("SiteNode.name"))
1431                        pathCandidate = siteNodeVO.getName();
1432                    else
1433                    {
1434                        pathCandidate = ContentDeliveryController.getContentDeliveryController().getContentAttribute(db, content.getContentId(), language.getLanguageId(), attributeName, siteNodeVO.getSiteNodeId(), true, deliveryContext, infogluePrincipal, false);
1435                        
1436                        if((pathCandidate == null || pathCandidate.equals("")) && !attributeName.equals(NAV_TITLE_ATTRIBUTE_NAME))
1437                            pathCandidate = ContentDeliveryController.getContentDeliveryController().getContentAttribute(db, content.getContentId(), language.getLanguageId(), NAV_TITLE_ATTRIBUTE_NAME, siteNodeVO.getSiteNodeId(), true, deliveryContext, infogluePrincipal, false);
1438                    }
1439                    
1440                    logger.info(attributeName + " ["+pathCandidate.trim()+"]==[" + path + "]");
1441                    if (pathCandidate != null && pathCandidate.toLowerCase().trim().equals(path.toLowerCase()))
1442                    {
1443                        return siteNodeVO.getSiteNodeId();
1444                    }
1445                }
1446            }
1447            else
1448            {
1449                throw new SystemException("You must run validation service in the management tool against this db - it needs to become up2date with the new model.");
1450            }
1451        }
1452        
1453            /*
1454            List bindings = new ArrayList();
1455            StringBuffer sb = new StringBuffer(256);
1456            sb.append("SELECT s FROM ");
1457            sb.append("org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl s ");
1458            sb.append("WHERE ");
1459            if (parentSiteNodeId == null || parentSiteNodeId.intValue() == -1)
1460            {
1461                sb.append("is_undefined(s.parentSiteNode) ");
1462            }
1463            else
1464            {
1465                sb.append("s.parentSiteNode.siteNodeId = $").append((bindings.size()+1)).append(" ");
1466                bindings.add(parentSiteNodeId);
1467            }
1468            
1469            sb.append("and s.repository.repositoryId = $").append((bindings.size()+1)).append(" ");
1470            bindings.add(repositoryId);
1471    
1472            logger.info("OQL ["+sb.toString()+"]");
1473            OQLQuery oql = db.getOQLQuery( sb.toString() );
1474            for (int i=0;i<bindings.size();i++)
1475            {
1476                oql.bind(bindings.get(i));
1477            }
1478            
1479            QueryResults results = oql.execute(Database.ReadOnly);
1480            while (results.hasMore())
1481            {
1482                SiteNode siteNode = (SiteNode) results.next();
1483                if (path == null || path.length() == 0)
1484                {
1485                    logger.info("Returning siteNode:" + siteNode.getName());
1486                    return siteNode.getSiteNodeId();
1487                }
1488                
1489                logger.info("Continued with siteNode: " + siteNode.getName());
1490                ContentVO content = getBoundContent(db, infogluePrincipal, siteNode.getSiteNodeId(), languageId, true, META_INFO_BINDING_NAME, deliveryContext);
1491                if(content != null)
1492                {
1493                    //logger.info("Content "+content.getContentId());
1494                    String pathCandidate = null;
1495                    for (int i=0;i<languages.size();i++)
1496                    {
1497                        LanguageVO language = (LanguageVO) languages.get(i);
1498                        //logger.info("Language : "+language.getLanguageCode());
1499                        
1500                        if(attributeName.equals("SiteNode.name"))
1501                            pathCandidate = siteNode.getName();
1502                        else
1503                        {
1504                            pathCandidate = ContentDeliveryController.getContentDeliveryController().getContentAttribute(db, content.getContentId(), language.getLanguageId(), attributeName, siteNode.getSiteNodeId(), true, deliveryContext, infogluePrincipal, false);
1505                            
1506                            if((pathCandidate == null || pathCandidate.equals("")) && !attributeName.equals(NAV_TITLE_ATTRIBUTE_NAME))
1507                                pathCandidate = ContentDeliveryController.getContentDeliveryController().getContentAttribute(db, content.getContentId(), language.getLanguageId(), NAV_TITLE_ATTRIBUTE_NAME, siteNode.getSiteNodeId(), true, deliveryContext, infogluePrincipal, false);
1508                        }
1509                        
1510                        logger.info(attributeName + " ["+pathCandidate.trim()+"]==[" + path + "]");
1511                        if (pathCandidate != null && pathCandidate.toLowerCase().trim().equals(path.toLowerCase()))
1512                        {
1513                            return siteNode.getSiteNodeId();
1514                        }
1515                    }
1516                }
1517            }
1518            System.out.println("DDDD END");
1519        }
1520        */

1521        
1522        return null;
1523    }
1524
1525    public String JavaDoc getPageNavigationPath(Database db, InfoGluePrincipal infogluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, Integer JavaDoc contentId, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1526    {
1527        String JavaDoc path = "/";
1528
1529        SiteNodeVO parentSiteNode = this.getParentSiteNode(db, siteNodeId);
1530        if (parentSiteNode != null)
1531        {
1532            path = getPageNavigationPath(db, infogluePrincipal, parentSiteNode.getId(), languageId, null, deliveryContext) + "/";
1533        }
1534        else
1535        {
1536            return "";
1537        }
1538        
1539        String JavaDoc niceURIEncoding = CmsPropertyHandler.getNiceURIEncoding();
1540        if(niceURIEncoding == null || niceURIEncoding.length() == 0)
1541            niceURIEncoding = "UTF-8";
1542        
1543        String JavaDoc attributeName = ViewPageFilter.attributeName;
1544        
1545        String JavaDoc pathPart;
1546        
1547        if(attributeName.equals("SiteNode.name"))
1548        {
1549            SiteNode siteNode = this.getSiteNode(db, siteNodeId);
1550            pathPart = siteNode.getName();
1551        }
1552        else
1553        {
1554            pathPart = this.getPageNavigationTitle(db, infogluePrincipal, siteNodeId, languageId, null, META_INFO_BINDING_NAME, attributeName, true, deliveryContext, false);
1555            if((pathPart == null || pathPart.equals("")) && !attributeName.equals(NAV_TITLE_ATTRIBUTE_NAME))
1556                pathPart = this.getPageNavigationTitle(db, infogluePrincipal, siteNodeId, languageId, null, META_INFO_BINDING_NAME, NAV_TITLE_ATTRIBUTE_NAME, true, deliveryContext, false);
1557        }
1558        
1559        path += URLEncoder.encode(pathPart, niceURIEncoding);
1560     
1561        return path;
1562    }
1563
1564
1565    public static Integer JavaDoc getSiteNodeIdFromPath(Database db, InfoGluePrincipal infogluePrincipal, RepositoryVO repositoryVO, String JavaDoc[] path, String JavaDoc attributeName, Integer JavaDoc languageId, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1566    {
1567        Integer JavaDoc siteNodeId = null;
1568        URIMapperCache uriCache = URIMapperCache.getInstance();
1569
1570        int idx = path.length;
1571        while (idx >= 0)
1572        {
1573            //logger.info("Looking for cache nodeName at index "+idx);
1574
siteNodeId = uriCache.getCachedSiteNodeId(repositoryVO.getId(), path, idx);
1575            if (siteNodeId != null)
1576                break;
1577            idx = idx - 1;
1578        }
1579        
1580        String JavaDoc repositoryPath = null;
1581        int pathStartIndex = repositoryVO.getDnsName().indexOf("path=");
1582        if(pathStartIndex != -1)
1583        {
1584            repositoryPath = repositoryVO.getDnsName().substring(pathStartIndex + 5);
1585        }
1586
1587        logger.info("repositoryPath:" + repositoryPath);
1588        logger.info("path:" + path.length);
1589        
1590        if(repositoryPath != null && path.length > 0)
1591        {
1592            String JavaDoc[] repositoryPaths = repositoryPath.split("/");
1593            String JavaDoc[] newPath = path;
1594            
1595            logger.info("repositoryPaths:" + repositoryPaths.length);
1596            logger.info("newPath:" + newPath.length);
1597            
1598            for(int repPathIndex = 0; repPathIndex < repositoryPaths.length; repPathIndex++)
1599            {
1600                String JavaDoc repPath = repositoryPaths[repPathIndex];
1601                logger.info("repPath:" + repPath);
1602                if(path.length > repPathIndex)
1603                {
1604                    logger.info("path:" + path[repPathIndex]);
1605                    if(path[repPathIndex].equals(repPath))
1606                    {
1607                        String JavaDoc[] tempNewPath = new String JavaDoc[newPath.length - 1];
1608                        for(int i=1; i<newPath.length; i++)
1609                            tempNewPath[i-1] = newPath[i];
1610                        
1611                        newPath = tempNewPath;
1612                    }
1613                }
1614            }
1615            path = newPath;
1616        }
1617        logger.info("new path:" + path.length);
1618
1619        //logger.info("Idx = "+idx);
1620
for (int i = idx;i < path.length; i++)
1621        {
1622            if (i < 0)
1623            {
1624                siteNodeId = NodeDeliveryController.getNodeDeliveryController(null, null, null).getSiteNodeId(db, infogluePrincipal, repositoryVO.getId(), null, attributeName, null, languageId, deliveryContext);
1625            }
1626            else
1627            {
1628                siteNodeId = NodeDeliveryController.getNodeDeliveryController(null, null, null).getSiteNodeId(db, infogluePrincipal, repositoryVO.getId(), path[i], attributeName, siteNodeId, languageId, deliveryContext);
1629            }
1630            
1631            if (siteNodeId != null)
1632                uriCache.addCachedSiteNodeId(repositoryVO.getId(), path, i+1, siteNodeId);
1633        }
1634
1635        return siteNodeId;
1636    }
1637    
1638    public static Integer JavaDoc getSiteNodeIdFromPath(InfoGluePrincipal infogluePrincipal, RepositoryVO repositoryVO, String JavaDoc[] path, String JavaDoc attributeName, Integer JavaDoc languageId, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1639    {
1640        Integer JavaDoc siteNodeId = null;
1641
1642        Database db = CastorDatabaseService.getDatabase();
1643        
1644        beginTransaction(db);
1645
1646        try
1647        {
1648            URIMapperCache uriCache = URIMapperCache.getInstance();
1649    
1650            int numberOfPaths = path.length;
1651            while (numberOfPaths >= 0)
1652            {
1653                //logger.info("Looking for cached nodeName at index "+idx);
1654
siteNodeId = uriCache.getCachedSiteNodeId(repositoryVO.getId(), path, numberOfPaths);
1655
1656                if (siteNodeId != null)
1657                    break;
1658
1659                numberOfPaths = numberOfPaths - 1;
1660            }
1661            
1662            String JavaDoc repositoryPath = null;
1663            int pathStartIndex = repositoryVO.getDnsName().indexOf("path=");
1664            if(pathStartIndex != -1)
1665            {
1666                repositoryPath = repositoryVO.getDnsName().substring(pathStartIndex + 5);
1667            }
1668
1669            logger.info("repositoryPath:" + repositoryPath);
1670            logger.info("path:" + path.length);
1671            
1672            if(repositoryPath != null && path.length > 0)
1673            {
1674                String JavaDoc[] repositoryPaths = repositoryPath.split("/");
1675                String JavaDoc[] newPath = path;
1676                
1677                logger.info("repositoryPaths:" + repositoryPaths.length);
1678                logger.info("newPath:" + newPath.length);
1679                
1680                for(int repPathIndex = 0; repPathIndex < repositoryPaths.length; repPathIndex++)
1681                {
1682                    String JavaDoc repPath = repositoryPaths[repPathIndex];
1683                    logger.info("repPath:" + repPath);
1684                    if(path.length > repPathIndex)
1685                    {
1686                        logger.info("path:" + path[repPathIndex]);
1687                        if(path[repPathIndex].equals(repPath))
1688                        {
1689                            String JavaDoc[] tempNewPath = new String JavaDoc[newPath.length - 1];
1690                            for(int i=1; i<newPath.length; i++)
1691                                tempNewPath[i-1] = newPath[i];
1692                            
1693                            newPath = tempNewPath;
1694                        }
1695                    }
1696                }
1697                path = newPath;
1698            }
1699            logger.info("new path:" + path.length);
1700
1701            logger.info("numberOfPaths = "+numberOfPaths);
1702            for (int i = numberOfPaths;i < path.length; i++)
1703            {
1704                if (i < 0)
1705                {
1706                    logger.info("Getting root node");
1707                    siteNodeId = NodeDeliveryController.getNodeDeliveryController(null, null, null).getSiteNodeId(db, infogluePrincipal, repositoryVO.getId(), null, attributeName, null, languageId, deliveryContext);
1708                }
1709                else
1710                {
1711                    logger.info("Getting normal");
1712                    siteNodeId = NodeDeliveryController.getNodeDeliveryController(null, null, null).getSiteNodeId(db, infogluePrincipal, repositoryVO.getId(), path[i], attributeName, siteNodeId, languageId, deliveryContext);
1713                }
1714                
1715                if (siteNodeId != null)
1716                    uriCache.addCachedSiteNodeId(repositoryVO.getId(), path, i+1, siteNodeId);
1717            }
1718         
1719            commitTransaction(db);
1720        }
1721        catch(Exception JavaDoc e)
1722        {
1723            logger.error("An error occurred so we should not complete the transaction:" + e, e);
1724            rollbackTransaction(db);
1725            throw new SystemException(e.getMessage());
1726        }
1727        
1728        return siteNodeId;
1729    }
1730    
1731    /**
1732     * This method returns the contentId of the bound metainfo-content to the given page.
1733     */

1734
1735    public Integer JavaDoc getMetaInfoContentId(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, String JavaDoc metaBindingName, boolean inheritParentBindings, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1736    {
1737        ContentVO content = getBoundContent(db, infoGluePrincipal, siteNodeId, languageId, true, metaBindingName, inheritParentBindings, deliveryContext);
1738        if(content != null)
1739            return content.getContentId();
1740        
1741        return null;
1742    }
1743
1744
1745
1746    /**
1747     * This method returns the root siteNodeVO for the specified repository.
1748     * If the repositoryName is null we fetch the name of the master repository.
1749     */

1750    
1751    public static SiteNodeVO getRootSiteNode(Database db, String JavaDoc repositoryName) throws SystemException, Exception JavaDoc
1752    {
1753        if(repositoryName == null)
1754        {
1755            repositoryName = RepositoryDeliveryController.getRepositoryDeliveryController().getMasterRepository(db).getName();
1756            logger.info("Fetched name of master repository as none were given:" + repositoryName);
1757        }
1758         
1759        SiteNode siteNode = null;
1760
1761        logger.info("Fetching the root siteNode for the repository " + repositoryName);
1762        OQLQuery oql = db.getOQLQuery( "SELECT c FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl c WHERE is_undefined(c.parentSiteNode) AND c.repository.name = $1");
1763        oql.bind(repositoryName);
1764        
1765        QueryResults results = oql.execute(Database.ReadOnly);
1766
1767        if (results.hasMore())
1768        {
1769            siteNode = (SiteNode)results.next();
1770            logger.info("The root node was found:" + siteNode.getName());
1771        }
1772        
1773        results.close();
1774        oql.close();
1775
1776        logger.info("siteNode:" + siteNode);
1777        
1778        return (siteNode == null) ? null : siteNode.getValueObject();
1779    }
1780    
1781
1782    /**
1783     * This method returns the root siteNodeVO for the specified repository.
1784     * If the repositoryName is null we fetch the name of the master repository.
1785     */

1786    
1787    public SiteNodeVO getRootSiteNode(Database db, Integer JavaDoc repositoryId) throws SystemException, Exception JavaDoc
1788    {
1789        SiteNodeVO siteNodeVO = null;
1790
1791        String JavaDoc key = "" + repositoryId;
1792        logger.info("key in getRootSiteNode:" + key);
1793        siteNodeVO = (SiteNodeVO)CacheController.getCachedObject("rootSiteNodeCache", key);
1794        if(siteNodeVO != null)
1795        {
1796            logger.info("There was an cached master root siteNode:" + siteNodeVO.getName());
1797        }
1798        else
1799        {
1800            if(repositoryId == null)
1801            {
1802                repositoryId = RepositoryDeliveryController.getRepositoryDeliveryController().getMasterRepository(db).getRepositoryId();
1803                logger.info("Fetched name of master repository as none were given:" + repositoryId);
1804            }
1805            
1806            logger.info("Fetching the root siteNode for the repository " + repositoryId);
1807            OQLQuery oql = db.getOQLQuery( "SELECT c FROM org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl c WHERE is_undefined(c.parentSiteNode) AND c.repository = $1");
1808            oql.bind(repositoryId);
1809            
1810            QueryResults results = oql.execute(Database.ReadOnly);
1811            
1812            if (results.hasMore())
1813            {
1814                siteNodeVO = ((SiteNode)results.next()).getValueObject();
1815                logger.info("The root node was found:" + siteNodeVO.getName());
1816            }
1817
1818            results.close();
1819            oql.close();
1820
1821            logger.info("siteNodeVO:" + siteNodeVO);
1822
1823            CacheController.cacheObject("rootSiteNodeCache", key, siteNodeVO);
1824        }
1825
1826        return siteNodeVO;
1827    }
1828
1829
1830    
1831    /**
1832     * This method returns the list of siteNodeVO which is children to this one.
1833     */

1834    
1835    public List JavaDoc getChildSiteNodes(Database db, Integer JavaDoc siteNodeId) throws SystemException, Exception JavaDoc
1836    {
1837        //logger.warn("getChildSiteNodes:" + siteNodeId);
1838

1839        if(siteNodeId == null)
1840        {
1841            return null;
1842        }
1843
1844        String JavaDoc key = "" + siteNodeId;
1845        logger.info("key in getChildSiteNodes:" + key);
1846        List JavaDoc siteNodeVOList = (List JavaDoc)CacheController.getCachedObject("childSiteNodesCache", key);
1847        if(siteNodeVOList != null)
1848        {
1849            logger.info("There was a cached list of child sitenodes:" + siteNodeVOList.size());
1850        }
1851        else
1852        {
1853            siteNodeVOList = new ArrayList JavaDoc();
1854    
1855            OQLQuery oql = db.getOQLQuery( "SELECT s FROM org.infoglue.cms.entities.structure.impl.simple.SmallSiteNodeImpl s WHERE s.parentSiteNode.siteNodeId = $1 ORDER BY s.siteNodeId");
1856            oql.bind(siteNodeId);
1857            
1858            QueryResults results = oql.execute(Database.ReadOnly);
1859            
1860            while (results.hasMore())
1861            {
1862                SiteNode siteNode = (SiteNode)results.next();
1863                
1864                if(isValidSiteNode(siteNode, db))
1865                    siteNodeVOList.add(siteNode.getValueObject());
1866            }
1867
1868            results.close();
1869            oql.close();
1870
1871            CacheController.cacheObject("childSiteNodesCache", key, siteNodeVOList);
1872        }
1873
1874        //logger.warn("getChildSiteNodes end:" + siteNodeId);
1875

1876        return siteNodeVOList;
1877    }
1878
1879    
1880    /**
1881     * This method returns a sorted list of qualifyers.
1882     */

1883    
1884    private List JavaDoc getBindingQualifyers(Integer JavaDoc serviceBindingId, Database db) throws SystemException, Bug, Exception JavaDoc
1885    {
1886        List JavaDoc qualifyers = new ArrayList JavaDoc();
1887        
1888        OQLQuery oql = db.getOQLQuery( "SELECT q FROM org.infoglue.cms.entities.structure.impl.simple.QualifyerImpl q WHERE q.serviceBinding.serviceBindingId = $1 ORDER BY q.sortOrder");
1889        oql.bind(serviceBindingId);
1890        
1891        QueryResults results = oql.execute(Database.ReadOnly);
1892        while(results.hasMore())
1893        {
1894            Qualifyer qualifyer = (Qualifyer)results.next();
1895            qualifyers.add(qualifyer);
1896        }
1897        
1898        results.close();
1899        oql.close();
1900
1901        return qualifyers;
1902    }
1903    
1904    
1905    /**
1906     * This method validates that right now is between publishdate and expiredate.
1907     */

1908    
1909    private boolean isValidOnDates(Date JavaDoc publishDate, Date JavaDoc expireDate)
1910    {
1911        boolean isValid = true;
1912        Date JavaDoc now = new Date JavaDoc();
1913        
1914        if(publishDate.after(now) || expireDate.before(now))
1915            isValid = false;
1916        
1917        return isValid;
1918    }
1919    
1920    /**
1921     * Returns if a siteNode is between dates and has a siteNode version suitable for this delivery mode.
1922     * @throws Exception
1923     */

1924    
1925    public boolean isValidSiteNode(Database db, Integer JavaDoc siteNodeId) throws Exception JavaDoc
1926    {
1927        boolean isValidSiteNode = false;
1928        
1929        SiteNode siteNode = (SiteNode)getObjectWithId(SiteNodeImpl.class, siteNodeId, db);
1930        isValidSiteNode = isValidSiteNode(siteNode, db);
1931                
1932        return isValidSiteNode;
1933    }
1934
1935    /**
1936     * Returns if a siteNode is between dates and has a SiteNode version suitable for this delivery mode.
1937     * @throws Exception
1938     */

1939
1940    public boolean isValidSiteNode(SiteNode siteNode, Database db) throws Exception JavaDoc
1941    {
1942        boolean isValidContent = false;
1943        
1944        if(isValidOnDates(siteNode.getPublishDateTime(), siteNode.getExpireDateTime()))
1945        {
1946            if(this.getLatestActiveSiteNodeVersion(siteNode.getId(), db) != null)
1947                isValidContent = true;
1948            /*
1949            Collection versions = siteNode.getSiteNodeVersions();
1950            Iterator versionsIterator = versions.iterator();
1951            while(versionsIterator.hasNext())
1952            {
1953                SiteNodeVersion siteNodeVersion = (SiteNodeVersion)versionsIterator.next();
1954                if(siteNodeVersion.getIsActive().booleanValue() && siteNodeVersion.getStateId().intValue() >= getOperatingMode().intValue())
1955                    isValidContent = true;
1956            }
1957            */

1958        }
1959        
1960        if(isValidContent && !siteNode.getExpireDateTime().before(new Date JavaDoc()))
1961        {
1962            Date JavaDoc expireDateTimeCandidate = siteNode.getExpireDateTime();
1963            if(CacheController.expireDateTime == null || expireDateTimeCandidate.before(CacheController.expireDateTime))
1964            {
1965                CacheController.expireDateTime = expireDateTimeCandidate;
1966            }
1967        }
1968        else if(siteNode.getPublishDateTime().after(new Date JavaDoc())) //If it's a publish date to come we consider it
1969
{
1970            Date JavaDoc publishDateTimeCandidate = siteNode.getPublishDateTime();
1971            if(CacheController.publishDateTime == null || publishDateTimeCandidate.after(CacheController.publishDateTime))
1972            {
1973                CacheController.publishDateTime = publishDateTimeCandidate;
1974            }
1975        }
1976        
1977        return isValidContent;
1978    }
1979    
1980    /**
1981     * This method just sorts the list of qualifyers on sortOrder.
1982     */

1983    
1984    private List JavaDoc sortQualifyers(Collection JavaDoc qualifyers)
1985    {
1986        List JavaDoc sortedQualifyers = new ArrayList JavaDoc();
1987
1988        try
1989        {
1990            Iterator JavaDoc iterator = qualifyers.iterator();
1991            while(iterator.hasNext())
1992            {
1993                Qualifyer qualifyer = (Qualifyer)iterator.next();
1994                int index = 0;
1995                Iterator JavaDoc sortedListIterator = sortedQualifyers.iterator();
1996                while(sortedListIterator.hasNext())
1997                {
1998                    Qualifyer sortedQualifyer = (Qualifyer)sortedListIterator.next();
1999                    if(sortedQualifyer.getSortOrder().intValue() > qualifyer.getSortOrder().intValue())
2000                    {
2001                        break;
2002                    }
2003                    index++;
2004                }
2005                sortedQualifyers.add(index, qualifyer);
2006                                    
2007            }
2008        }
2009        catch(Exception JavaDoc e)
2010        {
2011            logger.warn("The sorting of qualifyers failed:" + e.getMessage(), e);
2012        }
2013            
2014        return sortedQualifyers;
2015    }
2016
2017}
Popular Tags