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.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.CoreConstants;
020import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021import org.kuali.rice.core.api.mo.ModelBuilder;
022import org.w3c.dom.Element;
023
024import javax.xml.bind.annotation.XmlAccessType;
025import javax.xml.bind.annotation.XmlAccessorType;
026import javax.xml.bind.annotation.XmlAnyElement;
027import javax.xml.bind.annotation.XmlElement;
028import javax.xml.bind.annotation.XmlRootElement;
029import javax.xml.bind.annotation.XmlType;
030import java.io.Serializable;
031import java.util.Collection;
032
033@XmlRootElement(name = ProcessDefinition.Constants.ROOT_ELEMENT_NAME)
034@XmlAccessorType(XmlAccessType.NONE)
035@XmlType(name = ProcessDefinition.Constants.TYPE_NAME, propOrder = {
036        ProcessDefinition.Elements.ID,
037        ProcessDefinition.Elements.NAME,
038        ProcessDefinition.Elements.DOCUMENT_TYPE_ID,
039        ProcessDefinition.Elements.INITIAL_ROUTE_NODE,
040        ProcessDefinition.Elements.INITIAL,
041        CoreConstants.CommonElements.VERSION_NUMBER,
042        CoreConstants.CommonElements.FUTURE_ELEMENTS
043})
044public final class ProcessDefinition extends AbstractDataTransferObject implements ProcessDefinitionContract {
045
046    private static final long serialVersionUID = 1076976038764944504L;
047
048    @XmlElement(name = Elements.ID, required = false)
049    private final String id;
050
051    @XmlElement(name = Elements.NAME, required = true)
052    private final String name;
053
054    @XmlElement(name = Elements.DOCUMENT_TYPE_ID, required = false)
055    private final String documentTypeId;
056
057    @XmlElement(name = Elements.INITIAL_ROUTE_NODE, required = false)
058    private final RouteNode initialRouteNode;
059
060    @XmlElement(name = Elements.INITIAL, required = true)
061    private final boolean initial;
062
063    @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
064    private final Long versionNumber;
065
066    @SuppressWarnings("unused")
067    @XmlAnyElement
068    private final Collection<Element> _futureElements = null;
069
070    /**
071     * Private constructor used only by JAXB.
072     */
073    private ProcessDefinition() {
074        this.id = null;
075        this.name = null;
076        this.documentTypeId = null;
077        this.initialRouteNode = null;
078        this.initial = false;
079        this.versionNumber = null;
080    }
081
082    private ProcessDefinition(Builder builder) {
083        this.id = builder.getId();
084        this.name = builder.getName();
085        this.documentTypeId = builder.getDocumentTypeId();
086        if (builder.getInitialRouteNode() != null) {
087            this.initialRouteNode = builder.getInitialRouteNode().build();
088        } else {
089            this.initialRouteNode = null;
090        }
091        this.initial = builder.isInitial();
092        this.versionNumber = builder.getVersionNumber();
093    }
094
095    @Override
096    public String getId() {
097        return this.id;
098    }
099
100    @Override
101    public String getName() {
102        return this.name;
103    }
104
105    @Override
106    public String getDocumentTypeId() {
107        return this.documentTypeId;
108    }
109
110    @Override
111    public RouteNodeContract getInitialRouteNode() {
112        return this.initialRouteNode;
113    }
114
115    @Override
116    public boolean isInitial() {
117        return this.initial;
118    }
119
120    @Override
121    public Long getVersionNumber() {
122        return this.versionNumber;
123    }
124
125    /**
126     * A builder which can be used to construct {@link ProcessDefinition} instances. Enforces the constraints
127     * of the {@link ProcessDefinitionContract}.
128     */
129    public final static class Builder implements Serializable, ModelBuilder, ProcessDefinitionContract {
130
131        private static final long serialVersionUID = 5897271312287857808L;
132
133        private String id;
134        private String name;
135        private String documentTypeId;
136        private RouteNode.Builder initialRouteNode;
137        private boolean initial;
138        private Long versionNumber;
139
140        private Builder(String name, RouteNode.Builder initialRouteNode, boolean initial) {
141            setName(name);
142            setInitialRouteNode(initialRouteNode);
143            setInitial(initial);
144        }
145
146        /**
147         * Create a new ProcessDefinition.Builder
148         * @param name The name for the process.
149         * @param initialRouteNode The first route node for the process.  May be null.
150         * @param initial
151         * @return
152         */
153        public static Builder create(String name, RouteNode.Builder initialRouteNode, boolean initial) {
154            return new Builder(name, initialRouteNode, initial);
155        }
156
157        public static Builder create(ProcessDefinitionContract contract) {
158            if (contract == null) {
159                throw new IllegalArgumentException("contract was null");
160            }
161            RouteNode.Builder initialRouteNode = null;
162            if (contract.getInitialRouteNode() != null) {
163                initialRouteNode = RouteNode.Builder.create(contract.getInitialRouteNode());
164            }
165
166            Builder builder = create(contract.getName(), initialRouteNode, contract.isInitial());
167            builder.setDocumentTypeId(contract.getDocumentTypeId());
168            builder.setId(contract.getId());
169            return builder;
170        }
171
172        public ProcessDefinition build() {
173            return new ProcessDefinition(this);
174        }
175
176        @Override
177        public String getId() {
178            return this.id;
179        }
180
181        @Override
182        public String getName() {
183            return this.name;
184        }
185
186        @Override
187        public String getDocumentTypeId() {
188            return this.documentTypeId;
189        }
190
191        @Override
192        public RouteNode.Builder getInitialRouteNode() {
193            return this.initialRouteNode;
194        }
195
196        @Override
197        public boolean isInitial() {
198            return this.initial;
199        }
200
201        @Override
202        public Long getVersionNumber() {
203            return this.versionNumber;
204        }
205
206        public void setId(String id) {
207            this.id = id;
208        }
209
210        public void setName(String name) {
211            if (StringUtils.isBlank(name)) {
212                throw new IllegalArgumentException("name was null or blank");
213            }
214            this.name = name;
215        }
216
217        public void setDocumentTypeId(String documentTypeId) {
218            this.documentTypeId = documentTypeId;
219        }
220
221        public void setInitialRouteNode(RouteNode.Builder initialRouteNode) {
222            this.initialRouteNode = initialRouteNode;
223        }
224
225        public void setInitial(boolean initial) {
226            this.initial = initial;
227        }
228
229        public void setVersionNumber(Long versionNumber) {
230            this.versionNumber = versionNumber;
231        }
232
233    }
234
235    /**
236     * Defines some internal constants used on this class.
237     */
238    static class Constants {
239        final static String ROOT_ELEMENT_NAME = "process";
240        final static String TYPE_NAME = "ProcessType";
241    }
242
243    /**
244     * A private class which exposes constants which define the XML element names to use when this
245     * object is marshalled to XML.
246     */
247    static class Elements {
248        final static String ID = "id";
249        final static String NAME = "name";
250        final static String DOCUMENT_TYPE_ID = "documentTypeId";
251        final static String INITIAL_ROUTE_NODE = "initialRouteNode";
252        final static String INITIAL = "initial";
253    }
254
255}