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