001/*
002 * (C) Copyright 2010 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 - initial API and implementation
018 */
019package org.nuxeo.ecm.platform.routing.core.impl;
020
021import java.io.Serializable;
022import java.util.Map;
023
024import org.nuxeo.ecm.automation.AutomationService;
025import org.nuxeo.ecm.automation.OperationContext;
026import org.nuxeo.ecm.automation.OperationException;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.NuxeoException;
029import org.nuxeo.ecm.platform.routing.api.DocumentRouteElement;
030import org.nuxeo.ecm.platform.routing.api.DocumentRouteStep;
031import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
032import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
033import org.nuxeo.runtime.api.Framework;
034
035/**
036 * Run the operation chain for this step.
037 *
038 * @deprecated since 5.9.2 - Use only routes of type 'graph'
039 * @author <a href="mailto:arussel@nuxeo.com">Alexandre Russel</a>
040 */
041@Deprecated
042public class StepElementRunner implements ElementRunner {
043
044    @Override
045    public void run(CoreSession session, DocumentRouteElement element) {
046        if (element.isRunning()) {
047            return;
048        } else {
049            element.setRunning(session);
050        }
051        if (!(element instanceof DocumentRouteStep)) {
052            throw new RuntimeException("Method run should be overriden in parent class.");
053        }
054        EventFirer.fireEvent(session, element, null, DocumentRoutingConstants.Events.beforeStepRunning.name());
055        try (OperationContext context = new OperationContext(session)) {
056            context.put(DocumentRoutingConstants.OPERATION_STEP_DOCUMENT_KEY, element);
057            context.setInput(element.getAttachedDocuments(session));
058            if (!element.isDone()) {
059                EventFirer.fireEvent(session, element, null, DocumentRoutingConstants.Events.stepWaiting.name());
060            }
061            String chainId = getDocumentRoutingService().getOperationChainId(element.getDocument().getType());
062            getAutomationService().run(context, chainId);
063        } catch (OperationException e) {
064            throw new NuxeoException(e);
065        }
066    }
067
068    @Override
069    public void run(CoreSession session, DocumentRouteElement element, Map<String, Serializable> map) {
070        run(session, element);
071    }
072
073    @Override
074    public void resume(CoreSession session, DocumentRouteElement element, String nodeId, String taskId,
075            Map<String, Object> data, String status) {
076        throw new UnsupportedOperationException();
077    }
078
079    public AutomationService getAutomationService() {
080        return Framework.getService(AutomationService.class);
081    }
082
083    public DocumentRoutingService getDocumentRoutingService() {
084        return Framework.getService(DocumentRoutingService.class);
085    }
086
087    @Override
088    public void undo(CoreSession session, DocumentRouteElement element) {
089        EventFirer.fireEvent(session, element, null, DocumentRoutingConstants.Events.beforeUndoingStep.name());
090        try (OperationContext context = new OperationContext(session)) {
091            context.put(DocumentRoutingConstants.OPERATION_STEP_DOCUMENT_KEY, element);
092            context.setInput(element.getAttachedDocuments(session));
093            String operationChainId;
094            String docType = element.getDocument().getType();
095            if (element.isDone()) {
096                operationChainId = getDocumentRoutingService().getUndoFromDoneOperationChainId(docType);
097            } else if (element.isRunning()) {
098                operationChainId = getDocumentRoutingService().getUndoFromRunningOperationChainId(docType);
099            } else {
100                throw new RuntimeException("Trying to undo a step neither in done nor running state.");
101            }
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}