001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 */
016package org.kuali.rice.kew.api.doctype;
017
018import org.kuali.rice.core.api.CoreConstants;
019import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
020import org.kuali.rice.core.api.mo.ModelBuilder;
021import org.kuali.rice.core.api.util.collect.CollectionUtils;
022import org.kuali.rice.kew.api.KewApiConstants;
023import org.w3c.dom.Element;
024
025import javax.xml.bind.annotation.XmlAccessType;
026import javax.xml.bind.annotation.XmlAccessorType;
027import javax.xml.bind.annotation.XmlAnyElement;
028import javax.xml.bind.annotation.XmlElement;
029import javax.xml.bind.annotation.XmlElementWrapper;
030import javax.xml.bind.annotation.XmlRootElement;
031import javax.xml.bind.annotation.XmlType;
032import java.io.Serializable;
033import java.util.ArrayList;
034import java.util.Collection;
035import java.util.List;
036
037@XmlRootElement(name = RoutePath.Constants.ROOT_ELEMENT_NAME)
038@XmlAccessorType(XmlAccessType.NONE)
039@XmlType(name = RoutePath.Constants.TYPE_NAME, propOrder = {
040        RoutePath.Elements.PROCESSES,
041        CoreConstants.CommonElements.FUTURE_ELEMENTS
042})
043public final class RoutePath extends AbstractDataTransferObject implements RoutePathContract {
044
045    private static final long serialVersionUID = -7177305375323986864L;
046
047    @XmlElementWrapper(name = Elements.PROCESSES, required = false)
048    @XmlElement(name = Elements.PROCESS, required = false)
049    private final List<ProcessDefinition> processDefinitions;
050
051    @SuppressWarnings("unused")
052    @XmlAnyElement
053    private final Collection<Element> _futureElements = null;
054
055    /**
056     * Private constructor used only by JAXB.
057     */
058    private RoutePath() {
059        this.processDefinitions = null;
060    }
061
062    private RoutePath(Builder builder) {
063        this.processDefinitions = new ArrayList<ProcessDefinition>();
064        if (builder.getProcessDefinitions() != null) {
065            for (ProcessDefinition.Builder processBuilder : builder.getProcessDefinitions()) {
066                this.processDefinitions.add(processBuilder.build());
067            }
068        }
069    }
070    
071    public ProcessDefinition getPrimaryProcess() {
072        for (ProcessDefinition processDefinition : processDefinitions) {
073            if (processDefinition.isInitial()) {
074                return processDefinition;
075            }
076        }        
077        return null;
078    }
079
080    @Override
081    public List<ProcessDefinition> getProcessDefinitions() {
082        return CollectionUtils.unmodifiableListNullSafe(this.processDefinitions);
083    }
084
085    /**
086     * A builder which can be used to construct {@link RoutePath} instances. Enforces the
087     * constraints of the {@link RoutePathContract}.
088     */
089    public final static class Builder implements Serializable, ModelBuilder, RoutePathContract {
090
091        private static final long serialVersionUID = -6916424305298043710L;
092
093        private List<ProcessDefinition.Builder> processes;
094
095        private Builder() {}
096
097        public static Builder create() {
098            Builder builder = new Builder();
099            builder.setProcesses(new ArrayList<ProcessDefinition.Builder>());
100            return builder;
101        }
102
103        public static Builder create(RoutePathContract contract) {
104            if (contract == null) {
105                throw new IllegalArgumentException("contract was null");
106            }
107            Builder builder = create();
108            List<ProcessDefinition.Builder> processBuilders = new ArrayList<ProcessDefinition.Builder>();
109            for (ProcessDefinitionContract process : contract.getProcessDefinitions()) {
110                processBuilders.add(ProcessDefinition.Builder.create(process));
111            }
112            builder.setProcesses(processBuilders);
113            return builder;
114        }
115
116        public RoutePath build() {
117            return new RoutePath(this);
118        }
119
120        @Override
121        public List<ProcessDefinition.Builder> getProcessDefinitions() {
122            return this.processes;
123        }
124
125        public void setProcesses(List<ProcessDefinition.Builder> processes) {
126            this.processes = processes;
127        }
128
129    }
130
131    /**
132     * Defines some internal constants used on this class.
133     */
134    static class Constants {
135        final static String ROOT_ELEMENT_NAME = "routePath";
136        final static String TYPE_NAME = "RoutePathType";
137    }
138
139    /**
140     * A private class which exposes constants which define the XML element names to use when this
141     * object is marshalled to XML.
142     */
143    static class Elements {
144        final static String PROCESSES = "processDefinitions";
145        final static String PROCESS = "processDefinition";
146    }
147
148    public static class Cache {
149        public static final String NAME = KewApiConstants.Namespaces.KEW_NAMESPACE_2_0 + "/" + RoutePath.Constants.TYPE_NAME;
150    }
151
152}