1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.actions;
17
18 import org.apache.log4j.MDC;
19 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
20 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
21 import org.kuali.rice.kew.api.exception.WorkflowException;
22 import org.kuali.rice.kew.engine.node.ProcessDefinitionBo;
23 import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
24 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
25 import org.kuali.rice.kew.service.KEWServiceLocator;
26 import org.kuali.rice.kew.api.KewApiConstants;
27 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
28
29 import java.util.List;
30
31
32
33
34
35
36
37
38 public class RouteDocumentAction extends ActionTakenEvent {
39 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RouteDocumentAction.class);
40
41 public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
42 super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal);
43 }
44
45 public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
46 super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal, annotation);
47 }
48
49
50
51
52 @Override
53 public String getActionPerformedCode() {
54 return KewApiConstants.ACTION_TAKEN_ROUTED_CD;
55 }
56
57
58
59
60 @Override
61 public String validateActionRules() {
62
63 if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
64 return "Document is not in a state to be routed";
65 }
66 if (! KEWServiceLocator.getDocumentTypePermissionService().canRoute(getPrincipal().getPrincipalId(), getRouteHeader())) {
67 return "User is not authorized to Route document";
68 }
69 return "";
70 }
71
72 @Override
73 public String validateActionRules(List<ActionRequestValue> actionRequests) {
74 return validateActionRules();
75 }
76
77
78
79
80
81 public void recordAction() throws InvalidActionTakenException {
82 MDC.put("docId", getRouteHeader().getDocumentId());
83 updateSearchableAttributesIfPossible();
84
85 LOG.debug("Routing document : " + annotation);
86
87 LOG.debug("Checking to see if the action is legal");
88 String errorMessage = validateActionRules();
89 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
90 throw new InvalidActionTakenException(errorMessage);
91 }
92
93
94
95 LOG.debug("Record the routing action");
96 ActionTakenValue actionTaken = saveActionTaken();
97
98
99 List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
100 LOG.debug("Deactivate all pending action requests");
101
102 for (ActionRequestValue actionRequest : actionRequests)
103 {
104
105 if ((getPrincipal().getPrincipalId().equals(actionRequest.getPrincipalId())) && (actionRequest.isActive()))
106 {
107 getActionRequestService().deactivateRequest(actionTaken, actionRequest);
108 }
109
110 else if (KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID.equals(actionRequest.getResponsibilityId()))
111 {
112 getActionRequestService().deactivateRequest(actionTaken, actionRequest);
113 }
114 }
115
116 notifyActionTaken(actionTaken);
117
118 try {
119 String oldStatus = getRouteHeader().getDocRouteStatus();
120 getRouteHeader().markDocumentEnroute();
121
122 if (((ProcessDefinitionBo)getRouteHeader().getDocumentType().getProcesses().get(0)).getInitialRouteNode() == null) {
123 getRouteHeader().markDocumentProcessed();
124 getRouteHeader().markDocumentFinalized();
125 }
126
127 getRouteHeader().setRoutedByUserWorkflowId(getPrincipal().getPrincipalId());
128
129 String newStatus = getRouteHeader().getDocRouteStatus();
130 notifyStatusChange(newStatus, oldStatus);
131 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
132 } catch (WorkflowException ex) {
133 LOG.warn(ex, ex);
134 throw new InvalidActionTakenException(ex.getMessage());
135 }
136
137 }
138 }