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.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.kim.impl.role.RoleBo;
25  import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
26  import org.kuali.rice.krad.data.platform.MaxValueIncrementerFactory;
27  import org.kuali.rice.krad.document.TransactionalDocumentBase;
28  import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
29  import org.springframework.util.AutoPopulatingList;
30  
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  public class IdentityManagementKimDocument extends TransactionalDocumentBase {
53  	private static final long serialVersionUID = 1L;
54  
55      protected static final Logger LOG = Logger.getLogger(IdentityManagementKimDocument.class);
56  	
57  	@OneToMany(targetEntity=RoleDocumentDelegation.class, fetch=FetchType.EAGER, cascade={CascadeType.ALL})
58      @JoinColumn(name="FDOC_NBR",insertable=false,updatable=false)
59  	protected List<RoleDocumentDelegation> delegations = new AutoPopulatingList<RoleDocumentDelegation>(RoleDocumentDelegation.class);
60  	@Transient
61  	protected List<RoleDocumentDelegationMember> delegationMembers = new AutoPopulatingList<RoleDocumentDelegationMember>(RoleDocumentDelegationMember.class);
62  	
63  	protected void addDelegationMemberToDelegation(RoleDocumentDelegationMember delegationMember){
64  		// the statement below will lazily load the RoleBo onto our delegation member so that we have access to i
65  		delegationMember.loadTransientRoleFields();
66  		// now that we've done that we can fetch the RoleBo
67  		RoleBo role = delegationMember.getRoleBo();
68  		RoleDocumentDelegation delegation;
69  		if (DelegationType.PRIMARY.getCode().equals(delegationMember.getDelegationTypeCode())) {
70  			delegation = getPrimaryDelegation(role);
71  			// if the delegation type was changed on the document, we need to make sure we remove the member from the secondary delegation
72  			getSecondaryDelegation(role).getMembers().remove(delegationMember);
73  		} else {
74  			delegation = getSecondaryDelegation(role);
75  			// if the delegation type was changed on the document, we need to make sure we remove the member from the primary delegation
76  			getPrimaryDelegation(role).getMembers().remove(delegationMember);
77  		}
78  		delegationMember.setDelegationId(delegation.getDelegationId());
79  		if (!delegation.getMembers().contains(delegationMember)) {
80  			delegation.getMembers().add(delegationMember);
81  		}
82  	}
83  
84  	protected RoleDocumentDelegation getPrimaryDelegation(RoleBo role) {
85  		RoleDocumentDelegation primaryDelegation = null;
86  		for(RoleDocumentDelegation delegation: getDelegations()){
87  			if(role.getId().equals(delegation.getRoleId()) && delegation.isDelegationPrimary()) {
88  				primaryDelegation = delegation;
89  				break;
90              }
91  		}
92  		if(primaryDelegation == null) {
93  			primaryDelegation = new RoleDocumentDelegation();
94  			primaryDelegation.setRoleId(role.getId());
95  			primaryDelegation.setKimTypeId(role.getKimTypeId());
96  			primaryDelegation.setDelegationId(getDelegationId());
97  			primaryDelegation.setDelegationTypeCode(DelegationType.PRIMARY.getCode());
98              primaryDelegation.setDocumentNumber(getDocumentNumber());
99  			getDelegations().add(primaryDelegation);
100 		}
101 		return primaryDelegation;
102 	}
103 
104 	protected String getDelegationId(){
105         DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(KimImplServiceLocator.getDataSource(), KimConstants.SequenceNames.KRIM_DLGN_ID_S);
106         return incrementer.nextStringValue();
107 	}
108 	
109 	protected RoleDocumentDelegation getSecondaryDelegation(RoleBo role) {
110 		RoleDocumentDelegation secondaryDelegation = null;
111 		for (RoleDocumentDelegation delegation: getDelegations()) {
112 			if (role.getId().equals(delegation.getRoleId()) && delegation.isDelegationSecondary()) {
113 				secondaryDelegation = delegation;
114 				break;
115             }
116 		}
117 		if(secondaryDelegation == null) {
118 			secondaryDelegation = new RoleDocumentDelegation();
119 			secondaryDelegation.setRoleId(role.getId());
120 			secondaryDelegation.setKimTypeId(role.getKimTypeId());
121 			secondaryDelegation.setDelegationId(getDelegationId());
122 			secondaryDelegation.setDelegationTypeCode(DelegationType.SECONDARY.getCode());
123             secondaryDelegation.setDocumentNumber(getDocumentNumber());
124 			getDelegations().add(secondaryDelegation);
125 		}
126 		return secondaryDelegation;
127 	}
128 
129 	public List<RoleDocumentDelegation> getDelegations() {
130 		return this.delegations;
131 	}
132 
133 	public void setDelegations(List<RoleDocumentDelegation> delegations) {
134 		this.delegations = delegations;
135 	}
136 
137 	public List<RoleDocumentDelegationMember> getDelegationMembers() {
138 		return this.delegationMembers;
139 	}
140 
141 	public void setDelegationMembers(
142 			List<RoleDocumentDelegationMember> delegationMembers) {
143 		this.delegationMembers = delegationMembers;
144 	}
145 
146     public String getKimAttributeDefnId(KimAttributeField definition){
147    		return definition.getId();
148     }
149 
150 }