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