Coverage Report - org.kuali.rice.kew.actions.ActionRegistryImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionRegistryImpl
0%
0/80
0%
0/38
6
 
 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 java.util.ArrayList;
 20  
 import java.util.Collections;
 21  
 import java.util.HashMap;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 
 25  
 import org.apache.commons.lang.StringUtils;
 26  
 import org.apache.log4j.Logger;
 27  
 import org.kuali.rice.core.api.reflect.DataDefinition;
 28  
 import org.kuali.rice.core.api.reflect.ObjectDefinition;
 29  
 import org.kuali.rice.core.framework.resourceloader.ObjectDefinitionResolver;
 30  
 import org.kuali.rice.core.util.ClassLoaderUtils;
 31  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 32  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 33  
 import org.kuali.rice.kew.api.action.ActionRequestStatus;
 34  
 import org.kuali.rice.kew.api.action.ActionType;
 35  
 import org.kuali.rice.kew.exception.ResourceUnavailableException;
 36  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 37  
 import org.kuali.rice.kew.util.KEWConstants;
 38  
 import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
 39  
 
 40  
 
 41  
 
 42  
 /**
 43  
  * A simple implementation of an ActionRegistry which includes all of the default Workflow Actions.
 44  
  *
 45  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 46  
  */
 47  0
 public class ActionRegistryImpl implements ActionRegistry {
 48  0
     private static final Logger LOG = Logger.getLogger(ActionRegistryImpl.class);
 49  
 
 50  0
         private static Map<String, String> actionMap = new HashMap<String, String>();
 51  
         static {
 52  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_ACKNOWLEDGED_CD, AcknowledgeAction.class.getName());
 53  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_ADHOC_CD, AdHocAction.class.getName());
 54  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_ADHOC_REVOKED_CD, RevokeAdHocAction.class.getName());
 55  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_APPROVED_CD, ApproveAction.class.getName());
 56  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_BLANKET_APPROVE_CD, BlanketApproveAction.class.getName());
 57  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_CANCELED_CD, CancelAction.class.getName());
 58  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_COMPLETED_CD, CompleteAction.class.getName());
 59  0
         actionMap.put(KEWConstants.ACTION_TAKEN_ROUTED_CD, RouteDocumentAction.class.getName());
 60  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_DENIED_CD, DisapproveAction.class.getName());
 61  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_FYI_CD, ClearFYIAction.class.getName());
 62  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_LOG_DOCUMENT_ACTION_CD, LogDocumentActionAction.class.getName());
 63  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_MOVE_CD, MoveDocumentAction.class.getName());
 64  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_TAKE_WORKGROUP_AUTHORITY_CD, TakeWorkgroupAuthority.class.getName());
 65  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_RELEASE_WORKGROUP_AUTHORITY_CD, ReleaseWorkgroupAuthority.class.getName());
 66  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_RETURNED_TO_PREVIOUS_CD, ReturnToPreviousNodeAction.class.getName());
 67  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_SAVED_CD, SaveActionEvent.class.getName());
 68  
                 //actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_ACKNOWLEDGED_CD, SuperUserActionRequestAcknowledgeEvent.class.getName());
 69  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_APPROVED_CD, SuperUserActionRequestApproveEvent.class.getName());
 70  
                 //actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_COMPLETED_CD, SuperUserActionRequestCompleteEvent.class.getName());
 71  
                 //actionMap.put(KEWConstants.ACTION_TAKEN_SU_ACTION_REQUEST_FYI_CD, SuperUserActionRequestFYIEvent.class.getName());
 72  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_SU_APPROVED_CD, SuperUserApproveEvent.class.getName());
 73  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_SU_CANCELED_CD, SuperUserCancelEvent.class.getName());
 74  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_SU_DISAPPROVED_CD, SuperUserDisapproveEvent.class.getName());
 75  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_SU_RETURNED_TO_PREVIOUS_CD, SuperUserReturnToPreviousNodeAction.class.getName());
 76  0
                 actionMap.put(KEWConstants.ACTION_TAKEN_SU_ROUTE_LEVEL_APPROVED_CD, SuperUserNodeApproveEvent.class.getName());
 77  0
         }
 78  
 
 79  
         public void registerAction(String actionCode, String actionClass) {
 80  0
                 if (actionClass == null) {
 81  0
                         throw new IllegalArgumentException("Action Code '" + actionCode + "' cannot be registered with a null action class.");
 82  
                 }
 83  0
                 if (actionMap.containsKey(actionCode)) {
 84  0
                         throw new WorkflowRuntimeException("Action Code is already in use.  [" +
 85  
                                         actionCode + ", " + actionClass + "].  "+
 86  
                                         "Please unregister the existing implementation first.");
 87  
                 }
 88  0
                 actionMap.put(actionCode, actionClass);
 89  0
         }
 90  
 
 91  
         public void unregisterAction(String actionCode) {
 92  0
                 actionMap.remove(actionCode);
 93  0
         }
 94  
 
 95  
         public Map getActionMap() {
 96  0
                 return Collections.unmodifiableMap(actionMap);
 97  
         }
 98  
 
 99  
         /* (non-Javadoc)
 100  
          * @see org.kuali.rice.kew.actions.ActionRegistry#createAction(java.lang.String, java.util.List)
 101  
          */
 102  
         public ActionTakenEvent createAction(String actionCode, List<DataDefinition> parameters) throws ResourceUnavailableException {
 103  0
                 String actionClassName = actionMap.get(actionCode);
 104  0
                 if (actionClassName == null) {
 105  0
                         throw new IllegalArgumentException("No action has been registered for the given action code of '" + actionCode + "'.");
 106  
                 }
 107  0
                 ObjectDefinition actionDefinition = new ObjectDefinition(actionClassName);
 108  0
                 if (parameters != null && !parameters.isEmpty()) {
 109  0
                         actionDefinition.setConstructorParameters(parameters);
 110  
                 }
 111  
                 try {
 112  
                         //ActionTakenEvent actionTaken = (ActionTakenEvent)GlobalResourceLoader.getResourceLoader().getObject(actionDefinition);
 113  
                         // TODO ActionTakenEvent is not an interface so we can't fetch them through the GlobalResourceLoader, for now, just use
 114  
                         // the ObjectDefinitionResolver
 115  0
                         ActionTakenEvent actionTaken = (ActionTakenEvent) ObjectDefinitionResolver.createObject(actionDefinition, ClassLoaderUtils.getDefaultClassLoader(), false);
 116  0
                         if (actionTaken == null) {
 117  
                                 // TODO the exception handling here is a bit wonky
 118  0
                                 throw new ResourceUnavailableException("Could not locate action taken class '" + actionClassName + "'");
 119  
                         }
 120  0
                         return actionTaken;
 121  0
                 } catch (Exception e) {
 122  0
             LOG.debug("createAction() Exception thrown while working with action class name '" + actionClassName + "'");
 123  0
                         if (e instanceof ResourceUnavailableException) {
 124  0
                                 throw (ResourceUnavailableException)e;
 125  
                         }
 126  0
                         throw new ResourceUnavailableException(e);
 127  
                 }
 128  
         }
 129  
 
 130  
     /* (non-Javadoc)
 131  
      * @see org.kuali.rice.kew.actions.ActionValidationService#getValidActions(org.kuali.rice.kew.user.WorkflowUser, org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue)
 132  
      */
 133  
     public ValidActions getValidActions(PrincipalContract principal, DocumentRouteHeaderValue document) throws ResourceUnavailableException {
 134  0
             ValidActions validActions = new ValidActions();
 135  0
             ArrayList<ActionRequestValue> activeRequests = new ArrayList<ActionRequestValue>();
 136  0
             for ( ActionRequestValue ar : document.getActionRequests() ) {
 137  0
                     if ( (ar.getCurrentIndicator() != null && ar.getCurrentIndicator()) && StringUtils.equals( ar.getStatus(), ActionRequestStatus.ACTIVATED.getCode() ) ) {
 138  0
                             activeRequests.add(ar);
 139  
                     }
 140  
             }
 141  0
             for (String actionTakenCode : actionMap.keySet())
 142  
             {
 143  0
                     List<DataDefinition> parameters = new ArrayList<DataDefinition>();
 144  0
                     parameters.add(new DataDefinition(document));
 145  0
                     parameters.add(new DataDefinition(principal));
 146  0
                     ActionTakenEvent actionEvent = createAction(actionTakenCode, parameters);
 147  0
                     if (StringUtils.isEmpty(actionEvent.validateActionRules(activeRequests)))
 148  
                     {
 149  0
                             validActions.addActionTakenCode(actionTakenCode);
 150  
                     }
 151  0
             }
 152  0
             return validActions;
 153  
     }
 154  
     
 155  
     public org.kuali.rice.kew.api.action.ValidActions getNewValidActions(PrincipalContract principal, DocumentRouteHeaderValue document) {
 156  
             try {
 157  0
                     org.kuali.rice.kew.api.action.ValidActions.Builder builder = org.kuali.rice.kew.api.action.ValidActions.Builder.create();
 158  0
                     List<ActionRequestValue> activeRequests = new ArrayList<ActionRequestValue>();
 159  0
                     for ( ActionRequestValue ar : document.getActionRequests() ) {
 160  0
                             if ( (ar.getCurrentIndicator() != null && ar.getCurrentIndicator()) && StringUtils.equals( ar.getStatus(), ActionRequestStatus.ACTIVATED.getCode() ) ) {
 161  0
                                     activeRequests.add(ar);
 162  
                             }
 163  
                     }
 164  0
                     for (String actionTakenCode : actionMap.keySet())
 165  
                     {
 166  0
                             List<DataDefinition> parameters = new ArrayList<DataDefinition>();
 167  0
                             parameters.add(new DataDefinition(document));
 168  0
                             parameters.add(new DataDefinition(principal));
 169  0
                             ActionTakenEvent actionEvent = createAction(actionTakenCode, parameters);
 170  0
                             if (StringUtils.isEmpty(actionEvent.validateActionRules(activeRequests)))
 171  
                             {
 172  0
                                     builder.addValidAction(ActionType.fromCode(actionTakenCode));
 173  
                             }
 174  0
                     }
 175  0
                     return builder.build();
 176  0
             } catch (ResourceUnavailableException e) {
 177  0
                     throw new WorkflowRuntimeException(e);
 178  
             }
 179  
     }
 180  
 }