Coverage Report - org.kuali.rice.kew.actions.SaveActionEvent
 
Classes in this File Line Coverage Branch Coverage Complexity
SaveActionEvent
0%
0/45
0%
0/16
2.857
 
 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.MDC;
 20  
 import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
 21  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 22  
 import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
 23  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 24  
 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
 25  
 import org.kuali.rice.kew.exception.InvalidActionTakenException;
 26  
 import org.kuali.rice.kew.exception.WorkflowException;
 27  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 28  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 29  
 import org.kuali.rice.kew.util.KEWConstants;
 30  
 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
 31  
 
 32  
 
 33  
 import java.util.List;
 34  
 
 35  
 
 36  
 /**
 37  
  * Saves a document.  Puts the document in the persons action list that saved the document.
 38  
  * This can currently only be done by the initiator of the document.
 39  
  *
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 41  
  *
 42  
  */
 43  
 public class SaveActionEvent extends ActionTakenEvent {
 44  
 
 45  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(SaveActionEvent.class);
 46  
 
 47  
     private static final String RESPONSIBILITY_DESCRIPTION = "Initiator needs to complete document.";
 48  
 
 49  
     public SaveActionEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
 50  0
         super(KEWConstants.ACTION_TAKEN_SAVED_CD, routeHeader, principal);
 51  0
     }
 52  
 
 53  
     public SaveActionEvent(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation) {
 54  0
         super(KEWConstants.ACTION_TAKEN_SAVED_CD, routeHeader, principal, annotation);
 55  0
     }
 56  
 
 57  
     /* (non-Javadoc)
 58  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
 59  
      */
 60  
     @Override
 61  
     public String validateActionRules() {
 62  0
             return validateActionRulesCustom(true);
 63  
     }
 64  
 
 65  
     private String validateActionRulesCustom(boolean checkIfActionIsValid) {
 66  0
             if (checkIfActionIsValid && (!getRouteHeader().isValidActionToTake(getActionPerformedCode()))) {
 67  0
                     return "Document is not in a state to be saved";
 68  
             }
 69  
             // check state before checking kim
 70  0
             if (! KEWServiceLocator.getDocumentTypePermissionService().canSave(getPrincipal().getPrincipalId(), getRouteHeader().getDocumentId(), getRouteHeader().getDocumentType(), getRouteHeader().getCurrentNodeNames(), getRouteHeader().getDocRouteStatus(), getRouteHeader().getInitiatorWorkflowId())) {
 71  0
                     return "User is not authorized to Save document";
 72  
             }
 73  0
             return "";
 74  
     }
 75  
     
 76  
     /**
 77  
      * This overridden method ...
 78  
      * 
 79  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules(java.util.List)
 80  
      */
 81  
     @Override
 82  
     public String validateActionRules(List<ActionRequestValue> actionRequests) {
 83  0
             return validateActionRules();
 84  
     }
 85  
 
 86  
     public void recordAction() throws InvalidActionTakenException {
 87  0
         MDC.put("docId", getRouteHeader().getDocumentId());
 88  0
         LOG.debug("Checking to see if the action is legal");
 89  
         /* Code below for variable 'checkIfActionIsValid' is used to identify when the 
 90  
          * DocumentRouteHeaderValue 'legal actions' should be checked for the current
 91  
          * document.  The 'legal actions' for a document that is in status ENROUTE or 
 92  
          * EXCEPTION will currently say that a Save action is not valid to be performed
 93  
          * however we still want to allow the Save action to occur if called for backward
 94  
          * compatibility issues.
 95  
          */
 96  0
         boolean checkIfActionIsValid = true;
 97  0
         if (getRouteHeader().isEnroute() || getRouteHeader().isInException()) {
 98  
             // if document is enroute or exception... don't check if the action is valid... we will assume it is valid
 99  0
             checkIfActionIsValid = false;
 100  
         }
 101  0
         String errorMessage = validateActionRulesCustom(checkIfActionIsValid);
 102  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
 103  0
             throw new InvalidActionTakenException(errorMessage);
 104  
         }
 105  
 
 106  0
         updateSearchableAttributesIfPossible();
 107  
 
 108  
         //    if (getRouteHeader().isValidActionToTake(getActionTakenCode())) {
 109  0
         if (getRouteHeader().isStateInitiated()) {
 110  0
             LOG.debug("Record the save action");
 111  0
             ActionTakenValue actionTaken = saveActionTaken();
 112  
             //getRouteHeader().getActionRequests().add(generateSaveRequest());
 113  0
             this.getActionRequestService().saveActionRequest(generateSaveRequest());
 114  0
             notifyActionTaken(actionTaken);
 115  0
             LOG.debug("Marking document saved");
 116  
             try {
 117  0
                 String oldStatus = getRouteHeader().getDocRouteStatus();
 118  0
                 getRouteHeader().markDocumentSaved();
 119  0
                 String newStatus = getRouteHeader().getDocRouteStatus();
 120  0
                 notifyStatusChange(newStatus, oldStatus);
 121  0
                 KEWServiceLocator.getRouteHeaderService().saveRouteHeader(routeHeader);
 122  0
             } catch (WorkflowException ex) {
 123  0
                 LOG.warn(ex, ex);
 124  0
                 throw new InvalidActionTakenException(ex.getMessage());
 125  0
             }
 126  
         }
 127  0
     }
 128  
 
 129  
     protected ActionRequestValue generateSaveRequest() {
 130  0
         RouteNodeInstance initialNode = null;
 131  0
         List initialNodes = KEWServiceLocator.getRouteNodeService().getInitialNodeInstances(getDocumentId());
 132  0
             if (!initialNodes.isEmpty()) {
 133  0
                 initialNode = (RouteNodeInstance)initialNodes.get(0);
 134  
             }
 135  
         //RouteNodeInstance initialNode = (RouteNodeInstance) KEWServiceLocator.getRouteNodeService().getInitialNodeInstances(getDocumentId()).get(0);
 136  0
             ActionRequestFactory arFactory = new ActionRequestFactory(getRouteHeader(), initialNode);
 137  0
             ActionRequestValue saveRequest = arFactory.createActionRequest(KEWConstants.ACTION_REQUEST_COMPLETE_REQ,
 138  
                 0, new KimPrincipalRecipient(getPrincipal()), RESPONSIBILITY_DESCRIPTION, KEWConstants.SAVED_REQUEST_RESPONSIBILITY_ID,
 139  
                     Boolean.TRUE, annotation);
 140  
             //      this.getActionRequestService().saveActionRequest(saveRequest);
 141  0
             this.getActionRequestService().activateRequest(saveRequest);
 142  0
             return saveRequest;
 143  
     }
 144  
 
 145  
 }