001/*
002 * (C) Copyright 2006-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 *     Tiago Cardoso <tcardoso@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.threed.listener;
020
021import org.nuxeo.ecm.core.api.DocumentModel;
022import org.nuxeo.ecm.core.event.Event;
023import org.nuxeo.ecm.core.event.EventContext;
024import org.nuxeo.ecm.core.event.EventListener;
025import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
026import org.nuxeo.ecm.core.work.api.Work;
027import org.nuxeo.ecm.core.work.api.WorkManager;
028import org.nuxeo.ecm.platform.threed.service.ThreeDBatchUpdateWork;
029import org.nuxeo.runtime.api.Framework;
030
031import static org.nuxeo.ecm.platform.threed.ThreeDConstants.THREED_FACET;
032import static org.nuxeo.ecm.platform.threed.listener.ThreeDBatchCleanerListener.GENERATE_BATCH_DATA;
033
034/**
035 * Listener batch updating transmission formats and renders if the main Blob has changed.
036 *
037 * @since 8.4
038 */
039public class ThreeDBatchGenerationListener implements EventListener {
040
041    @Override
042    public void handleEvent(Event event) {
043        EventContext ctx = event.getContext();
044        if (!(ctx instanceof DocumentEventContext)) {
045            return;
046        }
047        Boolean generate = (Boolean) event.getContext().getProperty(GENERATE_BATCH_DATA);
048        if (!Boolean.TRUE.equals(generate)) {
049            // ignore the event - we are blocked by the caller
050            return;
051        }
052
053        DocumentEventContext docCtx = (DocumentEventContext) ctx;
054        DocumentModel doc = docCtx.getSourceDocument();
055        if (doc.hasFacet(THREED_FACET) && !doc.isProxy()) {
056            ThreeDBatchUpdateWork work = new ThreeDBatchUpdateWork(doc.getRepositoryName(), doc.getId());
057            WorkManager manager = Framework.getService(WorkManager.class);
058
059            ThreeDBatchUpdateWork running = (ThreeDBatchUpdateWork) manager.find(work.getId(), Work.State.RUNNING);
060            ThreeDBatchUpdateWork scheduled = (ThreeDBatchUpdateWork) manager.find(work.getId(), Work.State.SCHEDULED);
061            if (running != null) {
062                running.suspended();
063                running.setStatus("Suspended");
064            } else if (scheduled != null) {
065                scheduled.suspended();
066                scheduled.setStatus("Suspended");
067            }
068
069            manager.schedule(work, WorkManager.Scheduling.ENQUEUE, true);
070        }
071    }
072}