001/*
002 * (C) Copyright 2006-2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 * Vladimir Pasquier <vpasquier@nuxeo.com>
018 * Laurent Doguin <ldoguin@nuxeo.com>
019 * Nelson Silva <nsilva@nuxeo.com>
020 */
021package org.nuxeo.ecm.platform.thumbnail.listener;
022
023import static org.nuxeo.ecm.core.api.CoreSession.ALLOW_VERSION_WRITE;
024
025import java.io.IOException;
026import java.io.InputStream;
027import java.io.Serializable;
028import java.util.HashSet;
029import java.util.Set;
030
031import org.nuxeo.ecm.core.api.Blob;
032import org.nuxeo.ecm.core.api.Blobs;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.NuxeoException;
036import org.nuxeo.ecm.core.api.VersioningOption;
037import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
038import org.nuxeo.ecm.core.api.thumbnail.ThumbnailAdapter;
039import org.nuxeo.ecm.core.blob.BlobManager;
040import org.nuxeo.ecm.core.event.DeletedDocumentModel;
041import org.nuxeo.ecm.core.event.Event;
042import org.nuxeo.ecm.core.event.EventBundle;
043import org.nuxeo.ecm.core.event.PostCommitEventListener;
044import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
045import org.nuxeo.ecm.core.versioning.VersioningService;
046import org.nuxeo.ecm.platform.dublincore.listener.DublinCoreListener;
047import org.nuxeo.ecm.platform.ec.notification.NotificationConstants;
048import org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants;
049import org.nuxeo.runtime.api.Framework;
050
051/**
052 * Thumbnail listener handling creation and update document event to store doc thumbnail preview (only for DocType File)
053 *
054 * @since 5.7
055 */
056public class UpdateThumbnailListener implements PostCommitEventListener {
057
058    public static final String THUMBNAIL_UPDATED = "thumbnailUpdated";
059
060    protected void processDoc(CoreSession session, DocumentModel doc) {
061        Blob thumbnailBlob = getManagedThumbnail(doc);
062        if (thumbnailBlob == null) {
063            ThumbnailAdapter thumbnailAdapter = doc.getAdapter(ThumbnailAdapter.class);
064            if (thumbnailAdapter == null) {
065                return;
066            }
067            thumbnailBlob = thumbnailAdapter.computeThumbnail(session);
068        }
069        if (thumbnailBlob != null) {
070            if (!doc.hasFacet(ThumbnailConstants.THUMBNAIL_FACET)) {
071                doc.addFacet(ThumbnailConstants.THUMBNAIL_FACET);
072            }
073            doc.setPropertyValue(ThumbnailConstants.THUMBNAIL_PROPERTY_NAME, (Serializable) thumbnailBlob);
074        } else {
075            if (doc.hasFacet(ThumbnailConstants.THUMBNAIL_FACET)) {
076                doc.setPropertyValue(ThumbnailConstants.THUMBNAIL_PROPERTY_NAME, null);
077                doc.removeFacet(ThumbnailConstants.THUMBNAIL_FACET);
078            }
079        }
080        if (doc.isDirty()) {
081            doc.putContextData(VersioningService.VERSIONING_OPTION, VersioningOption.NONE);
082            doc.putContextData(VersioningService.DISABLE_AUTO_CHECKOUT, Boolean.TRUE);
083            doc.putContextData(DublinCoreListener.DISABLE_DUBLINCORE_LISTENER, Boolean.TRUE);
084            doc.putContextData(NotificationConstants.DISABLE_NOTIFICATION_SERVICE, Boolean.TRUE);
085            doc.putContextData("disableAuditLogger", Boolean.TRUE);
086            if (doc.isVersion()) {
087                doc.putContextData(ALLOW_VERSION_WRITE, Boolean.TRUE);
088            }
089            doc.putContextData(THUMBNAIL_UPDATED, true);
090            session.saveDocument(doc);
091        }
092    }
093
094    private Blob getManagedThumbnail(DocumentModel doc) {
095        BlobHolder bh = doc.getAdapter(BlobHolder.class);
096        if (bh == null) {
097            return null;
098        }
099        Blob blob = bh.getBlob();
100        if (blob == null) {
101            return null;
102        }
103        BlobManager blobManager = Framework.getService(BlobManager.class);
104        try {
105            InputStream is = blobManager.getThumbnail(blob);
106            if (is == null) {
107                return null;
108            }
109            return Blobs.createBlob(is);
110        } catch (IOException e) {
111            throw new NuxeoException("Failed to get managed blob thumbnail", e);
112        }
113    }
114
115    @Override
116    public void handleEvent(EventBundle events) {
117        if (!events.containsEventName(ThumbnailConstants.EventNames.scheduleThumbnailUpdate.name())) {
118            return;
119        }
120        Set<String> processedDocs = new HashSet<String>();
121        for (Event event : events) {
122            if (!ThumbnailConstants.EventNames.scheduleThumbnailUpdate.name().equals(event.getName())) {
123                continue;
124            }
125            DocumentEventContext context = (DocumentEventContext) event.getContext();
126            DocumentModel doc = context.getSourceDocument();
127            if (doc instanceof DeletedDocumentModel) {
128                continue;
129            }
130            if (doc.isProxy()) {
131                continue;
132            }
133            if (processedDocs.contains(doc.getId())) {
134                continue;
135            }
136            CoreSession repo = context.getCoreSession();
137            processDoc(repo, doc);
138            processedDocs.add(doc.getId());
139        }
140    }
141}