]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c1531c162118c4dfee32ecd386119eef1aed57d4
[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
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;
13
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;
21
22 public class UpdateImageDerivatives extends AbstractCSEventSyncListenerImpl {
23
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);
28
29     @Override
30         public boolean shouldHandleEvent(Event event) {
31         return event.getContext() instanceof DocumentEventContext;
32     }
33     
34         @Override
35         public void handleCSEvent(Event event) {
36                 DocumentEventContext docEventContext = (DocumentEventContext) event.getContext();
37                 DocumentModel docModel = docEventContext.getSourceDocument();
38
39                 String source = (String)docModel.getProperty(CommonAPI.NUXEO_DUBLINCORE_SCHEMANAME,
40                                 CommonAPI.NUXEO_DUBLINCORE_SOURCE);
41
42                 if (source != null && source.equalsIgnoreCase(CommonAPI.URL_SOURCED_PICTURE)) {
43                         CoreSessionInterface nuxeoSession = new CoreSessionWrapper(docEventContext.getCoreSession());
44                         purgeOriginalImage(docModel, nuxeoSession);
45                         nuxeoSession.save();
46                 } else {
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()));
50                         }
51                 }       
52         }
53
54         private void purgeOriginalImage(DocumentModel docModel, CoreSessionInterface nuxeoSession) {
55                 //
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.
58                 //
59                 docModel.setPropertyValue("file:content", (Serializable) null);
60                 
61                 //
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
65                 // the derivatives.
66                 //
67                 NuxeoUtils.removeFacet(docModel, ImagingDocumentConstants.PICTURE_FACET);
68                 nuxeoSession.saveDocument(docModel); // persist the disassociation of the original blob/image
69                 //
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.
72                 //
73                 NuxeoUtils.addFacet(docModel, ImagingDocumentConstants.PICTURE_FACET);
74                 
75                 //
76                 // Finally, we need to remove the actual blob/image bits that are store on disk.
77                 //
78                 DocumentBlobHolder docBlobHolder = (DocumentBlobHolder) docModel.getAdapter(BlobHolder.class);
79                 Blob blob = docBlobHolder.getBlob();            
80                 if (blob == null) {
81                         logger.error(String.format("Could not get blob for original image. Trying to delete original for: '%s'",
82                                                         docModel.getTitle()));
83                 } else {
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()));
87                 }
88                 
89                 if (logger.isTraceEnabled()) {
90                         logger.trace(String.format("Exiting handleEvent in '%s'.", getClass().getName()));                      
91                 }       
92         }
93
94         @Override
95         public Log getLogger() {
96                 return logger;
97         }
98 }