KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import org.apache.log4j.Logger;
36 import org.exolab.castor.jdo.Database;
37 import org.exolab.castor.jdo.OQLQuery;
38 import org.exolab.castor.jdo.QueryResults;
39 import org.infoglue.cms.applications.common.VisualFormatter;
40 import org.infoglue.cms.controllers.kernel.impl.simple.AccessRightController;
41 import org.infoglue.cms.controllers.kernel.impl.simple.SiteNodeController;
42 import org.infoglue.cms.entities.content.Content;
43 import org.infoglue.cms.entities.content.ContentCategoryVO;
44 import org.infoglue.cms.entities.content.ContentVO;
45 import org.infoglue.cms.entities.content.ContentVersion;
46 import org.infoglue.cms.entities.content.ContentVersionVO;
47 import org.infoglue.cms.entities.content.DigitalAsset;
48 import org.infoglue.cms.entities.content.impl.simple.ContentImpl;
49 import org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl;
50 import org.infoglue.cms.entities.content.impl.simple.MediumContentImpl;
51 import org.infoglue.cms.entities.content.impl.simple.SmallContentImpl;
52 import org.infoglue.cms.entities.management.ContentTypeDefinitionVO;
53 import org.infoglue.cms.entities.management.LanguageVO;
54 import org.infoglue.cms.entities.management.Repository;
55 import org.infoglue.cms.entities.structure.SiteNode;
56 import org.infoglue.cms.entities.structure.SiteNodeVO;
57 import org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl;
58 import org.infoglue.cms.exception.SystemException;
59 import org.infoglue.cms.security.InfoGluePrincipal;
60 import org.infoglue.cms.util.CmsPropertyHandler;
61 import org.infoglue.deliver.applications.databeans.DeliveryContext;
62 import org.infoglue.deliver.controllers.kernel.URLComposer;
63 import org.infoglue.deliver.util.CacheController;
64 import org.infoglue.deliver.util.NullObject;
65
66
67 public class ContentDeliveryController extends BaseDeliveryController
68 {
69     private final static Logger logger = Logger.getLogger(ContentDeliveryController.class.getName());
70
71     private URLComposer urlComposer = null;
72     private VisualFormatter formatter = new VisualFormatter();
73     
74     /**
75      * Private constructor to enforce factory-use
76      */

77     
78     private ContentDeliveryController()
79     {
80         urlComposer = URLComposer.getURLComposer();
81     }
82     
83     /**
84      * Factory method
85      */

86     
87     public static ContentDeliveryController getContentDeliveryController()
88     {
89         return new ContentDeliveryController();
90     }
91     
92     /**
93      * This method returns which mode the delivery-engine is running in.
94      * The mode is important to be able to show working, preview and published data separate.
95      */

96     
97     private Integer JavaDoc getOperatingMode()
98     {
99         Integer JavaDoc operatingMode = new Integer JavaDoc(0); //Default is working
100
try
101         {
102             operatingMode = new Integer JavaDoc(CmsPropertyHandler.getOperatingMode());
103             //logger.info("Operating mode is:" + operatingMode);
104
}
105         catch(Exception JavaDoc e)
106         {
107             logger.warn("We could not get the operating mode from the propertyFile:" + e.getMessage(), e);
108         }
109         return operatingMode;
110     }
111     
112     
113     /**
114      * This method return a contentVO
115      */

116     
117     public ContentVO getContentVO(Integer JavaDoc contentId, Database db) throws SystemException, Exception JavaDoc
118     {
119         ContentVO contentVO = null;
120         
121         contentVO = (ContentVO)getVOWithId(SmallContentImpl.class, contentId, db);
122         
123         return contentVO;
124     }
125     
126     /**
127      * This method return a contentVO
128      */

129     
130     public ContentVO getContentVO(Database db, Integer JavaDoc contentId, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
131     {
132         deliveryContext.addUsedContent("content_" + contentId);
133
134         ContentVO contentVO = (ContentVO)getVOWithId(SmallContentImpl.class, contentId, db);
135                 
136         return contentVO;
137     }
138     
139     
140     /**
141      * This method returns that contentVersionVO which matches the parameters sent in and which
142      * also has the correct state for this delivery-instance.
143      */

144     
145     public ContentVersionVO getContentVersionVO(Database db, Integer JavaDoc siteNodeId, Integer JavaDoc contentId, Integer JavaDoc languageId, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
146     {
147         ContentVersionVO contentVersionVO = null;
148         
149         SiteNodeVO siteNodeVO = (SiteNodeVO)getVOWithId(SiteNodeImpl.class, siteNodeId, db);
150         String JavaDoc contentVersionKey = "contentVersionVO_" + siteNodeVO.getRepositoryId() + "_" + contentId + "_" + languageId + "_" + useLanguageFallback;
151         logger.info("contentVersionKey:" + contentVersionKey);
152         contentVersionVO = (ContentVersionVO)CacheController.getCachedObjectFromAdvancedCache("contentVersionCache", contentVersionKey);
153         
154         if(contentVersionVO != null)
155         {
156             //logger.info("There was an cached contentVersionVO:" + contentVersionVO.getContentVersionId());
157
}
158         else
159         {
160             ContentVersion contentVersion = this.getContentVersion(siteNodeId, contentId, languageId, db, useLanguageFallback, deliveryContext, infoGluePrincipal);
161             
162             if(contentVersion != null)
163             {
164                 contentVersionVO = contentVersion.getValueObject();
165                 
166                 CacheController.cacheObjectInAdvancedCache("contentVersionCache", contentVersionKey, contentVersionVO, new String JavaDoc[]{"contentVersion_" + contentVersionVO.getId(), "content_" + contentVersionVO.getContentId()}, true);
167             }
168         
169         }
170         
171         if(contentVersionVO != null)
172             deliveryContext.addUsedContentVersion("contentVersion_" + contentVersionVO.getId());
173         
174         return contentVersionVO;
175     }
176
177     
178     /**
179      * This method returns that contentVersion which matches the parameters sent in and which
180      * also has the correct state for this delivery-instance.
181      */

182     
183     private ContentVersion getContentVersion(Integer JavaDoc siteNodeId, Integer JavaDoc contentId, Integer JavaDoc languageId, Database db, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
184     {
185         if(contentId == null || contentId.intValue() < 1)
186             return null;
187         
188         ContentVersion contentVersion = null;
189         
190         MediumContentImpl content = (MediumContentImpl)getObjectWithId(MediumContentImpl.class, contentId, db);
191         boolean isValidContent = isValidContent(infoGluePrincipal, content, languageId, useLanguageFallback, false, db, deliveryContext);
192         if(isValidContent)
193         {
194             contentVersion = getContentVersion(content, languageId, getOperatingMode(), deliveryContext, db);
195             if(contentVersion == null && useLanguageFallback)
196             {
197                 logger.info("Did not find it in requested languge... lets check the masterlanguage....");
198                 
199                 Integer JavaDoc masterLanguageId = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForSiteNode(db, siteNodeId).getLanguageId();
200                 if(languageId != null && !languageId.equals(masterLanguageId))
201                 {
202                     contentVersion = getContentVersion(content, masterLanguageId, getOperatingMode(), deliveryContext, db);
203                 }
204             }
205         }
206         
207         return contentVersion;
208     }
209
210
211     /**
212      * This method gets a contentVersion with a state and a language which is active.
213      */

214     /*
215     private ContentVersion getContentVersion(Content content, Integer languageId, Integer operatingMode, DeliveryContext deliveryContext, Database db) throws Exception
216     {
217         logger.info("content:" + content.getId());
218         logger.info("operatingMode:" + operatingMode);
219         logger.info("languageId:" + languageId);
220         
221         ContentVersion contentVersion = null;
222         
223         String versionKey = "" + content.getId() + "_" + languageId + "_" + operatingMode + "_contentVersionId";
224         //System.out.println("versionKey:" + versionKey);
225         
226         Integer contentVersionId = (Integer)CacheController.getCachedObjectFromAdvancedCache("contentVersionCache", versionKey);
227         if(contentVersionId != null)
228         {
229             logger.info("There was a cached content version id:" + contentVersionId);
230             //System.out.println("There was a cached content version id:" + contentVersionId);
231             contentVersion = (ContentVersion)getObjectWithId(ContentVersionImpl.class, contentVersionId, db);
232             //System.out.println("Loaded the version from cache instead of querying it:" + contentVersionId);
233             logger.info("contentVersion read");
234         }
235         else
236         {
237             Collection contentVersions = content.getContentVersions();
238             
239             Iterator versionIterator = contentVersions.iterator();
240             while(versionIterator.hasNext())
241             {
242                 ContentVersion contentVersionCandidate = (ContentVersion)versionIterator.next();
243                 logger.info("contentVersionCandidate:" + contentVersionCandidate.getId() + ":" + contentVersionCandidate.getIsActive() + ":" + contentVersionCandidate.getLanguage() + ":" + contentVersionCandidate.getStateId() + ":" + operatingMode);
244                 logger.info("" + contentVersionCandidate.getIsActive().booleanValue());
245                 logger.info("" + contentVersionCandidate.getLanguage().getId().intValue());
246                 logger.info("" + languageId.intValue());
247                 logger.info("" + contentVersionCandidate.getStateId().intValue());
248                 logger.info("" + operatingMode.intValue());
249                 
250                 if(contentVersionCandidate.getIsActive().booleanValue() && contentVersionCandidate.getLanguage().getId().intValue() == languageId.intValue() && contentVersionCandidate.getStateId().intValue() >= operatingMode.intValue())
251                 {
252                     if(contentVersion == null || contentVersion.getId().intValue() < contentVersionCandidate.getId().intValue())
253                     {
254                         contentVersion = contentVersionCandidate;
255                     }
256                 }
257             }
258             
259             if(contentVersion != null)
260                 CacheController.cacheObjectInAdvancedCache("contentVersionCache", versionKey, contentVersion.getId(), new String[]{"contentVersion_" + contentVersion.getId(), "content_" + contentVersion.getValueObject().getContentId()}, true);
261         }
262         
263         if(contentVersion != null)
264             deliveryContext.addUsedContentVersion("contentVersion_" + contentVersion.getId());
265         
266         return contentVersion;
267     }
268     */

269
270     private ContentVersion getContentVersion(Content content, Integer JavaDoc languageId, Integer JavaDoc operatingMode, DeliveryContext deliveryContext, Database db) throws Exception JavaDoc
271     {
272         ContentVersion contentVersion = null;
273         
274         logger.info("content:" + content.getId());
275         logger.info("operatingMode:" + operatingMode);
276         logger.info("languageId:" + languageId);
277
278         String JavaDoc versionKey = "" + content.getId() + "_" + languageId + "_" + operatingMode + "_contentVersionId";
279         //System.out.println("versionKey:" + versionKey);
280

281         Object JavaDoc object = CacheController.getCachedObjectFromAdvancedCache("contentVersionCache", versionKey);
282         //Integer contentVersionId = (Integer)CacheController.getCachedObjectFromAdvancedCache("contentVersionCache", versionKey);
283
if(object instanceof NullObject)
284         {
285             logger.info("There was an cached parentSiteNodeVO but it was null:" + object);
286         }
287         else if(object != null)
288         {
289             Integer JavaDoc contentVersionId = (Integer JavaDoc)object;
290             logger.info("There was a cached content version id:" + contentVersionId);
291             //System.out.println("There was a cached content version id:" + contentVersionId);
292
contentVersion = (ContentVersion)getObjectWithId(ContentVersionImpl.class, contentVersionId, db);
293             //System.out.println("Loaded the version from cache instead of querying it:" + contentVersionId);
294
logger.info("contentVersion read");
295         }
296         else
297         {
298             //System.out.println("Querying for verson: " + versionKey);
299
OQLQuery oql = db.getOQLQuery( "SELECT cv FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl cv WHERE cv.contentId = $1 AND cv.language.languageId = $2 AND cv.stateId >= $3 AND cv.isActive = $4 ORDER BY cv.contentVersionId desc");
300             oql.bind(content.getId());
301             oql.bind(languageId);
302             oql.bind(operatingMode);
303             oql.bind(true);
304     
305             QueryResults results = oql.execute(Database.ReadOnly);
306             
307             if (results.hasMore())
308             {
309                 contentVersion = (ContentVersion)results.next();
310                 logger.info("found one:" + contentVersion.getId());
311
312                 //System.out.println("Caching content version for key:" + versionKey);
313
CacheController.cacheObjectInAdvancedCache("contentVersionCache", versionKey, contentVersion.getId(), new String JavaDoc[]{"contentVersion_" + contentVersion.getId(), "content_" + contentVersion.getValueObject().getContentId()}, true);
314             }
315             else
316             {
317                 CacheController.cacheObjectInAdvancedCache("contentVersionCache", versionKey, new NullObject(), new String JavaDoc[]{"content_" + content.getId()}, true);
318             }
319
320             results.close();
321             oql.close();
322         }
323         
324         if(contentVersion != null)
325             deliveryContext.addUsedContentVersion("contentVersion_" + contentVersion.getId());
326
327         logger.info("end getContentVersion");
328         
329         return contentVersion;
330     }
331
332
333     /**
334      * This is the most common way of getting attributes from a content.
335      * It selects the correct contentVersion depending on the language and then gets the attribute in the xml associated.
336      */

337
338     public String JavaDoc getContentAttribute(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc attributeName, Integer JavaDoc siteNodeId, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infogluePrincipal, boolean escapeHTML) throws SystemException, Exception JavaDoc
339     {
340         return getContentAttribute(db, contentId, languageId, attributeName, siteNodeId, useLanguageFallback, deliveryContext, infogluePrincipal, escapeHTML, null);
341     }
342     
343     /**
344      * This is the most common way of getting attributes from a content.
345      * It selects the correct contentVersion depending on the language and then gets the attribute in the xml associated.
346      */

347
348     public String JavaDoc getContentAttribute(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc attributeName, Integer JavaDoc siteNodeId, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infogluePrincipal, boolean escapeHTML, List JavaDoc usedContentVersionId) throws SystemException, Exception JavaDoc
349     {
350         //System.out.println("usedContentVersionId:" + usedContentVersionId);
351

352         String JavaDoc attributeKey = "" + contentId + "_" + languageId + "_" + attributeName + "_" + siteNodeId + "_" + useLanguageFallback + "_" + escapeHTML;
353         String JavaDoc versionKey = attributeKey + "_contentVersionId";
354         //logger.info("attributeKey:" + attributeKey);
355

356         //String attribute = (String)CacheController.getCachedObject("contentAttributeCache", attributeKey);
357
//Integer contentVersionId = (Integer)CacheController.getCachedObject("contentAttributeCache", versionKey);
358

359         String JavaDoc attribute = (String JavaDoc)CacheController.getCachedObjectFromAdvancedCache("contentAttributeCache", attributeKey);
360         Integer JavaDoc contentVersionId = (Integer JavaDoc)CacheController.getCachedObjectFromAdvancedCache("contentVersionCache", versionKey);
361         
362         try
363         {
364
365         if(attribute != null)
366         {
367             //logger.info("There was an cached content attribute:" + attribute);
368
//if(contentId != null && contentId.intValue() == 3135)
369
// System.out.println("There was an cached content attribute:" + attribute);
370
}
371         else
372         {
373             //if(contentId != null && contentId.intValue() == 3135)
374
// System.out.println("No cached attribute");
375

376             ContentVersionVO contentVersionVO = getContentVersionVO(db, siteNodeId, contentId, languageId, useLanguageFallback, deliveryContext, infogluePrincipal);
377            
378             if (contentVersionVO != null)
379             {
380                 logger.info("found one:" + contentVersionVO);
381                 attribute = getAttributeValue(db, contentVersionVO, attributeName, escapeHTML);
382                 contentVersionId = contentVersionVO.getId();
383             }
384             else
385                 attribute = "";
386
387             CacheController.cacheObjectInAdvancedCache("contentAttributeCache", attributeKey, attribute, new String JavaDoc[]{"contentVersion_" + contentVersionId, "content_" + contentId}, true);
388             if(contentVersionId != null)
389                 CacheController.cacheObjectInAdvancedCache("contentVersionCache", versionKey, contentVersionId, new String JavaDoc[]{"contentVersion_" + contentVersionId, "content_" + contentId}, true);
390         }
391         
392         //logger.info("Adding contentVersion:" + contentVersionId);
393
deliveryContext.addUsedContentVersion("contentVersion_" + contentVersionId);
394
395         if(usedContentVersionId != null && contentVersionId != null)
396             usedContentVersionId.add(contentVersionId);
397
398         }
399         catch(Exception JavaDoc e)
400         {
401             e.printStackTrace();
402             throw e;
403         }
404         
405         return (attribute == null) ? "" : attribute;
406     }
407
408
409     /**
410      * This is the most common way of getting attributes from a content.
411      * It selects the correct contentVersion depending on the language and then gets the attribute in the xml associated.
412      */

413
414     public String JavaDoc getContentAttribute(Database db, ContentVersionVO contentVersionVO, String JavaDoc attributeName, boolean escapeHTML) throws SystemException, Exception JavaDoc
415     {
416         String JavaDoc attribute = getAttributeValue(db, contentVersionVO, attributeName, escapeHTML);
417         
418         return attribute;
419     }
420
421     /**
422      * Find all ContentVersionVOs that are related to the provided Category.
423      *
424      * TODO: Right now this method depends on the ContentVersion having an owningContent
425      * TODO: This is potentially bad from a performance standpoint app-wide, so a workaround may
426      * TODO: be to look up each Content for the ContentVersions after we have done everything we
427      * TODO: can to wed down the list alot, so the overhead will not be too much.
428      *
429      * @param categoryId The Category to search on
430      * @param attributeName The attribute of the Category relationship
431      * @param infoGluePrincipal The user making the request
432      * @param siteNodeId The SiteNode that the request is coming from
433      * @param languageId The Language of the request
434      * @param useLanguageFallback True is the search is to use the fallback (default) language for the Repository
435      * @return A List of ContentVersionVOs matching the Category search, that are considered valid
436      * @throws SystemException
437      */

438     public List JavaDoc findContentVersionVOsForCategory(Database db, Integer JavaDoc categoryId, String JavaDoc attributeName, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, boolean useLanguageFallback, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
439     {
440         deliveryContext.addUsedContent("selectiveCacheUpdateNonApplicable");
441         
442         List JavaDoc results = findContentCategories(db, categoryId, attributeName);
443         List JavaDoc versions = findContentVersionsForCategories(results, db);
444
445         // Weed out irrelevant versions
446
for (Iterator JavaDoc iter = versions.iterator(); iter.hasNext();)
447         {
448             ContentVersion version = (ContentVersion) iter.next();
449             if(!isValidContentVersion(version, infoGluePrincipal, siteNodeId, languageId, useLanguageFallback, db, deliveryContext))
450                 iter.remove();
451         }
452
453         return toVOList(versions);
454     }
455
456     /**
457      * Find all ContentCategories for the given Category id and attributeName.
458      * @param categoryId The Category to find ContentCategories
459      * @param attributeName The ContentTYpeDefintion attribute name of a ContentCategory relationship.
460      * @return A List of ContentCategoryVOs for the supplied Category id.
461      * @throws SystemException If an error happens
462      */

463     private List JavaDoc findContentCategories(Database db, Integer JavaDoc categoryId, String JavaDoc attributeName) throws SystemException, Exception JavaDoc
464     {
465         StringBuffer JavaDoc oql = new StringBuffer JavaDoc();
466         oql.append("SELECT c FROM org.infoglue.cms.entities.content.impl.simple.ContentCategoryImpl c ");
467         oql.append("WHERE c.category.categoryId = $1 AND c.attributeName = $2");
468
469         ArrayList JavaDoc params = new ArrayList JavaDoc();
470         params.add(categoryId);
471         params.add(attributeName);
472         return toVOList(executeQuery(db, oql.toString(), params));
473     }
474
475     /**
476      * Find content versions that are in the provided list of version ids. However over time this
477      * could get to be a large list, so lets weed it out initially at the database restricted
478      * on the time parameters. That should keep the lists manageable
479      *
480      * @param contentCategories A ContentCategoryVO list used to find related ContentVersions
481      * @param db A Database to execute the query against
482      * @return A List of ContentVersions that were related to the provided ContentCategories and
483      * fell withing the publishing time frame
484      * @throws Exception if an error happens
485      */

486     private List JavaDoc findContentVersionsForCategories(List JavaDoc contentCategories, Database db) throws Exception JavaDoc
487     {
488         if(contentCategories.isEmpty())
489             return Collections.EMPTY_LIST;
490
491         /*
492         StringBuffer oql = new StringBuffer();
493         oql.append("SELECT c FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl c ")
494                 .append("WHERE c.owningContent.publishDateTime <= $1 AND c.owningContent.expireDateTime >= $2 ")
495                 .append("AND c.contentVersionId IN LIST ").append(toVersionIdList(contentCategories));
496         */

497         
498         StringBuffer JavaDoc oql = new StringBuffer JavaDoc();
499         oql.append("SELECT c FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl c ")
500                 .append("WHERE c.contentVersionId IN LIST ").append(toVersionIdList(contentCategories));
501
502         ArrayList JavaDoc params = new ArrayList JavaDoc();
503         //params.add(new Date());
504
//params.add(new Date());
505
return executeQuery(db, oql.toString(), params);
506     }
507
508     /**
509      * Is this a valid Content item based on defined rules for publican/expiration etc.,
510      * and is it the most recent ContentVersion for this deployment. If not then we retrieved
511      * based on categories attached to an old version.
512      */

513     private boolean isValidContentVersion(ContentVersion version, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc siteNodeId, Integer JavaDoc languageId, boolean useLanguageFallback, Database db, DeliveryContext deliveryContext) throws Exception JavaDoc
514     {
515         //Content content = version.getOwningContent();
516
Integer JavaDoc contentId = version.getValueObject().getContentId();
517         logger.info("contentId:" + contentId);
518         
519         Content content = (MediumContentImpl)getObjectWithId(MediumContentImpl.class, contentId, db);
520         //Content content = ContentController.getContentController().getContentWithId(contentId, db);
521

522         ContentVersionVO mostRecentVersion = getContentVersionVO(db, siteNodeId, content.getContentId(), languageId, useLanguageFallback, deliveryContext, infoGluePrincipal);
523         boolean isProperVersion = (mostRecentVersion != null) && (mostRecentVersion.getId().equals(version.getId()));
524
525         boolean isValidContent = isValidContent(infoGluePrincipal, content, languageId, useLanguageFallback, false, db, deliveryContext);
526
527         return isProperVersion && isValidContent;
528     }
529
530     /**
531      * Builds and IN list for the query to find all potentially relevant content versions.
532      */

533     private String JavaDoc toVersionIdList(List JavaDoc results)
534     {
535         StringBuffer JavaDoc ids = new StringBuffer JavaDoc("(");
536         for(Iterator JavaDoc iter = results.iterator(); iter.hasNext();)
537             ids.append(((ContentCategoryVO) iter.next()).getContentVersionId() + (iter.hasNext()? ", " : ""));
538         ids.append(")");
539         return ids.toString();
540     }
541
542
543     /**
544      * This method returns all the assetsKeys available in a contentVersion.
545      */

546
547     public Collection JavaDoc getAssetKeys(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
548     {
549         Collection JavaDoc assetKeys = new ArrayList JavaDoc();
550         
551         ContentVersion contentVersion = getContentVersion(siteNodeId, contentId, languageId, db, useLanguageFallback, deliveryContext, infoGluePrincipal);
552         if (contentVersion != null)
553         {
554             Collection JavaDoc assets = contentVersion.getDigitalAssets();
555             Iterator JavaDoc keysIterator = assets.iterator();
556             while(keysIterator.hasNext())
557             {
558                 DigitalAsset asset = (DigitalAsset)keysIterator.next();
559                 String JavaDoc assetKey = asset.getAssetKey();
560                 assetKeys.add(assetKey);
561             }
562         }
563         
564         return assetKeys;
565     }
566
567
568     /**
569      * This method is used by the getAssetUrl methods, to locate a digital asset in another
570      * languageversion. It is called in the case where no asset where found in the supplied language.
571      *
572      * This way an image is only required to exist in one of the language versions, reducing the need for
573      * many duplicates.
574      *
575      */

576     private DigitalAsset getLanguageIndependentAsset(Integer JavaDoc contentId, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, Database db, String JavaDoc assetKey, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
577     {
578         DigitalAsset asset = null;
579         // TODO: This method should only return a asset url depending on settings on the actual content in the future
580
// or possibly a systemwide setting.
581

582         // TODO: experimental
583
// addition ss - 030422
584
// Search digital asset among language versions.
585
List JavaDoc langs = LanguageDeliveryController.getLanguageDeliveryController().getAvailableLanguages(db, siteNodeId);
586         Iterator JavaDoc lit = langs.iterator();
587         while (lit.hasNext())
588         {
589             LanguageVO langVO = (LanguageVO) lit.next();
590             if (langVO.getLanguageId().compareTo(languageId)!=0)
591             {
592                 ContentVersion contentVersion = getContentVersion(siteNodeId, contentId, langVO.getLanguageId(), db, false, deliveryContext, infoGluePrincipal);
593                 if (contentVersion != null)
594                 {
595                     DigitalAsset digitalAsset =
596                         (assetKey == null) ? getLatestDigitalAsset(contentVersion) :getDigitalAssetWithKey(contentVersion, assetKey);
597                     
598                     if(digitalAsset != null)
599                     {
600                         asset = digitalAsset;
601                         break;
602                     }
603                 }
604             }
605         }
606         return asset;
607     }
608
609     private String JavaDoc getLanguageIndependentAssetUrl(Integer JavaDoc contentId, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, Database db, String JavaDoc assetKey, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
610     {
611         String JavaDoc assetUrl = "";
612         assetUrl = urlComposer.composeDigitalAssetUrl("", "", deliveryContext);
613         
614         DigitalAsset digitalAsset = getLanguageIndependentAsset(contentId, languageId, siteNodeId, db, assetKey, deliveryContext, infoGluePrincipal);
615         if(digitalAsset != null)
616         {
617             String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
618             
619             int i = 0;
620             File JavaDoc masterFile = null;
621             String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
622             while(filePath != null)
623             {
624                 try
625                 {
626                     if(masterFile == null)
627                         masterFile = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
628                     else
629                         DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(masterFile, fileName, filePath);
630                 }
631                 catch(Exception JavaDoc e)
632                 {
633                     logger.warn("An file could not be written:" + e.getMessage(), e);
634                 }
635                 
636                 i++;
637                 filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
638             }
639             //String filePath = CmsPropertyHandler.getDigitalAssetPath();
640
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
641

642             SiteNode siteNode = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId).getSiteNode(db, siteNodeId);
643             String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
644             if(siteNode != null && siteNode.getRepository().getDnsName() != null && !siteNode.getRepository().getDnsName().equals(""))
645                 dnsName = siteNode.getRepository().getDnsName();
646                 
647             //assetUrl = dnsName + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName;
648
assetUrl = urlComposer.composeDigitalAssetUrl(dnsName, fileName, deliveryContext);
649         }
650         return assetUrl;
651     }
652
653
654     private String JavaDoc getLanguageIndependentAssetThumbnailUrl(Integer JavaDoc contentId, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, Database db, String JavaDoc assetKey, int width, int height, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
655     {
656         String JavaDoc assetUrl = "";
657         assetUrl = urlComposer.composeDigitalAssetUrl("", "", deliveryContext);
658         
659         DigitalAsset digitalAsset = getLanguageIndependentAsset(contentId, languageId, siteNodeId, db, assetKey, deliveryContext, infoGluePrincipal);
660         if(digitalAsset != null)
661         {
662             String JavaDoc fileName = digitalAsset.getAssetFileName();
663             String JavaDoc thumbnailFileName = "thumbnail_" + width + "_" + height + "_" + fileName;
664
665             int i = 0;
666             File JavaDoc masterFile = null;
667             String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
668             while(filePath != null)
669             {
670                 if(masterFile == null)
671                     masterFile = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(fileName, thumbnailFileName, filePath, width, height);
672                 else
673                     DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(fileName, thumbnailFileName, filePath, width, height);
674                 
675                 i++;
676                 filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
677             }
678
679             //String filePath = CmsPropertyHandler.getDigitalAssetPath();
680
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(digitalAsset, fileName, thumbnailFileName, filePath, width, height);
681

682             SiteNode siteNode = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId).getSiteNode(db, siteNodeId);
683             String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
684             if(siteNode != null && siteNode.getRepository().getDnsName() != null && !siteNode.getRepository().getDnsName().equals(""))
685                 dnsName = siteNode.getRepository().getDnsName();
686                 
687             //assetUrl = dnsName + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + thumbnailFileName;
688
assetUrl = urlComposer.composeDigitalAssetUrl(dnsName, thumbnailFileName, deliveryContext);
689         }
690         return assetUrl;
691     }
692
693
694     /**
695      * This method returns the id of the digital asset.
696      * It selects the correct contentVersion depending on the language and then gets the digitalAsset associated.
697      */

698
699     public Integer JavaDoc getDigitalAssetId(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc assetKey, Integer JavaDoc siteNodeId, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
700     {
701         Integer JavaDoc digitalAssetId = null;
702         
703         ContentVersion contentVersion = getContentVersion(siteNodeId, contentId, languageId, db, useLanguageFallback, deliveryContext, infoGluePrincipal);
704         if (contentVersion != null)
705         {
706             DigitalAsset digitalAsset = getDigitalAssetWithKey(contentVersion, assetKey);
707             
708             if(digitalAsset != null)
709             {
710                 digitalAssetId = digitalAsset.getId();
711             }
712         }
713                     
714         return digitalAssetId;
715     }
716
717     /**
718      * This is the basic way of getting an asset-url for a content.
719      * It selects the correct contentVersion depending on the language and then gets the digitalAsset associated.
720      * If the asset is cached on disk it returns that path imediately it's ok - otherwise it dumps it fresh.
721      */

722
723     public String JavaDoc getAssetUrl(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
724     {
725         String JavaDoc assetCacheKey = "" + languageId + "_" + contentId + "_" + siteNodeId + "_" + useLanguageFallback;
726         logger.info("assetCacheKey:" + assetCacheKey);
727         String JavaDoc cacheName = "assetUrlCache";
728         String JavaDoc cachedAssetUrl = (String JavaDoc)CacheController.getCachedObject(cacheName, assetCacheKey);
729         if(cachedAssetUrl != null)
730         {
731             logger.info("There was an cached cachedAssetUrl:" + cachedAssetUrl);
732             return cachedAssetUrl;
733         }
734         
735         String JavaDoc assetUrl = "";
736         
737         ContentVersion contentVersion = getContentVersion(siteNodeId, contentId, languageId, db, useLanguageFallback, deliveryContext, infoGluePrincipal);
738         if (contentVersion != null)
739         {
740             DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion);
741             
742             if(digitalAsset != null)
743             {
744                 String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
745
746                 int i = 0;
747                 File JavaDoc masterFile = null;
748                 String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
749                 while(filePath != null)
750                 {
751                     try
752                     {
753                         if(masterFile == null)
754                             masterFile = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
755                         else
756                             DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(masterFile, fileName, filePath);
757                     }
758                     catch(Exception JavaDoc e)
759                     {
760                         logger.warn("An file could not be written:" + e.getMessage(), e);
761                     }
762                     
763                     i++;
764                     filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
765                 }
766
767                 //String filePath = CmsPropertyHandler.getDigitalAssetPath();
768
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
769

770                 SiteNode siteNode = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId).getSiteNode(db, siteNodeId);
771                 String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
772                 if(siteNode != null && siteNode.getRepository().getDnsName() != null && !siteNode.getRepository().getDnsName().equals(""))
773                     dnsName = siteNode.getRepository().getDnsName();
774
775                 //assetUrl = dnsName + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName;
776
assetUrl = urlComposer.composeDigitalAssetUrl(dnsName, fileName, deliveryContext);
777             }
778             else
779             {
780                 assetUrl = getLanguageIndependentAssetUrl(contentId, languageId, siteNodeId, db, null, deliveryContext, infoGluePrincipal);
781             }
782         }
783                     
784         CacheController.cacheObject(cacheName, assetCacheKey, assetUrl);
785         
786         return assetUrl;
787     }
788
789
790
791     /**
792      * This is the basic way of getting an asset-url for a content.
793      * It selects the correct contentVersion depending on the language and then gets the digitalAsset associated with the key.
794      * If the asset is cached on disk it returns that path imediately it's ok - otherwise it dumps it fresh.
795      */

796
797     public String JavaDoc getAssetUrl(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc assetKey, Integer JavaDoc siteNodeId, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
798     {
799         SiteNodeVO siteNodeVO = SiteNodeController.getController().getSiteNodeVOWithId(siteNodeId, db);
800         //String assetCacheKey = "" + languageId + "_" + contentId + "_" + siteNodeId + "_" + assetKey + "_" + useLanguageFallback + "_" + deliveryContext.getUseFullUrl();
801
String JavaDoc assetCacheKey = "" + languageId + "_" + contentId + "_" + siteNodeVO.getRepositoryId() + "_" + assetKey + "_" + useLanguageFallback + "_" + deliveryContext.getUseFullUrl();
802         logger.info("assetCacheKey:" + assetCacheKey);
803         //System.out.println("assetCacheKey:" + assetCacheKey);
804

805         String JavaDoc cacheName = "assetUrlCache";
806         String JavaDoc cachedAssetUrl = (String JavaDoc)CacheController.getCachedObject(cacheName, assetCacheKey);
807         if(cachedAssetUrl != null)
808         {
809             logger.info("There was an cached cachedAssetUrl:" + cachedAssetUrl);
810             return cachedAssetUrl;
811         }
812         
813         String JavaDoc assetUrl = "";
814         assetUrl = urlComposer.composeDigitalAssetUrl("", "", deliveryContext);
815         
816         ContentVersion contentVersion = getContentVersion(siteNodeId, contentId, languageId, db, useLanguageFallback, deliveryContext, infoGluePrincipal);
817         ContentVO contentVO = this.getContentVO(db, contentId, deliveryContext);
818         if (contentVersion != null)
819         {
820             DigitalAsset digitalAsset = getDigitalAssetWithKey(contentVersion, assetKey);
821             
822             if(digitalAsset != null)
823             {
824                 String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
825
826                 int i = 0;
827                 File JavaDoc masterFile = null;
828                 String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
829                 //System.out.println("filePath:" + filePath);
830
while(filePath != null)
831                 {
832                     try
833                     {
834                         if(masterFile == null)
835                             masterFile = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
836                         else
837                             DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(masterFile, fileName, filePath);
838                     }
839                     catch(Exception JavaDoc e)
840                     {
841                         logger.warn("An file could not be written:" + e.getMessage(), e);
842                     }
843                     
844                     i++;
845                     filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
846                     //System.out.println("filePath:" + filePath);
847
}
848
849                 //String filePath = CmsPropertyHandler.getDigitalAssetPath();
850
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
851

852                 SiteNode siteNode = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId).getSiteNode(db, siteNodeId);
853                 String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
854                 if(siteNode != null && siteNode.getRepository().getDnsName() != null && !siteNode.getRepository().getDnsName().equals(""))
855                     dnsName = siteNode.getRepository().getDnsName();
856                     
857                 //assetUrl = dnsName + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName;
858
assetUrl = urlComposer.composeDigitalAssetUrl(dnsName, fileName, deliveryContext);
859             }
860             else if(useLanguageFallback)
861             {
862                 assetUrl = getLanguageIndependentAssetUrl(contentId, languageId, siteNodeId, db, assetKey, deliveryContext, infoGluePrincipal);
863             }
864         }
865         else if(useLanguageFallback && languageId.intValue() != LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForRepository(contentVO.getRepositoryId(), db).getId().intValue())
866         {
867             contentVersion = this.getContentVersion(siteNodeId, contentId, languageId, db, useLanguageFallback, deliveryContext, infoGluePrincipal);
868             
869             logger.info("contentVersion:" + contentVersion);
870             if(contentVersion != null)
871             {
872                 DigitalAsset digitalAsset = getDigitalAssetWithKey(contentVersion, assetKey);
873                 
874                 if(digitalAsset != null)
875                 {
876                     String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
877                     
878                     int i = 0;
879                     File JavaDoc masterFile = null;
880                     String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
881                     //System.out.println("filePath:" + filePath);
882
while(filePath != null)
883                     {
884                         try
885                         {
886                             if(masterFile == null)
887                                 masterFile = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
888                             else
889                                 DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(masterFile, fileName, filePath);
890                         }
891                         catch(Exception JavaDoc e)
892                         {
893                             logger.warn("An file could not be written:" + e.getMessage(), e);
894                         }
895                         
896                         i++;
897                         filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
898                         //System.out.println("filePath:" + filePath);
899
}
900
901                     //String filePath = CmsPropertyHandler.getDigitalAssetPath();
902
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
903

904                     SiteNode siteNode = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId).getSiteNode(db, siteNodeId);
905                     String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
906                     if(siteNode != null && siteNode.getRepository().getDnsName() != null && !siteNode.getRepository().getDnsName().equals(""))
907                         dnsName = siteNode.getRepository().getDnsName();
908                         
909                     //assetUrl = dnsName + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName;
910
assetUrl = urlComposer.composeDigitalAssetUrl(dnsName, fileName, deliveryContext);
911                 }
912                 else if(useLanguageFallback)
913                 {
914                     assetUrl = getLanguageIndependentAssetUrl(contentId, languageId, siteNodeId, db, assetKey, deliveryContext, infoGluePrincipal);
915                 }
916             }
917         }
918             
919         CacheController.cacheObject(cacheName, assetCacheKey, assetUrl);
920         
921         return assetUrl;
922     }
923     
924         
925     
926     /**
927      * This is the basic way of getting an asset-url for a content.
928      * It selects the correct contentVersion depending on the language and then gets the digitalAsset associated.
929      * If the asset is cached on disk it returns that path imediately it's ok - otherwise it dumps it fresh.
930      */

931
932     public String JavaDoc getAssetThumbnailUrl(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, boolean useLanguageFallback, int width, int height, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
933     {
934         String JavaDoc assetCacheKey = "" + languageId + "_" + contentId + "_" + siteNodeId + "_" + useLanguageFallback + "_" + width + "_" + height;
935         logger.info("assetCacheKey:" + assetCacheKey);
936         String JavaDoc cacheName = "assetThumbnailUrlCache";
937         String JavaDoc cachedAssetUrl = (String JavaDoc)CacheController.getCachedObject(cacheName, assetCacheKey);
938         if(cachedAssetUrl != null)
939         {
940             logger.info("There was an cached cachedAssetUrl:" + cachedAssetUrl);
941             return cachedAssetUrl;
942         }
943         
944         String JavaDoc assetUrl = "";
945         
946         ContentVersion contentVersion = getContentVersion(siteNodeId, contentId, languageId, db, useLanguageFallback, deliveryContext, infoGluePrincipal);
947         if (contentVersion != null)
948         {
949             DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion);
950             
951             if(digitalAsset != null)
952             {
953                 String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
954                 String JavaDoc thumbnailFileName = "thumbnail_" + width + "_" + height + "_" + fileName;
955
956                 int i = 0;
957                 File JavaDoc masterFile = null;
958                 File JavaDoc masterThumbFile = null;
959                 String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
960                 while(filePath != null)
961                 {
962                     try
963                     {
964                         if(masterFile == null)
965                             masterFile = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
966                         else
967                             DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(masterFile, fileName, filePath);
968                         
969                         if(masterThumbFile == null)
970                             masterThumbFile = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(fileName, thumbnailFileName, filePath, width, height);
971                         else
972                             DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(fileName, thumbnailFileName, filePath, width, height);
973                     }
974                     catch(Exception JavaDoc e)
975                     {
976                         logger.warn("An file could not be written:" + e.getMessage(), e);
977                     }
978                     
979                     i++;
980                     filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
981                 }
982
983                 //String filePath = CmsPropertyHandler.getDigitalAssetPath();
984
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
985
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(digitalAsset, fileName, thumbnailFileName, filePath, width, height);
986

987                 SiteNode siteNode = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId).getSiteNode(db, siteNodeId);
988                 String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
989                 if(siteNode != null && siteNode.getRepository().getDnsName() != null && !siteNode.getRepository().getDnsName().equals(""))
990                     dnsName = siteNode.getRepository().getDnsName();
991
992                 //assetUrl = dnsName + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + thumbnailFileName;
993
assetUrl = urlComposer.composeDigitalAssetUrl(dnsName, thumbnailFileName, deliveryContext);
994             }
995             else
996             {
997                 assetUrl = getLanguageIndependentAssetThumbnailUrl(contentId, languageId, siteNodeId, db, null, width, height, deliveryContext, infoGluePrincipal);
998             }
999         }
1000        
1001        CacheController.cacheObject(cacheName, assetCacheKey, assetUrl);
1002        
1003        return assetUrl;
1004    }
1005
1006
1007
1008    /**
1009     * This is the basic way of getting an asset-url for a content.
1010     * It selects the correct contentVersion depending on the language and then gets the digitalAsset associated with the key.
1011     * If the asset is cached on disk it returns that path imediately it's ok - otherwise it dumps it fresh.
1012     */

1013
1014    public String JavaDoc getAssetThumbnailUrl(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc assetKey, Integer JavaDoc siteNodeId, boolean useLanguageFallback, int width, int height, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
1015    {
1016        String JavaDoc assetCacheKey = "" + languageId + "_" + contentId + "_" + siteNodeId + "_" + assetKey + "_" + useLanguageFallback + "_" + width + "_" + height;
1017        logger.info("assetCacheKey:" + assetCacheKey);
1018        String JavaDoc cacheName = "assetThumbnailUrlCache";
1019        String JavaDoc cachedAssetUrl = (String JavaDoc)CacheController.getCachedObject(cacheName, assetCacheKey);
1020        if(cachedAssetUrl != null)
1021        {
1022            logger.info("There was an cached cachedAssetUrl:" + cachedAssetUrl);
1023            return cachedAssetUrl;
1024        }
1025
1026        String JavaDoc assetUrl = "";
1027        
1028        ContentVersion contentVersion = getContentVersion(siteNodeId, contentId, languageId, db, useLanguageFallback, deliveryContext, infoGluePrincipal);
1029        if (contentVersion != null)
1030        {
1031            DigitalAsset digitalAsset = getDigitalAssetWithKey(contentVersion, assetKey);
1032            
1033            if(digitalAsset != null)
1034            {
1035                String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
1036                String JavaDoc thumbnailFileName = "thumbnail_" + width + "_" + height + "_" + fileName;
1037
1038                int i = 0;
1039                File JavaDoc masterFile = null;
1040                File JavaDoc masterThumbFile = null;
1041                String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
1042                while(filePath != null)
1043                {
1044                    try
1045                    {
1046                        if(masterFile == null)
1047                            masterFile = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
1048                        else
1049                            DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
1050                        
1051                        if(masterThumbFile == null)
1052                            masterThumbFile = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(fileName, thumbnailFileName, filePath, width, height);
1053                        else
1054                            DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(fileName, thumbnailFileName, filePath, width, height);
1055                    }
1056                    catch(Exception JavaDoc e)
1057                    {
1058                        logger.warn("An file could not be written:" + e.getMessage(), e);
1059                    }
1060                    
1061                    i++;
1062                    filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
1063                }
1064
1065                //String filePath = CmsPropertyHandler.getDigitalAssetPath();
1066
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAsset(digitalAsset, fileName, filePath);
1067
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpDigitalAssetThumbnail(digitalAsset, fileName, thumbnailFileName, filePath, width, height);
1068

1069                SiteNode siteNode = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId).getSiteNode(db, siteNodeId);
1070                String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
1071                if(siteNode != null && siteNode.getRepository().getDnsName() != null && !siteNode.getRepository().getDnsName().equals(""))
1072                    dnsName = siteNode.getRepository().getDnsName();
1073                
1074                //assetUrl = dnsName + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + thumbnailFileName;
1075
assetUrl = urlComposer.composeDigitalAssetUrl(dnsName, thumbnailFileName, deliveryContext);
1076            }
1077            else
1078            {
1079                assetUrl = getLanguageIndependentAssetThumbnailUrl(contentId, languageId, siteNodeId, db, assetKey, width, height, deliveryContext, infoGluePrincipal);
1080            }
1081            
1082        }
1083        
1084        CacheController.cacheObject(cacheName, assetCacheKey, assetUrl);
1085        
1086        return assetUrl;
1087    }
1088    
1089
1090
1091    /*
1092     * getAssetFileSize. Prelimenary, we should rather supply a assetvo to the template.
1093     */

1094     
1095    public Integer JavaDoc getAssetFileSize(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
1096    {
1097        return getAssetFileSize(db, contentId, languageId, null, siteNodeId, useLanguageFallback, deliveryContext, infoGluePrincipal);
1098    }
1099    
1100    public Integer JavaDoc getAssetFileSize(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc assetKey, Integer JavaDoc siteNodeId, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
1101    {
1102        Integer JavaDoc fileSize = null;
1103    
1104        ContentVersion contentVersion = getContentVersion(siteNodeId, contentId, languageId, db, useLanguageFallback, deliveryContext, infoGluePrincipal);
1105        if (contentVersion != null)
1106        {
1107            DigitalAsset digitalAsset =
1108                (assetKey == null) ? getLatestDigitalAsset(contentVersion) : getDigitalAssetWithKey(contentVersion, assetKey);
1109            
1110            if(digitalAsset == null)
1111                digitalAsset = getLanguageIndependentAsset(contentId, languageId, siteNodeId, db, assetKey, deliveryContext, infoGluePrincipal);
1112                
1113            if(digitalAsset != null)
1114            {
1115                fileSize = digitalAsset.getAssetFileSize();
1116            }
1117        }
1118            
1119        return fileSize;
1120    }
1121    
1122
1123    /**
1124     * This method deliveres a String with the URL to the base path of the directory resulting from
1125     * an unpacking of a uploaded zip-digitalAsset.
1126     */

1127
1128    public String JavaDoc getArchiveBaseUrl(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc assetKey, Integer JavaDoc siteNodeId, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
1129    {
1130        String JavaDoc archiveBaseUrl = null;
1131        
1132        ContentVersion contentVersion = getContentVersion(siteNodeId, contentId, languageId, db, useLanguageFallback, deliveryContext, infoGluePrincipal);
1133        if (contentVersion != null)
1134        {
1135            DigitalAsset digitalAsset = getDigitalAssetWithKey(contentVersion, assetKey);
1136            
1137            if(digitalAsset != null)
1138            {
1139                String JavaDoc fileName = digitalAsset.getAssetFileName();
1140                
1141                int i = 0;
1142                File JavaDoc masterFile = null;
1143                String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
1144                while(filePath != null)
1145                {
1146                    File JavaDoc unzipDirectory = new File JavaDoc(filePath + File.separator + fileName.substring(0, fileName.lastIndexOf(".")));
1147                    unzipDirectory.mkdirs();
1148                    
1149                    if(masterFile == null)
1150                        masterFile = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpAndUnzipDigitalAsset(digitalAsset, fileName, filePath, unzipDirectory);
1151                    else
1152                        DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpAndUnzipDigitalAsset(masterFile, fileName, filePath, unzipDirectory);
1153                        
1154                    i++;
1155                    filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
1156                }
1157
1158                //String filePath = CmsPropertyHandler.getDigitalAssetPath();
1159
//File unzipDirectory = new File(filePath + File.separator + fileName.substring(0, fileName.lastIndexOf(".")));
1160
//unzipDirectory.mkdirs();
1161
//DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpAndUnzipDigitalAsset(digitalAsset, fileName, filePath, unzipDirectory);
1162

1163                SiteNode siteNode = NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId).getSiteNode(db, siteNodeId);
1164                String JavaDoc dnsName = CmsPropertyHandler.getWebServerAddress();
1165                if(siteNode != null && siteNode.getRepository().getDnsName() != null && !siteNode.getRepository().getDnsName().equals(""))
1166                    dnsName = siteNode.getRepository().getDnsName();
1167                    
1168                //archiveBaseUrl = dnsName + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName.substring(0, fileName.lastIndexOf("."));
1169
archiveBaseUrl = urlComposer.composeDigitalAssetUrl(dnsName, fileName.substring(0, fileName.lastIndexOf(".")), deliveryContext);
1170            }
1171        }
1172        
1173        return archiveBaseUrl;
1174    }
1175
1176    public Vector JavaDoc getArchiveEntries(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc assetKey, Integer JavaDoc siteNodeId, boolean useLanguageFallback, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
1177    {
1178        Vector JavaDoc entries = null;
1179        
1180        ContentVersion contentVersion = getContentVersion(siteNodeId, contentId, languageId, db, useLanguageFallback, deliveryContext, infoGluePrincipal);
1181        if (contentVersion != null)
1182        {
1183            DigitalAsset digitalAsset = getDigitalAssetWithKey(contentVersion, assetKey);
1184            
1185            if(digitalAsset != null)
1186            {
1187                String JavaDoc fileName = digitalAsset.getAssetFileName();
1188
1189                int i = 0;
1190                File JavaDoc masterFile = null;
1191                String JavaDoc filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
1192                while(filePath != null)
1193                {
1194                    File JavaDoc unzipDirectory = new File JavaDoc(filePath + File.separator + fileName.substring(0, fileName.lastIndexOf(".")));
1195                    unzipDirectory.mkdirs();
1196                    
1197                    if(masterFile == null)
1198                    {
1199                        entries = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpAndGetZipEntries(digitalAsset, fileName, filePath, unzipDirectory);
1200                        masterFile = new File JavaDoc(filePath + File.separator + fileName);
1201                    }
1202                    else
1203                    {
1204                        entries = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpAndGetZipEntries(masterFile, fileName, filePath, unzipDirectory);
1205                    }
1206                    
1207                    i++;
1208                    filePath = CmsPropertyHandler.getProperty("digitalAssetPath." + i);
1209                }
1210
1211                //String filePath = CmsPropertyHandler.getDigitalAssetPath();
1212
//File unzipDirectory = new File(filePath + File.separator + fileName.substring(0, fileName.lastIndexOf(".")));
1213
//unzipDirectory.mkdirs();
1214
//entries = DigitalAssetDeliveryController.getDigitalAssetDeliveryController().dumpAndGetZipEntries(digitalAsset, fileName, filePath, unzipDirectory);
1215
}
1216        }
1217        
1218        return entries;
1219    }
1220
1221
1222    
1223    /**
1224     * Returns the digital asset for a contentversion that has a certain key.
1225     */

1226    
1227    private DigitalAsset getDigitalAssetWithKey(ContentVersion contentVersion, String JavaDoc assetKey)
1228    {
1229        Collection JavaDoc digitalAssets = contentVersion.getDigitalAssets();
1230        Iterator JavaDoc iterator = digitalAssets.iterator();
1231        
1232        while(iterator.hasNext())
1233        {
1234            DigitalAsset currentDigitalAsset = (DigitalAsset)iterator.next();
1235                
1236            if(currentDigitalAsset != null && currentDigitalAsset.getAssetKey().equalsIgnoreCase(assetKey))
1237            {
1238                return currentDigitalAsset;
1239            }
1240        }
1241
1242        return null;
1243    }
1244
1245    /**
1246     * Returns the latest digital asset for a contentversion.
1247     */

1248    
1249    private DigitalAsset getLatestDigitalAsset(ContentVersion contentVersion)
1250    {
1251        Collection JavaDoc digitalAssets = contentVersion.getDigitalAssets();
1252        Iterator JavaDoc iterator = digitalAssets.iterator();
1253        
1254        DigitalAsset digitalAsset = null;
1255        while(iterator.hasNext())
1256        {
1257            DigitalAsset currentDigitalAsset = (DigitalAsset)iterator.next();
1258            if(digitalAsset == null || currentDigitalAsset.getDigitalAssetId().intValue() > digitalAsset.getDigitalAssetId().intValue())
1259                digitalAsset = currentDigitalAsset;
1260        }
1261        return digitalAsset;
1262    }
1263    
1264    
1265
1266
1267    /**
1268     * This method fetches a value from the xml that is the contentVersions Value. If the
1269     * contentVersioVO is null the contentVersion has not been created yet and no values are present.
1270     */

1271    public String JavaDoc getAttributeValue(Database db, ContentVersionVO contentVersionVO, String JavaDoc key, boolean escapeHTML)
1272    {
1273        String JavaDoc value = "";
1274        if(contentVersionVO != null)
1275        {
1276            try
1277            {
1278                String JavaDoc xml = contentVersionVO.getVersionValue();
1279                
1280                int startTagIndex = xml.indexOf("<" + key + ">");
1281                int endTagIndex = xml.indexOf("]]></" + key + ">");
1282                
1283                if(startTagIndex > 0 && startTagIndex < xml.length() && endTagIndex > startTagIndex && endTagIndex < xml.length())
1284                    value = xml.substring(startTagIndex + key.length() + 11, endTagIndex);
1285
1286                if(escapeHTML)
1287                    value = formatter.escapeHTML(value);
1288                
1289                /*
1290                InputSource inputSource = new InputSource(new StringReader(contentVersionVO.getVersionValue()));
1291                
1292                DOMParser parser = new DOMParser();
1293                parser.parse(inputSource);
1294                Document document = parser.getDocument();
1295                
1296                NodeList nl = document.getDocumentElement().getChildNodes();
1297                Node n = nl.item(0);
1298                
1299                nl = n.getChildNodes();
1300                for(int i=0; i<nl.getLength(); i++)
1301                {
1302                    n = nl.item(i);
1303                    if(n.getNodeName().equalsIgnoreCase(key))
1304                    {
1305                        value = n.getFirstChild().getNodeValue();
1306                        logger.warn("Getting value: " + value);
1307
1308                        break;
1309                    }
1310                }
1311                */

1312            }
1313            catch(Exception JavaDoc e)
1314            {
1315                logger.error("An error occurred so we should not return the attribute value:" + e, e);
1316            }
1317        }
1318
1319        return value;
1320    }
1321    
1322
1323    /**
1324     * This method returns a sorted list of childContents to a content ordered by the given attribute in the direction given.
1325     */

1326
1327    public List JavaDoc getChildContents(Database db, InfoGluePrincipal infoGluePrincipal, Integer JavaDoc contentId, Integer JavaDoc languageId, boolean useLanguageFallback, boolean includeFolders, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1328    {
1329        List JavaDoc childContents = new ArrayList JavaDoc();
1330    
1331        getChildContents(infoGluePrincipal, childContents, contentId, languageId, useLanguageFallback, 0, false, includeFolders, 1, db, deliveryContext);
1332    
1333        return childContents;
1334    }
1335        
1336
1337    /**
1338     * This method returns a sorted list of childContents to a content ordered by the given attribute in the direction given.
1339     */

1340    
1341    public List JavaDoc getSortedChildContents(InfoGluePrincipal infoGluePrincipal, Integer JavaDoc languageId, Integer JavaDoc contentId, Integer JavaDoc siteNodeId, Database db, boolean searchRecursive, Integer JavaDoc maximumNumberOfLevels, String JavaDoc sortAttribute, String JavaDoc sortOrder, boolean useLanguageFallback, boolean includeFolders, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1342    {
1343        
1344        String JavaDoc sortedChildContentsKey = "" + infoGluePrincipal.getName() + "_" + languageId + "_" + contentId + "_" + siteNodeId + "_" + searchRecursive + "_" + maximumNumberOfLevels + "_" + sortAttribute + "_" + sortOrder + "_" + useLanguageFallback + "_" + includeFolders;
1345        logger.info("sortedChildContentsKey:" + sortedChildContentsKey);
1346        String JavaDoc cacheName = "sortedChildContentsCache";
1347        List JavaDoc cachedSortedContentVOList = (List JavaDoc)CacheController.getCachedObject(cacheName, sortedChildContentsKey);
1348        if(cachedSortedContentVOList != null)
1349        {
1350            logger.info("There was an cached content cachedSortedContentVOList:" + cachedSortedContentVOList.size());
1351            return cachedSortedContentVOList;
1352        }
1353        
1354        List JavaDoc sortedContentVOList = new ArrayList JavaDoc();
1355        
1356        List JavaDoc unsortedChildren = getChildContents(infoGluePrincipal, languageId, useLanguageFallback, contentId, siteNodeId, searchRecursive, maximumNumberOfLevels, db, includeFolders, deliveryContext);
1357        
1358        List JavaDoc sortedContents = sortContents(db, unsortedChildren, languageId, siteNodeId, sortAttribute, sortOrder, useLanguageFallback, includeFolders, deliveryContext, infoGluePrincipal);
1359
1360        Iterator JavaDoc boundContentsIterator = sortedContents.iterator();
1361        while(boundContentsIterator.hasNext())
1362        {
1363            Content content = (Content)boundContentsIterator.next();
1364            sortedContentVOList.add(content.getValueObject());
1365        }
1366        
1367        CacheController.cacheObject(cacheName, sortedChildContentsKey, sortedContentVOList);
1368            
1369        return sortedContentVOList;
1370    }
1371    
1372
1373
1374    /**
1375     * This method returns the contentTypeDefinitionVO of the content sent in.
1376     */

1377    
1378    public ContentTypeDefinitionVO getContentTypeDefinitionVO(Database db, Integer JavaDoc contentId) throws SystemException, Exception JavaDoc
1379    {
1380        ContentTypeDefinitionVO contentTypeDefinitionVO = null;
1381        
1382        Content content = (Content)getObjectWithId(ContentImpl.class, contentId, db);
1383        contentTypeDefinitionVO = content.getContentTypeDefinition().getValueObject();
1384        
1385        return contentTypeDefinitionVO;
1386    }
1387
1388
1389    /**
1390     * This method returns a list of children to the content given. It is mostly used to get all
1391     * children a folder has.
1392     */

1393    
1394    private List JavaDoc getChildContents(InfoGluePrincipal infoGluePrincipal, Integer JavaDoc languageId, boolean useLanguageFallback, Integer JavaDoc contentId, Integer JavaDoc siteNodeId, boolean searchRecursive, Integer JavaDoc maximumNumberOfLevels, Database db, boolean includeFolders, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1395    {
1396        List JavaDoc contents = new ArrayList JavaDoc();
1397        
1398        getChildContents(infoGluePrincipal, contents, contentId, languageId, useLanguageFallback, 0, searchRecursive, maximumNumberOfLevels.intValue(), db, includeFolders, deliveryContext);
1399        
1400        return contents;
1401    }
1402    
1403
1404    /**
1405     * This method recurses into the dept of the content-children and fills the list of contents.
1406     */

1407    
1408    private void getChildContents(InfoGluePrincipal infoGluePrincipal, List JavaDoc contents, Integer JavaDoc contentId, Integer JavaDoc languageId, boolean useLanguageFallback, int currentLevel, boolean searchRecursive, int maximumNumberOfLevels, Database db, boolean includeFolders, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1409    {
1410        /*
1411        OQLQuery oql = db.getOQLQuery("SELECT contentVersion FROM org.infoglue.cms.entities.content.impl.simple.ContentVersionImpl contentVersion WHERE contentVersion.stateId = $1 AND contentVersion.isActive = $2 AND contentVersion.owningContent.parentContent.contentId = $3");
1412        oql.bind(getOperatingMode());
1413        oql.bind(new Boolean(true));
1414        oql.bind(contentId);
1415        
1416        QueryResults results = oql.execute(Database.ReadOnly);
1417        
1418        while (results.hasMore())
1419        {
1420            ContentVersion contentVersion = (ContentVersion)results.next();
1421            Content content = contentVersion.getOwningContent();
1422            
1423            if(searchRecursive && currentLevel < maximumNumberOfLevels)
1424                getChildContents(contents, content.getContentId(), currentLevel + 1, searchRecursive, maximumNumberOfLevels, db);
1425    
1426            if(isValidContent(content, db))
1427            {
1428                if(!contents.contains(content))
1429                    contents.add(content);
1430            }
1431        }
1432        */

1433        
1434        deliveryContext.addUsedContent("selectiveCacheUpdateNonApplicable");
1435
1436        OQLQuery oql = db.getOQLQuery("SELECT content FROM org.infoglue.cms.entities.content.impl.simple.ContentImpl content WHERE content.parentContent.contentId = $1 ORDER BY content.contentId");
1437        oql.bind(contentId);
1438        
1439        QueryResults results = oql.execute(Database.ReadOnly);
1440        
1441        while (results.hasMore())
1442        {
1443            Content content = (Content)results.next();
1444    
1445            if(searchRecursive && currentLevel < maximumNumberOfLevels)
1446                getChildContents(infoGluePrincipal, contents, content.getContentId(), languageId, useLanguageFallback, currentLevel + 1, searchRecursive, maximumNumberOfLevels, db, includeFolders, deliveryContext);
1447
1448            if(isValidContent(infoGluePrincipal, content, languageId, useLanguageFallback, includeFolders, db, deliveryContext))
1449            {
1450                contents.add(content);
1451            }
1452        }
1453        
1454        results.close();
1455        oql.close();
1456    }
1457
1458    
1459    /**
1460     * This method recurses into the dept of the content-children and fills the list of contents.
1461     */

1462    
1463    private void getChildContents(InfoGluePrincipal infoGluePrincipal, List JavaDoc contents, Integer JavaDoc contentId, Integer JavaDoc languageId, boolean useLanguageFallback, int currentLevel, boolean searchRecursive, boolean includeFolders, int maximumNumberOfLevels, Database db, DeliveryContext deliveryContext) throws SystemException, Exception JavaDoc
1464    {
1465        deliveryContext.addUsedContent("selectiveCacheUpdateNonApplicable");
1466
1467        OQLQuery oql = db.getOQLQuery( "SELECT content FROM org.infoglue.cms.entities.content.impl.simple.ContentImpl content WHERE content.parentContent.contentId = $1 ORDER BY content.contentId");
1468        oql.bind(contentId);
1469        
1470        QueryResults results = oql.execute(Database.ReadOnly);
1471        
1472        while (results.hasMore())
1473        {
1474            Content content = (Content)results.next();
1475    
1476            if(searchRecursive && currentLevel < maximumNumberOfLevels)
1477                getChildContents(infoGluePrincipal, contents, content.getContentId(), languageId, useLanguageFallback, currentLevel + 1, searchRecursive, includeFolders, maximumNumberOfLevels, db, deliveryContext);
1478
1479            if(content.getIsBranch().booleanValue() && includeFolders && isValidContent(infoGluePrincipal, content, languageId, useLanguageFallback, includeFolders, db, deliveryContext))
1480            {
1481                contents.add(content);
1482            }
1483            else if(isValidContent(infoGluePrincipal, content, languageId, useLanguageFallback, includeFolders, db, deliveryContext))
1484            {
1485                contents.add(content);
1486            }
1487        }
1488        
1489        results.close();
1490        oql.close();
1491    }
1492    
1493    /**
1494     * This method validates that right now is between publishdate and expiredate.
1495     */

1496    
1497    private boolean isValidOnDates(Date JavaDoc publishDate, Date JavaDoc expireDate)
1498    {
1499        boolean isValid = true;
1500        Date JavaDoc now = new Date JavaDoc();
1501        
1502        if(publishDate.after(now) || expireDate.before(now))
1503            isValid = false;
1504        
1505        return isValid;
1506    }
1507
1508    /**
1509     * Returns if a content is between dates and has a content version suitable for this delivery mode.
1510     * @throws Exception
1511     */

1512/*
1513    public boolean isValidContent(Integer contentId, InfoGluePrincipal infoGluePrincipal, Database db) throws Exception
1514    {
1515        boolean isValidContent = false;
1516        
1517        Content content = (Content)getObjectWithId(ContentImpl.class, contentId, db);
1518        isValidContent = isValidContent(content, db);
1519        
1520        return isValidContent;
1521    }
1522*/

1523    /**
1524     * Returns if a content is between dates and has a content version suitable for this delivery mode.
1525     * @throws Exception
1526     */

1527
1528    public boolean isValidContent(Database db, Integer JavaDoc contentId, Integer JavaDoc languageId, boolean useLanguageFallback, boolean includeFolders, InfoGluePrincipal infoGluePrincipal, DeliveryContext deliveryContext) throws Exception JavaDoc
1529    {
1530        boolean isValidContent = false;
1531        
1532        Content content = (Content)getObjectWithId(ContentImpl.class, contentId, db);
1533        isValidContent = isValidContent(infoGluePrincipal, content, languageId, useLanguageFallback, includeFolders, db, deliveryContext);
1534        
1535        return isValidContent;
1536    }
1537    
1538    /**
1539     * Returns if a content is between dates and has a content version suitable for this delivery mode.
1540     * @throws Exception
1541     */

1542
1543    public boolean isValidContent(InfoGluePrincipal infoGluePrincipal, Content content, Integer JavaDoc languageId, boolean useLanguageFallBack, boolean includeFolders, Database db, DeliveryContext deliveryContext) throws Exception JavaDoc
1544    {
1545        boolean isValidContent = false;
1546        if(infoGluePrincipal == null)
1547            throw new SystemException("There was no anonymous user found in the system. There must be - add the user anonymous/anonymous and try again.");
1548        
1549        if(content.getContentTypeDefinition() != null && content.getContentTypeDefinition().getName().equalsIgnoreCase("Meta info"))
1550            return true;
1551
1552        logger.info("content:" + content.getName());
1553        
1554        Integer JavaDoc protectedContentId = getProtectedContentId(db, content);
1555        logger.info("IsProtected:" + protectedContentId);
1556        
1557        if(protectedContentId != null && !AccessRightController.getController().getIsPrincipalAuthorized(db, infoGluePrincipal, "Content.Read", protectedContentId.toString()))
1558        {
1559            return false;
1560        }
1561                
1562        if(includeFolders && content.getIsBranch().booleanValue() && isValidOnDates(content.getPublishDateTime(), content.getExpireDateTime()))
1563        {
1564            isValidContent = true;
1565        }
1566        else if(isValidOnDates(content.getPublishDateTime(), content.getExpireDateTime()))
1567        {
1568            ContentVersion contentVersion = getContentVersion(content, languageId, getOperatingMode(), deliveryContext, db);
1569
1570            Integer JavaDoc repositoryId = null;
1571            Repository repository = content.getRepository();
1572            if(repository == null)
1573            {
1574                if(content instanceof MediumContentImpl)
1575                    repositoryId = ((MediumContentImpl)content).getRepositoryId();
1576                else if(content instanceof SmallContentImpl)
1577                    repositoryId = ((SmallContentImpl)content).getRepositoryId();
1578            }
1579            else
1580            {
1581                repositoryId = repository.getId();
1582            }
1583            
1584            if(contentVersion == null && useLanguageFallBack && repositoryId != null)
1585            {
1586                LanguageVO masterLanguage = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForRepository(repositoryId, db);
1587                if(masterLanguage != null && !masterLanguage.getId().equals(languageId))
1588                    contentVersion = getContentVersion(content, masterLanguage.getId(), getOperatingMode(), deliveryContext, db);
1589            }
1590
1591            if(contentVersion != null)
1592                isValidContent = true;
1593            
1594            /*
1595            Collection versions = content.getContentVersions();
1596            Iterator versionsIterator = versions.iterator();
1597            while(versionsIterator.hasNext())
1598            {
1599                ContentVersion contentVersion = (ContentVersion)versionsIterator.next();
1600
1601                if(contentVersion.getIsActive().booleanValue() && contentVersion.getStateId().intValue() >= getOperatingMode().intValue())
1602                    isValidContent = true;
1603            }
1604            */

1605        }
1606        
1607        if(isValidContent && !content.getExpireDateTime().before(new Date JavaDoc()))
1608        {
1609            Date JavaDoc expireDateTimeCandidate = content.getExpireDateTime();
1610            if(CacheController.expireDateTime == null || expireDateTimeCandidate.before(CacheController.expireDateTime))
1611            {
1612                CacheController.expireDateTime = expireDateTimeCandidate;
1613            }
1614        }
1615        else if(content.getPublishDateTime().after(new Date JavaDoc())) //If it's a publish date to come we consider it
1616
{
1617            Date JavaDoc publishDateTimeCandidate = content.getPublishDateTime();
1618            if(CacheController.publishDateTime == null || publishDateTimeCandidate.after(CacheController.publishDateTime))
1619            {
1620                CacheController.publishDateTime = publishDateTimeCandidate;
1621            }
1622        }
1623        
1624        return isValidContent;
1625    }
1626
1627    
1628    
1629    /**
1630     * This method returns the id of the content that is protected if any. Looks recursive upwards.
1631     */

1632    
1633    public Integer JavaDoc getProtectedContentId(Database db, Integer JavaDoc contentId) throws SystemException, Exception JavaDoc
1634    {
1635        Integer JavaDoc protectedContentId = null;
1636        
1637        Content content = (Content)getObjectWithId(ContentImpl.class, contentId, db);
1638        protectedContentId = getProtectedContentId(db, content);
1639                
1640        return protectedContentId;
1641    }
1642
1643    
1644    /**
1645     * This method returns the id of the content that is protected if any. Looks recursive upwards.
1646     */

1647    
1648    public Integer JavaDoc getProtectedContentId(Database db, Content content)
1649    {
1650        Integer JavaDoc protectedContentId = null;
1651        
1652        try
1653        {
1654            logger.info("content:" + content.getId() + ":" + content.getIsProtected());
1655
1656            if(content != null && content.getIsProtected() != null)
1657            {
1658                if(content.getIsProtected().intValue() == ContentVO.NO.intValue())
1659                    protectedContentId = null;
1660                else if(content.getIsProtected().intValue() == ContentVO.YES.intValue())
1661                    protectedContentId = content.getId();
1662                else if(content.getIsProtected().intValue() == ContentVO.INHERITED.intValue())
1663                {
1664                    Content parentContent = null; //= content.getParentContent();
1665
if(content instanceof MediumContentImpl)
1666                    {
1667                        Integer JavaDoc parentContentId = ((MediumContentImpl)content).getParentContentId();
1668                        if(parentContentId != null)
1669                            parentContent = (MediumContentImpl)getObjectWithId(MediumContentImpl.class, parentContentId, db);
1670                    }
1671                    else if(content instanceof SmallContentImpl)
1672                    {
1673                        Integer JavaDoc parentContentId = ((SmallContentImpl)content).getParentContentId();
1674                        if(parentContentId != null)
1675                            parentContent = (SmallContentImpl)getObjectWithId(SmallContentImpl.class, parentContentId, db);
1676                    }
1677                    else if(content instanceof ContentImpl)
1678                    {
1679                        parentContent = content.getParentContent();
1680                    }
1681                    
1682                    if(parentContent != null)
1683                    {
1684                        protectedContentId = getProtectedContentId(db, parentContent);
1685                    }
1686                }
1687            }
1688
1689        }
1690        catch(Exception JavaDoc e)
1691        {
1692            logger.warn("An error occurred trying to get if the siteNodeVersion has disabled pageCache:" + e.getMessage(), e);
1693        }
1694                
1695        return protectedContentId;
1696    }
1697
1698    
1699    /**
1700     * This method just sorts the list of qualifyers on sortOrder.
1701     */

1702    
1703    public List JavaDoc sortContents(Database db, Collection JavaDoc contents, Integer JavaDoc languageId, Integer JavaDoc siteNodeId, String JavaDoc sortAttributeName, String JavaDoc sortOrder, boolean useLanguageFallback, boolean includeFolders, DeliveryContext deliveryContext, InfoGluePrincipal infoGluePrincipal)
1704    {
1705        List JavaDoc sortedContents = new ArrayList JavaDoc();
1706
1707        try
1708        {
1709            Iterator JavaDoc iterator = contents.iterator();
1710            while(iterator.hasNext())
1711            {
1712                Content content = (Content)iterator.next();
1713                if(includeFolders || content.getIsBranch().booleanValue() == false)
1714                {
1715                    int index = 0;
1716                    Iterator JavaDoc sortedListIterator = sortedContents.iterator();
1717                    while(sortedListIterator.hasNext())
1718                    {
1719                        Content sortedContent = (Content)sortedListIterator.next();
1720                        
1721                        //Here we sort on name if the name on a container is wanted
1722
if(sortAttributeName.equalsIgnoreCase("name"))
1723                        {
1724                            String JavaDoc name = content.getName();
1725                            String JavaDoc sortedName = sortedContent.getName();
1726                            if(name != null && sortedName != null && sortOrder.equalsIgnoreCase("asc") && name.compareTo(sortedName) < 0)
1727                            {
1728                                break;
1729                            }
1730                            else if(name != null && sortedName != null && sortOrder.equalsIgnoreCase("desc") && name.compareTo(sortedName) > 0)
1731                            {
1732                                break;
1733                            }
1734                        }
1735                        //Here we sort on date if the dates on a container is wanted
1736
else if(sortAttributeName.equalsIgnoreCase("publishDateTime"))
1737                        {
1738                            Date JavaDoc date = content.getPublishDateTime();
1739                            Date JavaDoc sortedDate = sortedContent.getPublishDateTime();
1740                            if(date != null && sortedDate != null && sortOrder.equalsIgnoreCase("asc") && date.before(sortedDate))
1741                            {
1742                                break;
1743                            }
1744                            else if(date != null && sortedDate != null && sortOrder.equalsIgnoreCase("desc") && date.after(sortedDate))
1745                            {
1746                                break;
1747                            }
1748                        }
1749                        else if(sortAttributeName.equalsIgnoreCase("expireDateTime"))
1750                        {
1751                            Date JavaDoc date = content.getExpireDateTime();
1752                            Date JavaDoc sortedDate = sortedContent.getExpireDateTime();
1753                            if(date != null && sortedDate != null && sortOrder.equalsIgnoreCase("asc") && date.before(sortedDate))
1754                            {
1755                                break;
1756                            }
1757                            else if(date != null && sortedDate != null && sortOrder.equalsIgnoreCase("desc") && date.after(sortedDate))
1758                            {
1759                                break;
1760                            }
1761                        }
1762                        else
1763                        {
1764                            String JavaDoc contentAttribute = this.getContentAttribute(db, content.getId(), languageId, sortAttributeName, siteNodeId, useLanguageFallback, deliveryContext, infoGluePrincipal, false);
1765                            String JavaDoc sortedContentAttribute = this.getContentAttribute(db, sortedContent.getId(), languageId, sortAttributeName, siteNodeId, useLanguageFallback, deliveryContext, infoGluePrincipal, false);
1766                            if(contentAttribute != null && sortedContentAttribute != null && sortOrder.equalsIgnoreCase("asc") && contentAttribute.compareTo(sortedContentAttribute) < 0)
1767                            {
1768                                break;
1769                            }
1770                            else if(contentAttribute != null && sortedContentAttribute != null && sortOrder.equalsIgnoreCase("desc") && contentAttribute.compareTo(sortedContentAttribute) > 0)
1771                            {
1772                                break;
1773                            }
1774                        }
1775                        index++;
1776                    }
1777                    sortedContents.add(index, content);
1778                }
1779            }
1780        }
1781        catch(Exception JavaDoc e)
1782        {
1783            logger.warn("The sorting of contents failed:" + e.getMessage(), e);
1784        }
1785            
1786        return sortedContents;
1787    }
1788    
1789
1790}
Popular Tags