ConfigData.java [src/java/d/util] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API, and application suite.
 *
 * 2012-2017, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package d.util;

import csip.SessionLogger;
import java.util.ArrayList;
import java.util.HashMap;
import javax.measure.Measurable;
import javax.measure.Measure;
import javax.measure.quantity.Length;
import javax.measure.unit.NonSI;
import javax.measure.unit.SI;

/**
 *
 * @author brad
 */
public class ConfigData {
    
    private SessionLogger LOG;
    
    private static final String CD = "CD-";
    public static final String REP = "Rep-";
    
    public static final String Units = CD + "measurement";
    public static final String SoilTestOrganic = CD + "soil.organictest";
    public static final String SoilMaxOrganicDepth = CD + "soil.maxorganicdepth";
    public static final String SoilSkipOrganicSurfaceLayers = CD + "soil.skiporganic";
    public static final String SoilAverageStratifiedLayers = CD + "soil.averagestratified";
    
    private DefaultConfigStorage c_defaultConfig;
    private ArrayList<ConfigStorage> c_storage;
    private ConfigStorage c_userConfig;
    private HashMap<String,String> c_reportMap;
    private HashMap<String,Boolean> c_reportChangeMap;
    
    private ConfigData(SessionLogger log) {
            LOG = log;
                
            //special storage with all the default values
            c_defaultConfig = new DefaultConfigStorage();
            c_defaultConfig.load(this); // I don't know if I like pass "this" in a constructor but I also don't like classes in classes

            c_storage = new ArrayList<>();
            c_storage.add(c_defaultConfig);
            c_reportMap = new HashMap<>();
            c_reportChangeMap = new HashMap<>();

    }
    
    public static ConfigData getDefault(SessionLogger log) {
        ConfigData c_default = null;
        if (c_default == null) {
            c_default = new ConfigData(log);
        }
        return c_default;
    }
    
    public String getData(String propertyName) {
            if(propertyName.startsWith(REP))
            {
                return c_reportMap.get(propertyName);
            }
            for (ConfigStorage storage : c_storage) {
                String value = storage.get(propertyName);
                if (value != null) {
                    return value;
                }
            }
            return null;
    }
    
    public boolean getSoilTestOrganic() {
        return "1".equals(getData(SoilTestOrganic));
    }
    
    public Measurable<Length> getSoilMaxOrganicDepth() {
        String valueString = getData(SoilMaxOrganicDepth);
        try {
            double depthMiliMeters = Double.valueOf(valueString);
            return Measure.valueOf(depthMiliMeters, SI.MILLIMETER);
        } catch (NumberFormatException e) {
            LOG.warning("Unable to parse the soil max organic depth. Defaulting to 4 inches." + valueString);
            return Measure.valueOf(4, NonSI.INCH);
        }
    }
    
    public boolean isSkipOrganicSoilSurfaceLayers() {
        String value = getData(SoilSkipOrganicSurfaceLayers);
        return value != null ? value.trim().equals("1") : false;
    }
    
    public boolean isAverageStratifiedSoilLayers() {
        String value = getData(SoilAverageStratifiedLayers);
        return value != null ? value.trim().equals("1") : false;
    }
    
    // Many assumtions made in this code that may need to be cleared out.
    public void setData(String propertyName, String newData) {
        if (c_userConfig == null) {
            //no user config can't set values, this could break things!
            return;
        }
        
        // I don't care about this right now because the method will return above every time
//        if(propertyName.equals(windDecNum))
//        {
//            if(newData != null && !newData.equals(""))
//            {
//                WindgenDecimal = "#0.";
//                try
//                {
//                    for(int index = 0; index < Integer.parseInt(newData); index ++)
//                    {
//                        WindgenDecimal += "0";
//                    }
//                    System.out.println(WindgenDecimal);
//                }
//                catch(NumberFormatException fail)
//                {
//                    LOGGER.warn("windDecNum must be an integer.  Resetting to 5.");
//                    //Something went wrong.  Reset to Decimal Value
//                    setData(windDecNum, "5");
//                    return;
//                }
//            }
//            else
//            {
//                //Something went wrong.  Reset to Decimal Value
//                setData(windDecNum, "5");
//                return;
//            }
//        }
//        
//        if(propertyName.equals(ReportsConfidenceIntervalEnabled))
//        {
//            changeCI(newData);
//        }
//
//        if (newData != null) {
//            newData = newData.trim();
//        } else {
//            newData = "";
//        }
//
//        String oldData = getData(propertyName);
//        if (newData.equals(oldData)) {
//            //no change, leaving
//            return;
//        }
//
//        //there is a change, do we have permission
//        AccessLevel level = getAccessLevel(propertyName);
//
//        //we only need permission on CD properties, others are not saved to file
//        if (!CurrentProj.equals(propertyName) && (propertyName.startsWith(CD) || propertyName.startsWith(REP)) && !AccessLevel.Write.equals(level)) {
//            //we don't have access level to change this value.
//            return;
//        }
//
//        if(propertyName.startsWith(CD)) c_userConfig.set(propertyName, newData);
//        else if(propertyName.startsWith(REP)) 
//        {
//            c_reportMap.put(propertyName, newData);
//            c_reportChangeMap.put(propertyName, true);
//        }
//
//        c_changes.firePropertyChange(propertyName, oldData, newData);
//
//        if (propertyName.equals(DefaultRunsLocationTemplate)) {
//            updateDefaultRunsLocation();
//        }
//        if (propertyName.equals(CurrentProj)) {
//            updateDefaultRunsLocation();
//        }
   }
}