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 initializing a workflow.<p>
031 * The following parameters are supported:
032 * <dl>
033 *  <dt>workflowName
034 *  <dd>the name of the workflow to instantiate
035 *  <dt>actionId
036 *  <dd>the id of the action to fire
037 * </dl>
038 * {@link Map} returned contains the workflow id on successful creation
039 * (<code>workflowId</code> key) and throw an exception on error.
040 */
041public class InitializeWorkflowAction 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        long workflowId = workflow.initialize(_getWorkflowName(objectModel, source, parameters), actionId, inputs);
048        return _getActionResult(redirector, objectModel, source, parameters, workflowId);
049    }
050
051    /**
052     * Provides the workflow name to use.
053     * @param objectModel the current object model.
054     * @param source the current source.
055     * @param parameters the current parameters.
056     * @return the workflow name.
057     */
058    protected String _getWorkflowName(Map objectModel, String source, Parameters parameters)
059    {
060        try
061        {
062            return parameters.getParameter("workflowName");
063        }
064        catch (ParameterException e)
065        {
066            throw new RuntimeException("Missing parameter workflowName", e);
067        }
068    }
069    
070    /**
071     * Provides the action result after successful workflow instance
072     * creation.<p>
073     * Default implementation provides a singleton {@link Map} with
074     * <code>workflowId</code> key and workflow id value.
075     * @param redirector the redirector.
076     * @param objectModel the current object model.
077     * @param source the current source.
078     * @param parameters the current parameters.
079     * @param workflowId the workflow instance id.
080     * @return the action result.
081     */
082    protected Map _getActionResult(Redirector redirector, Map objectModel, String source, Parameters parameters, long workflowId)
083    {
084        return Collections.singletonMap("workflowId", workflowId);
085    }
086    
087    @Override
088    protected String _getExceptionContext(Map objectModel, String source, Parameters parameters)
089    {
090        return String.format("for the workflow of name: '%s'", _getWorkflowName(objectModel, source, parameters));
091    }
092}