001/*
002 * Copyright (c) 2006-2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 * Vladimir Pasquier <vpasquier@nuxeo.com>
011 * Laurent Doguin <ldoguin@nuxeo.com>
012 */
013package org.nuxeo.ecm.platform.thumbnail.listener;
014
015import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE;
016import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED;
017
018import org.nuxeo.ecm.core.api.DocumentModel;
019import org.nuxeo.ecm.core.api.model.Property;
020import org.nuxeo.ecm.core.event.Event;
021import org.nuxeo.ecm.core.event.EventContext;
022import org.nuxeo.ecm.core.event.EventListener;
023import org.nuxeo.ecm.core.event.EventService;
024import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
025import org.nuxeo.ecm.platform.thumbnail.ThumbnailConstants;
026import org.nuxeo.runtime.api.Framework;
027
028/**
029 * Thumbnail listener handling document blob update and checking changes. Fire an event if it's the case
030 *
031 * @since 5.7
032 */
033public class CheckBlobUpdateListener implements EventListener {
034
035    @Override
036    public void handleEvent(Event event) {
037        EventContext ec = event.getContext();
038        if (!(ec instanceof DocumentEventContext)) {
039            return;
040        }
041        DocumentEventContext context = (DocumentEventContext) ec;
042        DocumentModel doc = context.getSourceDocument();
043        if (!doc.hasSchema("file")) {
044            return;
045        }
046
047        Property content = doc.getProperty("file:content");
048        if (DOCUMENT_CREATED.equals(event.getName()) || content.isDirty()) {
049
050            if (BEFORE_DOC_UPDATE.equals(event.getName()) && doc.hasFacet(ThumbnailConstants.THUMBNAIL_FACET)) {
051                doc.setPropertyValue(ThumbnailConstants.THUMBNAIL_PROPERTY_NAME, null);
052            }
053
054            if (content.getValue() != null) {
055                doc.addFacet(ThumbnailConstants.THUMBNAIL_FACET);
056                Framework.getLocalService(EventService.class).fireEvent(
057                        ThumbnailConstants.EventNames.scheduleThumbnailUpdate.name(), context);
058            }
059        }
060    }
061
062}