View Javadoc

1   /**
2    * Copyright 2005-2012 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.impl.action;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.log4j.Logger;
23  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
24  import org.kuali.rice.core.api.reflect.DataDefinition;
25  import org.kuali.rice.kew.actions.ActionTakenEvent;
26  import org.kuali.rice.kew.api.WorkflowRuntimeException;
27  import org.kuali.rice.kew.api.action.ActionInvocation;
28  import org.kuali.rice.kew.api.action.ActionInvocationQueue;
29  import org.kuali.rice.kew.api.action.ActionType;
30  import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
31  import org.kuali.rice.kew.api.exception.ResourceUnavailableException;
32  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
33  import org.kuali.rice.kew.service.KEWServiceLocator;
34  import org.kuali.rice.kim.api.identity.principal.Principal;
35  
36  /**
37   * Reference implementation of the ActionInvocationQueue.
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class ActionInvocationQueueImpl implements ActionInvocationQueue {
42  
43      private static final Logger LOG = Logger.getLogger(ActionInvocationQueueImpl.class);
44  
45      @Override
46      public void invokeAction(String principalId, String documentId, ActionInvocation invocation) {
47          if (StringUtils.isBlank(principalId)) {
48              throw new RiceIllegalArgumentException("principalId is null or blank");
49          }
50  
51          if (StringUtils.isBlank(documentId)) {
52              throw new RiceIllegalArgumentException("documentId is null");
53          }
54  
55          if (invocation == null) {
56              throw new RiceIllegalArgumentException("invocation is null");
57          }
58  
59  
60          KEWServiceLocator.getRouteHeaderService().lockRouteHeader(documentId, true);
61          DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
62  
63          Principal principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(principalId);
64          List<DataDefinition> parameters = new ArrayList<DataDefinition>();
65          parameters.add(new DataDefinition(document));
66          parameters.add(new DataDefinition(principal));
67          parameters.add(new DataDefinition(""));
68  
69          try {
70              final ActionTakenEvent action = KEWServiceLocator.getActionRegistry().createAction(invocation.getAction().getCode(), parameters);
71              if (!document.isValidActionToTake(invocation.getAction().getCode())) {
72                  LOG.warn("Action "
73                          + invocation.getAction()
74                          + " is not a valid action to take against document "
75                          + document.getDocumentId()
76                          + " by principal with name '"
77                          + principal.getPrincipalName()
78                          + "'");
79                  return;
80              } else if (!KEWServiceLocator.getActionRegistry().getValidActions(principal, document).getValidActions()
81                      .contains(ActionType.fromCode(action.getActionTakenCode()))) {
82                  LOG.warn("Action "
83                          + action.getActionTakenCode()
84                          + " is not valid for document "
85                          + document.getDocumentId()
86                          + " by principal with name '"
87                          + principal.getPrincipalName()
88                          + "'");
89                  return;
90              }
91              action.performAction();
92          } catch (ResourceUnavailableException e) {
93              throw new WorkflowRuntimeException(e);
94          } catch (InvalidActionTakenException e) {
95              throw new WorkflowRuntimeException(e);
96          }
97  
98      }
99  
100 }