001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.ametys.plugins.workflow.cocoon;
017
018import java.util.Collections;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.ParameterException;
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.cocoon.environment.Redirector;
024
025import com.opensymphony.workflow.InvalidInputException;
026import com.opensymphony.workflow.Workflow;
027import com.opensymphony.workflow.WorkflowException;
028
029/**
030 * Action for firing a transition on a workflow.<p>
031 * The following parameters are supported:
032 * <dl>
033 *  <dt>workflowId
034 *  <dd>the id of the workflow to use
035 *  <dt>actionId
036 *  <dd>the id of the action to fire
037 * </dl>
038 * {@link Map} returned is empty on successful transition
039 * and throw an exception on error.
040 */
041public class WorkflowAction extends AbstractWorkflowAction
042{
043    @Override
044    protected Map _act(Redirector redirector, Map objectModel, String source, Parameters parameters, int actionId, Map inputs) throws InvalidInputException, WorkflowException
045    {
046        Workflow workflow = _workflowProvider.getGenericWorkflow();
047        workflow.doAction(_getWorkflowId(objectModel, source, parameters), actionId, inputs);
048        return _getActionResult(redirector, objectModel, source, parameters);
049    }
050
051    /**
052     * Provide the workflow id.<p>
053     * Default implementation uses parameter <code>workflowId</code>.
054     * @param objectModel the current object model.
055     * @param source the current source.
056     * @param parameters the current parameters.
057     * @return the workflow id.
058     */
059    protected long _getWorkflowId(Map objectModel, String source, Parameters parameters)
060    {
061        try
062        {
063            return parameters.getParameterAsLong("workflowId");
064        }
065        catch (ParameterException e)
066        {
067            throw new RuntimeException("Missing parameter workflowId", e);
068        }
069    }
070    
071    /**
072     * Provides the action result after successful workflow transition.<p>
073     * Default implementation returns a empty {@link Map}.
074     * @param redirector the redirector.
075     * @param objectModel the current object model.
076     * @param source the current source.
077     * @param parameters the current parameters.
078     * @return the action result.
079     */
080    protected Map _getActionResult(Redirector redirector, Map objectModel, String source, Parameters parameters)
081    {
082        return Collections.emptyMap();
083    }
084    
085    @Override
086    protected String _getExceptionContext(Map objectModel, String source, Parameters parameters)
087    {
088        return String.format("for the workflow of instance id: '%d'", _getWorkflowId(objectModel, source, parameters));
089    }
090}