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