]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c21cd20946b6fdb129ae7800eabbcfc0bd939abc
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.nuxeo.extension.thumbnail;
2
3
4 import java.io.IOException;
5 import java.io.Serializable;
6 import java.security.NoSuchAlgorithmException;
7 import java.util.Calendar;
8 import java.util.GregorianCalendar;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.nuxeo.ecm.core.api.Blob;
13 import org.nuxeo.ecm.core.api.ClientException;
14 import org.nuxeo.ecm.core.api.CoreSession;
15 import org.nuxeo.ecm.core.api.DocumentModel;
16 import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
17 import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
18 import org.nuxeo.ecm.core.convert.api.ConversionService;
19 import org.nuxeo.ecm.platform.filemanager.api.FileManager;
20 import org.nuxeo.runtime.api.Framework;
21
22 public class AddThumbnailUnrestricted extends UnrestrictedSessionRunner {
23  
24     private static final Log logger = LogFactory
25             .getLog(AddThumbnailUnrestricted.class);
26  
27     protected ConversionService cs;
28  
29     protected DocumentModel doc;
30  
31     protected BlobHolder blobHolder;
32  
33     protected Thumbnail thumbnail = null;
34  
35     public AddThumbnailUnrestricted(CoreSession coreSession, DocumentModel doc,
36             BlobHolder blobHolder) {
37         super(coreSession);
38         this.doc = doc;
39         this.blobHolder = blobHolder;
40     }
41  
42     /*
43      * (non-Javadoc)
44      * @see org.nuxeo.ecm.core.api.UnrestrictedSessionRunner#run()
45      * 
46      * Creates a new thumbnail image and associates it with the document blob by adding a "Thumnail" facet
47      * to the document blob.
48      */
49     @Override
50     public void run() throws ClientException {
51         String errMsg = "Error while adding preview thumbnail.";
52         String documentId = doc.getId();
53         
54         try {
55             Blob blob = blobHolder.getBlob();
56             if (blob != null) {
57                 if (doc.hasFacet(ThumbnailConstants.THUMBNAIL_FACET) == false) { // Make sure we don't already have a "Thumbnail" facet
58                         cs = Framework.getService(ConversionService.class);                
59                         ensureModificationDateExists(doc); // For some reason, the ConversionService service requires the modification date of the blob is not null so we need to ensure it is not null. 
60                         BlobHolder thumbnailBlobHolder = cs.convert(ThumbnailConstants.THUMBNAIL_CONVERTER_NAME,
61                                 blobHolder, null /*no params*/);
62                         if (thumbnailBlobHolder != null && thumbnailBlobHolder.getBlob() != null) {
63                             Blob thumbnailBlob = thumbnailBlobHolder.getBlob();
64                                 doc.addFacet(ThumbnailConstants.THUMBNAIL_FACET); // Add the "Thumbnail" facet since we were able to create a thumnail image
65                                 // Give the thumbnail blob a name.
66                             String thumbnailName = documentId + ThumbnailConstants.THUMBNAIL_PROPERTY_NAME;
67                             thumbnailBlobHolder.getBlob().setFilename(thumbnailName); // Give it a name so we can manually search for it in the "nuxeo" database
68                             
69                             doc.setProperty(ThumbnailConstants.THUMBNAIL_SCHEMA_NAME,
70                                     ThumbnailConstants.THUMBNAIL_FILENAME_PROPERTY_NAME,
71                                     (Serializable) thumbnailName);
72                             doc.setProperty(ThumbnailConstants.THUMBNAIL_SCHEMA_NAME,
73                                     ThumbnailConstants.THUMBNAIL_PROPERTY_NAME,
74                                     (Serializable) thumbnailBlob);
75                             //
76                             // Save the new Thumnail facet data (including the new thumbnail image).  The save triggers a new create event and recurses us back to
77                             // this method, but the next time we'll have a Thumbnail facet and bypass this save -sparing us from an infinite event loop.
78                             //
79                             doc = session.saveDocument(doc); 
80                         } else {
81                                 logger.warn("Could not create a preview thumbnail image for Nuxeo blob document: " + doc.getId());
82                         }
83                 }
84             } else {
85                 logger.warn(errMsg + " " + "The Nuxeo blob holder had an empty blob object.  Document ID:" + doc.getId());
86             }
87         } catch (Exception e) {
88             logger.warn(errMsg, e);
89         }
90     }
91  
92     private String computeDigest(FileManager fileManager, Blob blob) throws Exception {
93         String result = null;
94         
95         // Compute the digest
96         result = fileManager.computeDigest(blob); // REM - Warning: Why is this operation so slow?
97         
98         return result;
99     }
100     
101     private String ensureModificationDateExists(DocumentModel docModel) throws Exception {
102         Calendar modificationDate = (Calendar)doc.getProperty("dublincore", "modified");
103         if (modificationDate == null) {
104                 // If the 'modified' field is null then try the 'created' field
105                 Calendar creationDate = (Calendar)doc.getProperty("dublincore", "created");
106                 if (creationDate != null) {
107                         modificationDate = creationDate;
108                 } else {
109                         // We *need* a 'modified' date, so let's use the current date
110                         modificationDate = new GregorianCalendar();
111                 }
112                 doc.setProperty("dublincore", "modified", modificationDate);
113         }
114         
115         return modificationDate.toString();
116     }
117     
118     public Thumbnail getAdapter() {
119         return thumbnail;
120     }
121  
122 }