Coverage Report - org.kuali.rice.kew.actions.DisapproveAction
 
Classes in this File Line Coverage Branch Coverage Complexity
DisapproveAction
0%
0/74
0%
0/32
4.143
 
 1  
 /*
 2  
  * Copyright 2006-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  
 
 17  
 package org.kuali.rice.kew.actions;
 18  
 
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.apache.log4j.MDC;
 21  
 import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
 22  
 import org.kuali.rice.kew.actionrequest.ActionRequestFactory;
 23  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 24  
 import org.kuali.rice.kew.actionrequest.Recipient;
 25  
 import org.kuali.rice.kew.actiontaken.ActionTakenValue;
 26  
 import org.kuali.rice.kew.engine.node.RouteNodeInstance;
 27  
 import org.kuali.rice.kew.exception.InvalidActionTakenException;
 28  
 import org.kuali.rice.kew.exception.WorkflowException;
 29  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 30  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 31  
 import org.kuali.rice.kew.util.KEWConstants;
 32  
 import org.kuali.rice.kew.util.Utilities;
 33  
 import org.kuali.rice.kim.api.entity.principal.PrincipalContract;
 34  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 35  
 import org.kuali.rice.kim.api.group.Group;
 36  
 
 37  
 import org.kuali.rice.kns.util.KNSConstants;
 38  
 
 39  
 import java.util.Collection;
 40  
 import java.util.HashSet;
 41  
 import java.util.Iterator;
 42  
 import java.util.List;
 43  
 import java.util.Set;
 44  
 
 45  
 
 46  
 /**
 47  
  * Disapproves a document. This deactivates all requests on the document and sends
 48  
  * acknowlegde requests to anybody who had already completed or approved the document.
 49  
  *
 50  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 51  
  */
 52  
 public class DisapproveAction extends ActionTakenEvent {
 53  0
     private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisapproveAction.class);
 54  
 
 55  
     /**
 56  
      * @param rh RouteHeader for the document upon which the action is taken.
 57  
      * @param principal User taking the action.
 58  
      */
 59  
     public DisapproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
 60  0
         super(KEWConstants.ACTION_TAKEN_DENIED_CD, rh, principal);
 61  0
     }
 62  
 
 63  
     /**
 64  
      * @param rh RouteHeader for the document upon which the action is taken.
 65  
      * @param principal User taking the action.
 66  
      * @param annotation User comment on the action taken
 67  
      */
 68  
     public DisapproveAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
 69  0
         super(KEWConstants.ACTION_TAKEN_DENIED_CD, rh, principal, annotation);
 70  0
     }
 71  
 
 72  
     /* (non-Javadoc)
 73  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
 74  
      */
 75  
     @Override
 76  
     public String validateActionRules() {
 77  0
             return validateActionRules(getActionRequestService().findAllPendingRequests(routeHeader.getDocumentId()));
 78  
     }
 79  
 
 80  
     public String validateActionRules(List<ActionRequestValue> actionRequests) {
 81  0
         if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
 82  0
             return "Document is not in a state to be disapproved";
 83  
         }
 84  0
         List<ActionRequestValue> filteredActionRequests = filterActionRequestsByCode(actionRequests, KEWConstants.ACTION_REQUEST_COMPLETE_REQ);
 85  0
         if (!isActionCompatibleRequest(filteredActionRequests)) {
 86  0
             return "No request for the user is compatible " + "with the DISAPPROVE or DENY action";
 87  
         }
 88  0
         return "";
 89  
     }
 90  
 
 91  
     /* (non-Javadoc)
 92  
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
 93  
      */
 94  
     @Override
 95  
     public boolean isActionCompatibleRequest(List requests) {
 96  
         // can always cancel saved or initiated document
 97  0
         if (routeHeader.isStateInitiated() || routeHeader.isStateSaved()) {
 98  0
             return true;
 99  
         }
 100  
 
 101  0
         boolean actionCompatible = false;
 102  0
         Iterator ars = requests.iterator();
 103  0
         ActionRequestValue actionRequest = null;
 104  
 
 105  0
         while (ars.hasNext()) {
 106  0
             actionRequest = (ActionRequestValue) ars.next();
 107  0
             String request = actionRequest.getActionRequested();
 108  
 
 109  
             // APPROVE request matches all but FYI and ACK
 110  0
             if ( (KEWConstants.ACTION_REQUEST_APPROVE_REQ.equals(request)) ||
 111  
                  (KEWConstants.ACTION_REQUEST_COMPLETE_REQ.equals(request)) ) {
 112  0
                 actionCompatible = true;
 113  0
                 break;
 114  
             }
 115  0
         }
 116  
 
 117  0
         return actionCompatible;
 118  
     }
 119  
 
 120  
     /**
 121  
      * Records the disapprove action. - Checks to make sure the document status allows the action. - Checks that the user has not taken a previous action. - Deactivates the pending requests for this user - Records the action
 122  
      *
 123  
      * @throws InvalidActionTakenException
 124  
      */
 125  
     public void recordAction() throws InvalidActionTakenException {
 126  0
         MDC.put("docId", getRouteHeader().getDocumentId());
 127  0
         updateSearchableAttributesIfPossible();
 128  
 
 129  0
         LOG.debug("Disapproving document : " + annotation);
 130  
 
 131  0
         List actionRequests = getActionRequestService().findAllValidRequests(getPrincipal().getPrincipalId(), getDocumentId(), KEWConstants.ACTION_REQUEST_COMPLETE_REQ);
 132  0
         LOG.debug("Checking to see if the action is legal");
 133  0
         String errorMessage = validateActionRules(actionRequests);
 134  0
         if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
 135  0
             throw new InvalidActionTakenException(errorMessage);
 136  
         }
 137  
 
 138  0
         LOG.debug("Record the disapproval action");
 139  0
         Recipient delegator = findDelegatorForActionRequests(actionRequests);
 140  0
         ActionTakenValue actionTaken = saveActionTaken(delegator);
 141  
 
 142  0
         LOG.debug("Deactivate all pending action requests");
 143  0
         actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
 144  0
         getActionRequestService().deactivateRequests(actionTaken, actionRequests);
 145  0
         notifyActionTaken(actionTaken);
 146  
 
 147  0
         LOG.debug("Sending Acknowledgements to all previous approvers/completers");
 148  
                     // Generate the notification requests in the first node we find that the current user has an approve request
 149  0
         RouteNodeInstance notificationNodeInstance = null;
 150  
 //        if (actionRequests.size() > 0) { //I don't see why this matters let me know if it does rk
 151  0
                 notificationNodeInstance = ((ActionRequestValue)actionRequests.get(0)).getNodeInstance();
 152  
 //        }
 153  0
         generateNotifications(notificationNodeInstance);
 154  
 
 155  0
         LOG.debug("Disapproving document");
 156  
         try {
 157  0
             String oldStatus = getRouteHeader().getDocRouteStatus();
 158  0
             routeHeader.markDocumentDisapproved();
 159  0
             String newStatus = getRouteHeader().getDocRouteStatus();
 160  0
             KEWServiceLocator.getRouteHeaderService().saveRouteHeader(routeHeader);
 161  0
             notifyStatusChange(newStatus, oldStatus);
 162  0
         } catch (WorkflowException ex) {
 163  0
             LOG.warn(ex, ex);
 164  0
             throw new InvalidActionTakenException(ex.getMessage());
 165  0
         }
 166  0
     }
 167  
 
 168  
     //generate notifications to all people that have approved the document including the initiator
 169  
     private void generateNotifications(RouteNodeInstance notificationNodeInstance)
 170  
     {
 171  0
         String groupName = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(
 172  
                 KEWConstants.KEW_NAMESPACE,
 173  
                 KNSConstants.DetailTypes.WORKGROUP_DETAIL_TYPE,
 174  
                 KEWConstants.NOTIFICATION_EXCLUDED_USERS_WORKGROUP_NAME_IND);
 175  
 
 176  0
         Set<String> systemPrincipalIds = new HashSet<String>();
 177  
 
 178  0
         if( !StringUtils.isBlank(groupName))
 179  
         {
 180  0
             Group systemUserWorkgroup = KimApiServiceLocator.getIdentityManagementService().
 181  
                 getGroupByName(Utilities.parseGroupNamespaceCode(groupName), Utilities.parseGroupName(groupName));
 182  
 
 183  0
             List<String> principalIds = KimApiServiceLocator.
 184  
             getIdentityManagementService().getGroupMemberPrincipalIds( systemUserWorkgroup.getId());
 185  
 
 186  0
             if (systemUserWorkgroup != null)
 187  
             {
 188  0
                 for( String id : principalIds)
 189  
                 {
 190  0
                     systemPrincipalIds.add(id);
 191  
                 }
 192  
             }
 193  
         }
 194  0
         ActionRequestFactory arFactory = new ActionRequestFactory(getRouteHeader(), notificationNodeInstance);
 195  0
         Collection<ActionTakenValue> actions = KEWServiceLocator.getActionTakenService().findByDocumentId(getDocumentId());
 196  
         //one notification per person
 197  0
         Set<String> usersNotified = new HashSet<String>();
 198  0
         for (ActionTakenValue action : actions)
 199  
         {
 200  0
             if ((action.isApproval() || action.isCompletion()) && !usersNotified.contains(action.getPrincipalId()))
 201  
             {
 202  0
                 if (!systemPrincipalIds.contains(action.getPrincipalId()))
 203  
                 {
 204  0
                     ActionRequestValue request = arFactory.createNotificationRequest(KEWConstants.ACTION_REQUEST_ACKNOWLEDGE_REQ, action.getPrincipal(), getActionTakenCode(), getPrincipal(), getActionTakenCode());
 205  0
                     KEWServiceLocator.getActionRequestService().activateRequest(request);
 206  0
                     usersNotified.add(request.getPrincipalId());
 207  0
                 }
 208  
             }
 209  
         }
 210  
 
 211  0
     }
 212  
 }