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