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 */
016
017package org.ametys.plugins.odfpilotage.cost.entity;
018
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.commons.lang3.StringUtils;
025
026import org.ametys.runtime.model.ModelItem;
027
028/**
029 * Class representing an {@link EqTD}
030 */
031public class EqTD
032{
033    /** The global eqTD value by coursePart */
034    private Map<String, Double> _globalEqTD;
035    
036    /** The pro-rated eqTD values */
037    private Map<String, Double> _proratedEqTD;
038    
039    /** The local eqTD values */
040    private Map<String, Double> _localEqTD;
041    
042    /** List of held occurrences (for course parts only) */
043    private Set<String> _heldList;
044    
045    /**
046     * The constructor
047     */
048    public EqTD()
049    {
050        _globalEqTD = new HashMap<>();
051        _proratedEqTD = new HashMap<>();
052        _localEqTD = new HashMap<>();
053        _heldList = new HashSet<>();
054    }
055    
056    /**
057     * Get the global eqTD value
058     * @return the global eqTD value
059     */
060    public Double getGlobalEqTD()
061    {
062        return _globalEqTD.values()
063                .stream()
064                .reduce(0D, Double::sum);
065    }
066
067    /**
068     * Get the global eqTD value by course part
069     * @return all global eqTD values by course part
070     */
071    public Map<String, Double> getAllGlobalEqTD()
072    {
073        return _globalEqTD;
074    }
075    
076    /**
077     * Set the global eqTD value
078     * @param coursePartId The course part identifier
079     * @param eqTD the value
080     */
081    public void addGlobalEqTD(String coursePartId, Double eqTD)
082    {
083        _globalEqTD.put(coursePartId, eqTD);
084    }
085    
086    /**
087     * Set the pro-rated eqTD value
088     * @param programItemPath the program item path
089     * @param eqTD the value
090     */
091    public void addProratedEqTD(String programItemPath, Double eqTD)
092    {
093        _proratedEqTD.put(programItemPath, eqTD);
094    }
095    
096    /**
097     * Get the pro-rated eqTD value
098     * @param programItemPath the program item path
099     * @return the pro-rated eqTD value
100     */
101    public Double getProratedEqTD(String programItemPath)
102    {
103        return _proratedEqTD.getOrDefault(programItemPath, 0d);
104    }
105    
106    /**
107     * Get all pro-rated eqTD values
108     * @return all pro-rated eqTD values
109     */
110    public Map<String, Double> getProratedEqTD()
111    {
112        return _proratedEqTD;
113    }
114
115    /**
116     * Set the local eqTD value
117     * @param programItemPath the program item path
118     * @param eqTD the value
119     */
120    public void addLocalEqTD(String programItemPath, Double eqTD)
121    {
122        _localEqTD.put(programItemPath, eqTD);
123    }
124    
125    /**
126     * Set the local eqTD value for a course part
127     * @param itemPath the course part path
128     * @param held <code>true</code> if the course part is held by the current step
129     */
130    public void addLocalEqTD4CoursePart(String itemPath, boolean held)
131    {
132        if (held)
133        {
134            _heldList.add(itemPath);
135            
136            Double coursePartLocalEqTD = getGlobalEqTD() / _heldList.size();
137            
138            for (String heldPath : _heldList)
139            {
140                _localEqTD.put(heldPath, coursePartLocalEqTD);
141            }
142        }
143        else
144        {
145            _localEqTD.put(itemPath, 0D);
146        }
147    }
148    
149    /**
150     * Get the local eqTD value
151     * @param programItemPath the program item path
152     * @return the local eqTD value
153     */
154    public Double getLocalEqTD(String programItemPath)
155    {
156        return _localEqTD.getOrDefault(programItemPath, 0d);
157    }
158
159    /**
160     * Get all local eqTD values
161     * @return all local eqTD values
162     */
163    public Map<String, Double> getLocalEqTD()
164    {
165        return _localEqTD;
166    }
167    
168    /**
169     * Sum pro-rated and local eqTD values
170     * @param eqTD The second eqTD object to sum
171     * @param nameInPath the name to search in the path
172     */
173    public void sum(EqTD eqTD, String nameInPath)
174    {
175        _globalEqTD.putAll(eqTD.getAllGlobalEqTD());
176        Map<String, Double> proratedEqTD = eqTD.getProratedEqTD();
177        for (String childItemPath : proratedEqTD.keySet())
178        {
179            if (StringUtils.isEmpty(childItemPath) || childItemPath.equals(nameInPath) || childItemPath.endsWith(ModelItem.ITEM_PATH_SEPARATOR + nameInPath))
180            {
181                String newItemPath = StringUtils.removeEnd(StringUtils.removeEnd(childItemPath, nameInPath), ModelItem.ITEM_PATH_SEPARATOR);
182                _proratedEqTD.put(newItemPath, _proratedEqTD.getOrDefault(newItemPath, 0D) + proratedEqTD.get(childItemPath));
183            }
184        }
185    }
186}