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.Map;
021import java.util.Optional;
022
023/**
024 * This class store all overridden data by the user
025 */
026public class OverriddenData
027{
028    /** Map of overriden volume of hours */
029    private Map<String, Map<String, Double>> _volumeOfHours;
030    
031    /** Map of overriden number of groups */
032    private Map<String, Double> _groups;
033    
034    /** Map of overriden effectives */
035    private Map<String, Double> _effectives;
036    
037    
038    /** 
039     * The constructor
040     * @param overriddenData overridden data by the user
041     */
042    public OverriddenData(Map<String, Map<String, String>> overriddenData)
043    {
044        _volumeOfHours = new HashMap<>();
045        _groups = new HashMap<>();
046        _effectives = new HashMap<>();
047        
048        for (Map.Entry<String, Map<String, String>> json : overriddenData.entrySet())
049        {
050            String contentId = json.getKey();
051            for (Map.Entry<String, String> values : json.getValue().entrySet())
052            {
053                if (values.getKey().equals("effectifLocal"))
054                {
055                    this._effectives.put(contentId, Double.parseDouble(values.getValue()));
056                }
057                else if (values.getKey().equals("groupe"))
058                {
059                    this._groups.put(contentId, Double.parseDouble(values.getValue()));
060                }
061                else
062                {
063                    if (!_volumeOfHours.containsKey(contentId))
064                    {
065                        _volumeOfHours.put(contentId, new HashMap<String, Double>());
066                    }
067                    Map<String, Double> volumeByNature = _volumeOfHours.get(contentId);
068                    volumeByNature.put(values.getKey(), Double.parseDouble(values.getValue()));
069                }
070            }
071        }
072    }
073    
074    /** 
075     * The constructor
076     */
077    public OverriddenData()
078    {
079        _volumeOfHours = new HashMap<>();
080        _groups = new HashMap<>();
081        _effectives = new HashMap<>();
082    }
083    
084    /**
085     * Get the overridden volume of hours of a program item
086     * @param programItemId the program item id
087     * @param natureId the nature of the volume of hours
088     * @return the overridden volume of hours
089     */
090    public Optional<Double> getVolumeOfHours(String programItemId, String natureId)
091    {
092        if (_volumeOfHours.get(programItemId) != null)
093        {
094            return Optional.ofNullable(_volumeOfHours.get(programItemId).get(natureId));
095        }
096        return Optional.empty();
097    } 
098    
099    /**
100     * Get the overridden effective of a program item
101     * @param programItemId the program item id
102     * @return the overridden effective
103     */
104    public Optional<Double> getEffective(String programItemId)
105    {
106        return Optional.ofNullable(_effectives.get(programItemId));
107    } 
108    
109    /**
110     * Get the overridden number groups of a program item
111     * @param programItemId the program item id
112     * @return the overridden number of groups
113     */
114    public Optional<Double> getNumberOfGroups(String programItemId)
115    {
116        return Optional.ofNullable(_groups.get(programItemId));
117    }
118}