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