001/*
002 * (C) Copyright 2011 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.util.List;
020
021import org.nuxeo.ecm.core.api.CoreSession;
022import org.nuxeo.ecm.core.api.PropertyException;
023import org.nuxeo.ecm.platform.routing.api.DocumentRouteElement;
024import org.nuxeo.ecm.platform.routing.api.DocumentRouteStepsContainer;
025import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
026
027/**
028 * Run the first step and then run the folder child in the position determined by the posOfChildStepToRunNext on this
029 * container. It's done once the selected branch to be run is done .
030 *
031 * @since 5.5
032 * @deprecated since 5.9.2 - Use only routes of type 'graph'
033 */
034@Deprecated
035public class ConditionalRunner extends SerialRunner {
036
037    @Override
038    public void run(CoreSession session, DocumentRouteElement element) {
039        String posOfchildToRun = null;
040        try {
041            posOfchildToRun = (String) element.getDocument().getPropertyValue(
042                    DocumentRoutingConstants.STEP_TO_BE_EXECUTED_NEXT_PROPERTY_NAME);
043        } catch (PropertyException e) {
044        }
045
046        List<DocumentRouteElement> children = getChildrenElement(session, element);
047        if (!element.isRunning()) {
048            element.setRunning(session);
049        }
050        if (children.isEmpty()) {
051            element.setDone(session);
052            return;
053        }
054        // run all the child unless there is a wait state
055        for (DocumentRouteElement child : children) {
056            if (!child.isDone() && !child.isCanceled()) {
057                if (!(child instanceof DocumentRouteStepsContainer)) {
058                    // run the simple step
059                    child.run(session);
060                    if (!child.isDone()) {
061                        return;
062                    }
063                } else if (child instanceof DocumentRouteStepsContainer) {
064                    // run only the child that was selected to be run by the
065                    // previous step
066                    if (String.valueOf(children.indexOf(child)).equals(posOfchildToRun)) {
067                        child.run(session);
068                        if (!child.isDone()) {
069                            return;
070                        }
071                    } else {
072                        // cancel the branch that won;t be run
073                        if (!child.isCanceled()) {
074                            child.cancel(session);
075                        }
076                    }
077                }
078            }
079        }
080        // all child ran, we're done
081        element.setDone(session);
082    }
083}