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;
7 import org.collectionspace.services.common.api.CommonAPI;
8 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
9 import org.collectionspace.services.nuxeo.client.java.CoreSessionWrapper;
10 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
11 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
12 import org.nuxeo.ecm.core.api.Blob;
13 import org.nuxeo.ecm.core.api.DocumentModel;
14 import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
15 import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
16 //import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
17 import org.nuxeo.ecm.core.event.Event;
18 import org.nuxeo.ecm.core.event.EventContext;
19 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
20 //import org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants;
21 import org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants;
23 public class UpdateImageDerivatives extends AbstractCSEventListenerImpl {
25 // All Nuxeo sessions that get passed around to CollectionSpace code need to
26 // be wrapped inside of a CoreSessionWrapper. For example:
27 // CoreSessionInterface coreSession = new
28 // CoreSessionWrapper(docEventContext.getCoreSession());
30 // FIXME: We might experiment here with using log4j instead of Apache
31 // Commons Logging; am using the latter to follow Ray's pattern for now
32 private final static Log logger = LogFactory.getLog(UpdateImageDerivatives.class);
35 public void handleEvent(Event event) {
36 if (logger.isTraceEnabled()) {
37 logger.trace(String.format("Entering handleEvent in '%s'...", getClass().getName()));
40 if (shouldProcessEvent(event) == true) {
41 DocumentEventContext docEventContext = (DocumentEventContext) event.getContext();
42 DocumentModel docModel = docEventContext.getSourceDocument();
44 String eventType = event.getName();
45 if (logger.isTraceEnabled()) {
46 logger.trace(String.format("A(n) '%s' event was received by the %s event listener.",
47 eventType, getClass().getName()));
51 String source = (String)docModel.getProperty(CommonAPI.NUXEO_DUBLINCORE_SCHEMANAME,
52 CommonAPI.NUXEO_DUBLINCORE_SOURCE);
54 if (source != null && source.equalsIgnoreCase(CommonAPI.URL_SOURCED_PICTURE)) {
55 CoreSessionInterface nuxeoSession = new CoreSessionWrapper(docEventContext.getCoreSession());
56 purgeOriginalImage(docModel, nuxeoSession);
59 if (logger.isTraceEnabled()) {
60 logger.trace(String.format("The Nuxeo document titled '%s' did not need processing by the '%s' Nuxeo listener.",
61 docModel.getTitle(), getClass().getName()));
66 if (logger.isTraceEnabled()) {
67 logger.trace(String.format("Exiting handleEvent in '%s'.", getClass().getName()));
71 private void purgeOriginalImage(DocumentModel docModel, CoreSessionInterface nuxeoSession) {
73 // Empty the document model's "content" property -this does not delete the actual file/blob it
74 // just disassociates the blob content (aka, the original image) from the document.
76 docModel.setPropertyValue("file:content", (Serializable) null);
79 // Removing this facet ensures the original derivatives are unchanged when
80 // we call the save method. If we didn't remove the face, then all the
81 // image derivatives would be disassociated with the document. We want to keep
84 NuxeoUtils.removeFacet(docModel, ImagingDocumentConstants.PICTURE_FACET);
85 nuxeoSession.saveDocument(docModel); // persist the disassociation of the original blob/image
87 // Now that we've emptied the document model's content field, we can add back the Picture facet so
88 // Nuxeo will still tread this document as a Picture document.
90 NuxeoUtils.addFacet(docModel, ImagingDocumentConstants.PICTURE_FACET);
93 // Finally, we need to remove the actual blob/image bits that are store on disk.
95 DocumentBlobHolder docBlobHolder = (DocumentBlobHolder) docModel.getAdapter(BlobHolder.class);
96 Blob blob = docBlobHolder.getBlob();
98 logger.error(String.format("Could not get blob for original image. Trying to delete original for: '%s'",
99 docModel.getTitle()));
101 Thread thread = NuxeoUtils.deleteFileOfBlobAsync(blob);
102 logger.debug(String.format("Started thread '%s' to delete file of blob '%s'.",
103 thread.getId(), blob.getFilename()));
106 if (logger.isTraceEnabled()) {
107 logger.trace(String.format("Exiting handleEvent in '%s'.", getClass().getName()));
111 private boolean shouldProcessEvent(Event event) {
112 boolean result = false;
114 EventContext eventContext = event.getContext();
115 if (eventContext != null) {
116 if (isRegistered(event) && eventContext instanceof DocumentEventContext) {