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.engine.node;
017
018import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
019
020import javax.persistence.CascadeType;
021import javax.persistence.Column;
022import javax.persistence.Entity;
023import javax.persistence.FetchType;
024import javax.persistence.GeneratedValue;
025import javax.persistence.Id;
026import javax.persistence.JoinColumn;
027import javax.persistence.ManyToOne;
028import javax.persistence.OneToMany;
029import javax.persistence.Table;
030import javax.persistence.Version;
031import java.io.Serializable;
032import java.util.ArrayList;
033import java.util.Iterator;
034import java.util.List;
035import java.util.Map;
036
037/**
038 * Represents a branch in the routing path of the document.
039 * 
040 * @author Kuali Rice Team (rice.collab@kuali.org)
041 */
042@Entity
043@Table(name = "KREW_RTE_BRCH_T")
044public class Branch implements Serializable {
045
046        private static final long serialVersionUID = 7164561979112939112L;
047        
048        @Id
049        @GeneratedValue(generator = "KREW_RTE_NODE_S")
050    @PortableSequenceGenerator(name = "KREW_RTE_NODE_S")
051        @Column(name = "RTE_BRCH_ID")
052        private String branchId;
053
054        @ManyToOne
055        @JoinColumn(name = "PARNT_ID")
056        private Branch parentBranch;
057
058        @Column(name = "NM")
059        private String name;
060
061    @OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "branch", orphanRemoval = true)
062        private List<BranchState> branchState = new ArrayList<BranchState>();
063
064    @ManyToOne(cascade = CascadeType.ALL)
065        @JoinColumn(name = "INIT_RTE_NODE_INSTN_ID")
066        private RouteNodeInstance initialNode;
067
068    @ManyToOne(cascade = CascadeType.ALL)
069        @JoinColumn(name = "SPLT_RTE_NODE_INSTN_ID")
070        private RouteNodeInstance splitNode;
071
072    @ManyToOne(cascade = CascadeType.ALL)
073        @JoinColumn(name = "JOIN_RTE_NODE_INSTN_ID")
074        private RouteNodeInstance joinNode;
075                
076        @Version
077        @Column(name="VER_NBR")
078        private Integer lockVerNbr;
079        
080        public String getName() {
081                return name;
082        }
083        
084        public void setName(String name) {
085                this.name = name;       
086        }
087        
088    public RouteNodeInstance getSplitNode() {
089        return splitNode;
090    }
091    public void setSplitNode(RouteNodeInstance splitNode) {
092        this.splitNode = splitNode;
093    }
094    public RouteNodeInstance getInitialNode() {
095                return initialNode;
096        }
097        public void setInitialNode(RouteNodeInstance activeNode) {
098                this.initialNode = activeNode;
099        }
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