001/*
002 * (C) Copyright 2015-2019 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;
023import static org.nuxeo.ecm.platform.picture.recompute.RecomputeViewsAction.ACTION_NAME;
024import static org.nuxeo.ecm.platform.picture.recompute.RecomputeViewsAction.PARAM_XPATH;
025
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.bulk.BulkService;
028import org.nuxeo.ecm.core.bulk.message.BulkCommand;
029import org.nuxeo.ecm.core.event.Event;
030import org.nuxeo.ecm.core.event.EventBundle;
031import org.nuxeo.ecm.core.event.EventContext;
032import org.nuxeo.ecm.core.event.PostCommitEventListener;
033import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * Listener updating pre-filling the views of a Picture if the main Blob has changed.
038 *
039 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
040 * @since 5.5
041 */
042public class PictureViewsGenerationListener implements PostCommitEventListener {
043
044    public static final String DISABLE_PICTURE_VIEWS_GENERATION_LISTENER = "disablePictureViewsGenerationListener";
045
046    @Override
047    public void handleEvent(EventBundle events) {
048        events.forEach(this::handleEvent);
049    }
050
051    protected void handleEvent(Event event) {
052        EventContext ctx = event.getContext();
053        if (!(ctx instanceof DocumentEventContext)) {
054            return;
055        }
056
057        Boolean block = (Boolean) event.getContext().getProperty(DISABLE_PICTURE_VIEWS_GENERATION_LISTENER);
058        if (Boolean.TRUE.equals(block)) {
059            // ignore the event - we are blocked by the caller
060            return;
061        }
062
063        DocumentEventContext docCtx = (DocumentEventContext) ctx;
064        DocumentModel doc = docCtx.getSourceDocument();
065        if (doc.hasFacet(PICTURE_FACET) && !doc.isProxy()) {
066            String query = "SELECT * FROM Document WHERE ecm:uuid='" + doc.getId() + "'";
067            BulkService service = Framework.getService(BulkService.class);
068            String username = ctx.getPrincipal().getName();
069            service.submit(
070                    new BulkCommand.Builder(ACTION_NAME, query, username).param(PARAM_XPATH, "file:content").build());
071        }
072    }
073
074}