001/*
002 * (C) Copyright 2009 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 *     arussel
016 */
017package org.nuxeo.ecm.platform.routing.core.adapter;
018
019import org.nuxeo.ecm.core.api.DocumentModel;
020import org.nuxeo.ecm.core.api.adapter.DocumentAdapterFactory;
021import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
022import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
023import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants.ExecutionTypeValues;
024import org.nuxeo.ecm.platform.routing.core.impl.ConditionalRunner;
025import org.nuxeo.ecm.platform.routing.core.impl.DocumentRouteElementImpl;
026import org.nuxeo.ecm.platform.routing.core.impl.DocumentRouteImpl;
027import org.nuxeo.ecm.platform.routing.core.impl.DocumentRouteStepsContainerImpl;
028import org.nuxeo.ecm.platform.routing.core.impl.GraphNodeImpl;
029import org.nuxeo.ecm.platform.routing.core.impl.GraphRouteImpl;
030import org.nuxeo.ecm.platform.routing.core.impl.ParallelRunner;
031import org.nuxeo.ecm.platform.routing.core.impl.SerialRunner;
032import org.nuxeo.ecm.platform.routing.core.impl.StepElementRunner;
033
034/**
035 * Provides {@link DocumentRoute} for a {@link DocumentModel}.
036 *
037 * @author arussel
038 */
039public class DocumentRouteAdapterFactory implements DocumentAdapterFactory {
040
041    @Override
042    public Object getAdapter(DocumentModel doc, @SuppressWarnings("rawtypes") Class itf) {
043        String type = doc.getType();
044        if (doc.hasFacet(DocumentRoutingConstants.DOCUMENT_ROUTE_DOCUMENT_FACET)) {
045            ExecutionTypeValues executionType = getExecutionType(doc, type);
046            switch (executionType) {
047            case serial:
048                return new DocumentRouteImpl(doc, new SerialRunner());
049            case parallel:
050                return new DocumentRouteImpl(doc, new ParallelRunner());
051            case graph:
052                return new GraphRouteImpl(doc);
053            }
054        } else if (type.equals("RouteNode")) {
055            return new GraphNodeImpl(doc);
056        } else if (doc.hasFacet(DocumentRoutingConstants.ROUTE_STEP_FACET)) {
057            return new DocumentRouteElementImpl(doc, new StepElementRunner());
058        } else if (doc.hasFacet(DocumentRoutingConstants.CONDITIONAL_STEP_FACET)) {
059            return new DocumentRouteStepsContainerImpl(doc, new ConditionalRunner());
060        } else if (doc.hasFacet(DocumentRoutingConstants.STEP_FOLDER_FACET)) {
061            ExecutionTypeValues executionType = getExecutionType(doc, type);
062            switch (executionType) {
063            case serial:
064                return new DocumentRouteStepsContainerImpl(doc, new SerialRunner());
065            case parallel:
066                return new DocumentRouteStepsContainerImpl(doc, new ParallelRunner());
067            case graph:
068                throw new UnsupportedOperationException();
069            }
070        }
071        return null;
072    }
073
074    protected ExecutionTypeValues getExecutionType(DocumentModel doc, String type) {
075        ExecutionTypeValues executionType = ExecutionTypeValues.valueOf(
076                (String) doc.getPropertyValue(DocumentRoutingConstants.EXECUTION_TYPE_PROPERTY_NAME));
077        return executionType;
078    }
079
080}