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 */
020package org.nuxeo.ecm.platform.thumbnail.listener;
021
022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE;
023import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED;
024import static org.nuxeo.ecm.platform.thumbnail.listener.UpdateThumbnailListener.THUMBNAIL_UPDATED;
025
026import org.apache.logging.log4j.LogManager;
027import org.apache.logging.log4j.Logger;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.model.Property;
030import org.nuxeo.ecm.core.event.Event;
031import org.nuxeo.ecm.core.event.EventContext;
032import org.nuxeo.ecm.core.event.EventListener;
033import org.nuxeo.ecm.core.event.EventService;
034import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
035import org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Thumbnail listener handling document blob update, checking for changes, for documents holding default the schema
040 * named "file".
041 * <P>
042 * Fires an event if it's the case, caught by asynchronous listener at {@link UpdateThumbnailListener}.
043 *
044 * @since 5.7
045 */
046public class CheckBlobUpdateListener implements EventListener {
047
048    private static final Logger log = LogManager.getLogger(CheckBlobUpdateListener.class);
049
050    @Override
051    public void handleEvent(Event event) {
052        EventContext ec = event.getContext();
053        if (!(ec instanceof DocumentEventContext)) {
054            return;
055        }
056        DocumentEventContext context = (DocumentEventContext) ec;
057        DocumentModel doc = context.getSourceDocument();
058        if (!doc.hasSchema("file")) {
059            return;
060        }
061
062        Property content = doc.getProperty("file:content");
063        // Only perform the thumbnail update at creation or modification if the content is marked as changed and the
064        // thumbnail has not already been updated. This additional check is needed to avoid an infinite loop.
065        if (DOCUMENT_CREATED.equals(event.getName())
066                || content.isDirty() && !Boolean.TRUE.equals(ec.getProperty(THUMBNAIL_UPDATED))) {
067
068            if (BEFORE_DOC_UPDATE.equals(event.getName()) && doc.hasFacet(ThumbnailConstants.THUMBNAIL_FACET)
069                    && content.getValue() == null) {
070                doc.setPropertyValue(ThumbnailConstants.THUMBNAIL_PROPERTY_NAME, null);
071            }
072
073            if (content.getValue() != null) {
074                doc.addFacet(ThumbnailConstants.THUMBNAIL_FACET);
075
076                // only skip sending the event: facet addition is needed for later recomputation
077                if (Boolean.TRUE.equals(context.getProperty(ThumbnailConstants.DISABLE_THUMBNAIL_COMPUTATION))) {
078                    log.trace("Thumbnail computation is disabled for document {}", doc::getId);
079                    return;
080                }
081                Framework.getService(EventService.class)
082                         .fireEvent(ThumbnailConstants.EventNames.scheduleThumbnailUpdate.name(), context);
083            }
084        }
085    }
086
087}