001/*
002 *  Copyright 2020 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.odf.ose.export.impl.odf;
017
018import java.util.List;
019
020import org.apache.avalon.framework.component.Component;
021
022import org.ametys.odf.ProgramItem;
023import org.ametys.odf.courselist.CourseList;
024import org.ametys.odf.ose.db.ParameterizableQuery;
025import org.ametys.odf.ose.export.impl.odf.db.LienHelper;
026import org.ametys.odf.ose.export.impl.odf.db.ScenarioLienHelper;
027import org.ametys.runtime.plugin.component.AbstractLogEnabled;
028
029/**
030 * Get the queries to export the hierarchy links (LIEN and SCENARIO_LIEN).
031 */
032public class HierarchyExporter extends AbstractLogEnabled implements Component
033{
034    /** Avalon Role */
035    public static final String ROLE = HierarchyExporter.class.getName();
036
037    /**
038     * Get queries to export the link between the parent and the child element.
039     * @param parent The parent element
040     * @param child The child element
041     * @param oseCatalog The OSE catalog
042     * @return A {@link List} of {@link ParameterizableQuery} to export the link between the given elements
043     */
044    public List<ParameterizableQuery> getQueries(ProgramItem parent, ProgramItem child, Long oseCatalog)
045    {
046        String linkCode = oseCatalog + "_" + parent.getCode() + "-" + child.getCode();
047        
048        Double weight = 1.0;
049        Long minChoice = null;
050        Long maxChoice = null;
051        if (parent instanceof CourseList)
052        {
053            CourseList courseList = (CourseList) parent;
054            switch (courseList.getType())
055            {
056                case OPTIONAL:
057                    weight = 0.0;
058                    break;
059                case CHOICE:
060                    minChoice = courseList.getMinNumberOfCourses();
061                    maxChoice = courseList.getMaxNumberOfCourses();
062                    long size = courseList.getCourses().size();
063                    if (minChoice > size)
064                    {
065                        getLogger().warn("[{}] La liste contient min={} mais n'a que {} éléments.", courseList.getId(), minChoice, size);
066                    }
067                    else
068                    {
069                        weight = minChoice / (double) size;
070                    }
071                    break;
072                default:
073                    break;
074            }
075        }
076        
077        return List.of(
078            LienHelper.insertInto(linkCode, oseCatalog + "_" + parent.getCode(), oseCatalog + "_" + child.getCode()),
079            ScenarioLienHelper.insertInto(linkCode, weight, minChoice, maxChoice)
080        );
081    }
082}