V1_0.java [src/java/m/wqm/wqm11_pesthazrating] Revision:   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.wqm.wqm11_pesthazrating;

import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.Resource;
import csip.utils.JSONUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Level;
import javax.ws.rs.Path;
import csip.annotations.Description;
import csip.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import wqm.utils.DBQueries;
import wqm.utils.DBResources;
import static wqm.utils.DBResources.WQM_ID;

/**
 *
 * @author Srinivas
 * @author Shaun Case
 */
@Name("WQM-11: Pesticide Hazard Ratings (PestHazRating)")
@Description("This service computes pesticide hazard ratings for leaching, "
        + "solution runoff, and adsorbed runoff for each pesticide applied in "
        + "an area of analysis, and then computes hazard ratings for each "
        + "concern representing the area of analysis (AoA). "
        + "The service consumes soil/pesticide interaction loss potentials "
        + "from the WQM-10 service to compute the hazard ratings, which are "
        + "used later by the WQM-13 service to compute mitigation threshold scores.")
@Path("m/pest_hazrating/1.0")
@Resource(from = DBResources.class)
public class V1_0 extends ModelDataService {

    private ArrayList<m.wqm.wqm11_pesthazrating.V1_0.Input> components;
    private ArrayList<m.wqm.wqm11_pesthazrating.V1_0.Result1> result1;
    private String aoa_phr_leach_human;
    private String aoa_phr_leach_matcfish;
    private String aoa_phr_sorun_human;
    private String aoa_phr_sorun_matcfish;
    private String aoa_phr_adrun_human;
    private String aoa_phr_adrun_stvfish;

    @Override
    // reading the inputs from the json file into input object and placing it in the arraylist
    protected void preProcess() throws ServiceException {
        aoa_phr_leach_human = "";
        aoa_phr_leach_matcfish = "";
        aoa_phr_sorun_human = "";
        aoa_phr_sorun_matcfish = "";
        aoa_phr_adrun_human = "";
        aoa_phr_adrun_stvfish = "";

        components = new ArrayList<>();
        result1 = new ArrayList<>();

        try {
            JSONArray groups = parameter().getJSONArray("pestcomponents");
            for (int i = 0; i < groups.length(); i++) {
                Map<String, JSONObject> group = JSONUtils.preprocess(groups.getJSONArray(i));

                int operation_id = JSONUtils.getIntParam(group, "operation_id", 0);
                String op_pest_id = JSONUtils.getStringParam(group, "op_pest_id", "err");
                String op_pest_ilp = JSONUtils.getStringParam(group, "op_pest_ilp", "err");
                String op_pest_isrp = JSONUtils.getStringParam(group, "op_pest_isrp", "err");
                String op_pest_iarp = JSONUtils.getStringParam(group, "op_pest_iarp", "err");
                double ai_eathuman = JSONUtils.getDoubleParam(group, "ai_eathuman", 0);
                double ai_eatmatc = JSONUtils.getDoubleParam(group, "ai_eatmatc", 0);
                double ai_koc = JSONUtils.getDoubleParam(group, "ai_koc", 0);
                components.add(new m.wqm.wqm11_pesthazrating.V1_0.Input(operation_id,
                        op_pest_id, op_pest_ilp, op_pest_isrp, op_pest_iarp,
                        ai_eathuman, ai_eatmatc, ai_koc));
            }
        } catch (ServiceException | JSONException ex) {
            LOG.log(Level.SEVERE, "Error in processing the request JSON for WQM-11!", ex);
            throw new ServiceException("Error in processing the request JSON.", ex);
        }
    }

    @Override
    protected void doProcess() throws ServiceException {
        String eat_rating_human = null;

        try (Connection conn = resources().getJDBC(WQM_ID);
                Statement statement = conn.createStatement()) {

            for (m.wqm.wqm11_pesthazrating.V1_0.Input ip : components) {
                if (ip.ai_eathuman < 1) {
                    eat_rating_human = "EXTRA HIGH";
                } else if (ip.ai_eathuman >= 1 && ip.ai_eathuman < 10) {
                    eat_rating_human = "HIGH";
                } else if (ip.ai_eathuman >= 10 && ip.ai_eathuman < 50) {
                    eat_rating_human = "INTERMEDIATE";
                } else if (ip.ai_eathuman >= 50 && ip.ai_eathuman < 100) {
                    eat_rating_human = "LOW";
                } else if (ip.ai_eathuman >= 100) {
                    eat_rating_human = "VERY LOW";
                }

                String eat_rating_human_temp = "'" + eat_rating_human + "'";
                String op_pest_ilp_temp = "'" + ip.op_pest_ilp + "'";
                /*String query = "SELECT wqm_phr FROM wqm.wqm_pesticide_hazard_potential "
                        + "WHERE wqm_ilr=" + op_pest_ilp_temp
                        + "AND wqm_eat=" + eat_rating_human_temp;*/
                String op_phr_leach_human = "";
                ResultSet results = statement.executeQuery(DBQueries.WQM11Query01(op_pest_ilp_temp, eat_rating_human_temp));
                while (results.next()) {
                    op_phr_leach_human = results.getString("wqm_phr");
                }
                String op_pest_isrp_temp = "'" + ip.op_pest_isrp + "'";
                /*query = "SELECT wqm_phr FROM wqm.wqm_pesticide_hazard_potential "
                        + "WHERE wqm_ilr=" + op_pest_isrp_temp
                        + "AND wqm_eat=" + eat_rating_human_temp;*/
                results = statement.executeQuery(DBQueries.WQM11Query02(op_pest_isrp_temp, eat_rating_human_temp));
                String op_phr_sorun_human = "";
                while (results.next()) {
                    op_phr_sorun_human = results.getString("wqm_phr");
                }
                String op_pest_iarp_temp = "'" + ip.op_pest_iarp + "'";
                /*query = "SELECT wqm_phr FROM wqm.wqm_pesticide_hazard_potential "
                        + "WHERE wqm_ilr=" + op_pest_iarp_temp
                        + "AND wqm_eat=" + eat_rating_human_temp;*/
                results = statement.executeQuery(DBQueries.WQM11Query03(op_pest_iarp_temp, eat_rating_human_temp));
                String op_phr_adrun_human = "";
                while (results.next()) {
                    op_phr_adrun_human = results.getString("wqm_phr");
                }
                // #Compute pesticide hazard rating for adsorbed runoff adjusted for toxicity to fish (STV)

                double ai_eatstv = ip.ai_eatmatc * ip.ai_koc;
                String eat_rating_stvfish = null;
                if (ai_eatstv < 10) {
                    eat_rating_stvfish = "EXTRA HIGH";
                } else if (ai_eatstv >= 10 && ai_eatstv < 100) {
                    eat_rating_stvfish = "HIGH";
                } else if (ai_eatstv >= 100 && ai_eatstv < 1500) {
                    eat_rating_stvfish = "INTERMEDIATE";
                } else if (ai_eatstv >= 1500 && ai_eatstv < 20000) {
                    eat_rating_stvfish = "LOW";
                } else if (ai_eatstv >= 20000) {
                    eat_rating_stvfish = "VERY LOW";
                }
                String eat_rating_stvfish_temp = "'" + eat_rating_stvfish + "'";
                /*query = "SELECT wqm_phr FROM wqm.wqm_pesticide_hazard_potential "
                        + "WHERE wqm_ilr=" + op_pest_iarp_temp
                        + "AND wqm_eat=" + eat_rating_stvfish_temp;*/
                results = statement.executeQuery(DBQueries.WQM11Query04(op_pest_iarp_temp, eat_rating_stvfish_temp));
                String op_phr_adrun_stvfish = "";
                while (results.next()) {
                    op_phr_adrun_stvfish = results.getString("wqm_phr");
                }

                // Compute pesticide hazard rating for leaching, solution runoff, and adsorbed runoff adjusted for toxicity to fish (MATC)
                String eat_rating_matcfish = null;
                if (ip.ai_eatmatc < 10) {
                    eat_rating_matcfish = "EXTRA HIGH";
                } else if (ip.ai_eatmatc >= 10 && ip.ai_eatmatc < 100) {
                    eat_rating_matcfish = "HIGH";
                } else if (ip.ai_eatmatc >= 100 && ip.ai_eatmatc < 1500) {
                    eat_rating_matcfish = "INTERMEDIATE";
                } else if (ip.ai_eatmatc >= 1500 && ip.ai_eatmatc < 20000) {
                    eat_rating_matcfish = "LOW";
                } else if (ip.ai_eatmatc >= 20000) {
                    eat_rating_matcfish = "VERY LOW";
                }
                String eat_rating_matcfish_temp = "'" + eat_rating_matcfish + "'";
                /*query = "SELECT wqm_phr FROM wqm.wqm_pesticide_hazard_potential "
                        + "WHERE wqm_ilr=" + op_pest_ilp_temp
                        + "AND wqm_eat=" + eat_rating_matcfish_temp;*/
                results = statement.executeQuery(DBQueries.WQM11Query05(op_pest_ilp_temp, eat_rating_matcfish_temp));
                String op_phr_leach_matcfish = "";
                while (results.next()) {
                    op_phr_leach_matcfish = results.getString("wqm_phr");
                }
                /*query = "SELECT wqm_phr FROM wqm.wqm_pesticide_hazard_potential "
                        + "WHERE wqm_ilr=" + op_pest_isrp_temp
                        + "AND wqm_eat=" + eat_rating_matcfish_temp;*/
                results = statement.executeQuery(DBQueries.WQM11Query06(op_pest_isrp_temp, eat_rating_matcfish_temp));
                String op_phr_sorun_matcfish = "";
                while (results.next()) {
                    op_phr_sorun_matcfish = results.getString("wqm_phr");
                }
                result1.add(new m.wqm.wqm11_pesthazrating.V1_0.Result1(ip.operation_id,
                        ip.op_pest_id, op_phr_leach_human, op_phr_leach_matcfish,
                        op_phr_sorun_human, op_phr_sorun_matcfish, op_phr_adrun_human,
                        op_phr_adrun_stvfish));
            }

            calAoANutSLP(result1);
        } catch (ServiceException | SQLException ex) {
            LOG.log(Level.SEVERE, "SQLException for WQM-11!", ex);
            throw new ServiceException("SQL problem.", ex);
        }
    }

    @Override
    //writing the results back to JSON
    protected void postProcess() throws ServiceException {

        try {
            JSONArray result1Arr = new JSONArray();
            for (m.wqm.wqm11_pesthazrating.V1_0.Result1 rs1 : result1) {
                JSONArray tmpArr = new JSONArray();
                tmpArr.put(JSONUtils.dataDesc("operation_id", rs1.operation_id, "operation identifier"));
                tmpArr.put(JSONUtils.dataDesc("pesticide_id", rs1.op_pest_id, "pesticide identifier"));
                tmpArr.put(JSONUtils.dataDesc("op_phr_leach_human", rs1.op_phr_leach_human, "Farm Operation Pesticide Leaching Hazard Rating - Humans"));
                tmpArr.put(JSONUtils.dataDesc("op_phr_leach_matcfish", rs1.op_phr_leach_matcfish, "Farm Operation Pesticide Leaching Hazard Rating - Fish"));
                tmpArr.put(JSONUtils.dataDesc("op_phr_sorun_human", rs1.op_phr_sorun_human, "Farm Operation Pesticide Solution Runoff Hazard Rating - Humans"));
                tmpArr.put(JSONUtils.dataDesc("op_phr_sorun_matcfish", rs1.op_phr_sorun_matcfish, "Farm Operation Pesticide Solution Runoff Hazard Rating - Fish"));
                tmpArr.put(JSONUtils.dataDesc("op_phr_adrun_human", rs1.op_phr_adrun_human, "Farm Operation Pesticide Adsorbed Runoff Hazard Rating - Humans"));
                tmpArr.put(JSONUtils.dataDesc("op_phr_adrun_stvfish", rs1.op_phr_adrun_stvfish, "Farm Operation Pesticide Adsorbed Runoff Hazard Rating - Fish"));
                result1Arr.put(JSONUtils.dataDesc("pesticide summary", tmpArr, "Pesticide Summary"));
            }

            results().put("operation", result1Arr);
            results().put("aoa_phr_leach_human", aoa_phr_leach_human, "Pesticide Leaching Hazard Rating for Humans in the Area of Analysis");
            results().put("aoa_phr_leach_matcfish", aoa_phr_leach_matcfish, "Pesticide Leaching Hazard Rating for Fish in the Area of Analysis");
            results().put("aoa_phr_sorun_human", aoa_phr_sorun_human, "Pesticide Solution Runoff Hazard Rating for Humans in the Area of Analysis");
            results().put("aoa_phr_sorun_matcfish", aoa_phr_sorun_matcfish, "Pesticide Solution Runoff Hazard Rating for Fish in the Area of Analysis");
            results().put("aoa_phr_adrun_human", aoa_phr_adrun_human, "Pesticide Adsorbed Runoff Hazard Rating for Humans in the Area of Analysis");
            results().put("aoa_phr_adrun_stvfish", aoa_phr_adrun_stvfish, "Pesticide Adsorbed Runoff Hazard Rating for Fish in the Area of Analysis");
        } catch (JSONException ex) {
            LOG.log(Level.SEVERE, "Error in processing the response JSON for WQM-11!", ex);
            throw new ServiceException("Error in processing the response JSON.", ex);
        }

    }

    // calculate the aoa_nslp
    void calAoANutSLP(ArrayList<m.wqm.wqm11_pesthazrating.V1_0.Result1> source) {
        int phr_leach_high = 1;
        int phr_leach_new = 1;
        //  #Compute pesticide hazard rating for Pesticide Leaching – Human concern

        for (m.wqm.wqm11_pesthazrating.V1_0.Result1 rs1 : source) {
            switch (rs1.op_phr_leach_human) {
                case "EXTRA HIGH":
                    phr_leach_new = 5;
                    break;
                case "HIGH":
                    phr_leach_new = 4;
                    break;
                case "INTERMEDIATE":
                    phr_leach_new = 3;
                    break;
                case "LOW":
                    phr_leach_new = 2;
                    break;
                case "VERY LOW":
                    phr_leach_new = 1;
                    break;
            }
            if (phr_leach_new > phr_leach_high) {
                phr_leach_high = phr_leach_new;
            }
        }

        String aoa_phr_leach_array[] = {"", "VERY LOW", "LOW", "INTERMEDIATE", "HIGH", "EXTRA HIGH"};
        aoa_phr_leach_human = aoa_phr_leach_array[phr_leach_high];

        phr_leach_new = 1;
        phr_leach_high = 1;
        //  #Compute pesticide hazard rating for Pesticide Leaching – Fish concern

        for (m.wqm.wqm11_pesthazrating.V1_0.Result1 rs1 : source) {
            switch (rs1.op_phr_leach_matcfish) {
                case "EXTRA HIGH":
                    phr_leach_new = 5;
                    break;
                case "HIGH":
                    phr_leach_new = 4;
                    break;
                case "INTERMEDIATE":
                    phr_leach_new = 3;
                    break;
                case "LOW":
                    phr_leach_new = 2;
                    break;
                case "VERY LOW":
                    phr_leach_new = 1;
                    break;
            }
            if (phr_leach_new > phr_leach_high) {
                phr_leach_high = phr_leach_new;
            }
        }
        aoa_phr_leach_matcfish = aoa_phr_leach_array[phr_leach_high];

        phr_leach_new = 1;
        phr_leach_high = 1;
        //  #Compute pesticide hazard rating for Pesticide Solution Runoff – Human concern

        for (m.wqm.wqm11_pesthazrating.V1_0.Result1 rs1 : source) {
            switch (rs1.op_phr_sorun_human) {
                case "EXTRA HIGH":
                    phr_leach_new = 4;
                    break;
                case "HIGH":
                    phr_leach_new = 3;
                    break;
                case "INTERMEDIATE":
                    phr_leach_new = 2;
                    break;
                case "LOW":
                    phr_leach_new = 1;
                    break;
            }

            if (phr_leach_new > phr_leach_high) {
                phr_leach_high = phr_leach_new;
            }
        }
        String aoa_phr_array[] = {"", "LOW", "INTERMEDIATE", "HIGH", "EXTRA HIGH"};
        aoa_phr_sorun_human = aoa_phr_array[phr_leach_high];

        phr_leach_new = 1;
        phr_leach_high = 1;
        //  #Compute pesticide hazard rating for Pesticide Solution Runoff – Fish concern

        for (m.wqm.wqm11_pesthazrating.V1_0.Result1 rs1 : source) {
            switch (rs1.op_phr_sorun_matcfish) {
                case "EXTRA HIGH":
                    phr_leach_new = 4;
                    break;
                case "HIGH":
                    phr_leach_new = 3;
                    break;
                case "INTERMEDIATE":
                    phr_leach_new = 2;
                    break;
                case "LOW":
                    phr_leach_new = 1;
                    break;
            }

            if (phr_leach_new > phr_leach_high) {
                phr_leach_high = phr_leach_new;
            }
        }
        //     String aoa_phr_sorun_matcfish_array[]={"","LOW","INTERMEDIATE","HIGH"};
        aoa_phr_sorun_matcfish = aoa_phr_array[phr_leach_high];

        phr_leach_new = 1;
        phr_leach_high = 1;
        // #Compute pesticide hazard rating for Pesticide Adsorbed Runoff – Human Concern

        for (m.wqm.wqm11_pesthazrating.V1_0.Result1 rs1 : source) {
            switch (rs1.op_phr_adrun_human) {
                case "EXTRA HIGH":
                    phr_leach_new = 4;
                    break;
                case "HIGH":
                    phr_leach_new = 3;
                    break;
                case "INTERMEDIATE":
                    phr_leach_new = 2;
                    break;
                case "LOW":
                    phr_leach_new = 1;
                    break;
            }

            if (phr_leach_new > phr_leach_high) {
                phr_leach_high = phr_leach_new;
            }
        }
        //     String aoa_phr_adrun_human_array[]={"","LOW","INTERMEDIATE","HIGH"};
        aoa_phr_adrun_human = aoa_phr_array[phr_leach_high];

        phr_leach_new = 1;
        phr_leach_high = 1;
        //  #Compute pesticide hazard rating for Pesticide Absorbed Runoff – Fish concern

        for (m.wqm.wqm11_pesthazrating.V1_0.Result1 rs1 : source) {
            switch (rs1.op_phr_adrun_stvfish) {
                case "EXTRA HIGH":
                    phr_leach_new = 4;
                    break;
                case "HIGH":
                    phr_leach_new = 3;
                    break;
                case "INTERMEDIATE":
                    phr_leach_new = 2;
                    break;
                case "LOW":
                    phr_leach_new = 1;
                    break;
            }

            if (phr_leach_new > phr_leach_high) {
                phr_leach_high = phr_leach_new;
            }
        }

        aoa_phr_adrun_stvfish = aoa_phr_array[phr_leach_high];

    }

    static class Input {

        int operation_id;
        String op_pest_id;
        String op_pest_ilp;
        String op_pest_isrp;
        String op_pest_iarp;
        double ai_eathuman;
        double ai_eatmatc;
        double ai_koc;

        public Input(int operation_id, String op_pest_id, String op_pest_ilp,
                String op_pest_isrp, String op_pest_iarp, double ai_eathuman,
                double ai_eatmatc, double ai_koc) {
            this.operation_id = operation_id;
            this.op_pest_id = op_pest_id;
            this.op_pest_ilp = op_pest_ilp;
            this.op_pest_isrp = op_pest_isrp;
            this.op_pest_iarp = op_pest_iarp;
            this.ai_eathuman = ai_eathuman;
            this.ai_eatmatc = ai_eatmatc;
            this.ai_koc = ai_koc;

        }

    }

    static class Result1 {

        int operation_id;
        String op_pest_id;
        String op_phr_leach_human;
        String op_phr_leach_matcfish;
        String op_phr_sorun_human;
        String op_phr_sorun_matcfish;
        String op_phr_adrun_human;
        String op_phr_adrun_stvfish;

        public Result1(int operation_id, String op_pest_id, String op_phr_leach_human,
                String op_phr_leach_matcfish, String op_phr_sorun_human,
                String op_phr_sorun_matcfish, String op_phr_adrun_human,
                String op_phr_adrun_stvfish) {
            this.operation_id = operation_id;
            this.op_pest_id = op_pest_id;
            this.op_phr_leach_human = op_phr_leach_human;
            this.op_phr_leach_matcfish = op_phr_leach_matcfish;
            this.op_phr_sorun_human = op_phr_sorun_human;
            this.op_phr_sorun_matcfish = op_phr_sorun_matcfish;
            this.op_phr_adrun_human = op_phr_adrun_human;
            this.op_phr_adrun_stvfish = op_phr_adrun_stvfish;
        }
    }

}