]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e04a6361523f7654d11a5738b0f471540d71f5ce
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener;
2
3 import java.io.Serializable;
4
5 import org.collectionspace.services.common.api.CommonAPI;
6 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
7 import org.collectionspace.services.nuxeo.client.java.CoreSessionWrapper;
8 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
9 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
10 import org.nuxeo.ecm.core.api.Blob;
11 import org.nuxeo.ecm.core.api.DocumentModel;
12 import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
13 import org.nuxeo.ecm.core.api.blobholder.DocumentBlobHolder;
14 //import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
15 import org.nuxeo.ecm.core.event.Event;
16 import org.nuxeo.ecm.core.event.EventContext;
17 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
18 //import org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants;
19 import org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class UpdateImageDerivatives extends AbstractCSEventListenerImpl {
24
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());
29
30         private final static Logger logger = LoggerFactory.getLogger(UpdateImageDerivatives.class);
31
32         @Override
33         public void handleEvent(Event event) {
34                 if (logger.isTraceEnabled()) {
35                         logger.trace(String.format("Entering handleEvent in '%s'...", getClass().getName()));
36                 }
37
38                 if (shouldProcessEvent(event) == true) {
39                         DocumentEventContext docEventContext = (DocumentEventContext) event.getContext();
40                         DocumentModel docModel = docEventContext.getSourceDocument();
41
42                         String eventType = event.getName();
43                         if (logger.isTraceEnabled()) {
44                                 logger.trace(String.format("A(n) '%s' event was received by the %s event listener.",
45                                                                 eventType, getClass().getName()));
46                                 //logg
47                         }
48
49                         String source = (String)docModel.getProperty(CommonAPI.NUXEO_DUBLINCORE_SCHEMANAME,
50                                         CommonAPI.NUXEO_DUBLINCORE_SOURCE);
51
52                         if (source != null && source.equalsIgnoreCase(CommonAPI.URL_SOURCED_PICTURE)) {
53                                 CoreSessionInterface nuxeoSession = new CoreSessionWrapper(docEventContext.getCoreSession());
54                                 purgeOriginalImage(docModel, nuxeoSession);
55                                 nuxeoSession.save();
56                         } else {
57                                 if (logger.isTraceEnabled()) {
58                                         logger.trace(String.format("The Nuxeo document titled '%s' did not need processing by the '%s' Nuxeo listener.",
59                                                                         docModel.getTitle(), getClass().getName()));
60                                 }
61                         }
62                 }
63
64                 if (logger.isTraceEnabled()) {
65                         logger.trace(String.format("Exiting handleEvent in '%s'.", getClass().getName()));
66                 }
67         }
68
69         private void purgeOriginalImage(DocumentModel docModel, CoreSessionInterface nuxeoSession) {
70                 //
71                 // Empty the document model's "content" property -this does not delete the actual file/blob it
72                 // just disassociates the blob content (aka, the original image) from the document.
73                 //
74                 docModel.setPropertyValue("file:content", (Serializable) null);
75
76                 //
77                 // Removing this facet ensures the original derivatives are unchanged when
78                 // we call the save method.  If we didn't remove the face, then all the
79                 // image derivatives would be disassociated with the document.  We want to keep
80                 // the derivatives.
81                 //
82                 NuxeoUtils.removeFacet(docModel, ImagingDocumentConstants.PICTURE_FACET);
83                 nuxeoSession.saveDocument(docModel); // persist the disassociation of the original blob/image
84                 //
85                 // Now that we've emptied the document model's content field, we can add back the Picture facet so
86                 // Nuxeo will still tread this document as a Picture document.
87                 //
88                 NuxeoUtils.addFacet(docModel, ImagingDocumentConstants.PICTURE_FACET);
89
90                 //
91                 // Finally, we need to remove the actual blob/image bits that are store on disk.
92                 //
93                 DocumentBlobHolder docBlobHolder = (DocumentBlobHolder) docModel.getAdapter(BlobHolder.class);
94                 Blob blob = docBlobHolder.getBlob();
95                 if (blob == null) {
96                         logger.error(String.format("Could not get blob for original image. Trying to delete original for: '%s'",
97                                                         docModel.getTitle()));
98                 } else {
99                         Thread thread = NuxeoUtils.deleteFileOfBlobAsync(blob);
100                         logger.debug(String.format("Started thread '%s' to delete file of blob '%s'.",
101                                         thread.getId(), blob.getFilename()));
102                 }
103
104                 if (logger.isTraceEnabled()) {
105                         logger.trace(String.format("Exiting handleEvent in '%s'.", getClass().getName()));
106                 }
107         }
108
109         private boolean shouldProcessEvent(Event event) {
110                 boolean result = false;
111
112                 EventContext eventContext = event.getContext();
113                 if (eventContext != null) {
114                         if (isRegistered(event) && eventContext instanceof DocumentEventContext) {
115                                 result = true;
116                         }
117                 }
118
119                 return result;
120         }
121
122 }