View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.engine.node;
17  
18  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
19  
20  import javax.persistence.CascadeType;
21  import javax.persistence.Column;
22  import javax.persistence.Entity;
23  import javax.persistence.FetchType;
24  import javax.persistence.GeneratedValue;
25  import javax.persistence.Id;
26  import javax.persistence.JoinColumn;
27  import javax.persistence.ManyToOne;
28  import javax.persistence.OneToMany;
29  import javax.persistence.Table;
30  import javax.persistence.Version;
31  import java.io.Serializable;
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.Map;
36  
37  /**
38   * Represents a branch in the routing path of the document.
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   */
42  @Entity
43  @Table(name = "KREW_RTE_BRCH_T")
44  public class Branch implements Serializable {
45  
46  	private static final long serialVersionUID = 7164561979112939112L;
47  	
48  	@Id
49  	@GeneratedValue(generator = "KREW_RTE_NODE_S")
50      @PortableSequenceGenerator(name = "KREW_RTE_NODE_S")
51  	@Column(name = "RTE_BRCH_ID")
52  	private String branchId;
53  
54  	@ManyToOne
55  	@JoinColumn(name = "PARNT_ID")
56  	private Branch parentBranch;
57  
58  	@Column(name = "NM")
59  	private String name;
60  
61      @OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "branch", orphanRemoval = true)
62  	private List<BranchState> branchState = new ArrayList<BranchState>();
63  
64      @ManyToOne(cascade = CascadeType.ALL)
65  	@JoinColumn(name = "INIT_RTE_NODE_INSTN_ID")
66  	private RouteNodeInstance initialNode;
67  
68      @ManyToOne(cascade = CascadeType.ALL)
69  	@JoinColumn(name = "SPLT_RTE_NODE_INSTN_ID")
70  	private RouteNodeInstance splitNode;
71  
72      @ManyToOne(cascade = CascadeType.ALL)
73  	@JoinColumn(name = "JOIN_RTE_NODE_INSTN_ID")
74  	private RouteNodeInstance joinNode;
75  		
76  	@Version
77  	@Column(name="VER_NBR")
78  	private Integer lockVerNbr;
79  	
80  	public String getName() {
81  		return name;
82  	}
83  	
84  	public void setName(String name) {
85  		this.name = name;	
86  	}
87  	
88      public RouteNodeInstance getSplitNode() {
89          return splitNode;
90      }
91      public void setSplitNode(RouteNodeInstance splitNode) {
92          this.splitNode = splitNode;
93      }
94      public RouteNodeInstance getInitialNode() {
95  		return initialNode;
96  	}
97  	public void setInitialNode(RouteNodeInstance activeNode) {
98  		this.initialNode = activeNode;
99  	}
100 	public String getBranchId() {
101 		return branchId;
102 	}
103 	public void setBranchId(String branchId) {
104 		this.branchId = branchId;
105 	}
106 	public RouteNodeInstance getJoinNode() {
107 		return joinNode;
108 	}
109 	public void setJoinNode(RouteNodeInstance joinNode) {
110 		this.joinNode = joinNode;
111 	}
112 	public Branch getParentBranch() {
113 		return parentBranch;
114 	}
115 	public void setParentBranch(Branch parentBranch) {
116 		this.parentBranch = parentBranch;
117 	}
118     public BranchState getBranchState(String key) {
119         for (Iterator iter = branchState.iterator(); iter.hasNext();) {
120             BranchState branchState = (BranchState) iter.next();
121             if (branchState.getKey().equals(key)) {
122                 return branchState;
123             }
124         }
125         return null;
126     }
127     public void addBranchState(BranchState state) {
128         branchState.add(state);
129         state.setBranch(this);
130     }
131     public List<BranchState> getBranchState() {
132         return branchState;
133     }
134     public void setBranchState(List<BranchState> branchState) {
135         this.branchState.clear();
136         this.branchState.addAll(branchState);
137     }
138     
139     public BranchState getDocBranchState(int index){
140     	while (branchState.size() <= index) {
141             branchState.add(new BranchState());
142         }
143         return branchState.get(index);
144    
145     }
146     
147 	public Integer getLockVerNbr() {
148         return lockVerNbr;
149     }
150     public void setLockVerNbr(Integer lockVerNbr) {
151         this.lockVerNbr = lockVerNbr;
152     }
153 
154     public Branch deepCopy(Map<Object, Object> visited) {
155         if (visited.containsKey(this)) {
156             return (Branch)visited.get(this);
157         }
158         Branch copy = new Branch();
159         visited.put(this, copy);
160         copy.branchId = branchId;
161         copy.name = name;
162         copy.lockVerNbr = lockVerNbr;
163         if (parentBranch != null) {
164             copy.parentBranch = parentBranch.deepCopy(visited);
165         }
166         if (branchState != null) {
167             List<BranchState> copies = new ArrayList<BranchState>();
168             for (BranchState state : branchState) {
169                 copies.add(state.deepCopy(visited));
170             }
171             copy.branchState = copies;
172         }
173         if (initialNode != null) {
174             copy.initialNode = initialNode.deepCopy(visited);
175         }
176         if (splitNode != null) {
177             copy.splitNode = splitNode.deepCopy(visited);
178         }
179         if (joinNode != null) {
180             copy.joinNode = joinNode.deepCopy(visited);
181         }
182         return copy;
183     }
184 
185 
186     public String toString() {
187         return "[Branch: branchId=" + branchId +
188                       ", parentBranch=" + (parentBranch == null ? "null" : parentBranch.getBranchId()) +
189                       "]";
190     }
191 
192 }
193