View Javadoc

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