001/*
002 *  Copyright 2021 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.report.consistency.impl;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.List;
022import java.util.Map;
023import java.util.Set;
024
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027
028import org.ametys.odf.ProgramItem;
029import org.ametys.odf.course.Course;
030import org.ametys.odf.program.Program;
031import org.ametys.plugins.odfpilotage.helper.PilotageHelper;
032import org.ametys.plugins.odfpilotage.helper.PilotageHelper.StepHolderStatus;
033import org.ametys.plugins.odfpilotage.report.consistency.AbstractConsistencyAnalysis;
034import org.ametys.plugins.odfpilotage.report.consistency.ConsistencyAnalysisResult;
035import org.ametys.plugins.odfpilotage.report.consistency.ConsistencyAnalysisStatus;
036import org.ametys.runtime.i18n.I18nizableText;
037
038/**
039 * Analysis on step holder for all courses of the {@link Program}.
040 */
041public class StepHolderAnalysis extends AbstractConsistencyAnalysis
042{
043    /** The pilotage helper */
044    protected PilotageHelper _pilotageHelper;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        super.service(manager);
050        _pilotageHelper = (PilotageHelper) manager.lookup(PilotageHelper.ROLE);
051    }
052    
053    
054    @Override
055    public ConsistencyAnalysisResult analyze(Program program)
056    {
057        ConsistencyAnalysisResult result = new ConsistencyAnalysisResult();
058
059        // Introduction
060        result.setIntroText(new I18nizableText("plugin." + _pluginName, "PLUGINS_ODF_PILOTAGE_CONSISTENCY_ANALYSIS_STEPHOLDER_INTRO"));
061        
062        // Columns
063        result.addColumn("courseCode", new I18nizableText("plugin." + _pluginName, "PLUGINS_ODF_PILOTAGE_CONSISTENCY_ANALYSIS_STEPHOLDER_COLUMN_COURSE_CODE"));
064        result.addColumn("courseTitle", new I18nizableText("plugin." + _pluginName, "PLUGINS_ODF_PILOTAGE_CONSISTENCY_ANALYSIS_STEPHOLDER_COLUMN_COURSE_TITLE"));
065        result.addColumn("stepHolderStatus", new I18nizableText("plugin." + _pluginName, "PLUGINS_ODF_PILOTAGE_CONSISTENCY_ANALYSIS_STEPHOLDER_COLUMN_STEP_HOLDER_STATUS"));
066        
067        // Errors
068        List<Map<String, Object>> errors = _controlStepHolderOnCourses(program);
069        result.addLines(errors);
070        
071        // Status
072        result.setStatus(errors.isEmpty() ? ConsistencyAnalysisStatus.OK : ConsistencyAnalysisStatus.KO);
073        
074        return result;
075    }
076    
077    /**
078     * Control the ECTS consistency between the current content and its children.
079     * @param program The {@link Program} to check step holder on each course
080     * @return The {@link List} of error messages
081     */
082    protected List<Map<String, Object>> _controlStepHolderOnCourses(Program program)
083    {
084        List<Map<String, Object>> errors = new ArrayList<>();
085
086        // For each course of the program, check if the step holder is single, if not, add a line to the report
087        for (Course course : getAllCourses(program))
088        {
089            StepHolderStatus stepHolderStatus = _pilotageHelper.getStepHolder(course).getLeft();
090            if (stepHolderStatus != StepHolderStatus.SINGLE)
091            {
092                Map<String, Object> error = new HashMap<>();
093                error.put("courseCode", course.getCode());
094                error.put("courseTitle", course.getTitle());
095                error.put("stepHolderStatus", new I18nizableText("plugin." + _pluginName, "PLUGINS_ODF_PILOTAGE_CONSISTENCY_ANALYSIS_STEPHOLDER_STATUS_" + stepHolderStatus.toString()));
096                errors.add(error);
097            }
098        }
099        
100        return errors;
101    }
102    
103    private Set<Course> getAllCourses(ProgramItem programItem)
104    {
105        Set<Course> courses = new HashSet<>();
106        
107        boolean alreadyAdded = false;
108        if (programItem instanceof Course)
109        {
110            alreadyAdded = !courses.add((Course) programItem);
111        }
112        
113        if (!alreadyAdded)
114        {
115            _odfHelper.getChildProgramItems(programItem).forEach(child -> courses.addAll(getAllCourses(child)));
116        }
117        
118        return courses;
119    }
120}