001/*
002 * (C) Copyright 2015 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 GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger
016 */
017
018package org.nuxeo.ecm.platform.picture.listener;
019
020import static org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants.PICTURE_FACET;
021
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.event.Event;
024import org.nuxeo.ecm.core.event.EventContext;
025import org.nuxeo.ecm.core.event.EventListener;
026import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
027import org.nuxeo.ecm.core.work.api.WorkManager;
028import org.nuxeo.ecm.platform.picture.PictureViewsGenerationWork;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Listener updating pre-filling the views of a Picture if the main Blob has changed.
033 *
034 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
035 * @since 5.5
036 */
037public class PictureViewsGenerationListener implements EventListener {
038
039    public static final String DISABLE_PICTURE_VIEWS_GENERATION_LISTENER = "disablePictureViewsGenerationListener";
040
041    @Override
042    public void handleEvent(Event event) {
043        EventContext ctx = event.getContext();
044        if (!(ctx instanceof DocumentEventContext)) {
045            return;
046        }
047
048        Boolean block = (Boolean) event.getContext().getProperty(DISABLE_PICTURE_VIEWS_GENERATION_LISTENER);
049        if (Boolean.TRUE.equals(block)) {
050            // ignore the event - we are blocked by the caller
051            return;
052        }
053
054        DocumentEventContext docCtx = (DocumentEventContext) ctx;
055        DocumentModel doc = docCtx.getSourceDocument();
056        if (doc.hasFacet(PICTURE_FACET) && !doc.isProxy()) {
057            PictureViewsGenerationWork work = new PictureViewsGenerationWork(doc.getRepositoryName(), doc.getId(),
058                    "file:content");
059            WorkManager workManager = Framework.getLocalService(WorkManager.class);
060            workManager.schedule(work, WorkManager.Scheduling.IF_NOT_SCHEDULED, true);
061        }
062    }
063
064}