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