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