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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
018 */
019
020package org.nuxeo.binary.metadata.internals.listeners;
021
022import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED;
023import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_UPDATED;
024
025import java.util.LinkedList;
026
027import org.nuxeo.binary.metadata.api.BinaryMetadataConstants;
028import org.nuxeo.binary.metadata.internals.BinaryMetadataWork;
029import org.nuxeo.binary.metadata.internals.MetadataMappingDescriptor;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.event.Event;
032import org.nuxeo.ecm.core.event.EventContext;
033import org.nuxeo.ecm.core.event.EventListener;
034import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
035import org.nuxeo.ecm.core.work.api.WorkManager;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * Handle document and blob updates according to {@link BinaryMetadataSyncListener} rules. If
040 * {@link org.nuxeo.binary.metadata.api.BinaryMetadataConstants#ASYNC_BINARY_METADATA_EXECUTE} flag is set into Document
041 * Event Context, a work should be executed.
042 *
043 * @since 7.2
044 */
045public class BinaryMetadataWorkListener implements EventListener {
046
047    @Override
048    public void handleEvent(Event event) {
049        EventContext ctx = event.getContext();
050        if (!(ctx instanceof DocumentEventContext)) {
051            return;
052        }
053        if (DOCUMENT_CREATED.equals(event.getName()) || DOCUMENT_UPDATED.equals(event.getName())) {
054            DocumentEventContext docCtx = (DocumentEventContext) ctx;
055            DocumentModel doc = docCtx.getSourceDocument();
056            Boolean execute = (Boolean) event.getContext().getProperty(
057                    BinaryMetadataConstants.ASYNC_BINARY_METADATA_EXECUTE);
058            LinkedList<MetadataMappingDescriptor> mappingDescriptors = (LinkedList<MetadataMappingDescriptor>) docCtx.getProperty(BinaryMetadataConstants.ASYNC_MAPPING_RESULT);
059            if (execute != null && execute && !doc.isProxy()) {
060                BinaryMetadataWork work = new BinaryMetadataWork(doc.getRepositoryName(), doc.getId(),
061                        mappingDescriptors, docCtx);
062                WorkManager workManager = Framework.getLocalService(WorkManager.class);
063                workManager.schedule(work, true);
064            }
065        }
066    }
067
068}