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