001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.impl.action;
017
018import java.util.ArrayList;
019import java.util.List;
020
021import org.apache.commons.lang.StringUtils;
022import org.apache.log4j.Logger;
023import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
024import org.kuali.rice.core.api.reflect.DataDefinition;
025import org.kuali.rice.kew.actions.ActionTakenEvent;
026import org.kuali.rice.kew.api.WorkflowRuntimeException;
027import org.kuali.rice.kew.api.action.ActionInvocation;
028import org.kuali.rice.kew.api.action.ActionInvocationQueue;
029import org.kuali.rice.kew.api.action.ActionType;
030import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
031import org.kuali.rice.kew.api.exception.ResourceUnavailableException;
032import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
033import org.kuali.rice.kew.service.KEWServiceLocator;
034import org.kuali.rice.kim.api.identity.principal.Principal;
035
036/**
037 * Reference implementation of the ActionInvocationQueue.
038 *
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 */
041public class ActionInvocationQueueImpl implements ActionInvocationQueue {
042
043    private static final Logger LOG = Logger.getLogger(ActionInvocationQueueImpl.class);
044
045    @Override
046    public void invokeAction(String principalId, String documentId, ActionInvocation invocation) {
047        if (StringUtils.isBlank(principalId)) {
048            throw new RiceIllegalArgumentException("principalId is null or blank");
049        }
050
051        if (StringUtils.isBlank(documentId)) {
052            throw new RiceIllegalArgumentException("documentId is null");
053        }
054
055        if (invocation == null) {
056            throw new RiceIllegalArgumentException("invocation is null");
057        }
058
059
060        KEWServiceLocator.getRouteHeaderService().lockRouteHeader(documentId);
061        DocumentRouteHeaderValue document = KEWServiceLocator.getRouteHeaderService().getRouteHeader(documentId);
062
063        Principal principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(principalId);
064        List<DataDefinition> parameters = new ArrayList<DataDefinition>();
065        parameters.add(new DataDefinition(document));
066        parameters.add(new DataDefinition(principal));
067        parameters.add(new DataDefinition(""));
068
069        try {
070            final ActionTakenEvent action = KEWServiceLocator.getActionRegistry().createAction(invocation.getAction().getCode(), parameters);
071            if (!document.isValidActionToTake(invocation.getAction().getCode())) {
072                LOG.warn("Action "
073                        + invocation.getAction()
074                        + " is not a valid action to take against document "
075                        + document.getDocumentId()
076                        + " by principal with name '"
077                        + principal.getPrincipalName()
078                        + "'");
079                return;
080            } else if (!KEWServiceLocator.getActionRegistry().getValidActions(principal, document).getValidActions()
081                    .contains(ActionType.fromCode(action.getActionTakenCode()))) {
082                LOG.warn("Action "
083                        + action.getActionTakenCode()
084                        + " is not valid for document "
085                        + document.getDocumentId()
086                        + " by principal with name '"
087                        + principal.getPrincipalName()
088                        + "'");
089                return;
090            }
091            action.performAction();
092        } catch (ResourceUnavailableException e) {
093            throw new WorkflowRuntimeException(e);
094        } catch (InvalidActionTakenException e) {
095            throw new WorkflowRuntimeException(e);
096        }
097
098    }
099
100}