View Javadoc

1   /**
2    * Copyright 2005-2012 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.commons.lang.StringUtils;
19  import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
20  import org.kuali.rice.kim.api.KimConstants;
21  import org.kuali.rice.kim.api.responsibility.ResponsibilityService;
22  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23  import org.kuali.rice.kim.api.type.KimAttributeField;
24  import org.kuali.rice.kim.api.type.KimType;
25  import org.kuali.rice.kim.bo.ui.KimDocumentRoleMember;
26  import org.kuali.rice.kim.bo.ui.KimDocumentRolePermission;
27  import org.kuali.rice.kim.bo.ui.KimDocumentRoleQualifier;
28  import org.kuali.rice.kim.bo.ui.KimDocumentRoleResponsibility;
29  import org.kuali.rice.kim.bo.ui.KimDocumentRoleResponsibilityAction;
30  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegation;
31  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMember;
32  import org.kuali.rice.kim.bo.ui.RoleDocumentDelegationMemberQualifier;
33  import org.kuali.rice.kim.impl.responsibility.ResponsibilityInternalService;
34  import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
35  import org.kuali.rice.kim.impl.type.IdentityManagementTypeAttributeTransactionalDocument;
36  import org.kuali.rice.kim.service.KIMServiceLocatorInternal;
37  import org.kuali.rice.kim.web.struts.form.IdentityManagementRoleDocumentForm;
38  import org.kuali.rice.krad.service.SequenceAccessorService;
39  import org.springframework.util.AutoPopulatingList;
40  
41  import java.util.ArrayList;
42  import java.util.Comparator;
43  import java.util.List;
44  
45  
46  /**
47   * This is a description of what this class does - bhargavp don't forget to fill this in. 
48   * 
49   * @author Kuali Rice Team (rice.collab@kuali.org)
50   *
51   */
52  public class IdentityManagementRoleDocument extends IdentityManagementTypeAttributeTransactionalDocument {
53  
54  	private static final long serialVersionUID = 1L;
55  	// principal data
56  	protected String roleId;
57  	protected String roleTypeId;
58  	protected String roleTypeName;
59  	protected String roleNamespace = "";
60  	protected String roleName = "";
61  	protected String roleDescription = "";
62  
63  	protected boolean active = true;
64  
65  	protected boolean editing;
66  	
67  	protected List<KimDocumentRolePermission> permissions = new AutoPopulatingList(KimDocumentRolePermission.class);
68  	protected List<KimDocumentRoleResponsibility> responsibilities = new AutoPopulatingList(KimDocumentRoleResponsibility.class);
69  	protected List<KimDocumentRoleMember> members = new AutoPopulatingList(KimDocumentRoleMember.class);
70  	private List<RoleDocumentDelegationMember> delegationMembers = new AutoPopulatingList(RoleDocumentDelegationMember.class);
71  	private List<RoleDocumentDelegation> delegations = new AutoPopulatingList(RoleDocumentDelegation.class);
72  	
73  	transient private ResponsibilityService responsibilityService;
74  	transient private ResponsibilityInternalService responsibilityInternalService;
75  
76  	public IdentityManagementRoleDocument() {
77  	}
78  
79  	/**
80  	 * @return the active
81  	 */
82  	public boolean isActive() {
83  		return this.active;
84  	}
85  
86  	/**
87  	 * @param active the active to set
88  	 */
89  	public void setActive(boolean active) {
90  		this.active = active;
91  	}
92  
93  	/**
94  	 * @param roleId the roleId to set
95  	 */
96  	public void setRoleId(String roleId) {
97  		this.roleId = roleId;
98  	}
99  
100 	/**
101 	 * @return the roleName
102 	 */
103 	public String getRoleName() {
104 		return this.roleName;
105 	}
106 
107 	/**
108 	 * @param roleName the roleName to set
109 	 */
110 	public void setRoleName(String roleName) {
111 		this.roleName = roleName;
112 	}
113 
114 	/**
115 	 * @return the roleDescription
116 	 */
117 	public String getRoleDescription() {
118 		return this.roleDescription;
119 	}
120 
121 	/**
122 	 * @param roleDescription the roleDescription to set
123 	 */
124 	public void setRoleDescription(String roleDescription) {
125 		this.roleDescription = roleDescription;
126 	}
127 
128 	/**
129 	 * @return the roleNamespace
130 	 */
131 	public String getRoleNamespace() {
132 		return this.roleNamespace;
133 	}
134 
135 	/**
136 	 * @param roleNamespace the roleNamespace to set
137 	 */
138 	public void setRoleNamespace(String roleNamespace) {
139 		this.roleNamespace = roleNamespace;
140 	}
141 
142 	/**
143 	 * @return the roleTypeId
144 	 */
145 	public String getRoleTypeId() {
146 		return this.roleTypeId;
147 	}
148 
149 	/**
150 	 * @param roleTypeId the roleTypeId to set
151 	 */
152 	public void setRoleTypeId(String roleTypeId) {
153 		this.roleTypeId = roleTypeId;
154 	}
155 
156 	/**
157 	 * @return the roleTypeName
158 	 */
159 	public String getRoleTypeName() {
160 		if ( roleTypeName == null ) {
161 			if ( kimType != null ) {
162 				roleTypeName = kimType.getName();
163 			} else if ( roleTypeId != null ) {
164 				setKimType( KimApiServiceLocator.getKimTypeInfoService().getKimType(roleTypeId) );
165 		        if ( kimType != null ) {
166 		        	roleTypeName = kimType.getName();
167 		        }
168 			}
169 		}
170 		return this.roleTypeName;
171 	}
172 
173 	/**
174 	 * @param roleTypeName the roleTypeName to set
175 	 */
176 	public void setRoleTypeName(String roleTypeName) {
177 		this.roleTypeName = roleTypeName;
178 	}
179 
180 	/**
181 	 * @return the delegationMembers
182 	 */
183 	public List<RoleDocumentDelegationMember> getDelegationMembers() {
184 		return this.delegationMembers;
185 	}
186 
187 	/**
188 	 * @param delegationMembers the delegationMembers to set
189 	 */
190 	public void setDelegationMembers(
191 			List<RoleDocumentDelegationMember> delegationMembers) {
192 		this.delegationMembers = delegationMembers;
193 	}
194 
195 	/**
196 	 * @return the permissions
197 	 */
198 	public List<KimDocumentRolePermission> getPermissions() {
199 		return this.permissions;
200 	}
201 
202 	/**
203 	 * @param permissions the permissions to set
204 	 */
205 	public void setPermissions(List<KimDocumentRolePermission> permissions) {
206 		this.permissions = permissions;
207 	}
208 
209 	/**
210 	 * @return the responsibilities
211 	 */
212 	public List<KimDocumentRoleResponsibility> getResponsibilities() {
213 		return this.responsibilities;
214 	}
215 
216 	/**
217 	 * @param responsibilities the responsibilities to set
218 	 */
219 	public void setResponsibilities(
220 			List<KimDocumentRoleResponsibility> responsibilities) {
221 		this.responsibilities = responsibilities;
222 	}
223 
224 	/**
225 	 * @return the members
226 	 */
227 	public List<KimDocumentRoleMember> getMembers() {
228 		return this.members;
229 	}
230 
231     public enum RoleMemberMetaDataType implements Comparator<KimDocumentRoleMember>{
232         MEMBER_ID("memberId"),
233         MEMBER_NAME("memberName"),
234         FULL_MEMBER_NAME("memberFullName");
235 
236         private final String attributeName;
237 
238         RoleMemberMetaDataType(String anAttributeName) {
239             this.attributeName = anAttributeName;
240         }
241 
242         public String getAttributeName() {
243             return attributeName;
244         }
245 
246         @Override
247         public int compare(KimDocumentRoleMember m1, KimDocumentRoleMember m2) {
248             if (m1 == null && m2 == null) {
249                 return 0;
250             } else if (m1 == null) {
251                 return -1;
252             } else if (m2 == null) {
253                 return 1;
254             }
255             if (this.getAttributeName().equals(MEMBER_ID.getAttributeName())) {
256                 return m1.getMemberId().compareToIgnoreCase(m2.getMemberId());
257             }
258             else if (this.getAttributeName().equals(FULL_MEMBER_NAME.getAttributeName())) {
259                 return m1.getMemberFullName().compareToIgnoreCase(m2.getMemberFullName());
260             }
261             return m1.getMemberName().compareToIgnoreCase(m2.getMemberName());
262         }
263     }
264 
265     public void setMemberMetaDataTypeToSort(Integer columnNumber)
266         {
267            switch( columnNumber )
268            {
269                case 1:
270                    this.memberMetaDataType = RoleMemberMetaDataType.MEMBER_ID;
271                    break;
272                case 2:
273                    this.memberMetaDataType = RoleMemberMetaDataType.MEMBER_NAME;
274                    break;
275                case 3:
276                    this.memberMetaDataType = RoleMemberMetaDataType.FULL_MEMBER_NAME;
277                    break;
278                default:
279                    this.memberMetaDataType = RoleMemberMetaDataType.MEMBER_NAME;
280                    break;
281            }
282         }
283 
284     protected RoleMemberMetaDataType memberMetaDataType = RoleMemberMetaDataType.MEMBER_NAME;
285 
286     public RoleMemberMetaDataType getMemberMetaDataType() {
287         return memberMetaDataType;
288     }
289 
290     public void setMemberMetaDataType(RoleMemberMetaDataType memberMetaDataType) {
291         this.memberMetaDataType = memberMetaDataType;
292     }
293 
294 	/**
295 	 * @return the members
296 	 */
297 	public KimDocumentRoleMember getMember(String roleMemberId) {
298 		if(StringUtils.isEmpty(roleMemberId)) {return null;}
299 		for(KimDocumentRoleMember roleMember: getMembers()){
300 			if(roleMemberId.equals(roleMember.getRoleMemberId())) {
301 				return roleMember;
302             }
303 		}
304 		return null;
305 	}
306 
307 	/**
308 	 * @param members the members to set
309 	 */
310 	public void setMembers(List<KimDocumentRoleMember> members) {
311 		this.members = members;
312 	}
313 
314 	public void addResponsibility(KimDocumentRoleResponsibility roleResponsibility){
315 		if(!getResponsibilityInternalService().areActionsAtAssignmentLevelById(roleResponsibility.getResponsibilityId())) {
316 			roleResponsibility.getRoleRspActions().add(getNewRespAction(roleResponsibility));
317 		}
318        	getResponsibilities().add(roleResponsibility);
319 	}
320 
321 	protected KimDocumentRoleResponsibilityAction getNewRespAction(KimDocumentRoleResponsibility roleResponsibility){
322 		KimDocumentRoleResponsibilityAction roleRspAction = new KimDocumentRoleResponsibilityAction();
323 		roleRspAction.setKimResponsibility(roleResponsibility.getKimResponsibility());
324 		roleRspAction.setRoleResponsibilityId(roleResponsibility.getRoleResponsibilityId());        		
325 		return roleRspAction;
326 	}
327 	
328 	public void addDelegationMember(RoleDocumentDelegationMember newDelegationMember){
329 		getDelegationMembers().add(newDelegationMember);
330 	}
331 
332 	/**
333 	 * @param member the members to set
334 	 */
335 	public void addMember(KimDocumentRoleMember member) {
336 		SequenceAccessorService sas = getSequenceAccessorService();
337 		Long nextSeq = sas.getNextAvailableSequenceNumber(
338 				KimConstants.SequenceNames.KRIM_ROLE_MBR_ID_S, 
339 				KimDocumentRoleMember.class);
340 		String roleMemberId = nextSeq.toString();
341 		member.setRoleMemberId(roleMemberId);
342 		setupMemberRspActions(member);
343        	getMembers().add(member);
344 	}
345 
346 	public KimDocumentRoleMember getBlankMember() {
347 		KimDocumentRoleMember member = new KimDocumentRoleMember();
348 		KimDocumentRoleQualifier qualifier;
349 		if(getDefinitions()!=null){
350 			for(KimAttributeField key : getDefinitions()) {
351 	        	qualifier = new KimDocumentRoleQualifier();
352 	        	qualifier.setKimAttrDefnId(getKimAttributeDefnId(key));
353 	        	member.getQualifiers().add(qualifier);
354 	        }
355 		}
356        	setupMemberRspActions(member);
357        	return member;
358 	}
359 
360 	public RoleDocumentDelegationMember getBlankDelegationMember() {
361 		RoleDocumentDelegationMember member = new RoleDocumentDelegationMember();
362 		RoleDocumentDelegationMemberQualifier qualifier;
363 		if(getDefinitions()!=null){
364 			for(KimAttributeField key : getDefinitions()) {
365 				qualifier = new RoleDocumentDelegationMemberQualifier();
366 				setAttrDefnIdForDelMemberQualifier(qualifier, key);
367 	        	member.getQualifiers().add(qualifier);
368 	        }
369 		}
370        	return member;
371 	}
372 
373     public void setupMemberRspActions(KimDocumentRoleMember member) {
374     	member.getRoleRspActions().clear();
375         for (KimDocumentRoleResponsibility roleResp: getResponsibilities()) {
376         	if (getResponsibilityInternalService().areActionsAtAssignmentLevelById(roleResp.getResponsibilityId())) {
377         		KimDocumentRoleResponsibilityAction action = new KimDocumentRoleResponsibilityAction();
378         		action.setRoleResponsibilityId("*");
379         		action.setRoleMemberId(member.getRoleMemberId());
380         		member.getRoleRspActions().add(action);
381         		break;
382         	}        	
383         }
384     }
385 
386     public void updateMembers(IdentityManagementRoleDocumentForm roleDocumentForm){
387     	for(KimDocumentRoleMember member: roleDocumentForm.getRoleDocument().getMembers()){
388     		roleDocumentForm.getRoleDocument().setupMemberRspActions(member);
389     	}
390     }
391     
392     public void updateMembers(KimDocumentRoleResponsibility newResponsibility){
393     	for(KimDocumentRoleMember member: getMembers()){
394     		setupMemberRspActions(newResponsibility, member);
395     	}
396     }
397     
398     public void setupMemberRspActions(KimDocumentRoleResponsibility roleResp, KimDocumentRoleMember member) {
399     	if ((member.getRoleRspActions()==null || member.getRoleRspActions().size()<1) && getResponsibilityInternalService().areActionsAtAssignmentLevelById(roleResp.getResponsibilityId())) {
400     		KimDocumentRoleResponsibilityAction action = new KimDocumentRoleResponsibilityAction();
401     		action.setRoleResponsibilityId("*");
402     		action.setRoleMemberId(member.getRoleMemberId());
403     		if(member.getRoleRspActions()==null) {
404     			member.setRoleRspActions(new ArrayList<KimDocumentRoleResponsibilityAction>());
405             }
406     		member.getRoleRspActions().add(action);
407     	}        	
408     }
409     
410     protected void setAttrDefnIdForDelMemberQualifier(RoleDocumentDelegationMemberQualifier qualifier,KimAttributeField definition) {
411    		qualifier.setKimAttrDefnId(definition.getId());
412     }
413     
414     /**
415      * @see org.kuali.rice.krad.document.DocumentBase#doRouteStatusChange(org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange)
416      */
417     @Override
418 	public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
419 		super.doRouteStatusChange(statusChangeEvent);
420 		if (getDocumentHeader().getWorkflowDocument().isProcessed()) {
421 			KIMServiceLocatorInternal.getUiDocumentService().saveRole(this);
422 		}
423 	}
424 
425 	public void initializeDocumentForNewRole() {
426 		if(StringUtils.isBlank(this.roleId)){
427 			SequenceAccessorService sas = getSequenceAccessorService();
428 			Long nextSeq = sas.getNextAvailableSequenceNumber(
429 					KimConstants.SequenceNames.KRIM_ROLE_ID_S, this.getClass());
430 			this.roleId = nextSeq.toString();
431 		}
432 		if(StringUtils.isBlank(this.roleTypeId)) {
433 			this.roleTypeId = "1";
434 		}
435 	}
436 	
437 	public String getRoleId(){
438 		if(StringUtils.isBlank(this.roleId)){
439 			initializeDocumentForNewRole();
440 		}
441 		return roleId;
442 	}
443 	
444 	@Override
445 	public void prepareForSave(){
446 		SequenceAccessorService sas = getSequenceAccessorService();
447 		
448 		String roleId;
449 		if(StringUtils.isBlank(getRoleId())){
450 			Long nextSeq = sas.getNextAvailableSequenceNumber(
451 					KimConstants.SequenceNames.KRIM_ROLE_ID_S, this.getClass()); 
452 			roleId = nextSeq.toString();
453 			setRoleId(roleId);
454 		} else {
455 			roleId = getRoleId();
456         }
457 
458 		if(getPermissions()!=null){
459 			String rolePermissionId;
460 			for(KimDocumentRolePermission permission: getPermissions()){
461 				permission.setRoleId(roleId);
462 				if(StringUtils.isBlank(permission.getRolePermissionId())){
463 					Long nextSeq = sas.getNextAvailableSequenceNumber(
464 							KimConstants.SequenceNames.KRIM_ROLE_PERM_ID_S, 
465 							KimDocumentRolePermission.class);
466 					rolePermissionId = nextSeq.toString();
467 					permission.setRolePermissionId(rolePermissionId);
468 				}
469 			}
470 		}
471 		if(getResponsibilities()!=null){
472 			String roleResponsibilityId;
473 			for(KimDocumentRoleResponsibility responsibility: getResponsibilities()){
474 				if(StringUtils.isBlank(responsibility.getRoleResponsibilityId())){
475 					Long nextSeq = sas.getNextAvailableSequenceNumber(
476 							KimConstants.SequenceNames.KRIM_ROLE_RSP_ID_S, 
477 							KimDocumentRoleResponsibility.class);
478 					roleResponsibilityId = nextSeq.toString();
479 					responsibility.setRoleResponsibilityId(roleResponsibilityId);
480 				}
481 				responsibility.setRoleId(roleId);
482 				if(!getResponsibilityInternalService().areActionsAtAssignmentLevelById(responsibility.getResponsibilityId())){
483 					if(StringUtils.isBlank(responsibility.getRoleRspActions().get(0).getRoleResponsibilityActionId())){
484 						Long nextSeq = sas.getNextAvailableSequenceNumber(
485 								KimConstants.SequenceNames.KRIM_ROLE_RSP_ACTN_ID_S,
486 								KimDocumentRoleResponsibilityAction.class);
487 						String roleResponsibilityActionId = nextSeq.toString();
488 						responsibility.getRoleRspActions().get(0).setRoleResponsibilityActionId(roleResponsibilityActionId);
489 					}
490 					responsibility.getRoleRspActions().get(0).setRoleMemberId("*");
491 					responsibility.getRoleRspActions().get(0).setDocumentNumber(getDocumentNumber());
492 				}
493 			}
494 		}
495 		if(getMembers()!=null){
496 			String roleMemberId;
497 			String roleResponsibilityActionId;
498 			for(KimDocumentRoleMember member: getMembers()){
499 				member.setRoleId(roleId);
500 				if(StringUtils.isBlank(member.getRoleMemberId())){
501 					Long nextSeq = sas.getNextAvailableSequenceNumber(
502 							KimConstants.SequenceNames.KRIM_ROLE_MBR_ID_S,
503 							KimDocumentRoleMember.class);
504 					roleMemberId = nextSeq.toString();
505 					member.setRoleMemberId(roleMemberId);
506 				}
507 				for(KimDocumentRoleQualifier qualifier: member.getQualifiers()){
508 					qualifier.setKimTypId(getKimType().getId());
509 				}
510 				for(KimDocumentRoleResponsibilityAction roleRespAction: member.getRoleRspActions()){
511 					if(StringUtils.isBlank(roleRespAction.getRoleResponsibilityActionId())){
512 						Long nextSeq = sas.getNextAvailableSequenceNumber(
513 								KimConstants.SequenceNames.KRIM_ROLE_RSP_ACTN_ID_S,
514 								KimDocumentRoleResponsibilityAction.class);
515 						roleResponsibilityActionId = nextSeq.toString();
516 						roleRespAction.setRoleResponsibilityActionId(roleResponsibilityActionId);
517 					}
518 					roleRespAction.setRoleMemberId(member.getRoleMemberId());
519 					roleRespAction.setDocumentNumber(getDocumentNumber());
520 					if ( !StringUtils.equals( roleRespAction.getRoleResponsibilityId(), "*" ) ) {
521 						for(KimDocumentRoleResponsibility responsibility: getResponsibilities()){
522 							if( StringUtils.equals( roleRespAction.getKimResponsibility().getId(), responsibility.getResponsibilityId() ) ) {
523 								roleRespAction.setRoleResponsibilityId(responsibility.getRoleResponsibilityId());
524 							}
525 						}
526 					}
527 				}
528 			}
529 		}
530 		if(getDelegationMembers()!=null){
531 			for(RoleDocumentDelegationMember delegationMember: getDelegationMembers()){
532 				delegationMember.setDocumentNumber(getDocumentNumber());
533 				addDelegationMemberToDelegation(delegationMember);
534 			}
535 			for(RoleDocumentDelegation delegation: getDelegations()){
536 				delegation.setDocumentNumber(getDocumentNumber());
537 				delegation.setKimTypeId(getKimType().getId());
538 				for(RoleDocumentDelegationMember member: delegation.getMembers()){
539 					for(RoleDocumentDelegationMemberQualifier qualifier: member.getQualifiers()){
540 						qualifier.setKimTypId(getKimType().getId());
541 					}
542 				}
543 				delegation.setRoleId(roleId);
544 			}
545 		}
546 	}
547 	
548     public ResponsibilityService getResponsibilityService() {
549     	if ( responsibilityService == null ) {
550     		responsibilityService = KimApiServiceLocator.getResponsibilityService();
551     	}
552 		return responsibilityService;
553 	}
554 
555     public ResponsibilityInternalService getResponsibilityInternalService() {
556     	if ( responsibilityInternalService == null ) {
557     		responsibilityInternalService = KimImplServiceLocator.getResponsibilityInternalService();
558     	}
559 		return responsibilityInternalService;
560 	}
561 
562 	/**
563 	 * @return the editing
564 	 */
565 	public boolean isEditing() {
566 		return this.editing;
567 	}
568 
569 	/**
570 	 * @param editing the editing to set
571 	 */
572 	public void setEditing(boolean editing) {
573 		this.editing = editing;
574 	}
575 
576 	/**
577 	 * @return the delegations
578 	 */
579 	public List<RoleDocumentDelegation> getDelegations() {
580 		return this.delegations;
581 	}
582 
583 	/**
584 	 * @param delegations the delegations to set
585 	 */
586 	public void setDelegations(List<RoleDocumentDelegation> delegations) {
587 		this.delegations = delegations;
588 	}
589 	
590 	public void setKimType(KimType kimType) {
591 		super.setKimType(kimType);
592 		if (kimType != null){
593 			setRoleTypeId(kimType.getId());
594 			setRoleTypeName(kimType.getName());
595 		}
596 	}
597 }