SoilsData.java [src/soils] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package soils;

import csip.api.server.ServiceException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class SoilsData {
    protected static boolean mapUnitsRequired = true;

    //JSON Array Names
    public final static String MAPUNIT_COMPONENT_LIST_NAME = "Components";
    public final static String HORIZON_LIST = "Horizons";
    public final static String TEXTURE_GROUPS = "Texture Groups";
    public final static String MAP_UNIT_LIST = "Map Units";

    protected synchronized static void setRequiredInputs(List<String> mapUnitInputs, List<String> componentInputs, List<String> horizonInputs) {
        MapUnit.setRequiredInputs(mapUnitInputs);
        Component.setRequiredInputs(componentInputs);
        Horizon.setRequiredInputs(horizonInputs);
    }

    public synchronized static void setMapUnitsRequired( boolean value ){
        mapUnitsRequired = value;
    }
    
    protected LinkedHashMap<String, MapUnit> map_units = new LinkedHashMap<>();

    public SoilsData(JSONArray mapUnitArray) throws ServiceException {
        readMapUnitsFromJSON(mapUnitArray, false);
    }

    public SoilsData() {

    }

    protected final void readMapUnitsFromJSON(JSONArray mapUnitArray, boolean mergeExisting) throws ServiceException {
        if (null != mapUnitArray) {
            if (mapUnitArray.length() > 0) {
                try {
                    for (int i = 0; i < mapUnitArray.length(); i++) {
                        JSONArray mapUnitObject = mapUnitArray.getJSONArray(i);
                        MapUnit mapUnit = new MapUnit(mapUnitObject);
                        if (!map_units.containsKey(mapUnit.mukey())) {
                            map_units.put(mapUnit.mukey(), mapUnit);
                        } else {
                            if (!mergeExisting) {
                                throw new ServiceException("Duplicate map unit found in input JSON for SOIL_DATA.SoilData.getMapUnitsFromJSON().  All map unit key values should be unique");
                            } else {
                                map_units.get(mapUnit.mukey()).merge(mapUnit);
                            }
                        }
                    }
                } catch (JSONException ex) {
                    throw new ServiceException("Error reading input JSON: " + ex.getMessage(), ex);
                }
            } else {
                throw new ServiceException("List of Map Units either empty or missing in input JSON for SOIL_DATA.SoilData.getMapUnitsFromJSON()");
            }

        } else {
            if (mapUnitsRequired){
                throw new ServiceException("Missing required parameter, '" + MAP_UNIT_LIST + "'.");
            }
        }
    }
    public LinkedHashMap<String, MapUnit> getMapUnits() {
        return map_units;
    }

    public void setMapUnitOutputColumns(List<String> usedMapUnitList, List<String> usedComponentList) throws ServiceException {
        if (null != usedMapUnitList) {
            for (String key : map_units.keySet()) {
                MapUnit mapUnit = map_units.get(key);
                mapUnit.setOutputColumns(usedMapUnitList);
                if (null != usedComponentList) {
                    mapUnit.setComponentOutputColumns(usedComponentList);
                }

            }
        }
    }

    public void setMapUnitUsedColumns(List<String> usedMapUnitList, List<String> usedComponentList) {
        if (null != usedMapUnitList) {
            for (String key : map_units.keySet()) {
                MapUnit mapUnit = map_units.get(key);
                mapUnit.setUsedColumns(usedMapUnitList);
                if (null != usedComponentList) {
                    mapUnit.setComponentUsedColumns(usedComponentList);
                }

            }
        }
    }


    public JSONObject toJSON(boolean selectedHorizonsOnly) throws JSONException {
        JSONObject ret_val = new JSONObject();
        JSONArray mapUnitArray = new JSONArray();

        for (MapUnit mapUnit : map_units.values()) {
            mapUnitArray.put(mapUnit.toJSON(selectedHorizonsOnly));
        }

        ret_val.put(SoilsData.MAP_UNIT_LIST, mapUnitArray);

        return ret_val;
    }

    public void toJSON(boolean selectedHorizonsOnly, JSONArray outArray) throws JSONException {
        for (MapUnit mapUnit : map_units.values()) {
            outArray.put(mapUnit.toJSON(selectedHorizonsOnly));
        }
    }

}