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