001/*
002 * (C) Copyright 2015 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 *     Thomas Roger
018 */
019
020package org.nuxeo.ecm.platform.picture.listener;
021
022import static org.nuxeo.ecm.platform.picture.api.ImagingDocumentConstants.PICTURE_FACET;
023
024import org.nuxeo.ecm.core.api.DocumentModel;
025import org.nuxeo.ecm.core.event.Event;
026import org.nuxeo.ecm.core.event.EventContext;
027import org.nuxeo.ecm.core.event.EventListener;
028import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
029import org.nuxeo.ecm.core.work.api.WorkManager;
030import org.nuxeo.ecm.platform.picture.PictureViewsGenerationWork;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * Listener updating pre-filling the views of a Picture if the main Blob has changed.
035 *
036 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
037 * @since 5.5
038 */
039public class PictureViewsGenerationListener implements EventListener {
040
041    public static final String DISABLE_PICTURE_VIEWS_GENERATION_LISTENER = "disablePictureViewsGenerationListener";
042
043    @Override
044    public void handleEvent(Event event) {
045        EventContext ctx = event.getContext();
046        if (!(ctx instanceof DocumentEventContext)) {
047            return;
048        }
049
050        Boolean block = (Boolean) event.getContext().getProperty(DISABLE_PICTURE_VIEWS_GENERATION_LISTENER);
051        if (Boolean.TRUE.equals(block)) {
052            // ignore the event - we are blocked by the caller
053            return;
054        }
055
056        DocumentEventContext docCtx = (DocumentEventContext) ctx;
057        DocumentModel doc = docCtx.getSourceDocument();
058        if (doc.hasFacet(PICTURE_FACET) && !doc.isProxy()) {
059            PictureViewsGenerationWork work = new PictureViewsGenerationWork(doc.getRepositoryName(), doc.getId(),
060                    "file:content");
061            WorkManager workManager = Framework.getLocalService(WorkManager.class);
062            workManager.schedule(work, WorkManager.Scheduling.IF_NOT_SCHEDULED, true);
063        }
064    }
065
066}