1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.actionrequest;
17
18 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.kuali.rice.core.api.delegation.DelegationType;
22 import org.kuali.rice.core.api.exception.RiceRuntimeException;
23 import org.kuali.rice.core.api.membership.MemberType;
24 import org.kuali.rice.coreservice.framework.CoreFrameworkServiceLocator;
25 import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
26 import org.kuali.rice.kew.api.KewApiConstants;
27 import org.kuali.rice.kew.api.WorkflowRuntimeException;
28 import org.kuali.rice.kew.api.action.ActionRequestPolicy;
29 import org.kuali.rice.kew.api.action.ActionRequestStatus;
30 import org.kuali.rice.kew.api.action.RecipientType;
31 import org.kuali.rice.kew.api.identity.Id;
32 import org.kuali.rice.kew.api.user.UserId;
33 import org.kuali.rice.kew.api.util.CodeTranslator;
34 import org.kuali.rice.kew.engine.RouteContext;
35 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
36 import org.kuali.rice.kew.identity.service.IdentityHelperService;
37 import org.kuali.rice.kew.role.KimRoleRecipient;
38 import org.kuali.rice.kew.role.KimRoleResponsibilityRecipient;
39 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
40 import org.kuali.rice.kew.rule.ResolvedQualifiedRole;
41 import org.kuali.rice.kew.service.KEWServiceLocator;
42 import org.kuali.rice.kew.user.RoleRecipient;
43 import org.kuali.rice.kew.util.Utilities;
44 import org.kuali.rice.kew.workgroup.GroupId;
45 import org.kuali.rice.kim.api.common.delegate.DelegateMember;
46 import org.kuali.rice.kim.api.common.delegate.DelegateType;
47 import org.kuali.rice.kim.api.group.Group;
48 import org.kuali.rice.kim.api.group.GroupService;
49 import org.kuali.rice.kim.api.identity.IdentityService;
50 import org.kuali.rice.kim.api.identity.principal.Principal;
51 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
52 import org.kuali.rice.kim.api.responsibility.ResponsibilityAction;
53 import org.kuali.rice.kim.api.role.Role;
54 import org.kuali.rice.kim.api.role.RoleMembership;
55 import org.kuali.rice.kim.api.role.RoleService;
56 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
57 import org.kuali.rice.krad.util.KRADConstants;
58 import org.kuali.rice.krad.util.KRADUtils;
59
60 import java.sql.Timestamp;
61 import java.util.ArrayList;
62 import java.util.Collection;
63 import java.util.HashSet;
64 import java.util.Iterator;
65 import java.util.List;
66 import java.util.Map;
67 import java.util.Set;
68
69
70
71
72
73
74
75 public class ActionRequestFactory {
76
77 private static final Logger LOG = Logger.getLogger(ActionRequestFactory.class);
78
79 private static RoleService roleService;
80 private static IdentityHelperService identityHelperService;
81 private static IdentityService identityService;
82 private static GroupService groupService;
83 private static ActionRequestService actionRequestService;
84
85 private DocumentRouteHeaderValue document;
86 private RouteNodeInstance routeNode;
87 private List<ActionRequestValue> requestGraphs = new ArrayList<ActionRequestValue>();
88
89 public ActionRequestFactory() {
90 }
91
92 public ActionRequestFactory(DocumentRouteHeaderValue document) {
93 this.document = document;
94 }
95
96 public ActionRequestFactory(DocumentRouteHeaderValue document, RouteNodeInstance routeNode) {
97 this.document = document;
98 this.routeNode = routeNode;
99 }
100
101 public ActionRequestFactory(RouteContext routeContext) {
102 this(routeContext.getDocument(), routeContext.getNodeInstance());
103 }
104
105
106
107
108
109
110
111
112
113
114
115 public ActionRequestValue createActionRequest(String actionRequested, Recipient recipient, String description, Boolean forceAction, String annotation) {
116 return createActionRequest(actionRequested, new Integer(0), recipient, description, KewApiConstants.MACHINE_GENERATED_RESPONSIBILITY_ID, forceAction, annotation);
117 }
118
119 public ActionRequestValue createActionRequest(String actionRequested, Integer priority, Recipient recipient, String description, String responsibilityId, Boolean forceAction, String annotation) {
120 return createActionRequest(actionRequested, priority, recipient, description, responsibilityId, forceAction, null, null, annotation);
121 }
122
123 public ActionRequestValue createActionRequest(String actionRequested, Integer priority, Recipient recipient, String description, String responsibilityId, Boolean forceAction, String approvePolicy, String ruleId, String annotation) {
124 return createActionRequest(actionRequested, priority, recipient, description, responsibilityId, forceAction, approvePolicy, ruleId, annotation, null);
125 }
126
127 public ActionRequestValue createActionRequest(String actionRequested, Integer priority, Recipient recipient, String description, String responsibilityId, Boolean forceAction, String approvePolicy, String ruleId, String annotation, String requestLabel) {
128 ActionRequestValue actionRequest = new ActionRequestValue();
129 actionRequest.setActionRequested(actionRequested);
130 actionRequest.setDocVersion(document.getDocVersion());
131 actionRequest.setPriority(priority);
132 actionRequest.setRouteHeader(document);
133 actionRequest.setDocumentId(document.getDocumentId());
134 actionRequest.setRouteLevel(document.getDocRouteLevel());
135 actionRequest.setNodeInstance(routeNode);
136 actionRequest.setResponsibilityId(responsibilityId);
137 actionRequest.setResponsibilityDesc(description);
138 actionRequest.setApprovePolicy(approvePolicy);
139 actionRequest.setForceAction(forceAction);
140 actionRequest.setRuleBaseValuesId(ruleId);
141 actionRequest.setAnnotation(annotation);
142 actionRequest.setRequestLabel(requestLabel);
143 setDefaultProperties(actionRequest);
144 resolveRecipient(actionRequest, recipient);
145
146 return actionRequest;
147 }
148
149 public ActionRequestValue createBlankActionRequest() {
150 ActionRequestValue request = new ActionRequestValue();
151 request.setRouteHeader(document);
152 if (document != null) {
153 request.setDocumentId(document.getDocumentId());
154 }
155 request.setNodeInstance(routeNode);
156 return request;
157 }
158
159
160 public ActionRequestValue createNotificationRequest(String actionRequestCode, PrincipalContract principal, String reasonActionCode, PrincipalContract reasonActionUser, String responsibilityDesc) {
161 ActionRequestValue request = createActionRequest(actionRequestCode, new KimPrincipalRecipient(principal), responsibilityDesc, Boolean.TRUE, null);
162 String annotation = generateNotificationAnnotation(reasonActionUser, actionRequestCode, reasonActionCode, request);
163 request.setAnnotation(annotation);
164 return request;
165 }
166
167
168 public List<ActionRequestValue> generateNotifications(List requests, PrincipalContract principal, Recipient delegator,
169 String notificationRequestCode, String actionTakenCode)
170 {
171 String groupName = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KewApiConstants.KEW_NAMESPACE,
172 KRADConstants.DetailTypes.WORKGROUP_DETAIL_TYPE,
173 KewApiConstants.NOTIFICATION_EXCLUDED_USERS_WORKGROUP_NAME_IND);
174
175
176 Group notifyExclusionWorkgroup = null;
177 if(!StringUtils.isBlank(groupName)){
178 notifyExclusionWorkgroup = getGroupService().getGroupByNamespaceCodeAndName(
179 Utilities.parseGroupNamespaceCode(groupName), Utilities.parseGroupName(groupName));
180 }
181
182
183
184 return generateNotifications(null, getActionRequestService().getRootRequests(requests), principal, delegator, notificationRequestCode, actionTakenCode, notifyExclusionWorkgroup);
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 private List<ActionRequestValue> generateNotifications(ActionRequestValue parentRequest,
202 List<ActionRequestValue> requests, PrincipalContract principal, Recipient delegator, String notificationRequestCode,
203 String actionTakenCode, Group notifyExclusionWorkgroup)
204 {
205 List<ActionRequestValue> notificationRequests = new ArrayList<ActionRequestValue>();
206 for (ActionRequestValue actionRequest : requests) {
207 if (!(actionRequest.isRecipientRoutedRequest(principal.getPrincipalId()) || actionRequest.isRecipientRoutedRequest(delegator))) {
208
209 if ((notifyExclusionWorkgroup != null) &&
210 (isRecipientInGroup(notifyExclusionWorkgroup, actionRequest.getRecipient()))) {
211 continue;
212 }
213 ActionRequestValue notificationRequest = createNotificationRequest(actionRequest, principal, notificationRequestCode, actionTakenCode);
214 if (parentRequest == null) {
215
216
217 notificationRequests.add(notificationRequest);
218 } else {
219 notificationRequest.setParentActionRequest(parentRequest);
220 parentRequest.getChildrenRequests().add(notificationRequest);
221 }
222 generateNotifications(notificationRequest, actionRequest.getChildrenRequests(), principal, delegator, notificationRequestCode, actionTakenCode, notifyExclusionWorkgroup);
223 }
224 }
225 return notificationRequests;
226 }
227
228 private boolean isRecipientInGroup(Group group, Recipient recipient)
229 {
230 boolean isMember = false;
231
232 if(recipient instanceof KimPrincipalRecipient)
233 {
234 String principalId = ((KimPrincipalRecipient) recipient).getPrincipalId();
235 String groupId = group.getId();
236 isMember = getGroupService().isMemberOfGroup(principalId, groupId);
237 }
238 else if (recipient instanceof KimGroupRecipient)
239 {
240 String kimRecipientId = ((KimGroupRecipient) recipient).getGroup().getId();
241 isMember = getGroupService().isGroupMemberOfGroup(kimRecipientId, group.getId() );
242 }
243 return isMember;
244 }
245
246 private ActionRequestValue createNotificationRequest(ActionRequestValue actionRequest, PrincipalContract reasonPrincipal, String notificationRequestCode, String actionTakenCode) {
247
248 String annotation = generateNotificationAnnotation(reasonPrincipal, notificationRequestCode, actionTakenCode, actionRequest);
249 ActionRequestValue request = createActionRequest(notificationRequestCode, actionRequest.getPriority(), actionRequest.getRecipient(), actionRequest.getResponsibilityDesc(), KewApiConstants.MACHINE_GENERATED_RESPONSIBILITY_ID, Boolean.TRUE, annotation);
250
251 request.setDocVersion(actionRequest.getDocVersion());
252 request.setApprovePolicy(actionRequest.getApprovePolicy());
253 request.setRoleName(actionRequest.getRoleName());
254 request.setQualifiedRoleName(actionRequest.getQualifiedRoleName());
255 request.setQualifiedRoleNameLabel(actionRequest.getQualifiedRoleNameLabel());
256 request.setDelegationType(actionRequest.getDelegationType());
257 return request;
258 }
259
260 private void setDefaultProperties(ActionRequestValue actionRequest) {
261 if (actionRequest.getApprovePolicy() == null) {
262 actionRequest.setApprovePolicy(ActionRequestPolicy.FIRST.getCode());
263 }
264 actionRequest.setCreateDate(new Timestamp(System.currentTimeMillis()));
265 actionRequest.setCurrentIndicator(Boolean.TRUE);
266 if (actionRequest.getForceAction() == null) {
267 actionRequest.setForceAction(Boolean.FALSE);
268 }
269 if (routeNode != null) {
270 actionRequest.setNodeInstance(routeNode);
271 }
272 actionRequest.setJrfVerNbr(new Integer(0));
273 actionRequest.setStatus(ActionRequestStatus.INITIALIZED.getCode());
274 actionRequest.setRouteHeader(document);
275 actionRequest.setDocumentId(document.getDocumentId());
276 }
277
278 private static void resolveRecipient(ActionRequestValue actionRequest, Recipient recipient) {
279 if (recipient instanceof KimPrincipalRecipient) {
280 actionRequest.setRecipientTypeCd(RecipientType.PRINCIPAL.getCode());
281 actionRequest.setPrincipalId(((KimPrincipalRecipient)recipient).getPrincipal().getPrincipalId());
282 } else if (recipient instanceof KimGroupRecipient) {
283 KimGroupRecipient kimGroupRecipient = (KimGroupRecipient)recipient;
284 actionRequest.setRecipientTypeCd(RecipientType.GROUP.getCode());
285 actionRequest.setGroupId(kimGroupRecipient.getGroup().getId());
286 } else if (recipient instanceof RoleRecipient){
287 RoleRecipient role = (RoleRecipient)recipient;
288 actionRequest.setRecipientTypeCd(RecipientType.ROLE.getCode());
289 actionRequest.setRoleName(role.getRoleName());
290 actionRequest.setQualifiedRoleName(role.getQualifiedRoleName());
291 ResolvedQualifiedRole qualifiedRole = role.getResolvedQualifiedRole();
292 if (qualifiedRole != null) {
293 actionRequest.setAnnotation(qualifiedRole.getAnnotation() == null ? "" : qualifiedRole.getAnnotation());
294 actionRequest.setQualifiedRoleNameLabel(qualifiedRole.getQualifiedRoleLabel());
295 }
296 Recipient targetRecipient = role.getTarget();
297 if (role.getTarget() != null) {
298 if (targetRecipient instanceof RoleRecipient) {
299 throw new WorkflowRuntimeException("Role Cannot Target a role problem activating request for document " + actionRequest.getDocumentId());
300 }
301 resolveRecipient(actionRequest, role.getTarget());
302 }
303 } else if (recipient instanceof KimRoleResponsibilityRecipient) {
304 KimRoleResponsibilityRecipient roleResponsibilityRecipient = (KimRoleResponsibilityRecipient)recipient;
305 actionRequest.setRecipientTypeCd(RecipientType.ROLE.getCode());
306 actionRequest.setRoleName(roleResponsibilityRecipient.getResponsibilities().get(0).getRoleId());
307 actionRequest.setQualifiedRoleName(
308 roleResponsibilityRecipient.getResponsibilities().get(0).getResponsibilityName());
309
310
311 Recipient targetRecipient = roleResponsibilityRecipient.getTarget();
312 if (targetRecipient != null) {
313 if (targetRecipient instanceof RoleRecipient) {
314 throw new WorkflowRuntimeException("Role Cannot Target a role problem activating request for document " + actionRequest.getDocumentId());
315 }
316 resolveRecipient(actionRequest, roleResponsibilityRecipient.getTarget());
317 }
318 } else if (recipient instanceof KimRoleRecipient) {
319 KimRoleRecipient roleRecipient = (KimRoleRecipient)recipient;
320 actionRequest.setRecipientTypeCd(RecipientType.ROLE.getCode());
321 Role role = roleRecipient.getRole();
322 actionRequest.setRoleName(role.getId());
323 actionRequest.setQualifiedRoleNameLabel(role.getName());
324 Recipient targetRecipient = roleRecipient.getTarget();
325 if (targetRecipient != null) {
326 if (targetRecipient instanceof RoleRecipient) {
327 throw new WorkflowRuntimeException("Role Cannot Target a role problem activating request for document " + actionRequest.getDocumentId());
328 }
329 resolveRecipient(actionRequest, targetRecipient);
330 }
331 }
332 }
333
334
335
336
337
338
339
340
341
342
343
344
345
346 public ActionRequestValue addRoleRequest(RoleRecipient role, String actionRequested, String approvePolicy, Integer priority, String responsibilityId, Boolean forceAction, String description, String ruleId) {
347
348 ActionRequestValue requestGraph = createActionRequest(actionRequested, priority, role, description, responsibilityId, forceAction, approvePolicy, ruleId, null);
349 if (role != null && role.getResolvedQualifiedRole() != null && role.getResolvedQualifiedRole().getRecipients() != null) {
350 int legitimateTargets = 0;
351 for (Iterator<Id> iter = role.getResolvedQualifiedRole().getRecipients().iterator(); iter.hasNext();) {
352 Id recipientId = (Id) iter.next();
353 if (recipientId.isEmpty())
354 {
355 throw new WorkflowRuntimeException("Failed to resolve id of type " + recipientId.getClass().getName() + " returned from role '" + role.getRoleName() + "'. Id returned contained a null or empty value.");
356 }
357 if (recipientId instanceof UserId)
358 {
359 Principal principal = getIdentityHelperService().getPrincipal((UserId) recipientId);
360 if(KRADUtils.isNotNull(principal)) {
361 role.setTarget(new KimPrincipalRecipient(principal));
362 }
363 } else if (recipientId instanceof GroupId)
364 {
365 role.setTarget(new KimGroupRecipient(getIdentityHelperService().getGroup((GroupId) recipientId)));
366 } else
367 {
368 throw new WorkflowRuntimeException("Could not process the given type of id: " + recipientId.getClass());
369 }
370 if (role.getTarget() != null)
371 {
372 legitimateTargets++;
373 ActionRequestValue request = createActionRequest(actionRequested, priority, role, description, responsibilityId, forceAction, null, ruleId, null);
374 request.setParentActionRequest(requestGraph);
375 requestGraph.getChildrenRequests().add(request);
376 }
377 }
378 if (legitimateTargets == 0) {
379 LOG.warn("Role did not yield any legitimate recipients");
380 }
381 } else {
382 LOG.warn("Didn't create action requests for action request description '" + description + "' because of null role or null part of role object graph.");
383 }
384 requestGraphs.add(requestGraph);
385 return requestGraph;
386 }
387
388
389
390
391
392
393 public void addRoleResponsibilityRequest(List<ResponsibilityAction> responsibilities, String approvePolicy) {
394 if (responsibilities == null || responsibilities.isEmpty()) {
395 LOG.warn("Didn't create action requests for action request description because no responsibilities were defined.");
396 return;
397 }
398
399 String actionTypeCode = responsibilities.get(0).getActionTypeCode();
400 Integer priority = responsibilities.get(0).getPriorityNumber();
401 boolean forceAction = responsibilities.get(0).isForceAction();
402 KimRoleResponsibilityRecipient roleResponsibilityRecipient = new KimRoleResponsibilityRecipient(responsibilities);
403
404
405 ActionRequestValue requestGraph = null;
406 StringBuffer parentAnnotation = null;
407
408 Set<String> uniqueChildAnnotations = null;
409 if ( responsibilities.size() > 1 ) {
410 requestGraph = createActionRequest(
411 actionTypeCode,
412 priority, roleResponsibilityRecipient,
413 "",
414 KewApiConstants.MACHINE_GENERATED_RESPONSIBILITY_ID,
415 forceAction,
416 approvePolicy,
417 null,
418 null );
419 requestGraphs.add(requestGraph);
420 parentAnnotation = new StringBuffer();
421 uniqueChildAnnotations = new HashSet<String>( responsibilities.size() );
422 }
423 StringBuffer annotation = new StringBuffer();
424 for (ResponsibilityAction responsibility : responsibilities) {
425 if ( LOG.isDebugEnabled() ) {
426 LOG.debug( "Processing Responsibility for action request: " + responsibility );
427 }
428
429 annotation.setLength( 0 );
430 Role role = getRoleService().getRole(responsibility.getRoleId());
431 annotation.append( role.getNamespaceCode() ).append( ' ' ).append( role.getName() ).append( ' ' );
432 Map<String, String> qualifier = responsibility.getQualifier();
433 if ( qualifier != null ) {
434 for ( String key : qualifier.keySet() ) {
435 annotation.append( qualifier.get( key ) ).append( ' ' );
436 }
437 }
438 if (responsibility.getPrincipalId() != null) {
439 roleResponsibilityRecipient.setTarget(new KimPrincipalRecipient(responsibility.getPrincipalId()));
440 } else if (responsibility.getGroupId() != null) {
441 Group group = getGroupService().getGroup(responsibility.getGroupId());
442 if(group != null){
443 if(!group.isActive() && !document.getDocumentType().getFailOnInactiveGroup().getPolicyValue()){
444 roleResponsibilityRecipient.setTarget(null);
445 }else if(getGroupService().getMemberPrincipalIds(group.getId()).isEmpty()){
446 roleResponsibilityRecipient.setTarget(null);
447 }else {
448 roleResponsibilityRecipient.setTarget(new KimGroupRecipient(group));
449 }
450 } else {
451 throw new IllegalArgumentException("Attempted to create a KimGroupRecipient with a null Group!");
452 }
453 } else {
454 throw new RiceRuntimeException("Failed to identify a group or principal on the given ResponsibilityResolutionInfo:" + responsibility);
455 }
456 if(roleResponsibilityRecipient.getTarget() != null){
457 String annotationStr = annotation.toString();
458 ActionRequestValue request = createActionRequest(
459 responsibility.getActionTypeCode(),
460 responsibility.getPriorityNumber(), roleResponsibilityRecipient,
461 responsibility.getParallelRoutingGroupingCode(),
462 responsibility.getResponsibilityId(),
463 responsibility.isForceAction(),
464
465
466
467 (responsibilities.size() == 1)?ActionRequestPolicy.FIRST.getCode():approvePolicy,
468 null,
469 annotationStr);
470
471 if ( responsibilities.size() > 1 ) {
472 request.setParentActionRequest(requestGraph);
473 requestGraph.getChildrenRequests().add(request);
474 if ( !uniqueChildAnnotations.contains(annotationStr) ) {
475 parentAnnotation.append( annotationStr ).append( " -- " );
476 uniqueChildAnnotations.add(annotationStr);
477 }
478 } else {
479 requestGraphs.add(request);
480 }
481 generateKimRoleDelegationRequests(responsibility.getDelegates(), request);
482 }
483 }
484 if ( responsibilities.size() > 1 ) {
485 requestGraph.setAnnotation( StringUtils.chomp( parentAnnotation.toString(), " -- " ) );
486 }
487 }
488
489 private String generateRoleResponsibilityDelegateAnnotation(DelegateMember member, boolean isPrincipal, boolean isGroup, ActionRequestValue parentRequest) {
490 StringBuffer annotation = new StringBuffer( "Delegation of: " );
491 annotation.append( parentRequest.getAnnotation() );
492 annotation.append( " to " );
493 if (isPrincipal) {
494 annotation.append( "principal " );
495 Principal principal = getIdentityService().getPrincipal(member.getMemberId());
496 if ( principal != null ) {
497 annotation.append( principal.getPrincipalName() );
498 } else {
499 annotation.append( member.getMemberId() );
500 }
501 } else if (isGroup) {
502 annotation.append( "group " );
503 Group group = getGroupService().getGroup( member.getMemberId() );
504 if ( group != null ) {
505 annotation.append( group.getNamespaceCode() ).append( '/' ).append( group.getName() );
506 } else {
507 annotation.append( member.getMemberId() );
508 }
509 } else {
510 annotation.append( "?????? '" );
511 annotation.append( member.getMemberId() );
512 annotation.append( "'" );
513 }
514 return annotation.toString();
515 }
516
517 public ActionRequestValue addDelegationRoleRequest(ActionRequestValue parentRequest, String approvePolicy, RoleRecipient role, String responsibilityId, Boolean forceAction, DelegationType delegationType, String description, String ruleId) {
518 Recipient parentRecipient = parentRequest.getRecipient();
519 if (parentRecipient instanceof RoleRecipient) {
520 throw new WorkflowRuntimeException("Cannot delegate on Role Request. It must be a request to a person or workgroup, although that request may be in a role");
521 }
522 if (! relatedToRoot(parentRequest)) {
523 throw new WorkflowRuntimeException("The parent request is not related to any request managed by this factory");
524 }
525 ActionRequestValue delegationRoleRequest = createActionRequest(parentRequest.getActionRequested(), parentRequest.getPriority(), role, description, responsibilityId, forceAction, approvePolicy, ruleId, null);
526 delegationRoleRequest.setDelegationType(delegationType);
527 int count = 0;
528 for (Iterator<Id> iter = role.getResolvedQualifiedRole().getRecipients().iterator(); iter.hasNext(); count++) {
529
530 Id recipientId = iter.next();
531 if (recipientId.isEmpty()) {
532 throw new WorkflowRuntimeException("Failed to resolve id of type " + recipientId.getClass().getName() + " returned from role '" + role.getRoleName() + "'. Id returned contained a null or empty value.");
533 }
534 if (recipientId instanceof UserId) {
535 role.setTarget(new KimPrincipalRecipient(getIdentityHelperService().getPrincipal((UserId) recipientId)));
536 } else if (recipientId instanceof GroupId) {
537 role.setTarget(new KimGroupRecipient(getIdentityHelperService().getGroup((GroupId) recipientId)));
538 } else {
539 throw new WorkflowRuntimeException("Could not process the given type of id: " + recipientId.getClass());
540 }
541 ActionRequestValue request = createActionRequest(parentRequest.getActionRequested(), parentRequest.getPriority(), role, description, responsibilityId, forceAction, null, ruleId, null);
542 request.setDelegationType(delegationType);
543
544 request.setParentActionRequest(delegationRoleRequest);
545 delegationRoleRequest.getChildrenRequests().add(request);
546 }
547
548
549 if (count > 0) {
550 parentRequest.getChildrenRequests().add(delegationRoleRequest);
551 delegationRoleRequest.setParentActionRequest(parentRequest);
552 }
553
554 return delegationRoleRequest;
555 }
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571 public ActionRequestValue addDelegationRequest(ActionRequestValue parentRequest, Recipient recipient, String responsibilityId, Boolean forceAction, DelegationType delegationType, String annotation, String ruleId) {
572 if (! relatedToRoot(parentRequest)) {
573 throw new WorkflowRuntimeException("The parent request is not related to any request managed by this factory");
574 }
575 ActionRequestValue delegationRequest = createActionRequest(parentRequest.getActionRequested(), parentRequest.getPriority(), recipient, parentRequest.getResponsibilityDesc(), responsibilityId, forceAction, null, ruleId, annotation);
576 delegationRequest.setDelegationType(delegationType);
577
578 parentRequest.getChildrenRequests().add(delegationRequest);
579 delegationRequest.setParentActionRequest(parentRequest);
580
581 return delegationRequest;
582 }
583
584
585 public ActionRequestValue addRootActionRequest(String actionRequested, Integer priority, Recipient recipient, String description, String responsibilityId, Boolean forceAction, String approvePolicy, String ruleId) {
586 ActionRequestValue requestGraph = createActionRequest(actionRequested, priority, recipient, description, responsibilityId, forceAction, approvePolicy, ruleId, null);
587 requestGraphs.add(requestGraph);
588 return requestGraph;
589 }
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605 public ActionRequestValue addKimRoleRequest(String actionRequestedCode, Integer priority, Role role,
606 List<RoleMembership> memberships, String description, String responsibilityId, boolean forceAction,
607 String actionRequestPolicyCode, String requestLabel) {
608 return addKimRoleRequest(actionRequestedCode, priority, role, memberships, description, responsibilityId,
609 forceAction, actionRequestPolicyCode, requestLabel, false);
610 }
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626 public ActionRequestValue addKimRoleRequest(String actionRequestedCode, Integer priority, Role role,
627 List<RoleMembership> memberships, String description, String responsibilityId, boolean forceAction,
628 String actionRequestPolicyCode, String requestLabel, boolean ignoreKimDelegates) {
629
630 ActionRequestValue roleMemberRequest = null;
631
632 if (CollectionUtils.isEmpty(memberships)) {
633 LOG.warn("Didn't create action requests for action request description because no role members were defined for role id " + role.getId());
634 return roleMemberRequest;
635 }
636
637 KimRoleRecipient roleRecipient = new KimRoleRecipient(role);
638
639
640 ActionRequestValue requestGraph = null;
641 if ( memberships.size() > 1 ) {
642 requestGraph = createActionRequest(
643 actionRequestedCode,
644 priority,
645 roleRecipient,
646 "",
647 responsibilityId,
648 forceAction,
649 actionRequestPolicyCode,
650 null,
651 null );
652 requestGraphs.add(requestGraph);
653 }
654
655 for (RoleMembership membership : memberships) {
656 if ( LOG.isDebugEnabled() ) {
657 LOG.debug( "Processing RoleMembership for action request: " + membership );
658 }
659
660 if (MemberType.PRINCIPAL.equals(membership.getType())) {
661 roleRecipient.setTarget(new KimPrincipalRecipient(membership.getMemberId()));
662 } else if (MemberType.GROUP.equals(membership.getType())) {
663 roleRecipient.setTarget(new KimGroupRecipient(membership.getMemberId()));
664 } else {
665 throw new RiceRuntimeException("Failed to identify a group or principal on the given RoleMembership:" + membership);
666 }
667
668 ActionRequestValue request = createActionRequest(
669 actionRequestedCode,
670 priority,
671 roleRecipient,
672 "",
673 responsibilityId,
674 forceAction,
675
676
677
678 (memberships.size() == 1) ? ActionRequestPolicy.FIRST.getCode() : actionRequestPolicyCode,
679 null,
680 null);
681
682
683 if ( memberships.size() > 1 ) {
684 request.setParentActionRequest(requestGraph);
685 requestGraph.getChildrenRequests().add(request);
686
687 if (roleMemberRequest == null) {
688 roleMemberRequest = requestGraph;
689 }
690 } else {
691 roleMemberRequest = request;
692 requestGraphs.add(request);
693 }
694
695 if (!ignoreKimDelegates) {
696 generateKimRoleDelegationRequests(membership.getDelegates(), request);
697 }
698 }
699
700 return roleMemberRequest;
701 }
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721 public ActionRequestValue addDelegateKimRoleRequest(ActionRequestValue parentRequest, DelegationType delegationType,
722 String actionRequestedCode, Integer priority, Role role, List<RoleMembership> memberships,
723 String description, String responsibilityId, boolean forceAction, String actionRequestPolicyCode,
724 String requestLabel) {
725
726
727
728
729 ActionRequestValue delegateRoleRequest = null;
730
731 if (CollectionUtils.isEmpty(memberships)) {
732 LOG.warn("Didn't create action requests for action request description because no role members were defined for role id " + role.getId());
733 return delegateRoleRequest;
734 }
735
736 KimRoleRecipient roleRecipient = new KimRoleRecipient(role);
737
738
739 ActionRequestValue requestGraph = null;
740 if ( memberships.size() > 1 ) {
741 requestGraph = createActionRequest(
742 actionRequestedCode,
743 priority,
744 roleRecipient,
745 "",
746 responsibilityId,
747 forceAction,
748 actionRequestPolicyCode,
749 null,
750 null );
751 requestGraphs.add(requestGraph);
752 }
753
754 for (RoleMembership membership : memberships) {
755 if ( LOG.isDebugEnabled() ) {
756 LOG.debug( "Processing RoleMembership for action request: " + membership );
757 }
758
759 if (MemberType.PRINCIPAL.equals(membership.getType())) {
760 roleRecipient.setTarget(new KimPrincipalRecipient(membership.getMemberId()));
761 } else if (MemberType.GROUP.equals(membership.getType())) {
762 roleRecipient.setTarget(new KimGroupRecipient(membership.getMemberId()));
763 } else {
764 throw new RiceRuntimeException("Failed to identify a group or principal on the given RoleMembership:" + membership);
765 }
766
767 ActionRequestValue request = createActionRequest(
768 actionRequestedCode,
769 priority,
770 roleRecipient,
771 "",
772 responsibilityId,
773 forceAction,
774
775
776
777 (memberships.size() == 1) ? ActionRequestPolicy.FIRST.getCode() : actionRequestPolicyCode,
778 null,
779 null);
780
781
782 if ( memberships.size() > 1 ) {
783 request.setParentActionRequest(requestGraph);
784 requestGraph.getChildrenRequests().add(request);
785
786 if (delegateRoleRequest == null) {
787 delegateRoleRequest = requestGraph;
788 }
789 } else {
790 delegateRoleRequest = request;
791 }
792 }
793
794 delegateRoleRequest.setDelegationType(delegationType);
795 delegateRoleRequest.setParentActionRequest(parentRequest);
796 parentRequest.getChildrenRequests().add(delegateRoleRequest);
797
798 return delegateRoleRequest;
799 }
800
801 private void generateKimRoleDelegationRequests(List<DelegateType> delegates, ActionRequestValue parentRequest) {
802 for (DelegateType delegate : delegates) {
803 for (DelegateMember member : delegate.getMembers()) {
804 Recipient recipient;
805 boolean isPrincipal = MemberType.PRINCIPAL.equals(member.getType());
806 boolean isGroup = MemberType.GROUP.equals(member.getType());
807 if (isPrincipal) {
808 recipient = new KimPrincipalRecipient(member.getMemberId());
809 } else if (isGroup) {
810 recipient = new KimGroupRecipient(member.getMemberId());
811 } else {
812 throw new RiceRuntimeException("Invalid DelegateInfo memberTypeCode encountered, was '" + member.getType() + "'");
813 }
814 String delegationAnnotation = generateRoleResponsibilityDelegateAnnotation(member, isPrincipal, isGroup, parentRequest);
815 addDelegationRequest(parentRequest, recipient, delegate.getDelegationId(), parentRequest.getForceAction(), delegate.getDelegationType(), delegationAnnotation, null);
816 }
817 }
818 }
819
820
821 public boolean relatedToRoot(ActionRequestValue request) {
822 int i = 0;
823 while(i < 3) {
824 if (requestGraphs.contains(request)) {
825 return true;
826 } else if (request == null) {
827 return false;
828 }
829 i++;
830 request = request.getParentActionRequest();
831 }
832 return false;
833 }
834
835 public List<ActionRequestValue> getRequestGraphs() {
836
837 requestGraphs.removeAll(cleanUpChildren(requestGraphs));
838 return requestGraphs;
839 }
840
841 private Collection<ActionRequestValue> cleanUpChildren(Collection<ActionRequestValue> children) {
842 Collection<ActionRequestValue> requestsToRemove = new ArrayList<ActionRequestValue>();
843 for (ActionRequestValue aChildren : children)
844 {
845
846 if (aChildren.isRoleRequest())
847 {
848 if (aChildren.getChildrenRequests().isEmpty())
849 {
850 requestsToRemove.add(aChildren);
851 } else
852 {
853 Collection<ActionRequestValue> childRequestsToRemove = cleanUpChildren(aChildren.getChildrenRequests());
854 aChildren.getChildrenRequests().removeAll(childRequestsToRemove);
855 }
856 }
857 }
858 return requestsToRemove;
859 }
860
861 private String generateNotificationAnnotation(PrincipalContract principal, String notificationRequestCode, String actionTakenCode, ActionRequestValue request) {
862 String notification = "Action " + CodeTranslator.getActionRequestLabel(notificationRequestCode) + " generated by Workflow because " + principal.getPrincipalName() + " took action "
863 + CodeTranslator.getActionTakenLabel(actionTakenCode);
864
865
866
867 if (request.getResponsibilityId() != null && !KewApiConstants.MACHINE_GENERATED_RESPONSIBILITY_ID.equals(request.getResponsibilityId())) {
868 notification += " Responsibility " + request.getResponsibilityId();
869 }
870 if (request.getRuleBaseValuesId() != null) {
871 notification += " Rule Id " + request.getRuleBaseValuesId();
872 }
873 if (request.getAnnotation() != null && request.getAnnotation().length()!=0){
874 notification += " " + request.getAnnotation();
875 }
876 return notification;
877 }
878
879 protected static ActionRequestService getActionRequestService() {
880 if ( actionRequestService == null ) {
881 actionRequestService = KEWServiceLocator.getActionRequestService();
882 }
883 return actionRequestService;
884 }
885
886
887
888
889 protected static RoleService getRoleService() {
890 if ( roleService == null ) {
891 roleService = KimApiServiceLocator.getRoleService();
892 }
893 return roleService;
894 }
895
896
897
898
899 protected static IdentityHelperService getIdentityHelperService() {
900 if ( identityHelperService == null ) {
901 identityHelperService = KEWServiceLocator.getIdentityHelperService();
902 }
903 return identityHelperService;
904 }
905
906
907
908
909 protected static IdentityService getIdentityService() {
910 if ( identityService == null ) {
911 identityService = KimApiServiceLocator.getIdentityService();
912 }
913 return identityService;
914 }
915
916 protected static GroupService getGroupService() {
917 if ( groupService == null ) {
918 groupService = KimApiServiceLocator.getGroupService();
919 }
920 return groupService;
921 }
922 }