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 java.util.Collections;
19 import java.util.HashSet;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.apache.log4j.MDC;
25 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
26 import org.kuali.rice.kew.actionrequest.Recipient;
27 import org.kuali.rice.kew.api.document.DocumentOrchestrationQueue;
28 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
29 import org.kuali.rice.kew.api.WorkflowRuntimeException;
30 import org.kuali.rice.kew.api.document.DocumentProcessingOptions;
31 import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
32 import org.kuali.rice.kew.doctype.bo.DocumentType;
33 import org.kuali.rice.kew.engine.BlanketApproveEngine;
34 import org.kuali.rice.kew.engine.CompatUtils;
35 import org.kuali.rice.kew.engine.OrchestrationConfig;
36 import org.kuali.rice.kew.engine.OrchestrationConfig.EngineCapability;
37 import org.kuali.rice.kew.engine.RouteContext;
38 import org.kuali.rice.kew.engine.node.RouteNode;
39 import org.kuali.rice.kew.engine.node.service.RouteNodeService;
40 import org.kuali.rice.kew.messaging.MessageServiceNames;
41 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
42 import org.kuali.rice.kew.service.KEWServiceLocator;
43 import org.kuali.rice.kew.api.KewApiConstants;
44 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
45
46
47
48
49
50
51
52 public class BlanketApproveAction extends ActionTakenEvent {
53
54 private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BlanketApproveAction.class);
55 private Set<String> nodeNames;
56
57 public BlanketApproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
58 super(KewApiConstants.ACTION_TAKEN_BLANKET_APPROVE_CD, rh, principal);
59
60 setQueueDocumentAfterAction(false);
61
62 }
63
64 public BlanketApproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation, Integer routeLevel) {
65 this(rh, principal, annotation, convertRouteLevel(rh.getDocumentType(), routeLevel));
66 }
67
68 public BlanketApproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation, String nodeName) {
69 this(rh, principal, annotation, Collections.singleton(nodeName));
70
71 }
72
73 public BlanketApproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation, Set<String> nodeNames) {
74 super(KewApiConstants.ACTION_TAKEN_BLANKET_APPROVE_CD, rh, principal, annotation);
75 this.nodeNames = (nodeNames == null ? new HashSet<String>() : nodeNames);
76 setQueueDocumentAfterAction(false);
77 }
78
79 private static Set<String> convertRouteLevel(DocumentType documentType, Integer routeLevel) {
80 Set<String> nodeNames = new HashSet<String>();
81 if (routeLevel == null) {
82 return nodeNames;
83 }
84 RouteNode node = CompatUtils.getNodeForLevel(documentType, routeLevel);
85 if (node == null) {
86 throw new WorkflowRuntimeException("Could not locate a valid node for the given route level: " + routeLevel);
87 }
88 nodeNames.add(node.getRouteNodeName());
89 return nodeNames;
90 }
91
92
93
94
95 @Override
96 public String validateActionRules() {
97 return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId()));
98 }
99
100 public String validateActionRules(List<ActionRequestValue> actionRequests) {
101 if ( (nodeNames != null) && (!nodeNames.isEmpty()) ) {
102 String nodeName = isGivenNodeListValid();
103 if (!org.apache.commons.lang.StringUtils.isEmpty(nodeName)) {
104 return "Document already at or beyond route node " + nodeName;
105 }
106 }
107 if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
108 return "Document is not in a state to be approved";
109 }
110 List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
111 if (!isActionCompatibleRequest(filteredActionRequests)) {
112 return "No request for the user is compatible with the BlanketApprove Action";
113 }
114
115 if (! KEWServiceLocator.getDocumentTypePermissionService().canBlanketApprove(getPrincipal().getPrincipalId(), getRouteHeader().getDocumentType(), getRouteHeader().getDocRouteStatus(), getRouteHeader().getInitiatorWorkflowId())) {
116 return "User is not authorized to BlanketApprove document";
117 }
118 return "";
119 }
120
121 private String isGivenNodeListValid() {
122 for (Iterator<String> iterator = nodeNames.iterator(); iterator.hasNext();) {
123 String nodeName = (String) iterator.next();
124 if (nodeName == null) {
125 iterator.remove();
126 continue;
127 }
128 if (!getRouteNodeService().isNodeInPath(getRouteHeader(), nodeName)) {
129 return nodeName;
130 }
131 }
132 return "";
133 }
134
135 public void recordAction() throws InvalidActionTakenException {
136 MDC.put("docId", getRouteHeader().getDocumentId());
137 updateSearchableAttributesIfPossible();
138
139 List<ActionRequestValue> actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KewApiConstants.ACTION_REQUEST_COMPLETE_REQ);
140 String errorMessage = validateActionRules(actionRequests);
141 if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
142 throw new InvalidActionTakenException(errorMessage);
143 }
144
145 LOG.debug("Checking to see if the action is legal");
146
147 LOG.debug("Blanket approving document : " + annotation);
148
149 if (getRouteHeader().isStateInitiated() || getRouteHeader().isStateSaved()) {
150 markDocumentEnroute(getRouteHeader());
151 getRouteHeader().setRoutedByUserWorkflowId(getPrincipal().getPrincipalId());
152 }
153
154 LOG.debug("Record the blanket approval action");
155 Recipient delegator = findDelegatorForActionRequests(actionRequests);
156 ActionTakenValue actionTaken = saveActionTaken(delegator);
157
158 LOG.debug("Deactivate pending action requests for user");
159 getActionRequestService().deactivateRequests(actionTaken, actionRequests);
160 notifyActionTaken(actionTaken);
161
162 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
163
164
165
166
167
168
169 queueDeferredWork(actionTaken);
170 }
171
172 protected void queueDeferredWork(ActionTakenValue actionTaken) {
173 try {
174 final boolean shouldIndex = getRouteHeader().getDocumentType().hasSearchableAttributes() && RouteContext.getCurrentRouteContext().isSearchIndexingRequestedForContext();
175
176 DocumentOrchestrationQueue blanketApprove = MessageServiceNames.getDocumentOrchestrationQueue(routeHeader);
177 org.kuali.rice.kew.api.document.OrchestrationConfig orchestrationConfig =
178 org.kuali.rice.kew.api.document.OrchestrationConfig.create(actionTaken.getActionTakenId(), nodeNames);
179 DocumentProcessingOptions options = DocumentProcessingOptions.create(true, shouldIndex);
180 blanketApprove.orchestrateDocument(routeHeader.getDocumentId(), getPrincipal().getPrincipalId(),
181 orchestrationConfig, options);
182 } catch (Exception e) {
183 LOG.error(e);
184 throw new WorkflowRuntimeException(e);
185 }
186 }
187
188 public void performDeferredBlanketApproveWork(ActionTakenValue actionTaken, DocumentProcessingOptions processingOptions) throws Exception {
189
190 if (getRouteHeader().isInException()) {
191 LOG.debug("Moving document back to Enroute from Exception");
192
193 String oldStatus = getRouteHeader().getDocRouteStatus();
194 getRouteHeader().markDocumentEnroute();
195
196 String newStatus = getRouteHeader().getDocRouteStatus();
197 notifyStatusChange(newStatus, oldStatus);
198 }
199 OrchestrationConfig config = new OrchestrationConfig(EngineCapability.BLANKET_APPROVAL, nodeNames, actionTaken, processingOptions.isSendNotifications(), processingOptions.isRunPostProcessor());
200 BlanketApproveEngine blanketApproveEngine = KEWServiceLocator.getWorkflowEngineFactory().newEngine(config);
201 blanketApproveEngine.process(getRouteHeader().getDocumentId(), null);
202
203 queueDocumentProcessing();
204 }
205
206 protected void markDocumentEnroute(DocumentRouteHeaderValue routeHeader) throws InvalidActionTakenException {
207 String oldStatus = getRouteHeader().getDocRouteStatus();
208 getRouteHeader().markDocumentEnroute();
209
210 String newStatus = getRouteHeader().getDocRouteStatus();
211 notifyStatusChange(newStatus, oldStatus);
212 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
213 }
214
215 private RouteNodeService getRouteNodeService() {
216 return KEWServiceLocator.getRouteNodeService();
217 }
218 }