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.component.WorkflowArgument;
026import org.ametys.plugins.workflow.support.WorkflowElementDefinitionHelper;
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 * This OSWorkflow condition checks all content metadata are valid ONLY if the content is in validation step.
037 */
038public class ValidationStepCondition extends ValidateContentCondition
039{
040    private WorkflowProvider _workflowProvider;
041
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        super.service(manager);
046        _workflowProvider = (WorkflowProvider) manager.lookup(WorkflowProvider.ROLE);
047    }
048    
049    @Override
050    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
051    {
052        int step = Integer.parseInt((String) args.get("validation-step"));
053        
054        WorkflowAwareContent content = getContent(transientVars);
055        AmetysObjectWorkflow workflow = _workflowProvider.getAmetysObjectWorkflow(content);
056        
057        long workflowId = content.getWorkflowId();
058        
059        List<Step> currentSteps = workflow.getCurrentSteps(workflowId);
060        for (Step currentStep : currentSteps)
061        {
062            if (currentStep.getStepId() != step)
063            {
064                // Content is not in validation step, do not check attributes
065                return true;
066            }
067        }
068        
069        return super.passesCondition(transientVars, args, ps);
070    }
071    @Override
072    public List<WorkflowArgument> getArguments()
073    {
074        return List.of(
075            WorkflowElementDefinitionHelper.getElementDefinition(
076                "validation-step",
077                new I18nizableText("plugin.cms", "PLUGINS_CMS_VALIDATION_STEP_CONDITION_ARGUMENT_LABEL"),
078                new I18nizableText("plugin.cms", "PLUGINS_CMS_VALIDATION_STEP_CONDITION_ARGUMENT_DESCRIPTION"),
079                true,
080                false
081            )
082        );
083    }
084    
085    @Override
086    public I18nizableText getLabel()
087    {
088        return new I18nizableText("plugin.cms", "PLUGINS_CMS_VALIDATION_STEP_CONDITION_LABEL");
089    }
090}