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.workflowsdef;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.plugins.workflow.AbstractWorkflowComponent;
025import org.ametys.plugins.workflow.repository.WorkflowAwareAmetysObject;
026import org.ametys.plugins.workflow.support.WorkflowProvider;
027import org.ametys.plugins.workflow.support.WorkflowProvider.AmetysObjectWorkflow;
028
029import com.opensymphony.module.propertyset.PropertySet;
030import com.opensymphony.workflow.Condition;
031import com.opensymphony.workflow.WorkflowException;
032import com.opensymphony.workflow.spi.Step;
033
034/**
035 * OSWorkflow condition to test the current step of a process
036 * The following configuration have to be used:<br>
037 * <pre>
038 * &lt;condition type="avalon"&gt;
039 * &nbsp;&nbsp;&lt;arg name="role"&gt;org.ametys.plugins.bpm.workflowsdef.ProcessCurrentStepCondition&lt;/arg&gt;
040 * &nbsp;&nbsp;&lt;arg name="step"&gt;2&lt;/arg&gt;
041 * &lt;/condition&gt;
042 * </pre>
043 *
044 */
045public class ProcessCurrentStepCondition  extends AbstractWorkflowComponent implements Condition
046{
047    private WorkflowProvider _workflowProvider;
048
049    @Override
050    public void service(ServiceManager manager) throws ServiceException
051    {
052        super.service(manager);
053        _workflowProvider = (WorkflowProvider) manager.lookup(WorkflowProvider.ROLE);
054    }
055    
056    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
057    {
058        int step = Integer.parseInt((String) args.get("step"));
059        
060        WorkflowAwareAmetysObject process = (WorkflowAwareAmetysObject) transientVars.get("process");
061        
062        if (process == null)
063        {
064            throw new WorkflowException("Unable to retrieve process");
065        }
066        AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(process, true);
067        
068        long workflowId = process.getWorkflowId();
069        
070        List<Step> currentSteps = workflow.getCurrentSteps(workflowId);
071        for (Step currentStep : currentSteps)
072        {
073            if (currentStep.getStepId() == step)
074            {
075                return true;
076            }
077        }
078        
079        return false;
080    }
081}