1 package org.collectionspace.services.nuxeo.extension.thumbnail;
4 * An example Nuxeo event "listener".
7 import java.io.Serializable;
8 import java.util.Calendar;
9 import java.util.GregorianCalendar;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.nuxeo.ecm.core.api.Blob;
14 import org.nuxeo.ecm.core.api.ClientException;
15 import org.nuxeo.ecm.core.api.CoreSession;
16 import org.nuxeo.ecm.core.api.DocumentModel;
17 import org.nuxeo.ecm.core.api.UnrestrictedSessionRunner;
18 import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
19 import org.nuxeo.ecm.core.convert.api.ConversionService;
20 import org.nuxeo.ecm.platform.filemanager.api.FileManager;
21 import org.nuxeo.runtime.api.Framework;
23 public class AddThumbnailUnrestricted extends UnrestrictedSessionRunner {
25 private static final Log logger = LogFactory
26 .getLog(AddThumbnailUnrestricted.class);
28 protected ConversionService cs;
30 protected DocumentModel doc;
32 protected BlobHolder blobHolder;
34 protected Thumbnail thumbnail = null;
36 public AddThumbnailUnrestricted(CoreSession coreSession, DocumentModel doc,
37 BlobHolder blobHolder) {
40 this.blobHolder = blobHolder;
45 * @see org.nuxeo.ecm.core.api.UnrestrictedSessionRunner#run()
47 * Creates a new thumbnail image and associates it with the document blob by adding a "Thumbnail" facet
48 * to the document blob.
51 public void run() throws ClientException {
52 String errMsg = "Error while adding preview thumbnail.";
53 String documentId = doc.getId();
56 Blob blob = blobHolder.getBlob();
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
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);
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.
80 doc = session.saveDocument(doc);
82 logger.warn("Could not create a preview thumbnail image for Nuxeo blob document: " + doc.getId());
86 logger.warn(errMsg + " " + "The Nuxeo blob holder had an empty blob object. Document ID:" + doc.getId());
88 } catch (Exception e) {
89 logger.warn(errMsg, e);
93 private String computeDigest(FileManager fileManager, Blob blob) throws Exception {
97 // result = fileManager.computeDigest(blob); // REM - Warning: Why is this operation so slow?
98 result = blob.getDigest();
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;
111 // We *need* a 'modified' date, so let's use the current date
112 modificationDate = new GregorianCalendar();
114 doc.setProperty("dublincore", "modified", modificationDate);
117 return modificationDate.toString();
120 public Thumbnail getAdapter() {