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