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