001/*
002 * (C) Copyright 2016 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 *     Nuxeo
018 */
019
020package org.nuxeo.apidoc.listener;
021
022import static org.nuxeo.apidoc.listener.AttributesExtractorScheduler.EXTRACT_XML_ATTRIBUTES_NEEDED;
023import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.ABOUT_TO_CREATE;
024
025import java.util.Arrays;
026import java.util.List;
027
028import org.nuxeo.apidoc.api.ExtensionInfo;
029import org.nuxeo.apidoc.api.ExtensionPointInfo;
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.model.Property;
033import org.nuxeo.ecm.core.event.Event;
034import org.nuxeo.ecm.core.event.EventListener;
035import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
036
037/**
038 * Listener triggered on "aboutTo*" events to let the other lister
039 * org.nuxeo.apidoc.listener.AttributeExtractorWorkerListener to trigger Worker
040 * when blob are ready to be extracted.
041 *
042 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
043 * @since 8.3
044 */
045public class AttributesExtractorStater implements EventListener {
046
047    public static final String ATTRIBUTES_PROPERTY = "adc:attributes";
048
049    public static final List<String> DOC_TYPES = Arrays.asList(ExtensionPointInfo.TYPE_NAME, ExtensionInfo.TYPE_NAME);
050
051    @Override
052    public void handleEvent(Event event) {
053        if (!(event.getContext() instanceof DocumentEventContext)) {
054            return;
055        }
056
057        DocumentEventContext ctx = (DocumentEventContext) event.getContext();
058        DocumentModel doc = ctx.getSourceDocument();
059        if (!DOC_TYPES.contains(doc.getType())) {
060            return;
061        }
062
063        Property fileProperty = doc.getProperty("file:content");
064        // Handling "migration case", when a blob is present but without any
065        // attributes.
066        boolean force = fileProperty.getValue() != null && doc.getPropertyValue(ATTRIBUTES_PROPERTY) == null;
067        if (!(force || fileProperty.isDirty() || ABOUT_TO_CREATE.equals(event.getName()))) {
068            return;
069        }
070
071        Blob blob = (Blob) fileProperty.getValue();
072        if (blob == null) {
073            doc.setPropertyValue(ATTRIBUTES_PROPERTY, null);
074        } else {
075            // Property will be read by
076            // org.nuxeo.apidoc.listener.AttributeExtractorWorkerListener to
077            // trigger worker when everything is good.
078            // Worker cannot be triggered on "aboutTo*" events, and dirty props
079            // cannot be checked post "aboutTo*" events
080            ctx.setProperty(EXTRACT_XML_ATTRIBUTES_NEEDED, true);
081        }
082    }
083}