V1_0.java [src/java/m/wqm/scorebar] Revision: ba67957b8066eafffd635845ab737282506f6933  Date: Fri Jul 17 11:02:40 MDT 2015
package m.wqm.scorebar;

/**
 *
 * @author RUMPAL SIDHU
 */
import csip.ModelDataService;
import csip.annotations.Polling;
import csip.utils.JSONUtils;
import java.util.ArrayList;
import java.util.Map;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

@Name("WQM-20")
@Description("Threshold and Mitigation Scores for WQM Scorebar")
@Path("m/wqmscorebar/1.0")
@Polling(first = 10000, next = 2000)

public class V1_0 extends ModelDataService {

    //Request
    private ArrayList<Input> components;
    //Response
    private ArrayList<Result> result;

    @Override
    // reading the inputs from the json file into input object and placing it in the arraylist
    protected void preProcess() throws Exception {
        components = new ArrayList<>();
        JSONArray groups = getJSONArrayParam("components");
        for (int i = 0; i < groups.length(); i++) {
            Map<String, JSONObject> group = JSONUtils.preprocess(groups.getJSONArray(i));
            int AoAId = JSONUtils.getIntParam(group, "AoAId", 0);
            int nleach_techn_score = JSONUtils.getIntParam(group, "nleach_techn_score", 0);
            int nsurf_techn_score = JSONUtils.getIntParam(group, "nsurf_techn_score", 0);
            int psurf_techn_score = JSONUtils.getIntParam(group, "psurf_techn_score", 0);
            int nleach_pract_score = JSONUtils.getIntParam(group, "nleach_pract_score", 0);
            int ssurf_pract_score = JSONUtils.getIntParam(group, "ssurf_pract_score", 0);
            int nsurf_pract_score = JSONUtils.getIntParam(group, "nsurf_pract_score", 0);
            int psurf_pract_score = JSONUtils.getIntParam(group, "psurf_pract_score", 0);
            int nleach_app_mgt_score = JSONUtils.getIntParam(group, "nleach_app_mgt_score", 0);
            int nsurf_app_mgt_score = JSONUtils.getIntParam(group, "nsurf_app_mgt_score", 0);
            int psurf_app_mgt_score = JSONUtils.getIntParam(group, "psurf_app_mgt_score", 0);
            int pleach_ipm_score = JSONUtils.getIntParam(group, "pleach_ipm_score", 0);
            int psolsurf_ipm_score = JSONUtils.getIntParam(group, "psolsurf_ipm_score", 0);
            int padsurf_ipm_score = JSONUtils.getIntParam(group, "padsurf_ipm_score", 0);
            int pdrift_ipm_score = JSONUtils.getIntParam(group, "pdrift_ipm_score", 0);
            int pleach_technique_score = JSONUtils.getIntParam(group, "pleach_technique_score", 0);
            int psolsurf_technique_score = JSONUtils.getIntParam(group, "psolsurf_technique_score", 0);
            int padsurf_technique_score = JSONUtils.getIntParam(group, "padsurf_technique_score", 0);
            int pdrift_technique_score = JSONUtils.getIntParam(group, "pdrift_technique_score", 0);
            int pleach_practice_score = JSONUtils.getIntParam(group, "pleach_practice_score", 0);
            int psolsurf_practice_score = JSONUtils.getIntParam(group, "psolsurf_practice_score", 0);
            int padsurf_practice_score = JSONUtils.getIntParam(group, "padsurf_practice_score", 0);
            int pdrift_practice_score = JSONUtils.getIntParam(group, "pdrift_practice_score", 0);
            int aoa_nleach_threshold = JSONUtils.getIntParam(group, "aoa_nleach_threshold", 0);
            int aoa_nrun_threshold = JSONUtils.getIntParam(group, "aoa_nrun_threshold", 0);
            int aoa_sedrun_threshold = JSONUtils.getIntParam(group, "aoa_sedrun_threshold", 0);
            int aoa_prun_threshold = JSONUtils.getIntParam(group, "aoa_prun_threshold", 0);
            int aoa_pleach_human_threshold = JSONUtils.getIntParam(group, "aoa_pleach_human_threshold", 0);
            int aoa_pleach_matcfish_threshold = JSONUtils.getIntParam(group, "aoa_pleach_matcfish_threshold", 0);
            int aoa_psorun_human_threshold = JSONUtils.getIntParam(group, "aoa_psorun_human_threshold", 0);
            int aoa_psorun_matcfish_threshold = JSONUtils.getIntParam(group, "aoa_psorun_matcfish_threshold", 0);
            int aoa_padrun_human_threshold = JSONUtils.getIntParam(group, "aoa_padrun_human_threshold", 0);
            int aoa_padrun_stvfish_threshold = JSONUtils.getIntParam(group, "aoa_padrun_stvfish_threshold", 0);
            int aoa_pdrift_human_threshold = JSONUtils.getIntParam(group, "aoa_pdrift_human_threshold", 0);
            int aoa_pdrift_fish_threshold = JSONUtils.getIntParam(group, "aoa_pdrift_fish_threshold", 0);

            Input input = new Input(AoAId, nleach_techn_score, nsurf_techn_score, psurf_techn_score, nleach_pract_score, ssurf_pract_score, nsurf_pract_score, psurf_pract_score, nleach_app_mgt_score, nsurf_app_mgt_score, psurf_app_mgt_score, pleach_ipm_score, psolsurf_ipm_score, padsurf_ipm_score, pdrift_ipm_score, pleach_technique_score, psolsurf_technique_score, padsurf_technique_score, pdrift_technique_score, pleach_practice_score, psolsurf_practice_score, padsurf_practice_score, pdrift_practice_score, aoa_nleach_threshold, aoa_nrun_threshold, aoa_sedrun_threshold, aoa_prun_threshold, aoa_pleach_human_threshold, aoa_pleach_matcfish_threshold, aoa_psorun_human_threshold, aoa_psorun_matcfish_threshold, aoa_padrun_human_threshold, aoa_padrun_stvfish_threshold, aoa_pdrift_human_threshold, aoa_pdrift_fish_threshold);
            components.add(input);
        }
    }

    @Override
    protected String process() throws Exception {
        result = new ArrayList<>();

        components.stream().map((ip) -> {
            //Nitrogen in Ground Water mitigation and threshold scores
            int bar_nleach_mit_score = ip.nleach_techn_score + ip.nleach_pract_score + ip.nleach_app_mgt_score;
            int bar_nleach_threshold = ip.aoa_nleach_threshold;
            //Nitrogen in Surface Water mitigation and threshold scores
            int bar_nsurf_mit_score = ip.nsurf_techn_score + ip.nsurf_pract_score + ip.nsurf_app_mgt_score;
            int bar_nsurf_threshold = ip.aoa_nrun_threshold;
            //Phosphorus in Surface Water mitigation and threshold scores
            int bar_psurf_mit_score = ip.psurf_techn_score + ip.psurf_pract_score + ip.psurf_app_mgt_score;
            int bar_psurf_threshold = ip.aoa_nrun_threshold;
            //Sediment in Surface Water mitigation and threshold scores
            int bar_ssurf_mit_score = ip.ssurf_pract_score;
            int bar_ssurf_threshold = ip.aoa_nrun_threshold;
            //Pesticide in Ground Water - Human mitigation and threshold scores
            int bar_pleach_human_mit_score = ip.pleach_ipm_score + ip.pleach_technique_score + ip.pleach_practice_score;
            int bar_pleach_human_threshold = ip.aoa_pleach_human_threshold;
            //Pesticide in Ground Water - Fish mitigation and threshold scores
            int bar_pleach_fish_mit_score = ip.pleach_ipm_score + ip.pleach_technique_score + ip.pleach_practice_score;
            int bar_pleach_fish_threshold = ip.aoa_pleach_matcfish_threshold;
            //Pesticide in Solution Runoff - Human mitigation and threshold scores
            int bar_psorun_human_mit_score = ip.psolsurf_ipm_score + ip.psolsurf_technique_score + ip.psolsurf_practice_score;
            int bar_psorun_human_threshold = ip.aoa_psorun_human_threshold;
            //Pesticide in Solution Runoff - Fish mitigation and threshold scores
            int bar_psorun_fish_mit_score = ip.psolsurf_ipm_score + ip.psolsurf_technique_score + ip.psolsurf_practice_score;
            int bar_psorun_fish_threshold = ip.aoa_psorun_matcfish_threshold;
            //Pesticide in Adsorbed Runoff - Human mitigation and threshold scores
            int bar_padrun_human_mit_score = ip.padsurf_ipm_score + ip.padsurf_technique_score + ip.padsurf_practice_score;
            int bar_padrun_human_threshold = ip.aoa_padrun_human_threshold;
            //Pesticide in Adsorbed Runoff - Fish mitigation and threshold scores
            int bar_padrun_fish_mit_score = ip.padsurf_ipm_score + ip.padsurf_technique_score + ip.padsurf_practice_score;
            int bar_padrun_fish_threshold = ip.aoa_padrun_stvfish_threshold;
            //Pesticide Drift - Human mitigation and threshold scores
            int bar_pdrift_human_mit_score = ip.pdrift_ipm_score + ip.pdrift_technique_score + ip.pdrift_practice_score;
            int bar_pdrift_human_threshold = ip.aoa_psorun_human_threshold;
            //Pesticide Drift - Fish mitigation and threshold scores
            int bar_pdrift_fish_mit_score = ip.pdrift_ipm_score + ip.pdrift_technique_score + ip.pdrift_practice_score;
            int bar_pdrift_fish_threshold = ip.aoa_psorun_matcfish_threshold;
            Result result1 = new Result(ip.AoAId, bar_nleach_mit_score, bar_nleach_threshold, bar_nsurf_mit_score, bar_nsurf_threshold, bar_psurf_mit_score, bar_psurf_threshold, bar_ssurf_mit_score, bar_ssurf_threshold, bar_pleach_human_mit_score, bar_pleach_human_threshold, bar_pleach_fish_mit_score, bar_pleach_fish_threshold, bar_psorun_human_mit_score, bar_psorun_human_threshold, bar_psorun_fish_mit_score, bar_psorun_fish_threshold, bar_padrun_human_mit_score, bar_padrun_human_threshold, bar_padrun_fish_mit_score, bar_padrun_fish_threshold, bar_pdrift_human_mit_score, bar_pdrift_human_threshold, bar_pdrift_fish_mit_score, bar_pdrift_fish_threshold);
            return result1;
        }).forEach((result1) -> {
            result.add(result1);
        });
        return EXEC_OK;
    }

    @Override
    //writing the results back to JSON
    protected void postProcess() throws Exception {
        JSONArray resultArr = new JSONArray();
        for (Result rs1 : result) {
            JSONArray tmpArr = new JSONArray();
            tmpArr.put(JSONUtils.dataDesc("AoAId", rs1.AoAId, "Area of Analysis Identifier"));
            tmpArr.put(JSONUtils.dataDesc("bar_nleach_mit_score", rs1.bar_nleach_mit_score, "Mitigation Score for Nitrogen In Ground Water"));
            tmpArr.put(JSONUtils.dataDesc("bar_nleach_threshold", rs1.bar_nleach_threshold, "Threshold Score for Nitrogen In Ground Water"));
            tmpArr.put(JSONUtils.dataDesc("bar_nsurf_mit_score", rs1.bar_nsurf_mit_score, "Mitigation Score for Nitrogen In Surface Water"));
            tmpArr.put(JSONUtils.dataDesc("bar_nsurf_threshold", rs1.bar_nsurf_threshold, "Threshold Score for Nitrogen In Surface Water"));
            tmpArr.put(JSONUtils.dataDesc("bar_psurf_mit_score", rs1.bar_psurf_mit_score, "Mitigation Score for Phosphorus In Surface Water"));
            tmpArr.put(JSONUtils.dataDesc("bar_psurf_threshold", rs1.bar_psurf_threshold, "Threshold Score for Phosphorus In Surface Water"));
            tmpArr.put(JSONUtils.dataDesc("bar_ssurf_mit_score", rs1.bar_ssurf_mit_score, "Mitigation Score for Sediment In Surface Water"));
            tmpArr.put(JSONUtils.dataDesc("bar_ssurf_threshold", rs1.bar_ssurf_threshold, "Threshold Score for Sediment In Surface Water"));
            tmpArr.put(JSONUtils.dataDesc("bar_pleach_human_mit_score", rs1.bar_pleach_human_mit_score, "Mitigation Score for Pesticide Leaching (Human)"));
            tmpArr.put(JSONUtils.dataDesc("bar_pleach_human_threshold", rs1.bar_pleach_human_threshold, "Threshold Score for Pesticide Leaching (Human)"));
            tmpArr.put(JSONUtils.dataDesc("bar_pleach_fish_mit_score", rs1.bar_pleach_fish_mit_score, "Mitigation Score for Pesticide Leaching (Fish)"));
            tmpArr.put(JSONUtils.dataDesc("bar_pleach_fish_threshold", rs1.bar_pleach_fish_threshold, "Threshold Score for Pesticide Leaching (Fish)"));
            tmpArr.put(JSONUtils.dataDesc("bar_psorun_human_mit_score", rs1.bar_psorun_human_mit_score, "Mitigation Score for Pesticide Solution Runoff (Human)"));
            tmpArr.put(JSONUtils.dataDesc("bar_psorun_human_threshold", rs1.bar_psorun_human_threshold, "Threshold Score for Pesticide Solution Runoff (Human)"));
            tmpArr.put(JSONUtils.dataDesc("bar_psorun_fish_mit_score", rs1.bar_psorun_fish_mit_score, "Mitigation Score for Pesticide Solution Runoff (Fish)"));
            tmpArr.put(JSONUtils.dataDesc("bar_psorun_fish_threshold", rs1.bar_psorun_fish_threshold, "Threshold Score for Pesticide Solution Runoff (Fish)"));
            tmpArr.put(JSONUtils.dataDesc("bar_padrun_human_mit_score", rs1.bar_padrun_human_mit_score, "Mitigation Score for Pesticide Adsorbed Runoff (Human)"));
            tmpArr.put(JSONUtils.dataDesc("bar_padrun_human_threshold", rs1.bar_padrun_human_threshold, "Threshold Score for Pesticide Adsorbed Runoff (Human)"));
            tmpArr.put(JSONUtils.dataDesc("bar_padrun_fish_mit_score", rs1.bar_padrun_fish_mit_score, "Mitigation Score for Pesticide Adsorbed Runoff (Fish)"));
            tmpArr.put(JSONUtils.dataDesc("bar_padrun_fish_threshold", rs1.bar_padrun_fish_threshold, "Threshold Score for Pesticide Adsorbed Ruoff (Fish)"));
            tmpArr.put(JSONUtils.dataDesc("bar_pdrift_human_mit_score", rs1.bar_pdrift_human_mit_score, "Mitigation Score for Pesticide Drift (Human)"));
            tmpArr.put(JSONUtils.dataDesc("bar_pdrift_human_threshold", rs1.bar_pdrift_human_threshold, "Threshold Score for Pesticide Drift (Human)"));
            tmpArr.put(JSONUtils.dataDesc("bar_pdrift_fish_mit_score", rs1.bar_pdrift_fish_mit_score, "Mitigation Score for Pesticide Drift (Fish)"));
            tmpArr.put(JSONUtils.dataDesc("bar_pdrift_fish_threshold", rs1.bar_pdrift_fish_threshold, "Threshold Score for Pesticide Drift (Fish)"));
            resultArr.put(JSONUtils.dataDesc("pesticide summary", tmpArr, "Pest"));
        }
        putResult("operation", resultArr);
    }

}