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