KickJava   Java API By Example, From Geeks To Geeks.

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


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

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.io.BufferedInputStream JavaDoc;
27 import java.io.BufferedOutputStream JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.FilenameFilter JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 import org.apache.log4j.Logger;
39 import org.exolab.castor.jdo.Database;
40 import org.infoglue.cms.entities.content.Content;
41 import org.infoglue.cms.entities.content.ContentVersion;
42 import org.infoglue.cms.entities.content.DigitalAsset;
43 import org.infoglue.cms.entities.content.DigitalAssetVO;
44 import org.infoglue.cms.entities.content.impl.simple.DigitalAssetImpl;
45 import org.infoglue.cms.entities.kernel.BaseEntityVO;
46 import org.infoglue.cms.entities.management.GroupProperties;
47 import org.infoglue.cms.entities.management.LanguageVO;
48 import org.infoglue.cms.entities.management.RoleProperties;
49 import org.infoglue.cms.entities.management.UserProperties;
50 import org.infoglue.cms.exception.Bug;
51 import org.infoglue.cms.exception.ConstraintException;
52 import org.infoglue.cms.exception.SystemException;
53 import org.infoglue.cms.util.CmsPropertyHandler;
54 import org.infoglue.cms.util.ConstraintExceptionBuffer;
55 import org.infoglue.cms.util.graphics.ThumbnailGenerator;
56 import org.infoglue.deliver.controllers.kernel.impl.simple.LanguageDeliveryController;
57
58 /**
59  * @author Mattias Bogeblad
60  */

61
62 public class DigitalAssetController extends BaseController
63 {
64     private final static Logger logger = Logger.getLogger(DigitalAssetController.class.getName());
65
66     public static DigitalAssetController getController()
67     {
68         return new DigitalAssetController();
69     }
70
71     
72     /**
73      * returns the digital asset VO
74      */

75     
76     public static DigitalAssetVO getDigitalAssetVOWithId(Integer JavaDoc digitalAssetId) throws SystemException, Bug
77     {
78         return (DigitalAssetVO) getVOWithId(DigitalAssetImpl.class, digitalAssetId);
79     }
80     
81     /**
82      * returns a digitalasset
83      */

84     
85     public static DigitalAsset getDigitalAssetWithId(Integer JavaDoc digitalAssetId, Database db) throws SystemException, Bug
86     {
87         return (DigitalAsset) getObjectWithId(DigitalAssetImpl.class, digitalAssetId, db);
88     }
89
90
91     /**
92      * This method creates a new digital asset in the database and connects it to the contentVersion it belongs to.
93      * The asset is send in as an InputStream which castor inserts automatically.
94      */

95
96     public static DigitalAssetVO create(DigitalAssetVO digitalAssetVO, InputStream JavaDoc is, Integer JavaDoc contentVersionId) throws SystemException
97     {
98         Database db = CastorDatabaseService.getDatabase();
99
100         beginTransaction(db);
101         
102         try
103         {
104             ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(contentVersionId, db);
105
106             digitalAssetVO = create(digitalAssetVO, is, contentVersion, db);
107             
108             commitTransaction(db);
109         }
110         catch(Exception JavaDoc e)
111         {
112             logger.error("An error occurred so we should not complete the transaction:" + e, e);
113             rollbackTransaction(db);
114             throw new SystemException(e.getMessage());
115         }
116                 
117         return digitalAssetVO;
118     }
119
120     /**
121      * This method creates a new digital asset in the database and connects it to the contentVersion it belongs to.
122      * The asset is send in as an InputStream which castor inserts automatically.
123      */

124
125     public static DigitalAssetVO create(DigitalAssetVO digitalAssetVO, InputStream JavaDoc is, ContentVersion contentVersion, Database db) throws SystemException, Exception JavaDoc
126     {
127         DigitalAsset digitalAsset = null;
128         
129         Collection JavaDoc contentVersions = new ArrayList JavaDoc();
130         contentVersions.add(contentVersion);
131         logger.info("Added contentVersion:" + contentVersion.getId());
132     
133         digitalAsset = new DigitalAssetImpl();
134         digitalAsset.setValueObject(digitalAssetVO.createCopy());
135         digitalAsset.setAssetBlob(is);
136         digitalAsset.setContentVersions(contentVersions);
137
138         db.create(digitalAsset);
139         
140         //if(contentVersion.getDigitalAssets() == null)
141
// contentVersion.setDigitalAssets(new ArrayList());
142

143         contentVersion.getDigitalAssets().add(digitalAsset);
144                         
145         return digitalAsset.getValueObject();
146     }
147
148     /**
149      * This method creates a new digital asset in the database and connects it to the contentVersion it belongs to.
150      * The asset is send in as an InputStream which castor inserts automatically.
151      */

152
153     public static DigitalAssetVO create(DigitalAssetVO digitalAssetVO, InputStream JavaDoc is, String JavaDoc entity, Integer JavaDoc entityId) throws ConstraintException, SystemException
154     {
155         Database db = CastorDatabaseService.getDatabase();
156         
157         DigitalAsset digitalAsset = null;
158         
159         beginTransaction(db);
160         
161         try
162         {
163             if(entity.equalsIgnoreCase("ContentVersion"))
164             {
165                 ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(entityId, db);
166                 Collection JavaDoc contentVersions = new ArrayList JavaDoc();
167                 contentVersions.add(contentVersion);
168                 logger.info("Added contentVersion:" + contentVersion.getId());
169             
170                 digitalAsset = new DigitalAssetImpl();
171                 digitalAsset.setValueObject(digitalAssetVO);
172                 digitalAsset.setAssetBlob(is);
173                 digitalAsset.setContentVersions(contentVersions);
174
175                 db.create(digitalAsset);
176             
177                 contentVersion.getDigitalAssets().add(digitalAsset);
178             }
179             else if(entity.equalsIgnoreCase(UserProperties.class.getName()))
180             {
181                 UserProperties userProperties = UserPropertiesController.getController().getUserPropertiesWithId(entityId, db);
182                 Collection JavaDoc userPropertiesList = new ArrayList JavaDoc();
183                 userPropertiesList.add(userProperties);
184                 logger.info("Added userProperties:" + userProperties.getId());
185             
186                 digitalAsset = new DigitalAssetImpl();
187                 digitalAsset.setValueObject(digitalAssetVO);
188                 digitalAsset.setAssetBlob(is);
189                 digitalAsset.setUserProperties(userPropertiesList);
190                 
191                 db.create(digitalAsset);
192             
193                 userProperties.getDigitalAssets().add(digitalAsset);
194             }
195             else if(entity.equalsIgnoreCase(RoleProperties.class.getName()))
196             {
197                 RoleProperties roleProperties = RolePropertiesController.getController().getRolePropertiesWithId(entityId, db);
198                 Collection JavaDoc rolePropertiesList = new ArrayList JavaDoc();
199                 rolePropertiesList.add(roleProperties);
200                 logger.info("Added roleProperties:" + roleProperties.getId());
201             
202                 digitalAsset = new DigitalAssetImpl();
203                 digitalAsset.setValueObject(digitalAssetVO);
204                 digitalAsset.setAssetBlob(is);
205                 digitalAsset.setRoleProperties(rolePropertiesList);
206                 
207                 db.create(digitalAsset);
208             
209                 roleProperties.getDigitalAssets().add(digitalAsset);
210             }
211             else if(entity.equalsIgnoreCase(GroupProperties.class.getName()))
212             {
213                 GroupProperties groupProperties = GroupPropertiesController.getController().getGroupPropertiesWithId(entityId, db);
214                 Collection JavaDoc groupPropertiesList = new ArrayList JavaDoc();
215                 groupPropertiesList.add(groupProperties);
216                 logger.info("Added groupProperties:" + groupProperties.getId());
217             
218                 digitalAsset = new DigitalAssetImpl();
219                 digitalAsset.setValueObject(digitalAssetVO);
220                 digitalAsset.setAssetBlob(is);
221                 digitalAsset.setGroupProperties(groupPropertiesList);
222                 
223                 db.create(digitalAsset);
224             
225                 groupProperties.getDigitalAssets().add(digitalAsset);
226             }
227         
228             commitTransaction(db);
229         }
230         catch(Exception JavaDoc e)
231         {
232             logger.error("An error occurred so we should not complete the transaction:" + e, e);
233             rollbackTransaction(db);
234             throw new SystemException(e.getMessage());
235         }
236                 
237         return digitalAsset.getValueObject();
238     }
239
240     /**
241      * This method creates a new digital asset in the database.
242      * The asset is send in as an InputStream which castor inserts automatically.
243      */

244
245     public DigitalAsset create(Database db, DigitalAssetVO digitalAssetVO, InputStream JavaDoc is) throws SystemException, Exception JavaDoc
246     {
247         DigitalAsset digitalAsset = new DigitalAssetImpl();
248         digitalAsset.setValueObject(digitalAssetVO);
249         digitalAsset.setAssetBlob(is);
250
251         db.create(digitalAsset);
252                 
253         return digitalAsset;
254     }
255
256     /**
257      * This method creates a new digital asset in the database and connects it to the contentVersion it belongs to.
258      * The asset is send in as an InputStream which castor inserts automatically.
259      */

260
261     public DigitalAssetVO createByCopy(Integer JavaDoc originalContentVersionId, String JavaDoc oldAssetKey, Integer JavaDoc newContentVersionId, String JavaDoc newAssetKey, Database db) throws ConstraintException, SystemException
262     {
263         logger.info("Creating by copying....");
264         logger.info("originalContentVersionId:" + originalContentVersionId);
265         logger.info("oldAssetKey:" + oldAssetKey);
266         logger.info("newContentVersionId:" + newContentVersionId);
267         logger.info("newAssetKey:" + newAssetKey);
268         
269         DigitalAsset oldDigitalAsset = getDigitalAsset(originalContentVersionId, oldAssetKey, db);
270         
271         ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(newContentVersionId, db);
272         Collection JavaDoc contentVersions = new ArrayList JavaDoc();
273         contentVersions.add(contentVersion);
274         logger.info("Added contentVersion:" + contentVersion.getId());
275         
276         DigitalAssetVO digitalAssetVO = new DigitalAssetVO();
277         digitalAssetVO.setAssetContentType(oldDigitalAsset.getAssetContentType());
278         digitalAssetVO.setAssetFileName(oldDigitalAsset.getAssetFileName());
279         digitalAssetVO.setAssetFilePath(oldDigitalAsset.getAssetFilePath());
280         digitalAssetVO.setAssetFileSize(oldDigitalAsset.getAssetFileSize());
281         digitalAssetVO.setAssetKey(newAssetKey);
282         
283         DigitalAsset digitalAsset = new DigitalAssetImpl();
284         digitalAsset.setValueObject(digitalAssetVO);
285         digitalAsset.setAssetBlob(oldDigitalAsset.getAssetBlob());
286         digitalAsset.setContentVersions(contentVersions);
287         
288         try
289         {
290             db.create(digitalAsset);
291         }
292         catch(Exception JavaDoc e)
293         {
294             logger.error("An error occurred so we should not complete the transaction:" + e, e);
295             throw new SystemException(e.getMessage());
296         }
297         //contentVersion.getDigitalAssets().add(digitalAsset);
298

299         return digitalAsset.getValueObject();
300     }
301
302     /**
303      * This method gets a asset with a special key inside the given transaction.
304      */

305     
306     public DigitalAsset getDigitalAsset(Integer JavaDoc contentVersionId, String JavaDoc assetKey, Database db) throws SystemException
307     {
308         DigitalAsset digitalAsset = null;
309         
310         ContentVersion contentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(contentVersionId, db);
311         Collection JavaDoc digitalAssets = contentVersion.getDigitalAssets();
312         Iterator JavaDoc assetIterator = digitalAssets.iterator();
313         while(assetIterator.hasNext())
314         {
315             DigitalAsset currentDigitalAsset = (DigitalAsset)assetIterator.next();
316             if(currentDigitalAsset.getAssetKey().equals(assetKey))
317             {
318                 digitalAsset = currentDigitalAsset;
319                 break;
320             }
321         }
322         
323         return digitalAsset;
324     }
325     
326     
327     /**
328      * This method deletes a digital asset in the database.
329      */

330
331     public static void delete(Integer JavaDoc digitalAssetId) throws ConstraintException, SystemException
332     {
333         deleteEntity(DigitalAssetImpl.class, digitalAssetId);
334     }
335
336     /**
337      * This method deletes a digital asset in the database.
338      */

339
340     public void delete(Integer JavaDoc digitalAssetId, Database db) throws ConstraintException, SystemException
341     {
342         deleteEntity(DigitalAssetImpl.class, digitalAssetId, db);
343     }
344
345     /**
346      * This method deletes a digital asset in the database.
347      */

348
349     public void delete(Integer JavaDoc digitalAssetId, String JavaDoc entity, Integer JavaDoc entityId) throws ConstraintException, SystemException
350     {
351         Database db = CastorDatabaseService.getDatabase();
352
353         beginTransaction(db);
354
355         try
356         {
357             DigitalAsset digitalAsset = DigitalAssetController.getDigitalAssetWithId(digitalAssetId, db);
358             
359             if(entity.equalsIgnoreCase("ContentVersion"))
360                 ContentVersionController.getContentVersionController().deleteDigitalAssetRelation(entityId, digitalAsset, db);
361             else if(entity.equalsIgnoreCase(UserProperties.class.getName()))
362                 UserPropertiesController.getController().deleteDigitalAssetRelation(entityId, digitalAsset, db);
363             else if(entity.equalsIgnoreCase(RoleProperties.class.getName()))
364                 RolePropertiesController.getController().deleteDigitalAssetRelation(entityId, digitalAsset, db);
365             else if(entity.equalsIgnoreCase(GroupProperties.class.getName()))
366                 GroupPropertiesController.getController().deleteDigitalAssetRelation(entityId, digitalAsset, db);
367
368             db.remove(digitalAsset);
369
370             commitTransaction(db);
371         }
372         catch(Exception JavaDoc e)
373         {
374             logger.error("An error occurred so we should not completes the transaction:" + e, e);
375             rollbackTransaction(db);
376             throw new SystemException(e.getMessage());
377         }
378     }
379     
380
381     /**
382      * This method removes all images in the digitalAsset directory which belongs to a certain digital asset.
383      */

384     
385     public static void deleteCachedDigitalAssets(Integer JavaDoc digitalAssetId) throws SystemException, Exception JavaDoc
386     {
387         try
388         {
389             File JavaDoc assetDirectory = new File JavaDoc(CmsPropertyHandler.getDigitalAssetPath());
390             File JavaDoc[] files = assetDirectory.listFiles(new FilenameFilterImpl(digitalAssetId.toString()));
391             for(int i=0; i<files.length; i++)
392             {
393                 File JavaDoc file = files[i];
394                 file.delete();
395             }
396     
397         }
398         catch(Exception JavaDoc e)
399         {
400             logger.error("Could not delete the assets for the digitalAsset " + digitalAssetId + ":" + e.getMessage(), e);
401         }
402     }
403     
404     /**
405      * This method updates a digital asset in the database.
406      */

407     
408     public static DigitalAssetVO update(DigitalAssetVO digitalAssetVO, InputStream JavaDoc is) throws ConstraintException, SystemException
409     {
410         Database db = CastorDatabaseService.getDatabase();
411         
412         DigitalAsset digitalAsset = null;
413         
414         beginTransaction(db);
415
416         try
417         {
418             digitalAsset = getDigitalAssetWithId(digitalAssetVO.getId(), db);
419             
420             digitalAsset.setValueObject(digitalAssetVO);
421             if(is != null)
422                 digitalAsset.setAssetBlob(is);
423
424             commitTransaction(db);
425         }
426         catch(Exception JavaDoc e)
427         {
428             logger.error("An error occurred so we should not complete the transaction:" + e, e);
429             rollbackTransaction(db);
430             throw new SystemException(e.getMessage());
431         }
432
433         return digitalAsset.getValueObject();
434     }
435     
436     /**
437      * This method deletes all contentVersions for the content sent in and also clears all the digital Assets.
438      * Should not be available probably as you might destroy for other versions and other contents.
439      * /
440     /*
441     public static void deleteDigitalAssetsForContentVersionWithId(Integer contentVersionId) throws ConstraintException, SystemException, Bug
442     {
443         Database db = CastorDatabaseService.getDatabase();
444         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
445
446         beginTransaction(db);
447         List digitalAssets = new ArrayList();
448         try
449         {
450             ContentVersion contentVersion = ContentVersionController.getContentVersionWithId(contentVersionId, db);
451             
452             Collection digitalAssetList = contentVersion.getDigitalAssets();
453             Iterator assets = digitalAssetList.iterator();
454             while (assets.hasNext())
455             {
456                 DigitalAsset digitalAsset = (DigitalAsset)assets.next();
457                 digitalAssets.add(digitalAsset.getValueObject());
458             }
459             
460             commitTransaction(db);
461         }
462         catch(Exception e)
463         {
464             e.printStackTrace();
465             logger.error("An error occurred so we should not completes the transaction:" + e, e);
466             rollbackTransaction(db);
467             throw new SystemException(e.getMessage());
468         }
469
470         Iterator i = digitalAssets.iterator();
471         while(i.hasNext())
472         {
473             DigitalAssetVO digitalAssetVO = (DigitalAssetVO)i.next();
474             logger.info("Deleting digitalAsset:" + digitalAssetVO.getDigitalAssetId());
475             delete(digitalAssetVO.getDigitalAssetId());
476         }
477
478     }
479     */

480
481     /**
482      * This method should return a list of those digital assets the contentVersion has.
483      */

484         
485     public static List JavaDoc getDigitalAssetVOList(Integer JavaDoc contentVersionId) throws SystemException, Bug
486     {
487         Database db = CastorDatabaseService.getDatabase();
488
489         List JavaDoc digitalAssetVOList = new ArrayList JavaDoc();
490
491         beginTransaction(db);
492
493         try
494         {
495             ContentVersion contentVersion = ContentVersionController.getContentVersionController().getReadOnlyContentVersionWithId(contentVersionId, db);
496             if(contentVersion != null)
497             {
498                 Collection JavaDoc digitalAssets = contentVersion.getDigitalAssets();
499                 digitalAssetVOList = toVOList(digitalAssets);
500             }
501                         
502             commitTransaction(db);
503         }
504         catch(Exception JavaDoc e)
505         {
506             logger.info("An error occurred when we tried to fetch the list of digitalAssets belonging to this contentVersion:" + e);
507             e.printStackTrace();
508             rollbackTransaction(db);
509             throw new SystemException(e.getMessage());
510         }
511         
512         return digitalAssetVOList;
513     }
514
515
516     /**
517      * This method should return a String containing the URL for this digital asset.
518      */

519         
520     public static List JavaDoc getDigitalAssetVOList(Integer JavaDoc contentId, Integer JavaDoc languageId, boolean useLanguageFallback) throws SystemException, Bug
521     {
522         Database db = CastorDatabaseService.getDatabase();
523
524         List JavaDoc digitalAssetVOList = null;
525
526         beginTransaction(db);
527
528         try
529         {
530             digitalAssetVOList = getDigitalAssetVOList(contentId, languageId, useLanguageFallback, db);
531             
532             commitTransaction(db);
533         }
534         catch(Exception JavaDoc e)
535         {
536             e.printStackTrace();
537             logger.info("An error occurred when we tried to cache and show the digital asset:" + e);
538             rollbackTransaction(db);
539             throw new SystemException(e.getMessage());
540         }
541         
542         return digitalAssetVOList;
543     }
544
545     /**
546      * This method should return a String containing the URL for this digital asset.
547      */

548         
549     public static List JavaDoc getDigitalAssetVOList(Integer JavaDoc contentId, Integer JavaDoc languageId, boolean useLanguageFallback, Database db) throws SystemException, Bug, Exception JavaDoc
550     {
551         List JavaDoc digitalAssetVOList = new ArrayList JavaDoc();
552
553         Content content = ContentController.getContentController().getContentWithId(contentId, db);
554         logger.info("content:" + content.getName());
555         logger.info("repositoryId:" + content.getRepository().getId());
556         logger.info("languageId:" + languageId);
557         ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentId, languageId, db);
558         LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForRepository(content.getRepository().getRepositoryId(), db);
559         
560         logger.info("contentVersion:" + contentVersion);
561         if(contentVersion != null)
562         {
563             Collection JavaDoc digitalAssets = contentVersion.getDigitalAssets();
564             digitalAssetVOList = toModifiableVOList(digitalAssets);
565             
566             logger.info("digitalAssetVOList:" + digitalAssetVOList.size());
567             if(useLanguageFallback && languageId.intValue() != masterLanguageVO.getId().intValue())
568             {
569                 List JavaDoc masterDigitalAssetVOList = getDigitalAssetVOList(contentId, masterLanguageVO.getId(), useLanguageFallback, db);
570                 Iterator JavaDoc digitalAssetVOListIterator = digitalAssetVOList.iterator();
571                 while(digitalAssetVOListIterator.hasNext())
572                 {
573                     DigitalAssetVO currentDigitalAssetVO = (DigitalAssetVO)digitalAssetVOListIterator.next();
574                     
575                     Iterator JavaDoc masterDigitalAssetVOListIterator = masterDigitalAssetVOList.iterator();
576                     while(masterDigitalAssetVOListIterator.hasNext())
577                     {
578                         DigitalAssetVO masterCurrentDigitalAssetVO = (DigitalAssetVO)masterDigitalAssetVOListIterator.next();
579                         if(currentDigitalAssetVO.getAssetKey().equalsIgnoreCase(masterCurrentDigitalAssetVO.getAssetKey()))
580                             masterDigitalAssetVOListIterator.remove();
581                     }
582                 }
583                 digitalAssetVOList.addAll(masterDigitalAssetVOList);
584             }
585         }
586         else if(useLanguageFallback && languageId.intValue() != masterLanguageVO.getId().intValue())
587         {
588             contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentId, masterLanguageVO.getId(), db);
589             
590             logger.info("contentVersion:" + contentVersion);
591             if(contentVersion != null)
592             {
593                 Collection JavaDoc digitalAssets = contentVersion.getDigitalAssets();
594                 digitalAssetVOList = toModifiableVOList(digitalAssets);
595             }
596         }
597         
598         return digitalAssetVOList;
599     }
600     
601     
602     
603     /**
604      * This method should return a String containing the URL for this digital asset.
605      */

606
607     public static String JavaDoc getDigitalAssetUrl(Integer JavaDoc digitalAssetId) throws SystemException, Bug
608     {
609         Database db = CastorDatabaseService.getDatabase();
610
611         String JavaDoc assetUrl = null;
612
613         beginTransaction(db);
614
615         try
616         {
617             DigitalAsset digitalAsset = getDigitalAssetWithId(digitalAssetId, db);
618                         
619             if(digitalAsset != null)
620             {
621                 logger.info("Found a digital asset:" + digitalAsset.getAssetFileName());
622                 String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
623                 //String filePath = digitalAsset.getAssetFilePath();
624
String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
625                 dumpDigitalAsset(digitalAsset, fileName, filePath);
626                 assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName;
627             }
628
629             commitTransaction(db);
630         }
631         catch(Exception JavaDoc e)
632         {
633             logger.info("An error occurred when we tried to cache and show the digital asset:" + e);
634             e.printStackTrace();
635             rollbackTransaction(db);
636             throw new SystemException(e.getMessage());
637         }
638         
639         return assetUrl;
640     }
641
642     /**
643      * This is a method that stores the asset on disk if not there allready and returns the asset as an InputStream
644      * from that location. To avoid trouble with in memory blobs.
645      */

646     
647     public InputStream JavaDoc getAssetInputStream(DigitalAsset digitalAsset) throws Exception JavaDoc
648     {
649         String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
650         String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
651         dumpDigitalAsset(digitalAsset, fileName, filePath);
652         return new FileInputStream JavaDoc(filePath + File.separator + fileName);
653     }
654
655     /**
656      * This method should return a String containing the URL for this digital asset.
657      */

658
659     public String JavaDoc getDigitalAssetUrl(DigitalAssetVO digitalAssetVO, Database db) throws SystemException, Bug, Exception JavaDoc
660     {
661         String JavaDoc assetUrl = null;
662
663         DigitalAsset digitalAsset = getDigitalAssetWithId(digitalAssetVO.getId(), db);
664                     
665         if(digitalAsset != null)
666         {
667             logger.info("Found a digital asset:" + digitalAsset.getAssetFileName());
668             String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
669             String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
670             dumpDigitalAsset(digitalAsset, fileName, filePath);
671             assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName;
672         }
673         
674         return assetUrl;
675     }
676
677
678     /**
679      * This method should return a String containing the URL for this digital assets icon/thumbnail.
680      * In the case of an image the downscaled image is returned - otherwise an icon that represents the
681      * content-type of the file. It always fetches the latest one if several assets exists.
682      */

683         
684     public static String JavaDoc getDigitalAssetThumbnailUrl(Integer JavaDoc digitalAssetId) throws SystemException, Bug
685     {
686         Database db = CastorDatabaseService.getDatabase();
687
688         String JavaDoc assetUrl = null;
689
690         beginTransaction(db);
691
692         try
693         {
694             DigitalAsset digitalAsset = getDigitalAssetWithId(digitalAssetId, db);
695             
696             if(digitalAsset != null)
697             {
698                 logger.info("Found a digital asset:" + digitalAsset.getAssetFileName());
699                 String JavaDoc contentType = digitalAsset.getAssetContentType();
700                 
701                 if(contentType.equalsIgnoreCase("image/gif") || contentType.equalsIgnoreCase("image/jpg") || contentType.equalsIgnoreCase("image/pjpeg") || contentType.equalsIgnoreCase("image/jpeg"))
702                 {
703                     String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
704                     logger.info("fileName:" + fileName);
705                     String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
706                     logger.info("filePath:" + filePath);
707                     String JavaDoc thumbnailFileName = digitalAsset.getDigitalAssetId() + "_thumbnail_" + digitalAsset.getAssetFileName();
708                     //String thumbnailFileName = "thumbnail_" + fileName;
709
File JavaDoc thumbnailFile = new File JavaDoc(filePath + File.separator + thumbnailFileName);
710                     if(!thumbnailFile.exists())
711                     {
712                         logger.info("transforming...");
713                         ThumbnailGenerator tg = new ThumbnailGenerator();
714                         tg.transform(filePath + File.separator + fileName, filePath + File.separator + thumbnailFileName, 75, 75, 100);
715                         logger.info("transform done...");
716                     }
717                     assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + thumbnailFileName;
718                     logger.info("assetUrl:" + assetUrl);
719                 }
720                 else
721                 {
722                     if(contentType.equalsIgnoreCase("application/pdf"))
723                     {
724                         assetUrl = "images/pdf.gif";
725                     }
726                     else if(contentType.equalsIgnoreCase("application/msword"))
727                     {
728                         assetUrl = "images/msword.gif";
729                     }
730                     else if(contentType.equalsIgnoreCase("application/vnd.ms-excel"))
731                     {
732                         assetUrl = "images/msexcel.gif";
733                     }
734                     else if(contentType.equalsIgnoreCase("application/vnd.ms-powerpoint"))
735                     {
736                         assetUrl = "images/mspowerpoint.gif";
737                     }
738                     else
739                     {
740                         assetUrl = "images/digitalAsset.gif";
741                     }
742                 }
743             }
744                         
745             commitTransaction(db);
746         }
747         catch(Exception JavaDoc e)
748         {
749             logger.info("An error occurred when we tried to cache and show the digital asset thumbnail:" + e);
750             rollbackTransaction(db);
751             throw new SystemException(e.getMessage());
752         }
753         
754         return assetUrl;
755     }
756
757     
758     /**
759      * This method should return a String containing the URL for this digital asset.
760      */

761         
762     public static String JavaDoc getDigitalAssetUrl(Integer JavaDoc contentId, Integer JavaDoc languageId) throws SystemException, Bug
763     {
764         Database db = CastorDatabaseService.getDatabase();
765
766         String JavaDoc assetUrl = null;
767
768         beginTransaction(db);
769
770         try
771         {
772             ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestContentVersion(contentId, languageId, db);
773             if(contentVersion != null)
774             {
775                 DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion);
776                 
777                 if(digitalAsset != null)
778                 {
779                     logger.info("Found a digital asset:" + digitalAsset.getAssetFileName());
780                     String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
781                     //String filePath = digitalAsset.getAssetFilePath();
782
String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
783                     
784                     dumpDigitalAsset(digitalAsset, fileName, filePath);
785                     assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName;
786                 }
787             }
788                         
789             commitTransaction(db);
790         }
791         catch(Exception JavaDoc e)
792         {
793             logger.info("An error occurred when we tried to cache and show the digital asset:" + e);
794             e.printStackTrace();
795             rollbackTransaction(db);
796             throw new SystemException(e.getMessage());
797         }
798         
799         return assetUrl;
800     }
801
802     /**
803      * This method should return a String containing the URL for this digital asset.
804      */

805         
806     public static String JavaDoc getDigitalAssetUrl(Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc assetKey, boolean useLanguageFallback) throws SystemException, Bug
807     {
808         Database db = CastorDatabaseService.getDatabase();
809
810         String JavaDoc assetUrl = null;
811
812         beginTransaction(db);
813
814         try
815         {
816             assetUrl = getDigitalAssetUrl(contentId, languageId, assetKey, useLanguageFallback, db);
817             
818             commitTransaction(db);
819         }
820         catch(Exception JavaDoc e)
821         {
822             logger.info("An error occurred when we tried to cache and show the digital asset:" + e);
823             e.printStackTrace();
824             rollbackTransaction(db);
825             throw new SystemException(e.getMessage());
826         }
827         
828         return assetUrl;
829     }
830
831     /**
832      * This method should return a String containing the URL for this digital asset.
833      */

834         
835     public static String JavaDoc getDigitalAssetUrl(Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc assetKey, boolean useLanguageFallback, Database db) throws SystemException, Bug, Exception JavaDoc
836     {
837         String JavaDoc assetUrl = null;
838
839         Content content = ContentController.getContentController().getContentWithId(contentId, db);
840         logger.info("content:" + content.getName());
841         logger.info("repositoryId:" + content.getRepository().getId());
842         logger.info("languageId:" + languageId);
843         logger.info("assetKey:" + assetKey);
844         ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentId, languageId, db);
845         LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForRepository(content.getRepository().getRepositoryId(), db);
846         logger.info("contentVersion:" + contentVersion);
847         if(contentVersion != null)
848         {
849             DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion, assetKey);
850             logger.info("digitalAsset:" + digitalAsset);
851             if(digitalAsset != null)
852             {
853                 logger.info("digitalAsset:" + digitalAsset.getAssetKey());
854                 logger.info("Found a digital asset:" + digitalAsset.getAssetFileName());
855                 String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
856                 String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
857                 
858                 dumpDigitalAsset(digitalAsset, fileName, filePath);
859                 assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName;
860             }
861             else
862             {
863                 //LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForRepository(content.getRepository().getRepositoryId(), db);
864
if(useLanguageFallback && languageId.intValue() != masterLanguageVO.getId().intValue())
865                     return getDigitalAssetUrl(contentId, masterLanguageVO.getId(), assetKey, useLanguageFallback, db);
866             }
867         }
868         else if(useLanguageFallback && languageId.intValue() != masterLanguageVO.getId().intValue())
869         {
870             contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentId, masterLanguageVO.getId(), db);
871             
872             logger.info("contentVersion:" + contentVersion);
873             if(contentVersion != null)
874             {
875                 DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion, assetKey);
876                 logger.info("digitalAsset:" + digitalAsset);
877                 if(digitalAsset != null)
878                 {
879                     logger.info("digitalAsset:" + digitalAsset.getAssetKey());
880                     logger.info("Found a digital asset:" + digitalAsset.getAssetFileName());
881                     String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
882                     String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
883                     
884                     dumpDigitalAsset(digitalAsset, fileName, filePath);
885                     assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName;
886                 }
887             }
888         }
889         
890         return assetUrl;
891     }
892
893     /**
894      * This method should return a String containing the URL for this digital asset.
895      */

896         
897     public static DigitalAssetVO getDigitalAssetVO(Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc assetKey, boolean useLanguageFallback) throws SystemException, Bug
898     {
899         Database db = CastorDatabaseService.getDatabase();
900
901         DigitalAssetVO digitalAssetVO = null;
902
903         beginTransaction(db);
904
905         try
906         {
907             digitalAssetVO = getDigitalAssetVO(contentId, languageId, assetKey, useLanguageFallback, db);
908             
909             commitTransaction(db);
910         }
911         catch(Exception JavaDoc e)
912         {
913             logger.info("An error occurred when we tried to get a digitalAssetVO:" + e);
914             e.printStackTrace();
915             rollbackTransaction(db);
916             throw new SystemException(e.getMessage());
917         }
918         
919         return digitalAssetVO;
920     }
921
922     /**
923      * This method should return a DigitalAssetVO
924      */

925         
926     public static DigitalAssetVO getDigitalAssetVO(Integer JavaDoc contentId, Integer JavaDoc languageId, String JavaDoc assetKey, boolean useLanguageFallback, Database db) throws SystemException, Bug, Exception JavaDoc
927     {
928         DigitalAssetVO digitalAssetVO = null;
929
930         Content content = ContentController.getContentController().getContentWithId(contentId, db);
931         ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestActiveContentVersion(contentId, languageId, db);
932         if(contentVersion != null)
933         {
934             DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion, assetKey);
935             if(digitalAsset != null)
936             {
937                 digitalAssetVO = digitalAsset.getValueObject();
938             }
939             else
940             {
941                 LanguageVO masterLanguageVO = LanguageDeliveryController.getLanguageDeliveryController().getMasterLanguageForRepository(content.getRepository().getRepositoryId(), db);
942                 if(useLanguageFallback && languageId.intValue() != masterLanguageVO.getId().intValue())
943                     return getDigitalAssetVO(contentId, masterLanguageVO.getId(), assetKey, useLanguageFallback, db);
944             }
945         }
946         
947         return digitalAssetVO;
948     }
949
950     
951     
952     /**
953      * This method should return a String containing the URL for this digital assets icon/thumbnail.
954      * In the case of an image the downscaled image is returned - otherwise an icon that represents the
955      * content-type of the file. It always fetches the latest one if several assets exists.
956      */

957         
958     public static String JavaDoc getDigitalAssetThumbnailUrl(Integer JavaDoc contentId, Integer JavaDoc languageId) throws SystemException, Bug
959     {
960         Database db = CastorDatabaseService.getDatabase();
961
962         String JavaDoc assetUrl = null;
963
964         beginTransaction(db);
965
966         try
967         {
968             ContentVersion contentVersion = ContentVersionController.getContentVersionController().getLatestContentVersion(contentId, languageId, db);
969             if(contentVersion != null)
970             {
971                 DigitalAsset digitalAsset = getLatestDigitalAsset(contentVersion);
972                 
973                 if(digitalAsset != null)
974                 {
975                     logger.info("Found a digital asset:" + digitalAsset.getAssetFileName());
976                     String JavaDoc contentType = digitalAsset.getAssetContentType();
977                     
978                     if(contentType.equalsIgnoreCase("image/gif") || contentType.equalsIgnoreCase("image/jpg"))
979                     {
980                         String JavaDoc fileName = digitalAsset.getDigitalAssetId() + "_" + digitalAsset.getAssetFileName();
981                         //String filePath = digitalAsset.getAssetFilePath();
982
String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
983                         String JavaDoc thumbnailFileName = digitalAsset.getDigitalAssetId() + "_thumbnail_" + digitalAsset.getAssetFileName();
984                         //String thumbnailFileName = "thumbnail_" + fileName;
985
File JavaDoc thumbnailFile = new File JavaDoc(filePath + File.separator + thumbnailFileName);
986                         if(!thumbnailFile.exists())
987                         {
988                             ThumbnailGenerator tg = new ThumbnailGenerator();
989                             tg.transform(filePath + File.separator + fileName, filePath + File.separator + thumbnailFileName, 150, 150, 100);
990                         }
991                         assetUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + thumbnailFileName;
992                     }
993                     else
994                     {
995                         if(contentType.equalsIgnoreCase("application/pdf"))
996                         {
997                             assetUrl = "images/pdf.gif";
998                         }
999                         else if(contentType.equalsIgnoreCase("application/msword"))
1000                        {
1001                            assetUrl = "images/msword.gif";
1002                        }
1003                        else if(contentType.equalsIgnoreCase("application/vnd.ms-excel"))
1004                        {
1005                            assetUrl = "images/msexcel.gif";
1006                        }
1007                        else if(contentType.equalsIgnoreCase("application/vnd.ms-powerpoint"))
1008                        {
1009                            assetUrl = "images/mspowerpoint.gif";
1010                        }
1011                        else
1012                        {
1013                            assetUrl = "images/digitalAsset.gif";
1014                        }
1015                    }
1016                }
1017                else
1018                {
1019                    assetUrl = "images/notDefined.gif";
1020                }
1021            }
1022                        
1023            commitTransaction(db);
1024        }
1025        catch(Exception JavaDoc e)
1026        {
1027            logger.info("An error occurred when we tried to cache and show the digital asset thumbnail:" + e);
1028            rollbackTransaction(db);
1029            throw new SystemException(e.getMessage());
1030        }
1031        
1032        return assetUrl;
1033    }
1034
1035    /**
1036     * Returns the latest digital asset for a contentversion.
1037     */

1038    
1039    private static DigitalAsset getLatestDigitalAsset(ContentVersion contentVersion)
1040    {
1041        Collection JavaDoc digitalAssets = contentVersion.getDigitalAssets();
1042        Iterator JavaDoc iterator = digitalAssets.iterator();
1043        
1044        DigitalAsset digitalAsset = null;
1045        while(iterator.hasNext())
1046        {
1047            DigitalAsset currentDigitalAsset = (DigitalAsset)iterator.next();
1048            if(digitalAsset == null || currentDigitalAsset.getDigitalAssetId().intValue() > digitalAsset.getDigitalAssetId().intValue())
1049                digitalAsset = currentDigitalAsset;
1050        }
1051        return digitalAsset;
1052    }
1053
1054    /**
1055     * Returns the latest digital asset for a contentversion.
1056     */

1057    
1058    private static DigitalAsset getLatestDigitalAsset(ContentVersion contentVersion, String JavaDoc assetKey)
1059    {
1060        Collection JavaDoc digitalAssets = contentVersion.getDigitalAssets();
1061        Iterator JavaDoc iterator = digitalAssets.iterator();
1062        
1063        DigitalAsset digitalAsset = null;
1064        while(iterator.hasNext())
1065        {
1066            DigitalAsset currentDigitalAsset = (DigitalAsset)iterator.next();
1067            if((digitalAsset == null || currentDigitalAsset.getDigitalAssetId().intValue() > digitalAsset.getDigitalAssetId().intValue()) && currentDigitalAsset.getAssetKey().equalsIgnoreCase(assetKey))
1068                digitalAsset = currentDigitalAsset;
1069        }
1070        return digitalAsset;
1071    }
1072
1073
1074    /**
1075     * This method checks if the given file exists on disk. If it does it's ignored because
1076     * that means that the file is allready cached on the server. If not we take out the stream from the
1077     * digitalAsset-object and dumps it.
1078     */

1079    
1080    public static void dumpDigitalAsset(DigitalAsset digitalAsset, String JavaDoc fileName, String JavaDoc filePath) throws Exception JavaDoc
1081    {
1082        long timer = System.currentTimeMillis();
1083
1084        File JavaDoc outputFile = new File JavaDoc(filePath + File.separator + fileName);
1085        if(outputFile.exists())
1086        {
1087            logger.info("The file allready exists so we don't need to dump it again..");
1088            return;
1089        }
1090        
1091        FileOutputStream JavaDoc fis = new FileOutputStream JavaDoc(outputFile);
1092        BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(fis);
1093        
1094        BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(digitalAsset.getAssetBlob());
1095        
1096        int character;
1097        while ((character = bis.read()) != -1)
1098        {
1099            bos.write(character);
1100        }
1101        bos.flush();
1102        
1103        bis.close();
1104        fis.close();
1105        bos.close();
1106        logger.info("Time for dumping file " + fileName + ":" + (System.currentTimeMillis() - timer));
1107    }
1108
1109    /**
1110     * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
1111     * is handling.
1112     */

1113
1114    public BaseEntityVO getNewVO()
1115    {
1116        return new DigitalAssetVO();
1117    }
1118
1119}
1120
1121class FilenameFilterImpl implements FilenameFilter JavaDoc
1122{
1123    private String JavaDoc filter = ".";
1124    
1125    public FilenameFilterImpl(String JavaDoc aFilter)
1126    {
1127        filter = aFilter;
1128    }
1129    
1130    public boolean accept(File JavaDoc dir, String JavaDoc name)
1131    {
1132        return name.startsWith(filter);
1133    }
1134};
1135
Popular Tags