| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kew.engine; |
| 17 | |
|
| 18 | |
import org.apache.log4j.MDC; |
| 19 | |
import org.kuali.rice.core.framework.parameter.ParameterService; |
| 20 | |
import org.kuali.rice.kew.actionrequest.ActionRequestFactory; |
| 21 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
| 22 | |
import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient; |
| 23 | |
import org.kuali.rice.kew.actionrequest.service.ActionRequestService; |
| 24 | |
import org.kuali.rice.kew.actions.NotificationContext; |
| 25 | |
import org.kuali.rice.kew.actiontaken.ActionTakenValue; |
| 26 | |
import org.kuali.rice.kew.api.WorkflowRuntimeException; |
| 27 | |
import org.kuali.rice.kew.api.exception.InvalidActionTakenException; |
| 28 | |
import org.kuali.rice.kew.api.exception.WorkflowException; |
| 29 | |
import org.kuali.rice.kew.engine.node.ProcessDefinitionBo; |
| 30 | |
import org.kuali.rice.kew.engine.node.RouteNode; |
| 31 | |
import org.kuali.rice.kew.engine.node.RouteNodeInstance; |
| 32 | |
import org.kuali.rice.kew.engine.node.service.RouteNodeService; |
| 33 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
| 34 | |
import org.kuali.rice.kew.routeheader.service.RouteHeaderService; |
| 35 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 36 | |
import org.kuali.rice.kew.api.KewApiConstants; |
| 37 | |
|
| 38 | |
import java.util.ArrayList; |
| 39 | |
import java.util.HashSet; |
| 40 | |
import java.util.Iterator; |
| 41 | |
import java.util.LinkedList; |
| 42 | |
import java.util.List; |
| 43 | |
import java.util.Set; |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public class BlanketApproveEngine extends StandardWorkflowEngine { |
| 52 | |
|
| 53 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BlanketApproveEngine.class); |
| 54 | |
|
| 55 | |
|
| 56 | |
BlanketApproveEngine(RouteNodeService routeNodeService, RouteHeaderService routeHeaderService, |
| 57 | |
ParameterService parameterService, OrchestrationConfig config) { |
| 58 | 0 | super(routeNodeService, routeHeaderService, parameterService, config); |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public void process(String documentId, String nodeInstanceId) throws Exception { |
| 65 | 0 | if (documentId == null) { |
| 66 | 0 | throw new IllegalArgumentException("Cannot process a null document id."); |
| 67 | |
} |
| 68 | 0 | MDC.put("docId", documentId); |
| 69 | 0 | RouteContext context = RouteContext.getCurrentRouteContext(); |
| 70 | |
try { |
| 71 | 0 | KEWServiceLocator.getRouteHeaderService().lockRouteHeader(documentId, true); |
| 72 | 0 | if ( LOG.isInfoEnabled() ) { |
| 73 | 0 | LOG.info("Processing document for Blanket Approval: " + documentId + " : " + nodeInstanceId); |
| 74 | |
} |
| 75 | 0 | DocumentRouteHeaderValue document = getRouteHeaderService().getRouteHeader(documentId); |
| 76 | 0 | if (!document.isRoutable()) { |
| 77 | 0 | LOG.debug("Document not routable so returning with doing no action"); |
| 78 | |
return; |
| 79 | |
} |
| 80 | 0 | List<RouteNodeInstance> activeNodeInstances = new ArrayList<RouteNodeInstance>(); |
| 81 | 0 | if (nodeInstanceId == null) { |
| 82 | 0 | activeNodeInstances.addAll(getRouteNodeService().getActiveNodeInstances(documentId)); |
| 83 | |
} else { |
| 84 | 0 | RouteNodeInstance instanceNode = getRouteNodeService().findRouteNodeInstanceById(nodeInstanceId); |
| 85 | 0 | if (instanceNode == null) { |
| 86 | 0 | throw new IllegalArgumentException("Invalid node instance id: " + nodeInstanceId); |
| 87 | |
} |
| 88 | 0 | activeNodeInstances.add(instanceNode); |
| 89 | |
} |
| 90 | 0 | List<RouteNodeInstance> nodeInstancesToProcess = determineNodeInstancesToProcess(activeNodeInstances, config.getDestinationNodeNames()); |
| 91 | |
|
| 92 | |
|
| 93 | 0 | context.setDoNotSendApproveNotificationEmails(true); |
| 94 | 0 | context.setDocument(document); |
| 95 | 0 | context.setEngineState(new EngineState()); |
| 96 | 0 | NotificationContext notifyContext = null; |
| 97 | 0 | if (config.isSendNotifications()) { |
| 98 | 0 | notifyContext = new NotificationContext(KewApiConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, config.getCause().getPrincipal(), config.getCause().getActionTaken()); |
| 99 | |
} |
| 100 | 0 | lockAdditionalDocuments(document); |
| 101 | |
try { |
| 102 | 0 | List<ProcessEntry> processingQueue = new LinkedList<ProcessEntry>(); |
| 103 | 0 | for (RouteNodeInstance nodeInstancesToProcesses : nodeInstancesToProcess) |
| 104 | |
{ |
| 105 | 0 | processingQueue.add(new ProcessEntry((RouteNodeInstance) nodeInstancesToProcesses)); |
| 106 | |
} |
| 107 | 0 | Set<String> nodesCompleted = new HashSet<String>(); |
| 108 | |
|
| 109 | |
|
| 110 | 0 | while (!processingQueue.isEmpty() && !isReachedDestinationNodes(config.getDestinationNodeNames(), nodesCompleted)) { |
| 111 | 0 | ProcessEntry entry = processingQueue.remove(0); |
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | 0 | if (entry.getTimesProcessed() > 20) { |
| 116 | 0 | throw new WorkflowException("Could not process document through to blanket approval." + " Document failed to progress past node " + entry.getNodeInstance().getRouteNode().getRouteNodeName()); |
| 117 | |
} |
| 118 | 0 | RouteNodeInstance nodeInstance = entry.getNodeInstance(); |
| 119 | 0 | context.setNodeInstance(nodeInstance); |
| 120 | 0 | if (config.getDestinationNodeNames().contains(nodeInstance.getName())) { |
| 121 | 0 | nodesCompleted.add(nodeInstance.getName()); |
| 122 | 0 | continue; |
| 123 | |
} |
| 124 | 0 | ProcessContext resultProcessContext = processNodeInstance(context, helper); |
| 125 | 0 | invokeBlanketApproval(config.getCause(), nodeInstance, notifyContext); |
| 126 | 0 | if (!resultProcessContext.getNextNodeInstances().isEmpty() || resultProcessContext.isComplete()) { |
| 127 | 0 | for (Iterator nodeIt = resultProcessContext.getNextNodeInstances().iterator(); nodeIt.hasNext();) { |
| 128 | 0 | addToProcessingQueue(processingQueue, (RouteNodeInstance) nodeIt.next()); |
| 129 | |
} |
| 130 | |
} else { |
| 131 | 0 | entry.increment(); |
| 132 | 0 | processingQueue.add(processingQueue.size(), entry); |
| 133 | |
} |
| 134 | 0 | } |
| 135 | |
|
| 136 | 0 | RouteContext.clearCurrentRouteContext(); |
| 137 | |
|
| 138 | |
|
| 139 | 0 | super.process(documentId, null); |
| 140 | 0 | } catch (Exception e) { |
| 141 | 0 | if (e instanceof RuntimeException) { |
| 142 | 0 | throw (RuntimeException)e; |
| 143 | |
} else { |
| 144 | 0 | throw new WorkflowRuntimeException(e.toString(), e); |
| 145 | |
} |
| 146 | 0 | } |
| 147 | |
} finally { |
| 148 | 0 | RouteContext.clearCurrentRouteContext(); |
| 149 | 0 | MDC.remove("docId"); |
| 150 | 0 | } |
| 151 | 0 | } |
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
private boolean isReachedDestinationNodes(Set destinationNodesNames, Set<String> nodeNamesCompleted) { |
| 157 | 0 | return !destinationNodesNames.isEmpty() && nodeNamesCompleted.equals(destinationNodesNames); |
| 158 | |
} |
| 159 | |
|
| 160 | |
private void addToProcessingQueue(List<ProcessEntry> processingQueue, RouteNodeInstance nodeInstance) { |
| 161 | |
|
| 162 | 0 | for (ProcessEntry entry : processingQueue) |
| 163 | |
{ |
| 164 | 0 | if (entry.getNodeInstance().getRouteNodeInstanceId().equals(nodeInstance.getRouteNodeInstanceId())) |
| 165 | |
{ |
| 166 | 0 | entry.setNodeInstance(nodeInstance); |
| 167 | 0 | return; |
| 168 | |
} |
| 169 | |
} |
| 170 | 0 | processingQueue.add(processingQueue.size(), new ProcessEntry(nodeInstance)); |
| 171 | 0 | } |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
private List<RouteNodeInstance> determineNodeInstancesToProcess(List<RouteNodeInstance> activeNodeInstances, Set nodeNames) throws Exception { |
| 177 | 0 | if (nodeNames.isEmpty()) { |
| 178 | 0 | return activeNodeInstances; |
| 179 | |
} |
| 180 | 0 | List<RouteNodeInstance> nodeInstancesToProcess = new ArrayList<RouteNodeInstance>(); |
| 181 | 0 | for (Iterator<RouteNodeInstance> iterator = activeNodeInstances.iterator(); iterator.hasNext();) { |
| 182 | 0 | RouteNodeInstance nodeInstance = (RouteNodeInstance) iterator.next(); |
| 183 | 0 | if (isNodeNameInPath(nodeNames, nodeInstance)) { |
| 184 | 0 | nodeInstancesToProcess.add(nodeInstance); |
| 185 | |
} |
| 186 | 0 | } |
| 187 | 0 | if (nodeInstancesToProcess.size() == 0) { |
| 188 | 0 | throw new InvalidActionTakenException("Could not locate nodes with the given names in the blanket approval path '" + printNodeNames(nodeNames) + "'. " + "The document is probably already passed the specified nodes or does not contain the nodes."); |
| 189 | |
} |
| 190 | 0 | return nodeInstancesToProcess; |
| 191 | |
} |
| 192 | |
|
| 193 | |
private boolean isNodeNameInPath(Set nodeNames, RouteNodeInstance nodeInstance) throws Exception { |
| 194 | 0 | boolean isInPath = false; |
| 195 | 0 | for (Object nodeName1 : nodeNames) |
| 196 | |
{ |
| 197 | 0 | String nodeName = (String) nodeName1; |
| 198 | 0 | for (RouteNode nextNode : nodeInstance.getRouteNode().getNextNodes()) |
| 199 | |
{ |
| 200 | 0 | isInPath = isInPath || isNodeNameInPath(nodeName, nextNode, new HashSet<String>()); |
| 201 | |
} |
| 202 | 0 | } |
| 203 | 0 | return isInPath; |
| 204 | |
} |
| 205 | |
|
| 206 | |
private boolean isNodeNameInPath(String nodeName, RouteNode node, Set<String> inspected) throws Exception { |
| 207 | 0 | boolean isInPath = !inspected.contains(node.getRouteNodeId()) && node.getRouteNodeName().equals(nodeName); |
| 208 | 0 | inspected.add(node.getRouteNodeId()); |
| 209 | 0 | if (helper.isSubProcessNode(node)) { |
| 210 | 0 | ProcessDefinitionBo subProcess = node.getDocumentType().getNamedProcess(node.getRouteNodeName()); |
| 211 | 0 | RouteNode subNode = subProcess.getInitialRouteNode(); |
| 212 | 0 | isInPath = isInPath || isNodeNameInPath(nodeName, subNode, inspected); |
| 213 | |
} |
| 214 | 0 | for (RouteNode nextNode : node.getNextNodes()) |
| 215 | |
{ |
| 216 | 0 | isInPath = isInPath || isNodeNameInPath(nodeName, nextNode, inspected); |
| 217 | |
} |
| 218 | 0 | return isInPath; |
| 219 | |
} |
| 220 | |
|
| 221 | |
private String printNodeNames(Set nodesNames) { |
| 222 | 0 | StringBuffer buffer = new StringBuffer(); |
| 223 | 0 | for (Iterator iterator = nodesNames.iterator(); iterator.hasNext();) { |
| 224 | 0 | String nodeName = (String) iterator.next(); |
| 225 | 0 | buffer.append(nodeName); |
| 226 | 0 | buffer.append((iterator.hasNext() ? ", " : "")); |
| 227 | 0 | } |
| 228 | 0 | return buffer.toString(); |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
private void invokeBlanketApproval(ActionTakenValue actionTaken, RouteNodeInstance nodeInstance, NotificationContext notifyContext) { |
| 235 | 0 | List actionRequests = getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(nodeInstance.getDocumentId(), nodeInstance.getRouteNodeInstanceId()); |
| 236 | 0 | actionRequests = getActionRequestService().getRootRequests(actionRequests); |
| 237 | 0 | List<ActionRequestValue> requestsToNotify = new ArrayList<ActionRequestValue>(); |
| 238 | 0 | for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) { |
| 239 | 0 | ActionRequestValue request = (ActionRequestValue) iterator.next(); |
| 240 | 0 | if (request.isApproveOrCompleteRequest()) { |
| 241 | 0 | getActionRequestService().deactivateRequest(actionTaken, request); |
| 242 | 0 | requestsToNotify.add(request); |
| 243 | |
} |
| 244 | 0 | } |
| 245 | 0 | if (notifyContext != null) { |
| 246 | 0 | ActionRequestFactory arFactory = new ActionRequestFactory(RouteContext.getCurrentRouteContext().getDocument(), nodeInstance); |
| 247 | 0 | KimPrincipalRecipient delegatorRecipient = null; |
| 248 | 0 | if (actionTaken.getDelegatorPrincipal() != null) { |
| 249 | 0 | delegatorRecipient = new KimPrincipalRecipient(actionTaken.getDelegatorPrincipal()); |
| 250 | |
} |
| 251 | 0 | List<ActionRequestValue> notificationRequests = arFactory.generateNotifications(requestsToNotify, notifyContext.getPrincipalTakingAction(), delegatorRecipient, notifyContext.getNotificationRequestCode(), notifyContext.getActionTakenCode()); |
| 252 | 0 | getActionRequestService().activateRequests(notificationRequests); |
| 253 | |
} |
| 254 | 0 | } |
| 255 | |
|
| 256 | |
private ActionRequestService getActionRequestService() { |
| 257 | 0 | return KEWServiceLocator.getActionRequestService(); |
| 258 | |
} |
| 259 | |
|
| 260 | |
private class ProcessEntry { |
| 261 | |
|
| 262 | |
private RouteNodeInstance nodeInstance; |
| 263 | 0 | private int timesProcessed = 0; |
| 264 | |
|
| 265 | 0 | public ProcessEntry(RouteNodeInstance nodeInstance) { |
| 266 | 0 | this.nodeInstance = nodeInstance; |
| 267 | 0 | } |
| 268 | |
|
| 269 | |
public RouteNodeInstance getNodeInstance() { |
| 270 | 0 | return nodeInstance; |
| 271 | |
} |
| 272 | |
|
| 273 | |
public void setNodeInstance(RouteNodeInstance nodeInstance) { |
| 274 | 0 | this.nodeInstance = nodeInstance; |
| 275 | 0 | } |
| 276 | |
|
| 277 | |
public void increment() { |
| 278 | 0 | timesProcessed++; |
| 279 | 0 | } |
| 280 | |
|
| 281 | |
public int getTimesProcessed() { |
| 282 | 0 | return timesProcessed; |
| 283 | |
} |
| 284 | |
|
| 285 | |
} |
| 286 | |
|
| 287 | |
} |