001/*
002 *  Copyright 2017 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.bpm.process;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.cocoon.environment.ObjectModelHelper;
024import org.apache.cocoon.environment.Redirector;
025import org.apache.cocoon.environment.Request;
026
027import org.ametys.core.user.CurrentUserProvider;
028import org.ametys.plugins.bpm.jcr.JCRWorkflowProcess;
029import org.ametys.plugins.repository.AmetysObjectResolver;
030import org.ametys.plugins.repository.UnknownAmetysObjectException;
031import org.ametys.plugins.repository.provider.RequestAttributeWorkspaceSelector;
032import org.ametys.plugins.workflow.cocoon.WorkflowAction;
033import org.ametys.plugins.workflow.support.WorkflowProvider.AmetysObjectWorkflow;
034
035import com.opensymphony.workflow.InvalidInputException;
036import com.opensymphony.workflow.WorkflowException;
037
038/**
039 * Execute a workflow action on a process
040 */
041public class ProcessWorkflowAction extends WorkflowAction
042{
043    private AmetysObjectResolver _resolver;
044    private CurrentUserProvider _userProvider;
045    
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
051        _userProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
052    }
053    
054    @Override
055    protected Map _act(Redirector redirector, Map objectModel, java.lang.String source, Parameters parameters, int actionId, Map inputs) throws InvalidInputException, WorkflowException
056    {
057        Request request = ObjectModelHelper.getRequest(objectModel);
058        String processId = request.getParameter("processId");
059        
060        if (processId == null)
061        {
062            throw new IllegalArgumentException("Missing id of process for executing an action on a process");
063        }
064        
065        String currentWorkspace = RequestAttributeWorkspaceSelector.getForcedWorkspace(request);
066        try
067        {
068            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, "default");
069                
070            request.setAttribute("sitemapLanguage", request.getParameter("lang"));
071            
072            String site = (String) request.getAttribute("site");
073            if (site == null)
074            {
075                request.setAttribute("site", request.getParameter("siteName"));
076            }
077            
078            JCRWorkflowProcess process = _resolver.resolveById(processId);
079            
080            if (getLogger().isInfoEnabled())
081            {
082                getLogger().info("User " + _userProvider.getUser() + " try to perform action " + actionId + " on process " + process.getName());
083            }
084            
085            parameters.setParameter("workflowId", Long.toString(process.getWorkflowId()));
086            
087            // Add the workflow used by the process in the request, to be used by any RegisterVariable of the workflow definition 
088            
089            request.setAttribute("workflowId", process.getWorkflow());
090            AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(process, true);
091            inputs.put("process", process);
092            
093            workflow.doAction(process.getWorkflowId(), actionId, inputs);
094        }
095        catch (UnknownAmetysObjectException e)
096        {
097            throw new WorkflowException("Unable to retrieve process with id " + processId, e);
098        }
099        finally
100        {
101            RequestAttributeWorkspaceSelector.setForcedWorkspace(request, currentWorkspace);
102        }
103        
104        return _getActionResult(redirector, objectModel, source, parameters);
105    }
106}