IpatOutput.java [src/java/m/ipat] 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 m.ipat;

import csip.ServiceException;
import csip.utils.JSONUtils;
import data.table.Table;
import data.table.column.ColumnDouble;
import data.table.column.ColumnString;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import utils.MDS_Result;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class IpatOutput extends Table {

    public static final String IRRIG_SYS_WATER_USE = "irrig_sys_water_use";
    public static final String IRRIG_SYS_COST = "irrig_sys_cost";
    public static final String LAND_UNITS_ARRAY = "land_unit_water_use";

    protected LandUnits land_units;

    private MDS_Result output;

    public class LandUnits extends Table {

        public static final String LAND_UNIT_ID = "land_unit_id";
        public static final String ROTATION_WATER_USE = "rotation_water_use";
        public static final String ROTATION_WATER_APPLIED = "rotation_water_applied";
        
        protected Crops crops;

        public LandUnits() {
            addDataColumn(LAND_UNIT_ID, new ColumnString(LAND_UNIT_ID, ""));
            addDataColumn(ROTATION_WATER_USE, new ColumnDouble(ROTATION_WATER_USE, "The total amount of water to apply to the land unit specified", "Acre Feet", "%.2f"));
            addDataColumn(ROTATION_WATER_APPLIED, new ColumnDouble(ROTATION_WATER_APPLIED, "The amount of water to apply to the land unit specified, by acre.", "Inches per acre", "%.2f"));

            setNonOutputColumns(null);
            
            crops = new Crops();
        }

        public class Crops extends Table {

            public static final String LAND_UNIT_ID = "land_unit_id";
            public static final String ROTATION_WATER_USE = "rotation_water_use";
            public static final String ROTATION_WATER_APPLIED = "rotation_water_applied";

            public Crops() {
                addDataColumn(LAND_UNIT_ID, new ColumnString(LAND_UNIT_ID, ""));
                addDataColumn(ROTATION_WATER_USE, new ColumnDouble(ROTATION_WATER_USE, "The total amount of water to apply to the land unit specified", "Acre Feet", "%.2f"));
                addDataColumn(ROTATION_WATER_APPLIED, new ColumnDouble(ROTATION_WATER_APPLIED, "The amount of water to apply to the land unit specified, by acre.", "Inches per acre", "%.2f"));

                setNonOutputColumns(null);
            }
        }
    }

    public IpatOutput(MDS_Result makeResult) {
        output = makeResult;
        land_units = new LandUnits();
        addDataColumn(IRRIG_SYS_WATER_USE, new ColumnDouble(IRRIG_SYS_WATER_USE, "The total amount of water provided to all land units of the irrigation system.", "Acre Feet", "%.2f"));
        addDataColumn(IRRIG_SYS_COST, new ColumnDouble(IRRIG_SYS_COST, "The total annual energy cost to operate the irrigation system, including water pumping, delivery, and application accounting for the application of energy savings methods and techniques.", "USD", "$%.2f"));

        setNonOutputColumns(null);
    }

    public void addLandUnit(String id, double water_use, double water_applied) throws ServiceException {
        Map<String, Object> row = new HashMap<>();

        row.put(IpatOutput.LandUnits.LAND_UNIT_ID, id);
        row.put(IpatOutput.LandUnits.ROTATION_WATER_USE, water_use);
        row.put(IpatOutput.LandUnits.ROTATION_WATER_APPLIED, water_applied);

        land_units.appendRow(row);
    }

    public void putResult() throws JSONException {
        setRowFirst();
        toJSON(output);

        JSONArray landUnitArray = new JSONArray();
        land_units.toJSONAll(landUnitArray);

        output.putResult(LAND_UNITS_ARRAY, landUnitArray, null, null);

    }
}