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
017
018package org.ametys.plugins.odfpilotage.cost.entity;
019
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023import java.util.Optional;
024
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.cms.data.ContentDataHelper;
028import org.ametys.odf.enumeration.OdfReferenceTableEntry;
029import org.ametys.odf.program.Container;
030import org.ametys.plugins.odfpilotage.helper.ReportUtils;
031import org.ametys.plugins.repository.data.holder.group.impl.ModelAwareRepeater;
032import org.ametys.plugins.repository.data.holder.group.impl.ModelAwareRepeaterEntry;
033import org.ametys.runtime.config.Config;
034
035/**
036 * This class store caches of the CostComputationDataComponent
037 */
038public class CostComputationDataCache
039{
040    /** The current step */
041    protected Container _currentStep;
042    
043    /** Map initialisées */
044    private Map<String, Double> _eqTDByNature;
045    private Map<String, Map<String, Long>> _effectiveMinMaxByNature;
046    private Map<String, Map<String, NormDetails>> _norms;
047    private Map<String, Double> _enteredEffectiveCache;
048    private Map<String, Effectives> _calculatedEffectiveCache;
049    private Map<String, Double> _weightCache;
050   
051    private OverriddenData _overriddenData;
052    
053    /**
054     * The constructor
055     * @param norms list of norms
056     * @param teachingNatures list of teaching natures
057     * @param overriddenData overridden data by the user
058     */
059    public CostComputationDataCache(List<OdfReferenceTableEntry> norms, List<OdfReferenceTableEntry> teachingNatures, OverriddenData overriddenData)
060    {
061        _eqTDByNature = new HashMap<>();
062        _effectiveMinMaxByNature = new HashMap<>();
063        _norms = new HashMap<>();
064        _enteredEffectiveCache = new HashMap<>();
065        _calculatedEffectiveCache = new HashMap<>();
066        _weightCache = new HashMap<>();
067        _initializeNatureData(teachingNatures);
068        _initializeNormData(norms);
069        _overriddenData = overriddenData;
070    }
071
072    /**
073     * Retrieve norm values created and initialize informations about them in the cache
074     * @param norms list of norms
075     */
076    protected void _initializeNormData(List<OdfReferenceTableEntry> norms)
077    {
078        for (OdfReferenceTableEntry normeEntry : norms)
079        {
080            Map<String, NormDetails> norm = new HashMap<>();
081            if (normeEntry.getContent().hasValue("groupEffectives"))
082            {
083                ModelAwareRepeater groupEffectives = normeEntry.getContent().getRepeater("groupEffectives");
084                for (ModelAwareRepeaterEntry groupEffectivesEntry : groupEffectives.getEntries())
085                {
086                    NormDetails normDetails = new NormDetails(groupEffectivesEntry.getValue("effectifMinSup"), groupEffectivesEntry.getValue("effectifMax"), normeEntry.getLabel(Config.getInstance().getValue("odf.programs.lang")));
087                    norm.put(ContentDataHelper.getContentIdFromContentData(groupEffectivesEntry, "natureEnseignement"), normDetails);
088                }
089            }
090            _norms.put(normeEntry.getId(), norm);
091        }
092    }
093    
094    /**
095     * Retrieve teaching nature values created and initialize informations about them in the cache
096     * @param teachingNatures list of teaching natures
097     */
098    protected void _initializeNatureData(List<OdfReferenceTableEntry> teachingNatures)
099    {
100        for (OdfReferenceTableEntry teachingNature : teachingNatures)
101        {
102            String id = teachingNature.getId();
103            
104            Double eqTD = ReportUtils.transformEqTD2Double(teachingNature.getContent().getValue("eqTD", false, StringUtils.EMPTY));
105            if (eqTD != null)
106            {
107                _eqTDByNature.put(id, eqTD);
108            }
109            
110            Long effectiveMax = teachingNature.getContent().getValue("effectifMax", false, 0L);
111            Long effectiveMinSup = teachingNature.getContent().getValue("effectifMinSup", false, 0L);
112            if (effectiveMax > 0 && effectiveMinSup > 0)
113            {
114                Map<String, Long> effectives = new HashMap<>();
115                effectives.put("effectifMax", effectiveMax);
116                effectives.put("effectifMinSup", effectiveMinSup);
117                _effectiveMinMaxByNature.put(id, effectives);
118            }
119        }
120    }
121    
122    /**
123     * Get an eqTD value from a natureId
124     * @param natureId the nature id
125     * @return the eqTD value
126     */
127    public Double getEqTDByNature(String natureId)
128    {
129        return _eqTDByNature.get(natureId);
130    }
131    
132    /**
133     * Put an eqTD value in the map
134     * @param natureId the nature id
135     * @param eqTD the eqTD value
136     */
137    public void putEqTDByNature(String natureId, Double eqTD)
138    {
139        _eqTDByNature.put(natureId, eqTD);
140    }
141    
142    /**
143     * Get an effectiveMinMax from a nature id
144     * @param natureId the nature id
145     * @return the effective min max
146     */
147    public Map<String, Long> getEffectiveMinMaxByNature(String natureId)
148    {
149        return _effectiveMinMaxByNature.get(natureId);
150    }
151    
152    /**
153     * Put an effectiveMinMax in the map
154     * @param natureId the nature id
155     * @param effectiveMinMax the effective min max
156     */
157    public void putEffectiveMinMaxByNature(String natureId, Map<String, Long> effectiveMinMax)
158    {
159        _effectiveMinMaxByNature.put(natureId, effectiveMinMax);
160    }
161   
162    /**
163     * Get the norm retrieved by a nature id
164     * @param normId the norm id
165     * @param natureId the nature id
166     * @return the norm
167     */
168    public NormDetails getNorms(String normId, String natureId)
169    {
170        return _norms.get(normId).get(natureId);
171    }
172    
173    /**
174     * Check if the norms map contains the norm id
175     * @param normId the norm id
176     * @return true if the norms map contains the norm id
177     */
178    public boolean normsContainsNormIdKey(String normId)
179    {
180        return _norms.containsKey(normId);
181    }
182    
183    /**
184     * Check if the norms map contains the nature id
185     * @param normId the norm id
186     * @param natureId the nature id
187     * @return true if the norms map contains the nature id
188     */
189    public boolean normsContainsNatureIdKey(String normId, String natureId)
190    {
191        return _norms.get(normId).containsKey(natureId);
192    }
193    
194    /**
195     * Put a norm value in the map
196     * @param normId the norm id
197     * @param normValue the value
198     */
199    public void putNorms(String normId, Map<String, NormDetails> normValue)
200    {
201        _norms.put(normId, normValue);
202    }
203    
204    /**
205     * Get an enteredEffective from a program item id
206     * @param programItemId the program item id
207     * @return the entered effective
208     */
209    public Double getEnteredEffectiveCache(String programItemId)
210    {
211        return _enteredEffectiveCache.get(programItemId);
212    }
213
214    /**
215     * Put an entered effective in the map
216     * @param programItemId the program item id
217     * @param enteredEffective the entered effective
218     */
219    public void putEnteredEffectiveCache(String programItemId, Double enteredEffective)
220    {
221        _enteredEffectiveCache.put(programItemId, enteredEffective);
222    }
223    
224    /**
225     * Get a calculated effective from a program item id
226     * @param programItemId the program item id
227     * @return the calculated effective
228     */
229    public Effectives getComputedEffectiveCache(String programItemId)
230    {
231        return _calculatedEffectiveCache.get(programItemId);
232    }
233
234    /**
235     * Put a calculated effective in the map
236     * @param programItemId the program item id
237     * @param calculatedEffective the calculated effective
238     */
239    public void putComputedEffectiveCache(String programItemId, Effectives calculatedEffective)
240    {
241        _calculatedEffectiveCache.put(programItemId, calculatedEffective);
242    } 
243    
244    /**
245     * Put a weight in the map
246     * @param programItemPath the program item id
247     * @param weight the weight
248     */
249    public void putWeight(String programItemPath, Double weight)
250    {
251        _weightCache.put(programItemPath, weight);
252    } 
253    
254    /**
255     * Get a calculated effective from a program item 
256     * @param programItemPath the path of the program item
257     * @return the calculated effective
258     */
259    public Double getWeight(String programItemPath)
260    {
261        return _weightCache.get(programItemPath);
262    }
263
264    /** 
265     * Set the current step
266     * @param currentStep the current step
267     */
268    public void setCurrentStep(Container currentStep)
269    {
270        _currentStep = currentStep;
271    }
272    
273    /**
274     * Get the current step
275     * @return the step
276     */
277    public Container getCurrentStep()
278    {
279        return _currentStep;
280    }
281    
282    /**
283     * Get the overridden effective of a program item
284     * @param programItemId the program item id
285     * @return the overridden effective
286     */
287    public Optional<Double> getOverriddenEffective(String programItemId)
288    {
289        return _overriddenData.getEffective(programItemId);
290    }
291    
292    /**
293     * Get the overridden number groups of a program item
294     * @param programItemId the program item id
295     * @return the overridden number of groups
296     */
297    public Optional<Double> getOverriddenGroups(String programItemId)
298    {
299        return _overriddenData.getNumberOfGroups(programItemId);
300    }
301    
302    /**
303     * Get the overridden volume of hours of a program item
304     * @param programItemId the program item id
305     * @param natureId the nature of the volume of hours
306     * @return the overridden volume of hours
307     */
308    public Optional<Double> getOverriddenVolumeOfHours(String programItemId, String natureId)
309    {
310        return _overriddenData.getVolumeOfHours(programItemId, natureId);
311    }
312}