Coverage Report - org.kuali.rice.kew.actions.ActionTakenEvent
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionTakenEvent
0%
0/96
0%
0/26
1.688
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kew.actions;
 17  
 
 18  
 import org.apache.log4j.Logger;
 19  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 20  
 import org.kuali.rice.kew.actionrequest.KimGroupRecipient;
 21  
 import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
 22  
 import org.kuali.rice.kew.actionrequest.Recipient;
 23  
 import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
 24  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 25  
 import org.kuali.rice.kew.api.KewApiServiceLocator;
 26  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 27  
 import org.kuali.rice.kew.api.document.DocumentProcessingOptions;
 28  
 import org.kuali.rice.kew.api.document.DocumentProcessingQueue;
 29  
 import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
 30  
 import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
 31  
 import org.kuali.rice.kew.engine.RouteContext;
 32  
 import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
 33  
 import org.kuali.rice.kew.framework.postprocessor.PostProcessor;
 34  
 import org.kuali.rice.kew.framework.postprocessor.ProcessDocReport;
 35  
 import org.kuali.rice.kew.messaging.MessageServiceNames;
 36  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 37  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 38  
 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
 39  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 40  
 
 41  
 import java.util.List;
 42  
 
 43  
 
 44  
 /**
 45  
  * Super class containing mostly often used methods by all actions. Holds common
 46  
  * state as well, {@link DocumentRouteHeaderValue} document,
 47  
  * {@link ActionTakenValue} action taken (once saved), {@link PrincipalContract} principal
 48  
  * that has taken the action
 49  
  *
 50  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 51  
  */
 52  
 public abstract class ActionTakenEvent {
 53  
 
 54  0
         private static final Logger LOG = Logger.getLogger(ActionTakenEvent.class);
 55  
 
 56  
         /**
 57  
          * Used when saving an ActionTakenValue, and for validation in validateActionRules
 58  
          */
 59  
         private String actionTakenCode;
 60  
 
 61  
         protected final String annotation;
 62  
 
 63  
         protected DocumentRouteHeaderValue routeHeader;
 64  
 
 65  
         private final PrincipalContract principal;
 66  
 
 67  
     private final boolean runPostProcessorLogic;
 68  
     
 69  
     private List<String> groupIdsForPrincipal;
 70  
     
 71  
 
 72  0
     private boolean queueDocumentAfterAction = true;
 73  
 
 74  
 
 75  
         public ActionTakenEvent(String actionTakenCode, DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
 76  0
                 this(actionTakenCode, routeHeader, principal, null, true);
 77  0
         }
 78  
 
 79  
     public ActionTakenEvent(String actionTakenCode, DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation) {
 80  0
         this(actionTakenCode, routeHeader, principal, annotation, true);
 81  0
     }
 82  
 
 83  0
         public ActionTakenEvent(String actionTakenCode, DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, boolean runPostProcessorLogic) {
 84  0
             this.actionTakenCode = actionTakenCode;
 85  0
             this.routeHeader = routeHeader;
 86  0
         this.principal = principal;
 87  0
         this.annotation = annotation == null ? "" : annotation;
 88  0
                 this.runPostProcessorLogic = runPostProcessorLogic;
 89  0
                 this.queueDocumentAfterAction = true;
 90  0
         }
 91  
 
 92  
         public ActionRequestService getActionRequestService() {
 93  0
                 return (ActionRequestService) KEWServiceLocator.getService(KEWServiceLocator.ACTION_REQUEST_SRV);
 94  
         }
 95  
 
 96  
         public DocumentRouteHeaderValue getRouteHeader() {
 97  0
                 return routeHeader;
 98  
         }
 99  
 
 100  
         public void setRouteHeader(DocumentRouteHeaderValue routeHeader) {
 101  0
                 this.routeHeader = routeHeader;
 102  0
         }
 103  
 
 104  
         public PrincipalContract getPrincipal() {
 105  0
                 return principal;
 106  
         }
 107  
 
 108  
         /**
 109  
          * Code of the action performed by the user
 110  
          *
 111  
          * Method may be overriden is action performed will be different than action
 112  
          * taken
 113  
      * @return
 114  
      */
 115  
         protected String getActionPerformedCode() {
 116  0
                 return getActionTakenCode();
 117  
         }
 118  
 
 119  
         /**
 120  
          * Validates whether or not this action is valid for the given principal
 121  
          * and DocumentRouteHeaderValue.
 122  
          */
 123  
         protected boolean isActionValid() {
 124  0
                 return org.apache.commons.lang.StringUtils.isEmpty(validateActionRules());
 125  
         }
 126  
 
 127  
         /**
 128  
          * Placeholder for validation rules for each action
 129  
          *
 130  
          * @return error message string of specific error message
 131  
          */
 132  
         public abstract String validateActionRules();
 133  
         public abstract String validateActionRules(List<ActionRequestValue> actionRequests);
 134  
         
 135  
         /**
 136  
          * Filters action requests based on if they occur after the given requestCode, and if they relate to this
 137  
          * event's principal
 138  
          * @param actionRequests the List of ActionRequestValues to filter
 139  
          * @param requestCode the request code for all ActionRequestValues to be after
 140  
          * @return the filtered List of ActionRequestValues
 141  
          */
 142  
         public List<ActionRequestValue> filterActionRequestsByCode(List<ActionRequestValue> actionRequests, String requestCode) {
 143  0
                 return getActionRequestService().filterActionRequestsByCode(actionRequests, getPrincipal().getPrincipalId(), getGroupIdsForPrincipal(), requestCode);
 144  
         }
 145  
 
 146  
         protected boolean isActionCompatibleRequest(List<ActionRequestValue> requests) {
 147  0
                 LOG.debug("isActionCompatibleRequest() Default method = returning true");
 148  0
                 return true;
 149  
         }
 150  
 
 151  
         public void performAction() throws InvalidActionTakenException {
 152  0
             recordAction();
 153  0
             if (queueDocumentAfterAction) {
 154  0
                     queueDocumentProcessing();
 155  
             }
 156  
 
 157  0
         }
 158  
 
 159  
         protected abstract void recordAction() throws InvalidActionTakenException;
 160  
 
 161  
         public void performDeferredAction() {
 162  
 
 163  0
         }
 164  
 
 165  
         protected void updateSearchableAttributesIfPossible() {
 166  
                 // queue the document up so that it can be indexed for searching if it
 167  
                 // has searchable attributes
 168  0
                 RouteContext routeContext = RouteContext.getCurrentRouteContext();
 169  0
                 if (routeHeader.getDocumentType().hasSearchableAttributes() && !routeContext.isSearchIndexingRequestedForContext()) {
 170  0
                         routeContext.requestSearchIndexingForContext();
 171  0
             DocumentAttributeIndexingQueue queue = KewApiServiceLocator.getDocumentAttributeIndexingQueue(routeHeader.getDocumentType().getApplicationId());
 172  0
             queue.indexDocument(getDocumentId());
 173  
                 }
 174  0
         }
 175  
 
 176  
         protected void notifyActionTaken(ActionTakenValue actionTaken) {
 177  0
             if (!isRunPostProcessorLogic()) {
 178  0
                 return;
 179  
             }
 180  0
                 if (actionTaken == null) {
 181  0
                         return;
 182  
                 }
 183  
                 try {
 184  0
                         LOG.debug("Notifying post processor of action taken");
 185  0
                         PostProcessor postProcessor = routeHeader.getDocumentType().getPostProcessor();
 186  0
                         ProcessDocReport report = postProcessor.doActionTaken(new org.kuali.rice.kew.framework.postprocessor.ActionTakenEvent(routeHeader.getDocumentId(), routeHeader.getAppDocId(), ActionTakenValue.to(actionTaken)));
 187  0
                         if (!report.isSuccess()) {
 188  0
                                 LOG.warn(report.getMessage(), report.getProcessException());
 189  0
                                 throw new InvalidActionTakenException(report.getMessage());
 190  
                         }
 191  
 
 192  0
                 } catch (Exception ex) {
 193  0
                     processPostProcessorException(ex);
 194  0
                 }
 195  0
         }
 196  
 
 197  
         protected void notifyStatusChange(String newStatusCode, String oldStatusCode) throws InvalidActionTakenException {
 198  0
         if (!isRunPostProcessorLogic()) {
 199  0
             return;
 200  
         }
 201  0
                 DocumentRouteStatusChange statusChangeEvent = new DocumentRouteStatusChange(routeHeader.getDocumentId(), routeHeader.getAppDocId(), oldStatusCode, newStatusCode);
 202  
                 try {
 203  0
                         LOG.debug("Notifying post processor of status change " + oldStatusCode + "->" + newStatusCode);
 204  0
                         PostProcessor postProcessor = routeHeader.getDocumentType().getPostProcessor();
 205  0
                         ProcessDocReport report = postProcessor.doRouteStatusChange(statusChangeEvent);
 206  0
                         if (!report.isSuccess()) {
 207  0
                                 LOG.warn(report.getMessage(), report.getProcessException());
 208  0
                                 throw new InvalidActionTakenException(report.getMessage());
 209  
                         }
 210  0
                 } catch (Exception ex) {
 211  0
                     processPostProcessorException(ex);
 212  0
                 }
 213  0
         }
 214  
 
 215  
         /**
 216  
          * Asynchronously queues the documented to be processed by the workflow engine.
 217  
          */
 218  
         protected void queueDocumentProcessing() {
 219  0
                 DocumentProcessingQueue documentProcessingQueue = (DocumentProcessingQueue) MessageServiceNames.getDocumentProcessingQueue(getRouteHeader());
 220  0
         DocumentProcessingOptions options = DocumentProcessingOptions.create(isRunPostProcessorLogic(), RouteContext.getCurrentRouteContext().isSearchIndexingRequestedForContext());
 221  0
         documentProcessingQueue.processWithOptions(getDocumentId(), options);
 222  0
         }
 223  
 
 224  
         protected ActionTakenValue saveActionTaken() {
 225  0
             return saveActionTaken(Boolean.TRUE);
 226  
         }
 227  
 
 228  
         protected ActionTakenValue saveActionTaken(Boolean currentInd) {
 229  0
                 return saveActionTaken(currentInd, null);
 230  
         }
 231  
 
 232  
         protected ActionTakenValue saveActionTaken(Recipient delegator) {
 233  0
             return saveActionTaken(Boolean.TRUE, delegator);
 234  
         }
 235  
 
 236  
         protected ActionTakenValue saveActionTaken(Boolean currentInd, Recipient delegator) {
 237  0
                 ActionTakenValue val = new ActionTakenValue();
 238  0
                 val.setActionTaken(getActionTakenCode());
 239  0
                 val.setAnnotation(annotation);
 240  0
                 val.setDocVersion(routeHeader.getDocVersion());
 241  0
                 val.setDocumentId(routeHeader.getDocumentId());
 242  0
                 val.setPrincipalId(principal.getPrincipalId());
 243  0
                 if (delegator instanceof KimPrincipalRecipient) {
 244  0
                         val.setDelegatorPrincipalId(((KimPrincipalRecipient)delegator).getPrincipalId());
 245  0
                 } else if (delegator instanceof KimGroupRecipient) {
 246  0
                         val.setDelegatorGroupId(((KimGroupRecipient) delegator).getGroupId());
 247  
                 }
 248  
                 //val.setRouteHeader(routeHeader);
 249  0
                 val.setCurrentIndicator(currentInd);
 250  0
                 KEWServiceLocator.getActionTakenService().saveActionTaken(val);
 251  0
                 return val;
 252  
         }
 253  
 
 254  
         /**
 255  
          * Returns the highest priority delegator in the list of action requests.
 256  
          */
 257  
         protected Recipient findDelegatorForActionRequests(List actionRequests) {
 258  0
                 return getActionRequestService().findDelegator(actionRequests);
 259  
         }
 260  
 
 261  
         public String getActionTakenCode() {
 262  0
                 return actionTakenCode;
 263  
         }
 264  
 
 265  
         protected void setActionTakenCode(String string) {
 266  0
                 actionTakenCode = string;
 267  0
         }
 268  
 
 269  
         protected String getDocumentId() {
 270  0
                 return this.routeHeader.getDocumentId();
 271  
         }
 272  
 
 273  
         /*protected void delete() {
 274  
             KEWServiceLocator.getActionTakenService().delete(actionTaken);
 275  
         }*/
 276  
 
 277  
         protected boolean isRunPostProcessorLogic() {
 278  0
         return this.runPostProcessorLogic;
 279  
     }
 280  
         
 281  
         protected List<String> getGroupIdsForPrincipal() {
 282  0
                 if (groupIdsForPrincipal == null) {
 283  0
                         groupIdsForPrincipal = KimApiServiceLocator.getGroupService().getGroupIdsByPrincipalId(
 284  
                     getPrincipal().getPrincipalId());
 285  
                 }
 286  0
                 return groupIdsForPrincipal;
 287  
         }
 288  
 
 289  
 
 290  
         public void setQueueDocumentAfterAction(boolean queueDocumentAfterAction) {
 291  0
                 this.queueDocumentAfterAction = queueDocumentAfterAction;
 292  0
         }
 293  
 
 294  
         private void processPostProcessorException(Exception e) {
 295  0
         if (e instanceof RuntimeException) {
 296  0
             throw (RuntimeException)e;
 297  
         }
 298  0
         throw new WorkflowRuntimeException(e);
 299  
         }
 300  
 
 301  
         
 302  
 }