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.AttributesExtractorStater.DOC_TYPES;
023
024import org.nuxeo.apidoc.worker.ExtractXmlAttributesWorker;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.event.Event;
027import org.nuxeo.ecm.core.event.EventListener;
028import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
029import org.nuxeo.ecm.core.work.api.Work;
030import org.nuxeo.ecm.core.work.api.WorkManager;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * Listener that trigger a Worker to extract XML Attributes and store them in a
035 * property that will be indexed.
036 *
037 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
038 * @since 8.3
039 */
040public class AttributesExtractorScheduler implements EventListener {
041
042    public static final String EXTRACT_XML_ATTRIBUTES_NEEDED = "extractXmlAttributesNeeded";
043
044    @Override
045    public void handleEvent(Event event) {
046        if (!(event.getContext() instanceof DocumentEventContext)) {
047            return;
048        }
049
050        DocumentEventContext ctx = (DocumentEventContext) event.getContext();
051        DocumentModel doc = ctx.getSourceDocument();
052        if (!DOC_TYPES.contains(doc.getType())) {
053            return;
054        }
055
056        Boolean flag = (Boolean) ctx.getProperty(EXTRACT_XML_ATTRIBUTES_NEEDED);
057        if (!Boolean.TRUE.equals(flag)) {
058            return;
059        }
060
061        Work worker = new ExtractXmlAttributesWorker(ctx.getRepositoryName(), ctx.getPrincipal().getName(), doc.getId());
062        Framework.getService(WorkManager.class).schedule(worker, true);
063    }
064}