001/*
002 *  Copyright 2022 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.component;
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 an Ametys object
036 * The following configuration have to be used:<br>
037 * 
038 * <pre>
039 * &lt;condition type="avalon"&gt;
040 * &nbsp;&nbsp;&lt;arg name="role"&gt;org.ametys.plugins.workflow.component.AmetysObjectCurrentStepCondition&lt;/arg&gt;
041 * &nbsp;&nbsp;&lt;arg name="object-key"&gt;content&lt;/arg&gt;
042 * &nbsp;&nbsp;&lt;arg name="step"&gt;2&lt;/arg&gt;
043 * &lt;/condition&gt;
044 * </pre>
045 *
046 */
047public class AmetysObjectCurrentStepCondition extends AbstractWorkflowComponent implements Condition
048{
049    private WorkflowProvider _workflowProvider;
050
051    @Override
052    public void service(ServiceManager manager) throws ServiceException
053    {
054        super.service(manager);
055        _workflowProvider = (WorkflowProvider) manager.lookup(WorkflowProvider.ROLE);
056    }
057    
058    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
059    {
060        int step = Integer.parseInt((String) args.get("step"));
061        
062        WorkflowAwareAmetysObject ametysObject = (WorkflowAwareAmetysObject) transientVars.get(_getAmetysObjectKey(args));
063        if (ametysObject == null)
064        {
065            throw new WorkflowException("Unable to retrieve the object");
066        }
067        
068        AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(ametysObject, true);
069        
070        long workflowId = ametysObject.getWorkflowId();
071        
072        List<Step> currentSteps = workflow.getCurrentSteps(workflowId);
073        for (Step currentStep : currentSteps)
074        {
075            if (currentStep.getStepId() == step)
076            {
077                return true;
078            }
079        }
080        
081        return false;
082    }
083    
084    /**
085     * Get the key to get the {@link WorkflowAwareAmetysObject} into the transient vars.
086     * @param args Arguments of the condition
087     * @return A key to get the {@link WorkflowAwareAmetysObject}
088     */
089    protected Object _getAmetysObjectKey(Map args)
090    {
091        return args.get("object-key");
092    }
093}