1 package org.collectionspace.services.listener;
3 import java.io.Serializable;
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
8 import org.collectionspace.services.common.api.CommonAPI;
9 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
10 import org.collectionspace.services.nuxeo.client.java.CoreSessionWrapper;
11 import org.collectionspace.services.nuxeo.listener.AbstractCSEventSyncListenerImpl;
12 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
14 import org.nuxeo.ecm.core.api.Blob;
15 import org.nuxeo.ecm.core.api.DocumentModel;
16 import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
17 import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
18 import org.nuxeo.ecm.core.event.Event;
19 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
20 import org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants;
22 public class UpdateImageDerivatives extends AbstractCSEventSyncListenerImpl {
24 // All Nuxeo sessions that get passed around to CollectionSpace code need to
25 // be wrapped inside of a CoreSessionWrapper. For example:
26 // CoreSessionInterface coreSession = new CoreSessionWrapper(docEventContext.getCoreSession());
27 private static final Log logger = LogFactory.getLog(UpdateImageDerivatives.class);
30 public boolean shouldHandleEvent(Event event) {
31 return event.getContext() instanceof DocumentEventContext;
35 public void handleCSEvent(Event event) {
36 DocumentEventContext docEventContext = (DocumentEventContext) event.getContext();
37 DocumentModel docModel = docEventContext.getSourceDocument();
39 String source = (String)docModel.getProperty(CommonAPI.NUXEO_DUBLINCORE_SCHEMANAME,
40 CommonAPI.NUXEO_DUBLINCORE_SOURCE);
42 if (source != null && source.equalsIgnoreCase(CommonAPI.URL_SOURCED_PICTURE)) {
43 CoreSessionInterface nuxeoSession = new CoreSessionWrapper(docEventContext.getCoreSession());
44 purgeOriginalImage(docModel, nuxeoSession);
47 if (logger.isTraceEnabled()) {
48 logger.trace(String.format("The Nuxeo document titled '%s' did not need processing by the '%s' Nuxeo listener.",
49 docModel.getTitle(), getClass().getName()));
54 private void purgeOriginalImage(DocumentModel docModel, CoreSessionInterface nuxeoSession) {
56 // Empty the document model's "content" property -this does not delete the actual file/blob it
57 // just disassociates the blob content (aka, the original image) from the document.
59 docModel.setPropertyValue("file:content", (Serializable) null);
62 // Removing this facet ensures the original derivatives are unchanged when
63 // we call the save method. If we didn't remove the face, then all the
64 // image derivatives would be disassociated with the document. We want to keep
67 NuxeoUtils.removeFacet(docModel, ImagingDocumentConstants.PICTURE_FACET);
68 nuxeoSession.saveDocument(docModel); // persist the disassociation of the original blob/image
70 // Now that we've emptied the document model's content field, we can add back the Picture facet so
71 // Nuxeo will still tread this document as a Picture document.
73 NuxeoUtils.addFacet(docModel, ImagingDocumentConstants.PICTURE_FACET);
76 // Finally, we need to remove the actual blob/image bits that are store on disk.
78 DocumentBlobHolder docBlobHolder = (DocumentBlobHolder) docModel.getAdapter(BlobHolder.class);
79 Blob blob = docBlobHolder.getBlob();
81 logger.error(String.format("Could not get blob for original image. Trying to delete original for: '%s'",
82 docModel.getTitle()));
84 Thread thread = NuxeoUtils.deleteFileOfBlobAsync(blob);
85 logger.debug(String.format("Started thread '%s' to delete file of blob '%s'.",
86 thread.getId(), blob.getFilename()));
89 if (logger.isTraceEnabled()) {
90 logger.trace(String.format("Exiting handleEvent in '%s'.", getClass().getName()));
95 public Log getLogger() {