001/**
002 * Copyright 2005-2014 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.document.node;
017
018import java.io.Serializable;
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.List;
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.XmlElementWrapper;
029import javax.xml.bind.annotation.XmlRootElement;
030import javax.xml.bind.annotation.XmlType;
031
032import org.kuali.rice.core.api.CoreConstants;
033import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
034import org.kuali.rice.core.api.mo.ModelBuilder;
035import org.w3c.dom.Element;
036
037@XmlRootElement(name = RouteNodeInstance.Constants.ROOT_ELEMENT_NAME)
038@XmlAccessorType(XmlAccessType.NONE)
039@XmlType(name = RouteNodeInstance.Constants.TYPE_NAME, propOrder = {
040    RouteNodeInstance.Elements.ID,
041    RouteNodeInstance.Elements.NAME,
042    RouteNodeInstance.Elements.STATE,
043    RouteNodeInstance.Elements.DOCUMENT_ID,
044    RouteNodeInstance.Elements.BRANCH_ID,
045    RouteNodeInstance.Elements.ROUTE_NODE_ID,
046    RouteNodeInstance.Elements.PROCESS_ID,
047    RouteNodeInstance.Elements.ACTIVE,
048    RouteNodeInstance.Elements.COMPLETE,
049    RouteNodeInstance.Elements.INITIAL,
050    RouteNodeInstance.Elements.NEXT_NODE_INSTANCES,
051    CoreConstants.CommonElements.FUTURE_ELEMENTS
052})
053public final class RouteNodeInstance extends AbstractDataTransferObject
054    implements RouteNodeInstanceContract
055{
056
057    @XmlElement(name = Elements.NAME, required = false)
058    private final String name;
059
060    @XmlElementWrapper(name = Elements.STATE, required = false)
061    @XmlElement(name = Elements.ROUTE_NODE_INSTANCE_STATE, required = false)
062    private final List<RouteNodeInstanceState> state;
063
064    @XmlElement(name = Elements.DOCUMENT_ID, required = false)
065    private final String documentId;
066
067    @XmlElement(name = Elements.BRANCH_ID, required = false)
068    private final String branchId;
069
070    @XmlElement(name = Elements.ROUTE_NODE_ID, required = false)
071    private final String routeNodeId;
072
073    @XmlElement(name = Elements.PROCESS_ID, required = false)
074    private final String processId;
075
076    @XmlElement(name = Elements.ACTIVE, required = false)
077    private final boolean active;
078
079    @XmlElement(name = Elements.COMPLETE, required = false)
080    private final boolean complete;
081
082    @XmlElement(name = Elements.INITIAL, required = false)
083    private final boolean initial;
084
085    @XmlElement(name = Elements.ID, required = false)
086    private final String id;
087
088    @XmlElementWrapper(name = Elements.NEXT_NODE_INSTANCES, required = false)
089    @XmlElement(name = Elements.NEXT_NODE_INSTANCE, required = false)
090    private final List<RouteNodeInstance> nextNodeInstances;
091
092    @SuppressWarnings("unused")
093    @XmlAnyElement
094    private final Collection<Element> _futureElements = null;
095
096    /**
097     * Private constructor used only by JAXB.
098     * 
099     */
100    private RouteNodeInstance() {
101        this.name = null;
102        this.state = null;
103        this.documentId = null;
104        this.branchId = null;
105        this.routeNodeId = null;
106        this.processId = null;
107        this.active = false;
108        this.complete = false;
109        this.initial = false;
110        this.id = null;
111        this.nextNodeInstances = null;
112    }
113
114    private RouteNodeInstance(Builder builder) {
115        this.name = builder.getName();
116        if (builder.getState() != null) {
117            List<RouteNodeInstanceState> states = new ArrayList<RouteNodeInstanceState>();
118            for(RouteNodeInstanceState.Builder stateBuilder : builder.getState()) {
119                states.add(stateBuilder.build());
120            }
121            this.state = states;
122        } else {
123            this.state = Collections.emptyList();
124        }
125
126        if (builder.getNextNodeInstances() != null) {
127            List<RouteNodeInstance> nextInstances = new ArrayList<RouteNodeInstance>();
128            for (RouteNodeInstance.Builder instance : builder.getNextNodeInstances()) {
129                nextInstances.add(instance.build());
130            }
131            this.nextNodeInstances = nextInstances;
132        } else {
133            this.nextNodeInstances = Collections.emptyList();
134        }
135
136
137        this.documentId = builder.getDocumentId();
138        this.branchId = builder.getBranchId();
139        this.routeNodeId = builder.getRouteNodeId();
140        this.processId = builder.getProcessId();
141        this.active = builder.isActive();
142        this.complete = builder.isComplete();
143        this.initial = builder.isInitial();
144        this.id = builder.getId();
145    }
146
147    @Override
148    public String getName() {
149        return this.name;
150    }
151
152    @Override
153    public List<RouteNodeInstanceState> getState() {
154        return this.state;
155    }
156
157    @Override
158    public String getDocumentId() {
159        return this.documentId;
160    }
161
162    @Override
163    public String getBranchId() {
164        return this.branchId;
165    }
166
167    @Override
168    public String getRouteNodeId() {
169        return this.routeNodeId;
170    }
171
172    @Override
173    public String getProcessId() {
174        return this.processId;
175    }
176
177    @Override
178    public boolean isActive() {
179        return this.active;
180    }
181
182    @Override
183    public boolean isComplete() {
184        return this.complete;
185    }
186
187    @Override
188    public boolean isInitial() {
189        return this.initial;
190    }
191
192    @Override
193    public String getId() {
194        return this.id;
195    }
196
197    @Override
198    public List<RouteNodeInstance> getNextNodeInstances() {
199        return this.nextNodeInstances;
200    }
201
202
203    /**
204     * A builder which can be used to construct {@link RouteNodeInstance} instances.  Enforces the constraints of the {@link RouteNodeInstanceContract}.
205     * 
206     */
207    public final static class Builder
208        implements Serializable, ModelBuilder, RouteNodeInstanceContract
209    {
210
211        private String name;
212        private List<RouteNodeInstanceState.Builder> state;
213        private String documentId;
214        private String branchId;
215        private String routeNodeId;
216        private String processId;
217        private boolean active;
218        private boolean complete;
219        private boolean initial;
220        private String id;
221        private List<RouteNodeInstance.Builder> nextNodeInstances;
222
223        private Builder() {
224            // TODO modify this constructor as needed to pass any required values and invoke the appropriate 'setter' methods
225        }
226
227        public static Builder create() {
228            // TODO modify as needed to pass any required values and add them to the signature of the 'create' method
229            return new Builder();
230        }
231
232        public static Builder create(RouteNodeInstanceContract contract) {
233            if (contract == null) {
234                throw new IllegalArgumentException("contract was null");
235            }
236            // TODO if create() is modified to accept required parameters, this will need to be modified
237            Builder builder = create();
238            builder.setName(contract.getName());
239            if (contract.getState() != null) {
240                List<RouteNodeInstanceState.Builder> stateBuilders = new ArrayList<RouteNodeInstanceState.Builder>();
241                for (RouteNodeInstanceStateContract stateContract : contract.getState()) {
242                    stateBuilders.add(RouteNodeInstanceState.Builder.create(stateContract));
243                }
244                builder.setState(stateBuilders);
245            }
246
247            builder.setDocumentId(contract.getDocumentId());
248            builder.setBranchId(contract.getBranchId());
249            builder.setRouteNodeId(contract.getRouteNodeId());
250            builder.setProcessId(contract.getProcessId());
251            builder.setActive(contract.isActive());
252            builder.setComplete(contract.isComplete());
253            builder.setInitial(contract.isInitial());
254            builder.setId(contract.getId());
255            if (contract.getNextNodeInstances() != null) {
256                List<RouteNodeInstance.Builder> instanceBuilders = new ArrayList<RouteNodeInstance.Builder>();
257                for(RouteNodeInstanceContract instanceContract : contract.getNextNodeInstances()) {
258                    instanceBuilders.add(RouteNodeInstance.Builder.create(instanceContract));
259                }
260                builder.setNextNodeInstances(instanceBuilders);
261            }
262            return builder;
263        }
264
265        public RouteNodeInstance build() {
266            return new RouteNodeInstance(this);
267        }
268
269        @Override
270        public String getName() {
271            return this.name;
272        }
273
274        @Override
275        public List<RouteNodeInstanceState.Builder> getState() {
276            return this.state;
277        }
278
279        @Override
280        public String getDocumentId() {
281            return this.documentId;
282        }
283
284        @Override
285        public String getBranchId() {
286            return this.branchId;
287        }
288
289        @Override
290        public String getRouteNodeId() {
291            return this.routeNodeId;
292        }
293
294        @Override
295        public String getProcessId() {
296            return this.processId;
297        }
298
299        @Override
300        public boolean isActive() {
301            return this.active;
302        }
303
304        @Override
305        public boolean isComplete() {
306            return this.complete;
307        }
308
309        @Override
310        public boolean isInitial() {
311            return this.initial;
312        }
313
314        @Override
315        public String getId() {
316            return this.id;
317        }
318
319        @Override
320        public List<RouteNodeInstance.Builder> getNextNodeInstances() {
321            return this.nextNodeInstances;
322        }
323
324        public void setNextNodeInstances(List<RouteNodeInstance.Builder> nextNodeInstances) {
325            // TODO add validation of input value if required and throw IllegalArgumentException if needed
326            this.nextNodeInstances = Collections.unmodifiableList(nextNodeInstances);
327        }
328
329        public void setName(String name) {
330            // TODO add validation of input value if required and throw IllegalArgumentException if needed
331            this.name = name;
332        }
333
334        public void setState(List<RouteNodeInstanceState.Builder> state) {
335            // TODO add validation of input value if required and throw IllegalArgumentException if needed
336            this.state = state;
337        }
338
339        public void setDocumentId(String documentId) {
340            // TODO add validation of input value if required and throw IllegalArgumentException if needed
341            this.documentId = documentId;
342        }
343
344        public void setBranchId(String branchId) {
345            // TODO add validation of input value if required and throw IllegalArgumentException if needed
346            this.branchId = branchId;
347        }
348
349        public void setRouteNodeId(String routeNodeId) {
350            // TODO add validation of input value if required and throw IllegalArgumentException if needed
351            this.routeNodeId = routeNodeId;
352        }
353
354        public void setProcessId(String processId) {
355            // TODO add validation of input value if required and throw IllegalArgumentException if needed
356            this.processId = processId;
357        }
358
359        public void setActive(boolean active) {
360            // TODO add validation of input value if required and throw IllegalArgumentException if needed
361            this.active = active;
362        }
363
364        public void setComplete(boolean complete) {
365            // TODO add validation of input value if required and throw IllegalArgumentException if needed
366            this.complete = complete;
367        }
368
369        public void setInitial(boolean initial) {
370            // TODO add validation of input value if required and throw IllegalArgumentException if needed
371            this.initial = initial;
372        }
373
374        public void setId(String id) {
375            // TODO add validation of input value if required and throw IllegalArgumentException if needed
376            this.id = id;
377        }
378
379    }
380
381
382    /**
383     * Defines some internal constants used on this class.
384     * 
385     */
386    static class Constants {
387
388        final static String ROOT_ELEMENT_NAME = "routeNodeInstance";
389        final static String TYPE_NAME = "RouteNodeInstanceType";
390    }
391
392
393    /**
394     * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
395     * 
396     */
397    static class Elements {
398
399        final static String NAME = "name";
400        final static String STATE = "state";
401        final static String ROUTE_NODE_INSTANCE_STATE = "routeNodeInstanceState";
402        final static String DOCUMENT_ID = "documentId";
403        final static String BRANCH_ID = "branchId";
404        final static String ROUTE_NODE_ID = "routeNodeId";
405        final static String PROCESS_ID = "processId";
406        final static String ACTIVE = "active";
407        final static String COMPLETE = "complete";
408        final static String INITIAL = "initial";
409        final static String ID = "id";
410        final static String NEXT_NODE_INSTANCES = "nextNodeInstances";
411        final static String NEXT_NODE_INSTANCE = "nextNodeInstance";
412    }
413
414}