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.EnhancedCondition;
026import org.ametys.plugins.workflow.repository.WorkflowAwareAmetysObject;
027import org.ametys.plugins.workflow.support.WorkflowProvider;
028import org.ametys.plugins.workflow.support.WorkflowProvider.AmetysObjectWorkflow;
029import org.ametys.runtime.i18n.I18nizableText;
030
031import com.opensymphony.module.propertyset.PropertySet;
032import com.opensymphony.workflow.WorkflowException;
033import com.opensymphony.workflow.spi.Step;
034
035/**
036 * OSWorkflow condition to test the current step of an Ametys object
037 * The following configuration have to be used:<br>
038 * 
039 * <pre>
040 * &lt;condition type="avalon"&gt;
041 * &nbsp;&nbsp;&lt;arg name="role"&gt;org.ametys.plugins.workflow.component.AmetysObjectCurrentStepCondition&lt;/arg&gt;
042 * &nbsp;&nbsp;&lt;arg name="object-key"&gt;content&lt;/arg&gt;
043 * &nbsp;&nbsp;&lt;arg name="step"&gt;2&lt;/arg&gt;
044 * &lt;/condition&gt;
045 * </pre>
046 *
047 */
048public class AmetysObjectCurrentStepCondition extends AbstractWorkflowComponent implements EnhancedCondition
049{
050    private WorkflowProvider _workflowProvider;
051
052    @Override
053    public void service(ServiceManager manager) throws ServiceException
054    {
055        super.service(manager);
056        _workflowProvider = (WorkflowProvider) manager.lookup(WorkflowProvider.ROLE);
057    }
058    
059    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
060    {
061        int step = Integer.parseInt((String) args.get("step"));
062        
063        WorkflowAwareAmetysObject ametysObject = (WorkflowAwareAmetysObject) transientVars.get(_getAmetysObjectKey(args));
064        if (ametysObject == null)
065        {
066            throw new WorkflowException("Unable to retrieve the object");
067        }
068        
069        AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(ametysObject, true);
070        
071        long workflowId = ametysObject.getWorkflowId();
072        
073        List<Step> currentSteps = workflow.getCurrentSteps(workflowId);
074        for (Step currentStep : currentSteps)
075        {
076            if (currentStep.getStepId() == step)
077            {
078                return true;
079            }
080        }
081        
082        return false;
083    }
084    
085    /**
086     * Get the key to get the {@link WorkflowAwareAmetysObject} into the transient vars.
087     * @param args Arguments of the condition
088     * @return A key to get the {@link WorkflowAwareAmetysObject}
089     */
090    protected Object _getAmetysObjectKey(Map args)
091    {
092        return args.get("object-key");
093    }
094
095    @Override
096    public List<ConditionArgument> getArguments()
097    {
098        return List.of(new ConditionArgument("step"), new ConditionArgument("object-key"));
099    }
100
101    @Override
102    public I18nizableText getDescription(Map<String, String> argumentsValues)
103    {
104        @SuppressWarnings("cast")
105        String stepId = (String) argumentsValues.get("step");
106        return new I18nizableText("plugin.workflow", "UITOOL_WORKFLOW_EDITOR_AMETYS_OBJECT_CURRENT_STEP_CONDITION_DESCRIPTION", List.of(stepId));
107    }
108}