001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.routeheader.service.impl;
017    
018    import org.kuali.rice.core.api.exception.RiceRuntimeException;
019    import org.kuali.rice.kew.actionitem.ActionItem;
020    import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
021    import org.kuali.rice.kew.actionrequest.Recipient;
022    import org.kuali.rice.kew.actions.AcknowledgeAction;
023    import org.kuali.rice.kew.actions.ActionTakenEvent;
024    import org.kuali.rice.kew.actions.AdHocAction;
025    import org.kuali.rice.kew.actions.ApproveAction;
026    import org.kuali.rice.kew.actions.BlanketApproveAction;
027    import org.kuali.rice.kew.actions.CancelAction;
028    import org.kuali.rice.kew.actions.ClearFYIAction;
029    import org.kuali.rice.kew.actions.CompleteAction;
030    import org.kuali.rice.kew.actions.DisapproveAction;
031    import org.kuali.rice.kew.actions.LogDocumentActionAction;
032    import org.kuali.rice.kew.actions.MoveDocumentAction;
033    import org.kuali.rice.kew.actions.RecallAction;
034    import org.kuali.rice.kew.actions.ReleaseWorkgroupAuthority;
035    import org.kuali.rice.kew.actions.ReturnToPreviousNodeAction;
036    import org.kuali.rice.kew.actions.RevokeAdHocAction;
037    import org.kuali.rice.kew.actions.RouteDocumentAction;
038    import org.kuali.rice.kew.actions.SaveActionEvent;
039    import org.kuali.rice.kew.actions.SuperUserActionRequestApproveEvent;
040    import org.kuali.rice.kew.actions.SuperUserApproveEvent;
041    import org.kuali.rice.kew.actions.SuperUserCancelEvent;
042    import org.kuali.rice.kew.actions.SuperUserDisapproveEvent;
043    import org.kuali.rice.kew.actions.SuperUserNodeApproveEvent;
044    import org.kuali.rice.kew.actions.SuperUserReturnToPreviousNodeAction;
045    import org.kuali.rice.kew.actions.TakeWorkgroupAuthority;
046    import org.kuali.rice.kew.api.action.ActionInvocation;
047    import org.kuali.rice.kew.api.action.ActionInvocationQueue;
048    import org.kuali.rice.kew.actiontaken.ActionTakenValue;
049    import org.kuali.rice.kew.api.KewApiConstants;
050    import org.kuali.rice.kew.api.KewApiServiceLocator;
051    import org.kuali.rice.kew.api.WorkflowRuntimeException;
052    import org.kuali.rice.kew.api.action.AdHocRevoke;
053    import org.kuali.rice.kew.api.action.MovePoint;
054    import org.kuali.rice.kew.api.doctype.IllegalDocumentTypeException;
055    import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
056    import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
057    import org.kuali.rice.kew.api.exception.WorkflowException;
058    import org.kuali.rice.kew.engine.CompatUtils;
059    import org.kuali.rice.kew.engine.OrchestrationConfig;
060    import org.kuali.rice.kew.engine.RouteContext;
061    import org.kuali.rice.kew.engine.OrchestrationConfig.EngineCapability;
062    import org.kuali.rice.kew.engine.node.RouteNode;
063    import org.kuali.rice.kew.framework.postprocessor.PostProcessor;
064    import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
065    import org.kuali.rice.kew.routeheader.service.WorkflowDocumentService;
066    import org.kuali.rice.kew.service.KEWServiceLocator;
067    import org.kuali.rice.kim.api.identity.principal.Principal;
068    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
069    
070    import java.sql.Timestamp;
071    import java.util.Collections;
072    import java.util.Date;
073    import java.util.HashSet;
074    import java.util.List;
075    import java.util.Set;
076    
077    /**
078     * @author Kuali Rice Team (rice.collab@kuali.org)
079     *
080     * this class mainly interacts with ActionEvent 'action' classes and non-vo objects.
081     *
082     */
083    
084    public class WorkflowDocumentServiceImpl implements WorkflowDocumentService {
085    
086            private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(WorkflowDocumentServiceImpl.class);
087    
088            private void init(DocumentRouteHeaderValue routeHeader) {
089                    KEWServiceLocator.getRouteHeaderService().lockRouteHeader(routeHeader.getDocumentId(), true);
090                    KEWServiceLocator.getRouteHeaderService().saveRouteHeader(routeHeader);
091            }
092    
093        private DocumentRouteHeaderValue finish(DocumentRouteHeaderValue routeHeader) {
094            // reload the document from the database to get a "fresh and clean" copy if we aren't in the context of a
095            // document being routed
096            if (RouteContext.getCurrentRouteContext().getDocument() == null) {
097                    return KEWServiceLocator.getRouteHeaderService().getRouteHeader(routeHeader.getDocumentId(), true);
098            } else {
099                    // we could enter this case if someone calls a method on WorkflowDocument (such as app specific route)
100                    // from their post processor, in that case, if we cleared the database case as above we would
101                    // end up getting an optimistic lock exception when the engine attempts to save the document after
102                    // the post processor call
103                    return routeHeader;
104            }
105        }
106    
107            public DocumentRouteHeaderValue acknowledgeDocument(String principalId, DocumentRouteHeaderValue routeHeader, String annotation) throws InvalidActionTakenException {
108                    Principal principal = loadPrincipal(principalId);
109                    AcknowledgeAction action = new AcknowledgeAction(routeHeader, principal, annotation);
110                    action.performAction();
111                    return finish(routeHeader);
112            }
113    
114            public DocumentRouteHeaderValue releaseGroupAuthority(String principalId, DocumentRouteHeaderValue routeHeader, String groupId, String annotation) throws InvalidActionTakenException {
115                    Principal principal = loadPrincipal(principalId);
116                    ReleaseWorkgroupAuthority action = new ReleaseWorkgroupAuthority(routeHeader, principal, annotation, groupId);
117                    action.performAction();
118                    return finish(routeHeader);
119            }
120    
121            public DocumentRouteHeaderValue takeGroupAuthority(String principalId, DocumentRouteHeaderValue routeHeader, String groupId, String annotation) throws InvalidActionTakenException {
122                    Principal principal = loadPrincipal(principalId);
123                    TakeWorkgroupAuthority action = new TakeWorkgroupAuthority(routeHeader, principal, annotation, groupId);
124                    action.performAction();
125                    return finish(routeHeader);
126            }
127    
128            public DocumentRouteHeaderValue approveDocument(String principalId, DocumentRouteHeaderValue routeHeader, String annotation) throws InvalidActionTakenException {
129                    Principal principal = loadPrincipal(principalId);
130                    ApproveAction action = new ApproveAction(routeHeader, principal, annotation);
131                    action.performAction();
132                    return finish(routeHeader);
133            }
134            
135            public DocumentRouteHeaderValue placeInExceptionRouting(String principalId, DocumentRouteHeaderValue routeHeader, String annotation) throws InvalidActionTakenException {
136                    try {
137                // sends null as the PersistedMessage since this is an explicit external call.
138                            KEWServiceLocator.getExceptionRoutingService().placeInExceptionRouting(annotation, null, routeHeader.getDocumentId());
139                    } catch (Exception e) {
140                            throw new RiceRuntimeException("Failed to place the document into exception routing!", e);
141                    }
142                    return finish(routeHeader);
143             }
144    
145            public DocumentRouteHeaderValue adHocRouteDocumentToPrincipal(String principalId, DocumentRouteHeaderValue document, String actionRequested, String nodeName, Integer priority, String annotation, String targetPrincipalId,
146                            String responsibilityDesc, Boolean forceAction, String requestLabel) throws WorkflowException {
147                    Principal principal = loadPrincipal(principalId);
148                    Recipient recipient = KEWServiceLocator.getIdentityHelperService().getPrincipalRecipient(targetPrincipalId);
149                    AdHocAction action = new AdHocAction(document, principal, annotation, actionRequested, nodeName, priority, recipient, responsibilityDesc, forceAction, requestLabel);
150                    action.performAction();
151                    return finish(document);
152            }
153    
154            public DocumentRouteHeaderValue adHocRouteDocumentToGroup(String principalId, DocumentRouteHeaderValue document, String actionRequested, String nodeName, Integer priority, String annotation, String groupId,
155                            String responsibilityDesc, Boolean forceAction, String requestLabel) throws WorkflowException {
156                    Principal principal = loadPrincipal(principalId);
157                    final Recipient recipient = new KimGroupRecipient(KimApiServiceLocator.getGroupService().getGroup(groupId));
158                    AdHocAction action = new AdHocAction(document, principal, annotation, actionRequested, nodeName, priority, recipient, responsibilityDesc, forceAction, requestLabel);
159                    action.performAction();
160                    return finish(document);
161            }
162    
163            public DocumentRouteHeaderValue blanketApproval(String principalId, DocumentRouteHeaderValue routeHeader, String annotation, Integer routeLevel) throws InvalidActionTakenException {
164                    RouteNode node = (routeLevel == null ? null : CompatUtils.getNodeForLevel(routeHeader.getDocumentType(), routeLevel));
165                    if (node == null && routeLevel != null) {
166                            throw new InvalidActionTakenException("Could not locate node for route level " + routeLevel);
167                    }
168                    Set<String> nodeNames = new HashSet<String>();
169                    if (node != null) {
170                            nodeNames = Collections.singleton(node.getRouteNodeName());
171                    }
172                    Principal principal = loadPrincipal(principalId);
173                    ActionTakenEvent action = new BlanketApproveAction(routeHeader, principal, annotation, nodeNames);
174                    action.performAction();
175                    return finish(routeHeader);
176            }
177    
178            public DocumentRouteHeaderValue blanketApproval(String principalId, DocumentRouteHeaderValue routeHeader, String annotation, Set nodeNames) throws InvalidActionTakenException {
179                    Principal principal = loadPrincipal(principalId);
180                    BlanketApproveAction action = new BlanketApproveAction(routeHeader, principal, annotation, nodeNames);
181                    action.recordAction();
182    
183                    return finish(routeHeader);
184            }
185    
186            public DocumentRouteHeaderValue cancelDocument(String principalId, DocumentRouteHeaderValue routeHeader, String annotation) throws InvalidActionTakenException {
187                    // init(routeHeader);
188                    Principal principal = loadPrincipal(principalId);
189                    CancelAction action = new CancelAction(routeHeader, principal, annotation);
190                    action.recordAction();
191                    indexForSearchAfterActionIfNecessary(routeHeader);
192                    return finish(routeHeader);
193            }
194    
195        public DocumentRouteHeaderValue recallDocument(String principalId, DocumentRouteHeaderValue routeHeader, String annotation, boolean cancel) throws InvalidActionTakenException {
196            // init(routeHeader);
197            Principal principal = loadPrincipal(principalId);
198            RecallAction action = new RecallAction(routeHeader, principal, annotation, cancel);
199            action.performAction();
200            indexForSearchAfterActionIfNecessary(routeHeader);
201            return finish(routeHeader);
202        }
203            
204            /**
205             * Does a search index after a non-post processing action completes
206             * @param routeHeader the route header of the document just acted upon
207             */
208            protected void indexForSearchAfterActionIfNecessary(DocumentRouteHeaderValue routeHeader) {
209                    RouteContext routeContext = RouteContext.getCurrentRouteContext();
210                    if (routeHeader.getDocumentType().hasSearchableAttributes() && routeContext.isSearchIndexingRequestedForContext()) {
211                DocumentAttributeIndexingQueue queue = KewApiServiceLocator.getDocumentAttributeIndexingQueue(routeHeader.getDocumentType().getApplicationId());
212                queue.indexDocument(routeHeader.getDocumentId());
213                    }
214            }
215    
216            public DocumentRouteHeaderValue clearFYIDocument(String principalId, DocumentRouteHeaderValue routeHeader, String annotation) throws InvalidActionTakenException {
217                    // init(routeHeader);
218                    Principal principal = loadPrincipal(principalId);
219                    ClearFYIAction action = new ClearFYIAction(routeHeader, principal, annotation);
220                    action.recordAction();
221                    return finish(routeHeader);
222            }
223    
224            public DocumentRouteHeaderValue completeDocument(String principalId, DocumentRouteHeaderValue routeHeader, String annotation) throws InvalidActionTakenException {
225                    Principal principal = loadPrincipal(principalId);
226                    CompleteAction action = new CompleteAction(routeHeader, principal, annotation);
227                    action.performAction();
228                    return finish(routeHeader);
229            }
230    
231            public DocumentRouteHeaderValue createDocument(String principalId, DocumentRouteHeaderValue routeHeader) throws WorkflowException {
232    
233                    if (routeHeader.getDocumentId() != null) { // this is a debateable
234                                                                                                                    // check - means the
235                                                                                                                    // client is off
236                            throw new InvalidActionTakenException("Document already has a Document id");
237                    }
238                    Principal principal = loadPrincipal(principalId);
239                    boolean canInitiate = KEWServiceLocator.getDocumentTypePermissionService().canInitiate(principalId, routeHeader.getDocumentType());
240    
241                    if (!canInitiate) {
242                            throw new InvalidActionTakenException("Principal with name '" + principal.getPrincipalName() + "' is not authorized to initiate documents of type '" + routeHeader.getDocumentType().getName());
243                    }
244    
245            if (!routeHeader.getDocumentType().isDocTypeActive()) {
246                // don't allow creation if document type is inactive
247                throw new IllegalDocumentTypeException("Document type '" + routeHeader.getDocumentType().getName() + "' is inactive");
248            }
249    
250                    routeHeader.setInitiatorWorkflowId(principalId);
251                    if (routeHeader.getDocRouteStatus() == null) {
252                            routeHeader.setDocRouteStatus(KewApiConstants.ROUTE_HEADER_INITIATED_CD);
253                    }
254                    if (routeHeader.getDocRouteLevel() == null) {
255                            routeHeader.setDocRouteLevel(Integer.valueOf(KewApiConstants.ADHOC_ROUTE_LEVEL));
256                    }
257                    if (routeHeader.getCreateDate() == null) {
258                            routeHeader.setCreateDate(new Timestamp(new Date().getTime()));
259                    }
260                    if (routeHeader.getDocVersion() == null) {
261                            routeHeader.setDocVersion(Integer.valueOf(KewApiConstants.DocumentContentVersions.CURRENT));
262                    }
263                    if (routeHeader.getDocContent() == null) {
264                            routeHeader.setDocContent(KewApiConstants.DEFAULT_DOCUMENT_CONTENT);
265                    }
266                    routeHeader.setDateModified(new Timestamp(new Date().getTime()));
267                    KEWServiceLocator.getRouteHeaderService().saveRouteHeader(routeHeader);
268                    OrchestrationConfig config = new OrchestrationConfig(EngineCapability.STANDARD);
269                    KEWServiceLocator.getWorkflowEngineFactory().newEngine(config).initializeDocument(routeHeader);
270                    KEWServiceLocator.getRouteHeaderService().saveRouteHeader(routeHeader);
271                    return routeHeader;
272            }
273    
274            public DocumentRouteHeaderValue disapproveDocument(String principalId, DocumentRouteHeaderValue routeHeader, String annotation) throws InvalidActionTakenException {
275                    Principal principal = loadPrincipal(principalId);
276                    DisapproveAction action = new DisapproveAction(routeHeader, principal, annotation);
277                    action.recordAction();
278                    indexForSearchAfterActionIfNecessary(routeHeader);
279                    return finish(routeHeader);
280            }
281    
282            public DocumentRouteHeaderValue returnDocumentToPreviousRouteLevel(String principalId, DocumentRouteHeaderValue routeHeader, Integer destRouteLevel, String annotation)
283                    throws InvalidActionTakenException {
284                    DocumentRouteHeaderValue result = null;
285                    
286                    if (destRouteLevel != null) {
287                            RouteNode node = CompatUtils.getNodeForLevel(routeHeader.getDocumentType(), destRouteLevel);
288                            if (node == null) {
289                                    throw new InvalidActionTakenException("Could not locate node for route level " + destRouteLevel);
290                            }
291    
292                            Principal principal = loadPrincipal(principalId);
293                            ReturnToPreviousNodeAction action = new ReturnToPreviousNodeAction(routeHeader, principal, annotation, node.getRouteNodeName(), true);
294                            action.performAction();
295                            result = finish(routeHeader);
296                    }
297                    return result;
298            }
299    
300            public DocumentRouteHeaderValue returnDocumentToPreviousNode(String principalId, DocumentRouteHeaderValue routeHeader, String destinationNodeName, String annotation)
301                            throws InvalidActionTakenException {
302                    Principal principal = loadPrincipal(principalId);
303                    ReturnToPreviousNodeAction action = new ReturnToPreviousNodeAction(routeHeader, principal, annotation, destinationNodeName, true);
304                    action.performAction();
305                    return finish(routeHeader);
306            }
307    
308            public DocumentRouteHeaderValue routeDocument(String principalId, DocumentRouteHeaderValue routeHeader, String annotation) throws WorkflowException,
309                            InvalidActionTakenException {
310                    Principal principal = loadPrincipal(principalId);
311                    RouteDocumentAction actionEvent = new RouteDocumentAction(routeHeader, principal, annotation);
312                    actionEvent.performAction();
313            LOG.info("routeDocument: " + routeHeader);
314                    return finish(routeHeader);
315            }
316    
317            public DocumentRouteHeaderValue saveRoutingData(String principalId, DocumentRouteHeaderValue routeHeader) {
318                    KEWServiceLocator.getRouteHeaderService().saveRouteHeader(routeHeader);
319                    
320                    // save routing data should invoke the post processor doActionTaken for SAVE
321                    ActionTakenValue val = new ActionTakenValue();
322                    val.setActionTaken(KewApiConstants.ACTION_TAKEN_SAVED_CD);
323                    val.setDocumentId(routeHeader.getDocumentId());
324            val.setPrincipalId(principalId);
325                    PostProcessor postProcessor = routeHeader.getDocumentType().getPostProcessor();
326                    try {
327                            postProcessor.doActionTaken(new org.kuali.rice.kew.framework.postprocessor.ActionTakenEvent(routeHeader.getDocumentId(), routeHeader.getAppDocId(), ActionTakenValue.to(val)));
328                    } catch (Exception e) {
329                            if (e instanceof RuntimeException) {
330                                    throw (RuntimeException)e;
331                            }
332                            throw new WorkflowRuntimeException(e);
333                    }
334    
335                    RouteContext routeContext = RouteContext.getCurrentRouteContext();
336                    if (routeHeader.getDocumentType().hasSearchableAttributes() && !routeContext.isSearchIndexingRequestedForContext()) {
337                            routeContext.requestSearchIndexingForContext();
338                DocumentAttributeIndexingQueue queue = KewApiServiceLocator.getDocumentAttributeIndexingQueue(routeHeader.getDocumentType().getApplicationId());
339                queue.indexDocument(routeHeader.getDocumentId());
340                    }
341                    return finish(routeHeader);
342            }
343    
344            public DocumentRouteHeaderValue saveDocument(String principalId, DocumentRouteHeaderValue routeHeader, String annotation) throws InvalidActionTakenException {
345                    Principal principal = loadPrincipal(principalId);
346                    SaveActionEvent action = new SaveActionEvent(routeHeader, principal, annotation);
347                    action.performAction();
348                    return finish(routeHeader);
349            }
350    
351            public void deleteDocument(String principalId, DocumentRouteHeaderValue routeHeader) throws WorkflowException {
352                    if (routeHeader.getDocumentId() == null) {
353                            LOG.debug("Null Document id passed.");
354                            throw new WorkflowException("Document id must not be null.");
355                    }
356                    KEWServiceLocator.getRouteHeaderService().deleteRouteHeader(routeHeader);
357            }
358    
359            public void logDocumentAction(String principalId, DocumentRouteHeaderValue routeHeader, String annotation) throws InvalidActionTakenException {
360                    Principal principal = loadPrincipal(principalId);
361                    LogDocumentActionAction action = new LogDocumentActionAction(routeHeader, principal, annotation);
362                    action.recordAction();
363            }
364    
365            public DocumentRouteHeaderValue moveDocument(String principalId, DocumentRouteHeaderValue routeHeader, MovePoint movePoint, String annotation) throws InvalidActionTakenException {
366                    Principal principal = loadPrincipal(principalId);
367                    MoveDocumentAction action = new MoveDocumentAction(routeHeader, principal, annotation, movePoint);
368                    action.performAction();
369                    return finish(routeHeader);
370            }
371    
372            public DocumentRouteHeaderValue superUserActionRequestApproveAction(String principalId, DocumentRouteHeaderValue routeHeader, String actionRequestId, String annotation, boolean runPostProcessor)
373                            throws InvalidActionTakenException {
374                    init(routeHeader);
375                    Principal principal = loadPrincipal(principalId);
376                    SuperUserActionRequestApproveEvent suActionRequestApprove = new SuperUserActionRequestApproveEvent(routeHeader, principal, actionRequestId, annotation, runPostProcessor);
377                    suActionRequestApprove.recordAction();
378                    // suActionRequestApprove.queueDocument();
379                    RouteContext.getCurrentRouteContext().requestSearchIndexingForContext(); // make sure indexing is requested
380                    indexForSearchAfterActionIfNecessary(routeHeader);
381                    return finish(routeHeader);
382            }
383    
384        /**
385         * TODO As with superUserReturnDocumentToPreviousNode, we allow for the passing in of a document ID here to allow for
386         * the document load inside the current running transaction.  Otherwise we get an optimistic lock exception
387         * when attempting to save the branch after the transition to the 'A' status.
388         */
389        public DocumentRouteHeaderValue superUserActionRequestApproveAction(String principalId, String documentId, String actionRequestId, String annotation, boolean runPostProcessor)
390            throws InvalidActionTakenException {
391            return superUserActionRequestApproveAction(principalId, KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId), actionRequestId, annotation, runPostProcessor);
392        }
393    
394            public DocumentRouteHeaderValue superUserApprove(String principalId, DocumentRouteHeaderValue routeHeader, String annotation, boolean runPostProcessor) throws InvalidActionTakenException {
395                    init(routeHeader);
396                    Principal principal = loadPrincipal(principalId);
397                    new SuperUserApproveEvent(routeHeader, principal, annotation, runPostProcessor).recordAction();
398                    RouteContext.getCurrentRouteContext().requestSearchIndexingForContext(); // make sure indexing is requested
399                    indexForSearchAfterActionIfNecessary(routeHeader);
400                    return finish(routeHeader);
401            }
402    
403            public DocumentRouteHeaderValue superUserCancelAction(String principalId, DocumentRouteHeaderValue routeHeader, String annotation, boolean runPostProcessor) throws InvalidActionTakenException {
404                    init(routeHeader);
405                    Principal principal = loadPrincipal(principalId);
406                    new SuperUserCancelEvent(routeHeader, principal, annotation, runPostProcessor).recordAction();
407                    RouteContext.getCurrentRouteContext().requestSearchIndexingForContext(); // make sure indexing is requested
408                    indexForSearchAfterActionIfNecessary(routeHeader);
409                    return finish(routeHeader);
410            }
411    
412            public DocumentRouteHeaderValue superUserDisapproveAction(String principalId, DocumentRouteHeaderValue routeHeader, String annotation, boolean runPostProcessor) throws InvalidActionTakenException {
413                    init(routeHeader);
414                    Principal principal = loadPrincipal(principalId);
415                    new SuperUserDisapproveEvent(routeHeader, principal, annotation, runPostProcessor).recordAction();
416                    RouteContext.getCurrentRouteContext().requestSearchIndexingForContext(); // make sure indexing is requested
417                    indexForSearchAfterActionIfNecessary(routeHeader);
418                    return finish(routeHeader);
419            }
420    
421            public DocumentRouteHeaderValue superUserNodeApproveAction(String principalId, DocumentRouteHeaderValue routeHeader, String nodeName, String annotation, boolean runPostProcessor) throws InvalidActionTakenException {
422                    init(routeHeader);
423                    Principal principal = loadPrincipal(principalId);
424                    new SuperUserNodeApproveEvent(routeHeader, principal, annotation, runPostProcessor, nodeName).recordAction();
425                    indexForSearchAfterActionIfNecessary(routeHeader);
426                    return finish(routeHeader);
427            }
428    
429            /**
430             * TODO As with superUserReturnDocumentToPreviousNode, we allow for the passing in of a document ID here to allow for
431             * the document load inside the current running transaction.  Otherwise we get an optimistic lock exception
432             * when attempting to save the branch after the transition to the 'A' status.
433             */
434            public DocumentRouteHeaderValue superUserNodeApproveAction(String principalId, String documentId, String nodeName, String annotation, boolean runPostProcessor) throws InvalidActionTakenException {
435                    return superUserNodeApproveAction(principalId, KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId), nodeName, annotation, runPostProcessor);
436            }
437    
438            /**
439             * TODO remove this implementation in favor of having the SuperUserAction call through the WorkflowDocument object.  This
440             * method is here to resolve KULWF-727 where we were getting an optimistic lock exception from the super user screen on
441             * return to previous node.  This allows us to load the DocumentRouteHeaderValue inside of the transaction interceptor
442             * so that we can stay within the same PersistenceBroker cache.
443             */
444            public DocumentRouteHeaderValue superUserReturnDocumentToPreviousNode(String principalId, String documentId, String nodeName, String annotation, boolean runPostProcessor)
445                    throws InvalidActionTakenException {
446                    return superUserReturnDocumentToPreviousNode(principalId, KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId), nodeName, annotation, runPostProcessor);
447            }
448    
449            public DocumentRouteHeaderValue superUserReturnDocumentToPreviousNode(String principalId, DocumentRouteHeaderValue routeHeader, String nodeName, String annotation, boolean runPostProcessor)
450                            throws InvalidActionTakenException {
451                    init(routeHeader);
452                    Principal principal = loadPrincipal(principalId);
453                    SuperUserReturnToPreviousNodeAction action = new SuperUserReturnToPreviousNodeAction(routeHeader, principal, annotation, runPostProcessor, nodeName);
454                    action.recordAction();
455                    RouteContext.getCurrentRouteContext().requestSearchIndexingForContext(); // make sure indexing is requested
456                    indexForSearchAfterActionIfNecessary(routeHeader);
457                    return finish(routeHeader);
458            }
459    
460            public void takeMassActions(String principalId, List<ActionInvocation> actionInvocations) {
461                    Principal principal = loadPrincipal(principalId);
462                    for (ActionInvocation invocation : actionInvocations) {
463                            ActionItem actionItem = KEWServiceLocator.getActionListService().findByActionItemId(invocation.getActionItemId());
464                            if (actionItem == null) {
465                                    LOG.warn("Could not locate action item for the given action item id [" + invocation.getActionItemId() + "], not taking mass action on it.");
466                                    continue;
467                            }
468                            KEWServiceLocator.getActionListService().deleteActionItem(actionItem, true);
469                DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(actionItem.getDocumentId());
470                String applicationId = document.getDocumentType().getApplicationId();
471                            ActionInvocationQueue actionInvocQueue = KewApiServiceLocator.getActionInvocationProcessorService(
472                        document.getDocumentId(), applicationId);
473                            actionInvocQueue.invokeAction(principalId, actionItem.getDocumentId(), invocation);
474    //                      ActionInvocationQueueImpl.queueActionInvocation(user, actionItem.getDocumentId(), invocation);
475                    }
476            }
477    
478            public DocumentRouteHeaderValue revokeAdHocRequests(String principalId, DocumentRouteHeaderValue document, AdHocRevoke revoke, String annotation) throws InvalidActionTakenException {
479                    Principal principal = loadPrincipal(principalId);
480                    RevokeAdHocAction action = new RevokeAdHocAction(document, principal, revoke, annotation);
481                    action.performAction();
482                    return finish(document);
483            }
484            
485            public DocumentRouteHeaderValue revokeAdHocRequests(String principalId, DocumentRouteHeaderValue document, String actionRequestId, String annotation) throws InvalidActionTakenException {
486                    Principal principal = loadPrincipal(principalId);
487                    RevokeAdHocAction action = new RevokeAdHocAction(document, principal, actionRequestId, annotation);
488                    action.performAction();
489                    return finish(document);
490            }
491    
492            protected Principal loadPrincipal(String principalId) {
493                    return KEWServiceLocator.getIdentityHelperService().getPrincipal(principalId);
494            }
495    
496    }