View Javadoc

1   /**
2    * Copyright 2005-2011 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.kim.document;
17  
18  import org.apache.log4j.Logger;
19  import org.kuali.rice.core.api.delegation.DelegationType;
20  import org.kuali.rice.kim.api.KimConstants;
21  import org.kuali.rice.kim.api.type.KimAttributeField;
22  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegation;
23  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMember;
24  import org.kuali.rice.krad.document.TransactionalDocumentBase;
25  import org.kuali.rice.krad.service.KRADServiceLocator;
26  import org.kuali.rice.krad.service.SequenceAccessorService;
27  import org.springframework.util.AutoPopulatingList;
28  
29  import javax.persistence.AssociationOverride;
30  import javax.persistence.AssociationOverrides;
31  import javax.persistence.AttributeOverride;
32  import javax.persistence.AttributeOverrides;
33  import javax.persistence.CascadeType;
34  import javax.persistence.Column;
35  import javax.persistence.FetchType;
36  import javax.persistence.JoinColumn;
37  import javax.persistence.MappedSuperclass;
38  import javax.persistence.OneToMany;
39  import javax.persistence.Transient;
40  import java.util.List;
41  
42  /**
43   * This is a description of what this class does - bhargavp don't forget to fill this in. 
44   * 
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   *
47   */
48  @MappedSuperclass
49  @AttributeOverrides({
50  	@AttributeOverride(name="documentNumber",column=@Column(name="FDOC_NBR"))
51  })
52  @AssociationOverrides({
53  	@AssociationOverride(name="documentHeader",joinColumns=@JoinColumn(name="FDOC_NBR",referencedColumnName="DOC_HDR_ID",insertable=false,updatable=false))
54  })
55  public class IdentityManagementKimDocument extends TransactionalDocumentBase {
56  
57  	protected static final Logger LOG = Logger.getLogger(IdentityManagementKimDocument.class);
58  	
59  	@OneToMany(targetEntity=RoleDocumentDelegation.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL})
60      @JoinColumn(name="FDOC_NBR",insertable=false,updatable=false)
61  	protected List<RoleDocumentDelegation> delegations = new AutoPopulatingList(RoleDocumentDelegation.class);
62  	@Transient
63  	protected List<RoleDocumentDelegationMember> delegationMembers = new AutoPopulatingList(RoleDocumentDelegationMember.class);
64  	@Transient
65  	protected transient SequenceAccessorService sequenceAccessorService;
66  	
67  	protected void addDelegationMemberToDelegation(RoleDocumentDelegationMember delegationMember){
68  		RoleDocumentDelegation delegation;
69  		if(DelegationType.PRIMARY.getCode().equals(delegationMember.getDelegationTypeCode())){
70  			delegation = getPrimaryDelegation();
71  		} else{
72  			delegation = getSecondaryDelegation();
73  		}
74  		delegationMember.setDelegationId(delegation.getDelegationId());
75  		delegation.getMembers().add(delegationMember);
76  		delegation.setRoleId(delegationMember.getRoleBo().getId());
77  		delegation.setKimTypeId(delegationMember.getRoleBo().getKimTypeId());
78  	}
79  
80  	protected RoleDocumentDelegation getPrimaryDelegation(){
81  		RoleDocumentDelegation primaryDelegation = null;
82  		for(RoleDocumentDelegation delegation: getDelegations()){
83  			if(delegation.isDelegationPrimary()) {
84  				primaryDelegation = delegation;
85              }
86  		}
87  		if(primaryDelegation==null){
88  			primaryDelegation = new RoleDocumentDelegation();
89  			primaryDelegation.setDelegationId(getDelegationId());
90  			primaryDelegation.setDelegationTypeCode(DelegationType.PRIMARY.getCode());
91  			getDelegations().add(primaryDelegation);
92  		}
93  		return primaryDelegation;
94  	}
95  
96  	protected String getDelegationId(){
97  		return getSequenceAccessorService().getNextAvailableSequenceNumber(KimConstants.SequenceNames.KRIM_DLGN_ID_S).toString();
98  	}
99  	
100 	protected RoleDocumentDelegation getSecondaryDelegation(){
101 		RoleDocumentDelegation secondaryDelegation = null;
102 		for(RoleDocumentDelegation delegation: getDelegations()){
103 			if(delegation.isDelegationSecondary()) {
104 				secondaryDelegation = delegation;
105             }
106 		}
107 		if(secondaryDelegation==null){
108 			secondaryDelegation = new RoleDocumentDelegation();
109 			secondaryDelegation.setDelegationId(getDelegationId());
110 			secondaryDelegation.setDelegationTypeCode(DelegationType.SECONDARY.getCode());
111 			getDelegations().add(secondaryDelegation);
112 		}
113 		return secondaryDelegation;
114 	}
115 
116 	/**
117 	 * @return the delegations
118 	 */
119 	public List<RoleDocumentDelegation> getDelegations() {
120 		return this.delegations;
121 	}
122 
123 	/**
124 	 * @param delegations the delegations to set
125 	 */
126 	public void setDelegations(List<RoleDocumentDelegation> delegations) {
127 		this.delegations = delegations;
128 	}
129 
130 	/**
131 	 * @return the delegationMembers
132 	 */
133 	public List<RoleDocumentDelegationMember> getDelegationMembers() {
134 		return this.delegationMembers;
135 	}
136 
137 	/**
138 	 * @param delegationMembers the delegationMembers to set
139 	 */
140 	public void setDelegationMembers(
141 			List<RoleDocumentDelegationMember> delegationMembers) {
142 		this.delegationMembers = delegationMembers;
143 	}
144 	
145 	protected SequenceAccessorService getSequenceAccessorService(){
146 		if(this.sequenceAccessorService==null){
147 	    	this.sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService();
148 		}
149 		return this.sequenceAccessorService;
150 	}
151 
152     public String getKimAttributeDefnId(KimAttributeField definition){
153    		return definition.getId();
154     }
155 
156 }