001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017package org.nuxeo.ecm.platform.routing.core.impl;
018
019import java.io.Serializable;
020import java.util.Map;
021
022import org.nuxeo.ecm.automation.AutomationService;
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.OperationException;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.NuxeoException;
027import org.nuxeo.ecm.platform.routing.api.DocumentRouteElement;
028import org.nuxeo.ecm.platform.routing.api.DocumentRouteStep;
029import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
030import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * Run the operation chain for this step.
035 *
036 * @deprecated since 5.9.2 - Use only routes of type 'graph'
037 * @author <a href="mailto:arussel@nuxeo.com">Alexandre Russel</a>
038 */
039@Deprecated
040public class StepElementRunner implements ElementRunner {
041
042    @Override
043    public void run(CoreSession session, DocumentRouteElement element) {
044        if (element.isRunning()) {
045            return;
046        } else {
047            element.setRunning(session);
048        }
049        if (!(element instanceof DocumentRouteStep)) {
050            throw new RuntimeException("Method run should be overriden in parent class.");
051        }
052        EventFirer.fireEvent(session, element, null, DocumentRoutingConstants.Events.beforeStepRunning.name());
053        OperationContext context = new OperationContext(session);
054        context.put(DocumentRoutingConstants.OPERATION_STEP_DOCUMENT_KEY, element);
055        context.setInput(element.getAttachedDocuments(session));
056        if (!element.isDone()) {
057            EventFirer.fireEvent(session, element, null, DocumentRoutingConstants.Events.stepWaiting.name());
058        }
059        try {
060            String chainId = getDocumentRoutingService().getOperationChainId(element.getDocument().getType());
061            getAutomationService().run(context, chainId);
062        } catch (OperationException e) {
063            throw new NuxeoException(e);
064        }
065    }
066
067    @Override
068    public void run(CoreSession session, DocumentRouteElement element, Map<String, Serializable> map) {
069        run(session, element);
070    }
071
072    @Override
073    public void resume(CoreSession session, DocumentRouteElement element, String nodeId, String taskId,
074            Map<String, Object> data, String status) {
075        throw new UnsupportedOperationException();
076    }
077
078    public AutomationService getAutomationService() {
079        return Framework.getService(AutomationService.class);
080    }
081
082    public DocumentRoutingService getDocumentRoutingService() {
083        return Framework.getService(DocumentRoutingService.class);
084    }
085
086    @Override
087    public void undo(CoreSession session, DocumentRouteElement element) {
088        EventFirer.fireEvent(session, element, null, DocumentRoutingConstants.Events.beforeUndoingStep.name());
089        OperationContext context = new OperationContext(session);
090        context.put(DocumentRoutingConstants.OPERATION_STEP_DOCUMENT_KEY, element);
091        context.setInput(element.getAttachedDocuments(session));
092        String operationChainId;
093        String docType = element.getDocument().getType();
094        if (element.isDone()) {
095            operationChainId = getDocumentRoutingService().getUndoFromDoneOperationChainId(docType);
096        } else if (element.isRunning()) {
097            operationChainId = getDocumentRoutingService().getUndoFromRunningOperationChainId(docType);
098        } else {
099            throw new RuntimeException("Trying to undo a step neither in done nor running state.");
100        }
101        try {
102            getAutomationService().run(context, operationChainId);
103        } catch (OperationException e) {
104            throw new NuxeoException(e);
105        }
106        EventFirer.fireEvent(session, element, null, DocumentRoutingConstants.Events.afterUndoingStep.name());
107    }
108
109    @Override
110    public void cancel(CoreSession session, DocumentRouteElement element) {
111        if (element.isCanceled()) {
112            return;
113        }
114        if (element.isReady() || element.isDone()) {
115            element.setCanceled(session);
116        } else if (element.isRunning()) {
117            try {
118                undo(session, element);
119            } finally {
120                element.setCanceled(session);
121            }
122        } else {
123            throw new RuntimeException("Not allowed to cancel an element neither in ready, done or running state.");
124        }
125    }
126}