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        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        try {
062            String chainId = getDocumentRoutingService().getOperationChainId(element.getDocument().getType());
063            getAutomationService().run(context, chainId);
064        } catch (OperationException e) {
065            throw new NuxeoException(e);
066        }
067    }
068
069    @Override
070    public void run(CoreSession session, DocumentRouteElement element, Map<String, Serializable> map) {
071        run(session, element);
072    }
073
074    @Override
075    public void resume(CoreSession session, DocumentRouteElement element, String nodeId, String taskId,
076            Map<String, Object> data, String status) {
077        throw new UnsupportedOperationException();
078    }
079
080    public AutomationService getAutomationService() {
081        return Framework.getService(AutomationService.class);
082    }
083
084    public DocumentRoutingService getDocumentRoutingService() {
085        return Framework.getService(DocumentRoutingService.class);
086    }
087
088    @Override
089    public void undo(CoreSession session, DocumentRouteElement element) {
090        EventFirer.fireEvent(session, element, null, DocumentRoutingConstants.Events.beforeUndoingStep.name());
091        OperationContext context = new OperationContext(session);
092        context.put(DocumentRoutingConstants.OPERATION_STEP_DOCUMENT_KEY, element);
093        context.setInput(element.getAttachedDocuments(session));
094        String operationChainId;
095        String docType = element.getDocument().getType();
096        if (element.isDone()) {
097            operationChainId = getDocumentRoutingService().getUndoFromDoneOperationChainId(docType);
098        } else if (element.isRunning()) {
099            operationChainId = getDocumentRoutingService().getUndoFromRunningOperationChainId(docType);
100        } else {
101            throw new RuntimeException("Trying to undo a step neither in done nor running state.");
102        }
103        try {
104            getAutomationService().run(context, operationChainId);
105        } catch (OperationException e) {
106            throw new NuxeoException(e);
107        }
108        EventFirer.fireEvent(session, element, null, DocumentRoutingConstants.Events.afterUndoingStep.name());
109    }
110
111    @Override
112    public void cancel(CoreSession session, DocumentRouteElement element) {
113        if (element.isCanceled()) {
114            return;
115        }
116        if (element.isReady() || element.isDone()) {
117            element.setCanceled(session);
118        } else if (element.isRunning()) {
119            try {
120                undo(session, element);
121            } finally {
122                element.setCanceled(session);
123            }
124        } else {
125            throw new RuntimeException("Not allowed to cancel an element neither in ready, done or running state.");
126        }
127    }
128}