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.sql.Timestamp;
30 import java.util.List;
31
32
33
34
35
36
37
38
39 public class RouteDocumentAction extends ActionTakenEvent {
40 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RouteDocumentAction.class);
41
42 public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
43 super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal);
44 }
45
46 public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
47 super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal, annotation);
48 }
49
50
51
52
53 @Override
54 public String getActionPerformedCode() {
55 return KewApiConstants.ACTION_TAKEN_ROUTED_CD;
56 }
57
58
59
60
61 @Override
62 public String validateActionRules() {
63
64 if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
65 return "Document is not in a state to be routed";
66 }
67 if (! KEWServiceLocator.getDocumentTypePermissionService().canRoute(getPrincipal().getPrincipalId(), getRouteHeader())) {
68 return "User is not authorized to Route document";
69 }
70 return "";
71 }
72
73 @Override
74 public String validateActionRules(List<ActionRequestValue> actionRequests) {
75 return validateActionRules();
76 }
77
78
79
80
81
82 public void recordAction() throws InvalidActionTakenException {
83 MDC.put("docId", getRouteHeader().getDocumentId());
84 updateSearchableAttributesIfPossible();
85
86 LOG.debug("Routing document : " + annotation);
87
88 LOG.debug("Checking to see if the action is legal");
89 String errorMessage = validateActionRules();
90 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
91 throw new InvalidActionTakenException(errorMessage);
92 }
93
94
95
96 LOG.debug("Record the routing action");
97 ActionTakenValue actionTaken = saveActionTaken();
98
99
100 List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
101 LOG.debug("Deactivate all pending action requests");
102
103 for (ActionRequestValue actionRequest : actionRequests)
104 {
105
106 if ((getPrincipal().getPrincipalId().equals(actionRequest.getPrincipalId())) && (actionRequest.isActive()))
107 {
108 getActionRequestService().deactivateRequest(actionTaken, actionRequest);
109 }
110
111 else if (KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID.equals(actionRequest.getResponsibilityId()))
112 {
113 getActionRequestService().deactivateRequest(actionTaken, actionRequest);
114 }
115 }
116
117 notifyActionTaken(actionTaken);
118
119 try {
120 String oldStatus = getRouteHeader().getDocRouteStatus();
121 getRouteHeader().markDocumentEnroute();
122
123 if (((ProcessDefinitionBo)getRouteHeader().getDocumentType().getProcesses().get(0)).getInitialRouteNode() == null) {
124 getRouteHeader().setApprovedDate(new Timestamp(System.currentTimeMillis()));
125
126 notifyStatusChange(getRouteHeader().getDocRouteStatus(), oldStatus);
127 oldStatus = getRouteHeader().getDocRouteStatus();
128
129 getRouteHeader().markDocumentProcessed();
130
131 notifyStatusChange(getRouteHeader().getDocRouteStatus(), oldStatus);
132 oldStatus = getRouteHeader().getDocRouteStatus();
133
134 getRouteHeader().markDocumentFinalized();
135 }
136
137 getRouteHeader().setRoutedByUserWorkflowId(getPrincipal().getPrincipalId());
138
139 String newStatus = getRouteHeader().getDocRouteStatus();
140 notifyStatusChange(newStatus, oldStatus);
141 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
142 } catch (WorkflowException ex) {
143 LOG.warn(ex, ex);
144 throw new InvalidActionTakenException(ex.getMessage());
145 }
146
147 }
148 }