]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2d2164e126c84aafa6dbe58bde7429287bdeafad
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener;
2
3 import java.io.Serializable;
4
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;
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         // 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);
33
34         @Override
35         public void handleEvent(Event event) {
36                 if (logger.isTraceEnabled()) {
37                         logger.trace(String.format("Entering handleEvent in '%s'...", getClass().getName()));
38                 }
39
40                 if (shouldProcessEvent(event) == true) {
41                         DocumentEventContext docEventContext = (DocumentEventContext) event.getContext();
42                         DocumentModel docModel = docEventContext.getSourceDocument();
43
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()));
48                                 //logg
49                         }
50
51                         String source = (String)docModel.getProperty(CommonAPI.NUXEO_DUBLINCORE_SCHEMANAME,
52                                         CommonAPI.NUXEO_DUBLINCORE_SOURCE);
53
54                         if (source != null && source.equalsIgnoreCase(CommonAPI.URL_SOURCED_PICTURE)) {
55                                 CoreSessionInterface nuxeoSession = new CoreSessionWrapper(docEventContext.getCoreSession());
56                                 purgeOriginalImage(docModel, nuxeoSession);
57                                 nuxeoSession.save();
58                         } else {
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()));
62                                 }
63                         }
64                 }
65
66                 if (logger.isTraceEnabled()) {
67                         logger.trace(String.format("Exiting handleEvent in '%s'.", getClass().getName()));
68                 }
69         }
70
71         private void purgeOriginalImage(DocumentModel docModel, CoreSessionInterface nuxeoSession) {
72                 //
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.
75                 //
76                 docModel.setPropertyValue("file:content", (Serializable) null);
77                 
78                 //
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
82                 // the derivatives.
83                 //
84                 NuxeoUtils.removeFacet(docModel, ImagingDocumentConstants.PICTURE_FACET);
85                 nuxeoSession.saveDocument(docModel); // persist the disassociation of the original blob/image
86                 //
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.
89                 //
90                 NuxeoUtils.addFacet(docModel, ImagingDocumentConstants.PICTURE_FACET);
91                 
92                 //
93                 // Finally, we need to remove the actual blob/image bits that are store on disk.
94                 //
95                 DocumentBlobHolder docBlobHolder = (DocumentBlobHolder) docModel.getAdapter(BlobHolder.class);
96                 Blob blob = docBlobHolder.getBlob();            
97                 if (blob == null) {
98                         logger.error(String.format("Could not get blob for original image. Trying to delete original for: '%s'",
99                                                         docModel.getTitle()));
100                 } else {
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()));
104                 }
105                 
106                 if (logger.isTraceEnabled()) {
107                         logger.trace(String.format("Exiting handleEvent in '%s'.", getClass().getName()));                      
108                 }       
109         }
110         
111         private boolean shouldProcessEvent(Event event) {
112                 boolean result = false;
113
114                 EventContext eventContext = event.getContext();
115                 if (eventContext != null) {
116                         if (isRegistered(event) && eventContext instanceof DocumentEventContext) {
117                                 result = true;
118                         }
119                 }
120
121                 return result;
122         }
123
124 }