1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.engine.node; |
18 | |
|
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Collections; |
21 | |
import java.util.Iterator; |
22 | |
import java.util.List; |
23 | |
|
24 | |
import org.apache.log4j.MDC; |
25 | |
import org.kuali.rice.kew.actionitem.ActionItem; |
26 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
27 | |
import org.kuali.rice.kew.engine.RouteContext; |
28 | |
import org.kuali.rice.kew.engine.RouteHelper; |
29 | |
import org.kuali.rice.kew.exception.ResourceUnavailableException; |
30 | |
import org.kuali.rice.kew.exception.WorkflowException; |
31 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
32 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
33 | |
import org.kuali.rice.kew.util.KEWConstants; |
34 | |
import org.kuali.rice.kew.util.PerformanceLogger; |
35 | |
import org.kuali.rice.kew.util.Utilities; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class RequestActivationNode extends RequestActivationNodeBase { |
45 | |
|
46 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger( RequestActivationNode.class ); |
47 | 0 | private static long generatedRequestPriority = 0; |
48 | |
|
49 | |
public SimpleResult process(RouteContext routeContext, RouteHelper routeHelper) throws Exception { |
50 | 0 | DocumentRouteHeaderValue document = routeContext.getDocument(); |
51 | 0 | RouteNodeInstance nodeInstance = routeContext.getNodeInstance(); |
52 | 0 | if (routeContext.isSimulation()) { |
53 | 0 | if (routeContext.getActivationContext().isActivateRequests()) { |
54 | 0 | activateRequests(routeContext, document, nodeInstance); |
55 | |
} |
56 | 0 | return new SimpleResult(true); |
57 | 0 | } else if (!activateRequests(routeContext, document, nodeInstance) && shouldTransition(document, nodeInstance)) { |
58 | 0 | return new SimpleResult(true); |
59 | |
} else { |
60 | 0 | return new SimpleResult(false); |
61 | |
} |
62 | |
} |
63 | |
|
64 | |
public boolean shouldTransition(DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) { |
65 | 0 | List requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(document.getRouteHeaderId(), nodeInstance.getRouteNodeInstanceId()); |
66 | 0 | boolean shouldTransition = true; |
67 | 0 | for (Iterator iterator = requests.iterator(); iterator.hasNext();) { |
68 | 0 | ActionRequestValue request = (ActionRequestValue) iterator.next(); |
69 | 0 | if (request.isApproveOrCompleteRequest()) { |
70 | 0 | shouldTransition = false; |
71 | 0 | break; |
72 | |
} |
73 | 0 | } |
74 | 0 | return shouldTransition; |
75 | |
} |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
public boolean activateRequests(RouteContext context, DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) throws WorkflowException { |
91 | 0 | MDC.put("docId", document.getRouteHeaderId()); |
92 | 0 | PerformanceLogger performanceLogger = new PerformanceLogger(document.getRouteHeaderId()); |
93 | 0 | List<ActionItem> generatedActionItems = new ArrayList<ActionItem>(); |
94 | 0 | List<ActionRequestValue> requests = new ArrayList<ActionRequestValue>(); |
95 | 0 | if (context.isSimulation()) { |
96 | 0 | for (ActionRequestValue ar : context.getDocument().getActionRequests()) { |
97 | |
|
98 | 0 | if (ar.getCurrentIndicator() |
99 | |
&& (KEWConstants.ACTION_REQUEST_INITIALIZED.equals(ar.getStatus()) || KEWConstants.ACTION_REQUEST_ACTIVATED.equals(ar.getStatus())) |
100 | |
&& ar.getNodeInstance().getRouteNodeInstanceId().equals(nodeInstance.getRouteNodeInstanceId()) |
101 | |
&& ar.getParentActionRequest() == null) { |
102 | 0 | requests.add(ar); |
103 | |
} |
104 | |
} |
105 | 0 | requests.addAll(context.getEngineState().getGeneratedRequests()); |
106 | |
} else { |
107 | 0 | requests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(document.getRouteHeaderId(), nodeInstance.getRouteNodeInstanceId()); |
108 | |
} |
109 | 0 | if ( LOG.isDebugEnabled() ) { |
110 | 0 | LOG.debug("Pending Root Requests " + requests.size()); |
111 | |
} |
112 | 0 | boolean activatedApproveRequest = activateRequestsCustom( context, requests, generatedActionItems, document, nodeInstance ); |
113 | |
|
114 | |
|
115 | |
|
116 | 0 | notify(context, generatedActionItems, nodeInstance); |
117 | |
|
118 | 0 | performanceLogger.log("Time to activate requests."); |
119 | 0 | return activatedApproveRequest; |
120 | |
} |
121 | |
|
122 | |
protected boolean activateRequestsCustom( RouteContext context, List<ActionRequestValue> requests, List<ActionItem> generatedActionItems, |
123 | |
DocumentRouteHeaderValue document, RouteNodeInstance nodeInstance) throws WorkflowException { |
124 | 0 | Collections.sort(requests, new Utilities.PrioritySorter()); |
125 | 0 | String activationType = nodeInstance.getRouteNode().getActivationType(); |
126 | 0 | boolean isParallel = KEWConstants.ROUTE_LEVEL_PARALLEL.equals(activationType); |
127 | 0 | boolean activatedApproveRequest = false; |
128 | 0 | for (ActionRequestValue request : requests ) { |
129 | 0 | if (activatedApproveRequest && !isParallel && (!context.isSimulation() || !context.getActivationContext().isActivateRequests() )) { |
130 | 0 | break; |
131 | |
} |
132 | 0 | if (request.getParentActionRequest() != null || request.getNodeInstance() == null) { |
133 | |
|
134 | |
|
135 | 0 | continue; |
136 | |
} |
137 | 0 | if (request.isActive()) { |
138 | 0 | activatedApproveRequest = activatedApproveRequest || request.isApproveOrCompleteRequest(); |
139 | 0 | continue; |
140 | |
} |
141 | 0 | logProcessingMessage(request); |
142 | 0 | if ( LOG.isDebugEnabled() ) { |
143 | 0 | LOG.debug("Activating request: " + request); |
144 | |
} |
145 | 0 | activatedApproveRequest = activateRequest(context, request, nodeInstance, generatedActionItems) || activatedApproveRequest; |
146 | |
} |
147 | 0 | return activatedApproveRequest; |
148 | |
} |
149 | |
|
150 | |
protected boolean activateRequest(RouteContext context, ActionRequestValue actionRequest, RouteNodeInstance nodeInstance, List generatedActionItems) { |
151 | 0 | if (actionRequest.isRoleRequest()) { |
152 | 0 | List actionRequests = KEWServiceLocator.getActionRequestService().findPendingRootRequestsByDocIdAtRouteNode(actionRequest.getRouteHeaderId(), nodeInstance.getRouteNodeInstanceId()); |
153 | 0 | for (Iterator iterator = actionRequests.iterator(); iterator.hasNext();) { |
154 | 0 | ActionRequestValue siblingRequest = (ActionRequestValue) iterator.next(); |
155 | 0 | if (actionRequest.getRoleName().equals(siblingRequest.getRoleName())) { |
156 | 0 | generatedActionItems.addAll(KEWServiceLocator.getActionRequestService().activateRequestNoNotification(siblingRequest, context.getActivationContext())); |
157 | |
} |
158 | 0 | } |
159 | |
} |
160 | 0 | generatedActionItems.addAll(KEWServiceLocator.getActionRequestService().activateRequestNoNotification(actionRequest, context.getActivationContext())); |
161 | 0 | return actionRequest.isApproveOrCompleteRequest() && ! actionRequest.isDone(); |
162 | |
} |
163 | |
|
164 | |
protected void saveActionRequest(RouteContext context, ActionRequestValue actionRequest) { |
165 | 0 | if (!context.isSimulation()) { |
166 | 0 | KEWServiceLocator.getActionRequestService().saveActionRequest(actionRequest); |
167 | |
} else { |
168 | 0 | actionRequest.setActionRequestId(new Long(generatedRequestPriority++)); |
169 | 0 | context.getEngineState().getGeneratedRequests().add(actionRequest); |
170 | |
} |
171 | |
|
172 | 0 | } |
173 | |
|
174 | |
protected void saveDocument(RouteContext context, DocumentRouteHeaderValue document) { |
175 | 0 | if (!context.isSimulation()) { |
176 | 0 | KEWServiceLocator.getRouteHeaderService().saveRouteHeader(document); |
177 | |
} |
178 | 0 | } |
179 | |
|
180 | |
protected void logProcessingMessage(ActionRequestValue request) { |
181 | 0 | if (LOG.isDebugEnabled()) { |
182 | 0 | RouteNodeInstance nodeInstance = request.getNodeInstance(); |
183 | 0 | StringBuffer buffer = new StringBuffer(); |
184 | 0 | buffer.append("Processing AR: ").append(request.getActionRequestId()).append("\n"); |
185 | 0 | buffer.append("AR Node Name: ").append(nodeInstance != null ? nodeInstance.getName() : "null").append("\n"); |
186 | 0 | buffer.append("AR RouteLevel: ").append(request.getRouteLevel()).append("\n"); |
187 | 0 | buffer.append("AR Request Code: ").append(request.getActionRequested()).append("\n"); |
188 | 0 | buffer.append("AR Request priority: ").append(request.getPriority()).append("\n"); |
189 | 0 | LOG.debug(buffer); |
190 | |
} |
191 | 0 | } |
192 | |
|
193 | |
} |