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;
027import org.ametys.runtime.i18n.I18nizableText;
028
029import com.opensymphony.module.propertyset.PropertySet;
030import com.opensymphony.workflow.WorkflowException;
031import com.opensymphony.workflow.spi.Step;
032
033/**
034 * This OSWorkflow condition checks all content metadata are valid ONLY if the content is in validation step.
035 */
036public class ValidationStepCondition extends ValidateContentCondition
037{
038    private WorkflowProvider _workflowProvider;
039
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        _workflowProvider = (WorkflowProvider) manager.lookup(WorkflowProvider.ROLE);
045    }
046    
047    @Override
048    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
049    {
050        int step = Integer.parseInt((String) args.get("validation-step"));
051        
052        WorkflowAwareContent content = getContent(transientVars);
053        AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(content);
054        
055        long workflowId = content.getWorkflowId();
056        
057        List<Step> currentSteps = workflow.getCurrentSteps(workflowId);
058        for (Step currentStep : currentSteps)
059        {
060            if (currentStep.getStepId() != step)
061            {
062                // Content is not in validation step, do not check attributes
063                return true;
064            }
065        }
066        
067        return super.passesCondition(transientVars, args, ps);
068    }
069    @Override
070    public List<ConditionArgument> getArguments()
071    {
072        return List.of(new ConditionArgument("validation-step"));
073    }
074    
075    @Override
076    public I18nizableText getDescription(Map<String, String> argumentsValues)
077    {
078        return new I18nizableText("plugin.cms", "PLUGINS_CMS_VALIDATION_STEP_CONDITION_DESCRIPTION");
079    }
080}