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.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.model.Property;
028import org.nuxeo.ecm.core.event.Event;
029import org.nuxeo.ecm.core.event.EventContext;
030import org.nuxeo.ecm.core.event.EventListener;
031import org.nuxeo.ecm.core.event.EventService;
032import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
033import org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Thumbnail listener handling document blob update and checking changes. Fire an event if it's the case
038 *
039 * @since 5.7
040 */
041public class CheckBlobUpdateListener implements EventListener {
042
043    @Override
044    public void handleEvent(Event event) {
045        EventContext ec = event.getContext();
046        if (!(ec instanceof DocumentEventContext)) {
047            return;
048        }
049        DocumentEventContext context = (DocumentEventContext) ec;
050        DocumentModel doc = context.getSourceDocument();
051        if (!doc.hasSchema("file")) {
052            return;
053        }
054
055        Property content = doc.getProperty("file:content");
056        // Only perform the thumbnail update at creation or modification if the content is marked as changed and the
057        // thumbnail has not already been updated. This additional check is needed to avoid an infinite loop.
058        if (DOCUMENT_CREATED.equals(event.getName())
059                || content.isDirty() && !Boolean.TRUE.equals(ec.getProperty(THUMBNAIL_UPDATED))) {
060
061            if (BEFORE_DOC_UPDATE.equals(event.getName()) && doc.hasFacet(ThumbnailConstants.THUMBNAIL_FACET)
062                    && content.getValue() == null) {
063                doc.setPropertyValue(ThumbnailConstants.THUMBNAIL_PROPERTY_NAME, null);
064            }
065
066            if (content.getValue() != null) {
067                doc.addFacet(ThumbnailConstants.THUMBNAIL_FACET);
068                Framework.getService(EventService.class)
069                         .fireEvent(ThumbnailConstants.EventNames.scheduleThumbnailUpdate.name(), context);
070            }
071        }
072    }
073
074}