Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2013 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.opensource.org/licenses/ecl2.php Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.kuali.rice.kew.actions;
 
 
 
 import java.util.List;


Responsible for creating adhoc requests that are requested from the client.

Author(s):
Kuali Rice Team (rice.collab@kuali.org)
 
 public class AdHocAction extends ActionTakenEvent {
    
AdHoc actions don't actually result in an action being taken...it's a special case that generates other action requests
 
     private static final String NO_ACTION_TAKEN_CODE = null;
 
 	private String nodeName;
     private Integer priority;
 	private Boolean forceAction;
 	private Recipient recipient;
 	private String annotation;
 	private String requestLabel;
 
     public AdHocAction(DocumentRouteHeaderValue routeHeaderPrincipalContract principal) {
         super(routeHeaderprincipal);
     }
 
 	public AdHocAction(DocumentRouteHeaderValue routeHeaderPrincipalContract principalString annotationString actionRequestedString nodeNameInteger priorityRecipient recipientString responsibilityDescBoolean forceActionString requestLabel) {
 		super(routeHeaderprincipalannotation);
 		this. = actionRequested;
 		this. = nodeName;
         this. = priority;
 		this. = responsibilityDesc;
 		this. = forceAction;
 		this. = recipient;
 		this. = annotation;
 		this. = requestLabel;
 	}
 
 	public void recordAction() throws InvalidActionTakenException {
 		String errorMessage = validateActionRules();
         if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
             throw new InvalidActionTakenException(errorMessage);
         }
 		List targetNodes = KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(getDocumentId());
         String error = adhocRouteAction(targetNodesfalse);
         if (!org.apache.commons.lang.StringUtils.isEmpty(error)) {
             throw new InvalidActionTakenException(error);
         }
     }
 
     /* (non-Javadoc)
      * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules()
      */
     @Override
     public String validateActionRules() {
         List targetNodes = KEWServiceLocator.getRouteNodeService().getCurrentNodeInstances(getDocumentId());
         return validateActionRulesInternal(targetNodes);
     }
     
     @Override
     public String validateActionRules(List<ActionRequestValueactionRequests) {
     	return validateActionRules();
     }
 
     private String validateActionRulesInternal(List<RouteNodeInstancetargetNodes) {
     	// recipient will be null when this is invoked from ActionRegistry.getValidActions
    	if ( != null) {
    		if ( instanceof KimPrincipalRecipient) {
    			KimPrincipalRecipient principalRecipient = (KimPrincipalRecipient);
    			if (!KEWServiceLocator.getDocumentTypePermissionService().canReceiveAdHocRequest(principalRecipient.getPrincipalId(), getRouteHeader(), )) {
    				return "The principal '" + principalRecipient.getPrincipal().getPrincipalName() + "' does not have permission to recieve ad hoc requests on DocumentType '" + getRouteHeader().getDocumentType().getName() + "'";
    			}
    		} else if ( instanceof KimGroupRecipient) {
    			Group group = ((KimGroupRecipient)).getGroup();
    			if (!KEWServiceLocator.getDocumentTypePermissionService().canGroupReceiveAdHocRequest("" + group.getId(), getRouteHeader(), )) {
    				return "The group '" + group.getName() + "' does not have permission to recieve ad hoc requests on DocumentType '" + getRouteHeader().getDocumentType().getName() + "'";
    			}
    		} else {
    			return "Invalid Recipient type encountered: " + .getClass();
    		}
    	}
        return adhocRouteAction(targetNodestrue);
    }
    private String adhocRouteAction(List<RouteNodeInstancetargetNodesboolean forValidationOnly) {
        //if (targetNodes.isEmpty()) {
        //    return "Could not locate an node instance on the document with the name '" + nodeName + "'";
        //}
        boolean requestCreated = false;
        for (RouteNodeInstance routeNode : targetNodes)
        {
            // if the node name is null, then adhoc it to the first available node
            if ( == null || routeNode.getName().equals())
            {
                String message = createAdHocRequest(routeNodeforValidationOnly);
                if (StringUtils.isNotBlank(message))
                {
                    return message;
                }
                requestCreated = true;
                if ( == null)
                {
                    break;
                }
            }
        }
        
        if (!requestCreated && targetNodes.isEmpty()) {
            String message = createAdHocRequest(nullforValidationOnly);
            if (StringUtils.isNotBlank(message)) {
                return message;
            }
            requestCreated = true;
        }
        
        if (!requestCreated) {
            return "Didn't create request.  The node name " +  + " given is probably invalid ";
        }
        return "";
    }
    
    private String createAdHocRequest(RouteNodeInstance routeNodeboolean forValidationOnly) {
                ActionRequestValue adhocRequest = new ActionRequestValue();
                if (!forValidationOnly) {
                    ActionRequestFactory arFactory = new ActionRequestFactory(routeNode);
                    adhocRequest = arFactory.createActionRequest();
                    adhocRequest.setResponsibilityId(.);
                    adhocRequest.setRequestLabel();
                    if ( != null) {
                        adhocRequest.setPriority();
                    }
                } else {
                    adhocRequest.setActionRequested();
                }
                if (adhocRequest.isApproveOrCompleteRequest() && ! (.isEnroute() || .isStateInitiated() ||
                        .isStateSaved())) {
            return "Cannot AdHoc a Complete or Approve request when document is in state '" + .getDocRouteStatusLabel() + "'.";
                }
                if (!forValidationOnly) {
     
                	if (.isDisaproved() || .isCanceled() || .isFinal() || .isProcessed()) {
                        getActionRequestService().activateRequest(adhocRequest);
                    } else {
                        KEWServiceLocator.getActionRequestService().saveActionRequest(adhocRequest);
                    }
                }
        return "";
    }
New to GrepCode? Check out our FAQ X