1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
45
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
65 delegationMember.loadTransientRoleFields();
66
67 RoleBo role = delegationMember.getRoleBo();
68 RoleDocumentDelegation delegation;
69 if (DelegationType.PRIMARY.getCode().equals(delegationMember.getDelegationTypeCode())) {
70 delegation = getPrimaryDelegation(role);
71
72 getSecondaryDelegation(role).getMembers().remove(delegationMember);
73 } else {
74 delegation = getSecondaryDelegation(role);
75
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 }