001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.kew.engine.node;
017    
018    import java.io.Serializable;
019    import java.util.ArrayList;
020    import java.util.Iterator;
021    import java.util.List;
022    
023    import javax.persistence.CascadeType;
024    import javax.persistence.Column;
025    import javax.persistence.Entity;
026    import javax.persistence.FetchType;
027    import javax.persistence.GeneratedValue;
028    import javax.persistence.Id;
029    import javax.persistence.JoinColumn;
030    import javax.persistence.ManyToOne;
031    import javax.persistence.OneToMany;
032    import javax.persistence.OneToOne;
033    import javax.persistence.Table;
034    import javax.persistence.Version;
035    
036    import org.hibernate.annotations.Fetch;
037    import org.hibernate.annotations.FetchMode;
038    import org.hibernate.annotations.GenericGenerator;
039    import org.hibernate.annotations.Parameter;
040    import org.kuali.rice.core.framework.persistence.jpa.OrmUtils;
041    import org.kuali.rice.kew.service.KEWServiceLocator;
042    
043    /**
044     * Represents a branch in the routing path of the document.
045     * 
046     * @author Kuali Rice Team (rice.collab@kuali.org)
047     */
048    @Entity
049    //@Sequence(name="KREW_RTE_NODE_S",property="branchId")
050    @Table(name="KREW_RTE_BRCH_T")
051    public class Branch implements Serializable {
052    
053            private static final long serialVersionUID = 7164561979112939112L;
054            
055            @Id
056            @GeneratedValue(generator="KREW_RTE_NODE_S")
057            @GenericGenerator(name="KREW_RTE_NODE_S",strategy="org.hibernate.id.enhanced.SequenceStyleGenerator",parameters={
058                            @Parameter(name="sequence_name",value="KREW_RTE_NODE_S"),
059                            @Parameter(name="value_column",value="id")
060            })
061            @Column(name="RTE_BRCH_ID")
062            private String branchId;
063            @ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.PERSIST)
064            @JoinColumn(name="PARNT_ID")
065            private Branch parentBranch;
066            @Column(name="NM")
067            private String name;
068        @OneToMany(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST,CascadeType.MERGE}, mappedBy="branch", orphanRemoval=true)
069        @Fetch(value=FetchMode.SELECT)
070            private List<BranchState> branchState = new ArrayList<BranchState>();
071    //        apache lazy list commented out due to not being serializable
072    //    private List branchState = ListUtils.lazyList(new ArrayList(),
073    //            new Factory() {
074    //                              public Object create() {
075    //                                      return new BranchState();
076    //                              }
077    //                      });
078        @OneToOne(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST})
079            @JoinColumn(name="INIT_RTE_NODE_INSTN_ID")
080            private RouteNodeInstance initialNode;
081        @ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.PERSIST)
082            @JoinColumn(name="SPLT_RTE_NODE_INSTN_ID")
083            private RouteNodeInstance splitNode;
084            @ManyToOne(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST,CascadeType.MERGE})
085            @JoinColumn(name="JOIN_RTE_NODE_INSTN_ID")
086            private RouteNodeInstance joinNode;
087                    
088            @Version
089            @Column(name="VER_NBR")
090            private Integer lockVerNbr;
091            
092            public String getName() {
093                    return name;
094            }
095            
096            public void setName(String name) {
097                    this.name = name;       
098            }
099            
100        public RouteNodeInstance getSplitNode() {
101            return splitNode;
102        }
103        public void setSplitNode(RouteNodeInstance splitNode) {
104            this.splitNode = splitNode;
105        }
106        public RouteNodeInstance getInitialNode() {
107                    return initialNode;
108            }
109            public void setInitialNode(RouteNodeInstance activeNode) {
110                    this.initialNode = activeNode;
111            }
112            public String getBranchId() {
113                    return branchId;
114            }
115            public void setBranchId(String branchId) {
116                    this.branchId = branchId;
117            }
118            public RouteNodeInstance getJoinNode() {
119                    return joinNode;
120            }
121            public void setJoinNode(RouteNodeInstance joinNode) {
122                    this.joinNode = joinNode;
123            }
124            public Branch getParentBranch() {
125                    return parentBranch;
126            }
127            public void setParentBranch(Branch parentBranch) {
128                    this.parentBranch = parentBranch;
129            }
130        public BranchState getBranchState(String key) {
131            for (Iterator iter = branchState.iterator(); iter.hasNext();) {
132                BranchState branchState = (BranchState) iter.next();
133                if (branchState.getKey().equals(key)) {
134                    return branchState;
135                }
136            }
137            return null;
138        }
139        public void addBranchState(BranchState state) {
140            branchState.add(state);
141            state.setBranch(this);
142        }
143        public List<BranchState> getBranchState() {
144            return branchState;
145        }
146        public void setBranchState(List<BranchState> branchState) {
147            this.branchState.clear();
148            this.branchState.addAll(branchState);
149            //this.branchState = branchState;
150        }
151        
152        public BranchState getDocBranchState(int index){
153            while (branchState.size() <= index) {
154                branchState.add(new BranchState());
155            }
156            return (BranchState) branchState.get(index);
157       
158        }
159        
160            public Integer getLockVerNbr() {
161            return lockVerNbr;
162        }
163        public void setLockVerNbr(Integer lockVerNbr) {
164            this.lockVerNbr = lockVerNbr;
165        }
166    
167        public String toString() {
168            return "[Branch: branchId=" + branchId +
169                          ", parentBranch=" + (parentBranch == null ? "null" : parentBranch.getBranchId()) +
170                          "]";
171        }
172    
173            //@PrePersist
174        public void beforeInsert(){
175            OrmUtils.populateAutoIncValue(this, KEWServiceLocator.getEntityManagerFactory().createEntityManager());
176        }    
177    }
178