001/*
002 *  Copyright 2018 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.odfpilotage.workflow;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.activity.Disposable;
021import org.apache.avalon.framework.activity.Initializable;
022
023import org.ametys.cms.repository.WorkflowAwareContent;
024import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
025import org.ametys.cms.workflow.EditContentFunction;
026import org.ametys.odf.ProgramItem;
027import org.ametys.odf.course.Course;
028import org.ametys.odf.courselist.CourseList;
029import org.ametys.odf.program.Program;
030import org.ametys.odf.program.TraversableProgramPart;
031import org.ametys.plugins.odfpilotage.helper.PilotageStatusHelper;
032import org.ametys.plugins.workflow.AbstractWorkflowComponent;
033
034import com.opensymphony.module.propertyset.PropertySet;
035import com.opensymphony.workflow.Condition;
036import com.opensymphony.workflow.WorkflowException;
037
038/**
039 * Check the pilotage status and check if we try to edit the structure
040 */
041public class PilotageStatusCheckCondition extends AbstractWorkflowComponent implements Condition, Initializable, Disposable
042{
043    /** The pilotage status helper */
044    protected PilotageStatusHelper _pilotageStatusHelper;
045    
046    @Override
047    public void initialize() throws Exception
048    {
049        _pilotageStatusHelper = (PilotageStatusHelper) _manager.lookup(PilotageStatusHelper.ROLE);
050    }
051    
052    @Override
053    public boolean passesCondition(Map transientVars, Map args, PropertySet ps) throws WorkflowException
054    {
055        WorkflowAwareContent content = getContent(transientVars);
056        
057        if (content instanceof ProgramItem)
058        {
059            Program parentProgramWithHigherPilotageStatus = _pilotageStatusHelper.getParentProgramWithHigherPilotageStatus((ProgramItem) content);
060            if (parentProgramWithHigherPilotageStatus != null && !_pilotageStatusHelper.hasEditSuperRight(parentProgramWithHigherPilotageStatus))
061            {
062                return passesPilotageStatusCondition(transientVars);
063            }
064        }
065        
066        return true;
067    }
068    
069    /**
070     * Return true if the pilotage status passes conditions
071     * @param transientVars the transient vars
072     * @return true if the pilotage status passes conditions
073     * @throws WorkflowException if an error occurred
074     */
075    protected boolean passesPilotageStatusCondition(Map transientVars) throws WorkflowException
076    {
077        Map<String, Object> contextParameters = getContextParameters(transientVars);
078        if (contextParameters != null && contextParameters.containsKey("values"))
079        {
080            @SuppressWarnings("unchecked")
081            Map<String, Object> values = (Map<String, Object>) contextParameters.get("values");
082            
083            if (values.containsKey(EditContentFunction.FORM_ELEMENTS_PREFIX + TraversableProgramPart.CHILD_PROGRAM_PARTS)
084                    || values.containsKey(EditContentFunction.FORM_ELEMENTS_PREFIX + CourseList.CHILD_COURSES)
085                    || values.containsKey(EditContentFunction.FORM_ELEMENTS_PREFIX + Course.CHILD_COURSE_LISTS)
086                    || values.containsKey(EditContentFunction.FORM_ELEMENTS_PREFIX + ProgramItem.CATALOG)
087                )
088            {
089                return false;
090            }
091        }
092        
093        return true;
094    }
095    
096    /**
097     * Retrieve the content associated with the workflow.
098     * @param transientVars the parameters from the call.
099     * @return the content.
100     * @throws WorkflowException if the content is not found.
101     */
102    protected WorkflowAwareContent getContent(Map transientVars) throws WorkflowException
103    {
104        WorkflowAwareContent content = (WorkflowAwareContent) transientVars.get(AbstractContentWorkflowComponent.CONTENT_KEY);
105        
106        if (content == null)
107        {
108            throw new WorkflowException("Unable to retrieve content");
109        }
110        
111        // Found in transient variables map
112        return content;
113    }
114    
115    @Override
116    public void dispose()
117    {
118        _manager.release(_pilotageStatusHelper);
119        _pilotageStatusHelper = null;
120        _manager = null;
121    }
122}