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