View Javadoc

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