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