View Javadoc
1   /**
2    * Copyright 2005-2014 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.bo.ui;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  import javax.persistence.CascadeType;
24  import javax.persistence.Column;
25  import javax.persistence.Entity;
26  import javax.persistence.Id;
27  import javax.persistence.JoinColumn;
28  import javax.persistence.JoinColumns;
29  import javax.persistence.OneToMany;
30  import javax.persistence.Table;
31  import javax.persistence.Transient;
32  
33  import org.apache.commons.lang.StringUtils;
34  import org.kuali.rice.core.api.membership.MemberType;
35  import org.kuali.rice.kim.api.KimConstants;
36  import org.kuali.rice.kim.api.group.Group;
37  import org.kuali.rice.kim.api.identity.principal.Principal;
38  import org.kuali.rice.kim.api.role.Role;
39  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
40  import org.kuali.rice.kim.impl.role.RoleMemberBo;
41  import org.kuali.rice.krad.data.KradDataServiceLocator;
42  import org.springframework.util.AutoPopulatingList;
43  
44  /**
45   * This is a description of what this class does - shyu don't forget to fill this in. 
46   * 
47   * @author Kuali Rice Team (rice.collab@kuali.org)
48   *
49   */
50  @Entity
51  @Table(name = "KRIM_PND_ROLE_MBR_MT")
52  public class KimDocumentRoleMember extends KimDocumentBoActivatableToFromEditableBase {
53  
54      private static final long serialVersionUID = -2463865643038170979L;
55  
56      @Id
57      @Column(name = "ROLE_MBR_ID")
58      protected String roleMemberId;
59  
60      @Column(name = "ROLE_ID")
61      protected String roleId;
62  
63      @Column(name = "MBR_ID")
64      protected String memberId;
65  
66      //TODO: remove the default                       
67      @Column(name = "MBR_TYP_CD")
68      protected String memberTypeCode = KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.getCode();
69  
70      @Transient
71      protected String memberName;
72  
73      @Transient
74      protected String memberNamespaceCode;
75  
76      @Transient
77      protected String memberFullName;
78  
79      @OneToMany(targetEntity = KimDocumentRoleQualifier.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
80      @JoinColumns({ 
81          @JoinColumn(name = "FDOC_NBR", referencedColumnName = "FDOC_NBR", insertable = false, updatable = false), 
82          @JoinColumn(name = "ROLE_MBR_ID", referencedColumnName = "ROLE_MBR_ID", insertable = false, updatable = false) })
83      protected List<KimDocumentRoleQualifier> qualifiers = new AutoPopulatingList<KimDocumentRoleQualifier>(KimDocumentRoleQualifier.class);
84  
85      @Transient
86      protected String qualifiersToDisplay;
87  
88      @OneToMany(targetEntity = KimDocumentRoleResponsibilityAction.class, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
89      @JoinColumns({ 
90          @JoinColumn(name = "FDOC_NBR", referencedColumnName = "FDOC_NBR", insertable = false, updatable = false), 
91          @JoinColumn(name = "ROLE_MBR_ID", referencedColumnName = "ROLE_MBR_ID", insertable = false, updatable = false) })
92      private List<KimDocumentRoleResponsibilityAction> roleRspActions;
93  
94      public KimDocumentRoleMember() {
95          qualifiers = new ArrayList<KimDocumentRoleQualifier>();
96          roleRspActions = new ArrayList<KimDocumentRoleResponsibilityAction>();
97      }
98  
99      public int getNumberOfQualifiers() {
100         return qualifiers == null ? 0 : qualifiers.size();
101     }
102 
103     /**
104 	 * @return the memberId
105 	 */
106     public String getMemberId() {
107         return this.memberId;
108     }
109 
110     /**
111 	 * @param memberId the memberId to set
112 	 */
113     public void setMemberId(String memberId) {
114         this.memberId = memberId;
115     }
116 
117     public String getRoleMemberId() {
118         return this.roleMemberId;
119     }
120 
121     public void setRoleMemberId(String roleMemberId) {
122         this.roleMemberId = roleMemberId;
123     }
124 
125     public String getRoleId() {
126         return this.roleId;
127     }
128 
129     public void setRoleId(String roleId) {
130         this.roleId = roleId;
131     }
132 
133     public KimDocumentRoleQualifier getQualifier(String kimAttributeDefnId) {
134         for (KimDocumentRoleQualifier qualifier : qualifiers) {
135             if (qualifier.getKimAttrDefnId().equals(kimAttributeDefnId)) {
136                 return qualifier;
137             }
138         }
139         return null;
140     }
141 
142     public List<KimDocumentRoleQualifier> getQualifiers() {
143         return this.qualifiers;
144     }
145 
146     public void setQualifiers(List<KimDocumentRoleQualifier> qualifiers) {
147         this.qualifiers = qualifiers;
148     }
149 
150     /**
151 	 * @return the memberTypeCode
152 	 */
153     public String getMemberTypeCode() {
154         return this.memberTypeCode;
155     }
156 
157     /**
158 	 * @param memberTypeCode the memberTypeCode to set
159 	 */
160     public void setMemberTypeCode(String memberTypeCode) {
161         this.memberTypeCode = memberTypeCode;
162     }
163 
164     /**
165 	 * @return the memberName
166 	 */
167     public String getMemberName() {
168         if (memberName == null) {
169             populateDerivedValues();
170         }
171         return memberName;
172     }
173 
174     /**
175 	 * @param memberName the memberName to set
176 	 */
177     public void setMemberName(String memberName) {
178         this.memberName = memberName;
179     }
180 
181     public List<KimDocumentRoleResponsibilityAction> getRoleRspActions() {
182         return this.roleRspActions;
183     }
184 
185     public void setRoleRspActions(List<KimDocumentRoleResponsibilityAction> roleRspActions) {
186         this.roleRspActions = roleRspActions;
187     }
188 
189     /**
190 	 * @return the memberNamespaceCode
191 	 */
192     public String getMemberNamespaceCode() {
193         if (memberNamespaceCode == null) {
194             populateDerivedValues();
195         }
196         return memberNamespaceCode;
197     }
198 
199     /**
200 	 * @param memberNamespaceCode the memberNamespaceCode to set
201 	 */
202     public void setMemberNamespaceCode(String memberNamespaceCode) {
203         this.memberNamespaceCode = memberNamespaceCode;
204     }
205 
206     protected void populateDerivedValues() {
207         if (!StringUtils.isEmpty(getMemberId())) {
208             if (MemberType.PRINCIPAL.getCode().equals(getMemberTypeCode())) {
209                 Principal principalInfo = KimApiServiceLocator.getIdentityService().getPrincipal(getMemberId());
210                 if (principalInfo != null) {
211                     setMemberName(principalInfo.getPrincipalName());
212                 }
213             } else if (MemberType.GROUP.getCode().equals(getMemberTypeCode())) {
214                 Group groupInfo = KimApiServiceLocator.getGroupService().getGroup(getMemberId());
215                 if (groupInfo != null) {
216                     setMemberName(groupInfo.getName());
217                     setMemberNamespaceCode(groupInfo.getNamespaceCode());
218                 }
219             } else if (MemberType.ROLE.getCode().equals(getMemberTypeCode())) {
220                 Role roleInfo = KimApiServiceLocator.getRoleService().getRole(getMemberId());
221                 if (roleInfo != null) {
222                     setMemberName(roleInfo.getName());
223                     setMemberNamespaceCode(roleInfo.getNamespaceCode());
224                 }
225             }
226         }
227     }
228 
229     public boolean isRole() {
230         return getMemberTypeCode() != null && getMemberTypeCode().equals(MemberType.ROLE.getCode());
231     }
232 
233     public boolean isGroup() {
234         return getMemberTypeCode() != null && getMemberTypeCode().equals(MemberType.GROUP.getCode());
235     }
236 
237     public boolean isPrincipal() {
238         return getMemberTypeCode() != null && getMemberTypeCode().equals(MemberType.PRINCIPAL.getCode());
239     }
240 
241     public Map<String, String> getQualifierAsMap() {
242         Map<String, String> m = new HashMap<String, String>();
243         for (KimDocumentRoleQualifier data : getQualifiers()) {
244             if (data.getKimAttribute() == null) {
245                 KradDataServiceLocator.getDataObjectService().wrap(this).fetchRelationship("kimAttribute");
246             }
247             if (data.getKimAttribute() != null) {
248                 m.put(data.getKimAttribute().getAttributeName(), data.getAttrVal());
249             }
250         }
251         return m;
252     }
253 
254     public String getQualifiersToDisplay() {
255         return this.qualifiersToDisplay;
256     }
257 
258     public void setQualifiersToDisplay(String qualifiersToDisplay) {
259         this.qualifiersToDisplay = qualifiersToDisplay;
260     }
261 
262     public String getMemberFullName() {
263         return this.memberFullName;
264     }
265 
266 	public void setMemberFullName(String memberFullName) {
267 		this.memberFullName = memberFullName;
268 	}
269 
270     public boolean isMemberNameNull() {
271         if (memberName==null) {
272             return true;
273         }
274         return false;
275     }
276 
277     public boolean isMemberNameSpaceCodeNull() {
278         if (memberNamespaceCode==null) {
279             return true;
280         }
281         return false;
282     }
283 
284     public static void copyProperties(KimDocumentRoleMember copyToKimDocRoleMember, RoleMemberBo copyFromRoleMbrBo){
285         if(copyToKimDocRoleMember!=null && copyFromRoleMbrBo!=null) {
286             copyToKimDocRoleMember.setRoleId(copyFromRoleMbrBo.getRoleId());
287             copyToKimDocRoleMember.setMemberId(copyFromRoleMbrBo.getMemberId());
288             copyToKimDocRoleMember.setMemberName(copyFromRoleMbrBo.getMemberName());
289             copyToKimDocRoleMember.setMemberNamespaceCode(copyFromRoleMbrBo.getMemberNamespaceCode());
290             copyToKimDocRoleMember.setActive(copyFromRoleMbrBo.isActive());
291             copyToKimDocRoleMember.setActiveFromDate(copyFromRoleMbrBo.getActiveFromDateValue());
292             copyToKimDocRoleMember.setActiveToDate(copyFromRoleMbrBo.getActiveToDateValue());
293             copyToKimDocRoleMember.setVersionNumber(copyFromRoleMbrBo.getVersionNumber());
294             copyToKimDocRoleMember.setObjectId(copyFromRoleMbrBo.getObjectId());
295         }
296     }
297 }