V1_0.java [src/java/m/wqm/pestlosspot] Revision: d4e5c230d855e631706b46dd08625538a324d934  Date: Thu Aug 13 16:01:47 MDT 2015
package m.wqm.pestlosspot;

import csip.ModelDataService;
import java.util.ArrayList;
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;
import csip.utils.JSONUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

/**
 *
 * @ Srinivas
 * @ Update Rumpal Sidhu
 */
@Name("WQM-4")
@Description("Pesticide Attributes (WQMPestAttr)")
@Path("m/pestlosspot/1.0")

public class V1_0 extends ModelDataService {

    //SQL params names here for quick modification
    private final String USER = "postgres";
    private final String PASS = "admin";
    private final String HOST = "localhost";
    private final String PORT = "5432";
    private final String DBNAME = "wqm";
    private final String JDBC_TYPE = "jdbc:postgresql://";
    private final String CONNECTION = JDBC_TYPE + HOST + ":" + PORT + "/" + DBNAME;
    private final String CLASS_NAME = "org.postgresql.Driver";

    //JSONArray getArray
    private ArrayList<Input> components; // store the set of all input soilcomponents as objects
    private ArrayList<Result1> result1;  // store the result as objects

    @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("pestcomponents");
        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 operation_id = JSONUtils.getIntParam(group, "operation_id", 0);
            String op_pest_id = JSONUtils.getStringParam(group, "op_pest_id", "err");
            String app_rate = JSONUtils.getStringParam(group, "app_rate", "err");
            String app_area = JSONUtils.getStringParam(group, "app_area", "err");
            String app_method = JSONUtils.getStringParam(group, "app_method", "err");
            Input input = new Input(AoAId, operation_id, op_pest_id, app_rate, app_area, app_method);
            components.add(input);
        }
    }

    @Override
    protected String process() throws Exception {
        result1 = new ArrayList<>();
        Connection conn = null;
        Statement statement = null;
        try {
            Class.forName(CLASS_NAME);
            conn = DriverManager.getConnection(CONNECTION, USER, PASS);
            conn.setAutoCommit(false);
            statement = conn.createStatement();

            for (Input ip : components) {
                String ai_name = "";
                double ai_ph = 0.0;
                double ai_sol = 0.0;
                double ai_koc = 0.0;
                double ai_hl = 0.0;
                double ai_fishtox = 0.0;
                String ai_fishtoxtype = "";
                double ai_humtox = 0.0;
                String ai_humtoxtype = "";

                String query = "SELECT * FROM wqm_pesticides WHERE \"PC_CODE\"='" + ip.op_pest_id + "';";
                ResultSet results = statement.executeQuery(query);
                while (results.next()) {
                    ai_name = results.getString("AI_NAME");
                    ai_ph = results.getDouble("PH");
                    ai_hl = results.getDouble("SOIL_HL_RV");
                    ai_koc = results.getDouble("KOC_RV");
                    ai_sol = results.getDouble("SOL_RV");
                    ai_humtox = results.getDouble("HUMAN_TOX_PPB");
                    ai_humtoxtype = results.getString("HUMAN_TOX_TYPE");
                    ai_fishtox = results.getDouble("FISH_TOX_PPB");
                    ai_fishtoxtype = results.getString("FISH_TOX_TYPE");
                }

                //     #Compute pesticide leaching potential fore each operation pesticide
                double log_val = Math.log10(ai_hl) * (4 - Math.log10(ai_koc));
                String ai_plp;
                if (log_val >= 2.8) {
                    if (ip.app_area.equals("Banded")
                            || ip.app_method.equals("Foliar Application")
                            || ip.app_rate.equals("LOW")) {
                        ai_plp = "INTERMEDIATE";
                    } else if (ip.app_area.equals("Spot Treatment")
                            || ip.app_rate.equals("ULTRA LOW")) {
                        ai_plp = "LOW";
                    } else {
                        ai_plp = "HIGH";
                    }
                } else if (log_val < 0.0 || (ai_sol < 1 && ai_hl <= 1)) {
                    ai_plp = "VERY LOW";
                } else if (log_val <= 1.8) {
                    if (ip.app_area.equals("Banded")
                            || ip.app_area.equals("Spot Treatment")
                            || ip.app_method.equals("Foliar Application")
                            || ip.app_rate.equals("LOW")
                            || ip.app_rate.equals("ULTRA LOW")) {
                        ai_plp = "VERY LOW";
                    } else {
                        ai_plp = "LOW";
                    }
                } else {
                    if (ip.app_area.equals("Banded")
                            || ip.app_method.equals("Foliar Application")
                            || ip.app_rate.equals("LOW")) {
                        ai_plp = "LOW";
                    } else if (ip.app_area.equals("Spot Treatment")
                            || ip.app_rate.equals("ULTRA LOW")) {
                        ai_plp = "VERY LOW";
                    } else {
                        ai_plp = "INTERMEDIATE";
                    }
                }

                String ai_psrp;
                //   #Compute pesticide solution runoff potential for each operation pesticide
                if ((ai_sol >= 1 && ai_hl > 35 && ai_koc < 100000)
                        || (ai_sol >= 10 && ai_sol < 100 && ai_koc <= 700)) {
                    if (ip.app_area.equals("Banded")
                            || ip.app_method.equals("Foliar Application")
                            || ip.app_method.equals("Soil Incorporated")
                            || ip.app_rate.equals("LOW")) {
                        ai_psrp = "INTERMEDIATE";
                    } else if (ip.app_area.equals("Spot Treatment")
                            || ip.app_rate.equals("ULTRA LOW")) {
                        ai_psrp = "LOW";
                    } else {
                        ai_psrp = "HIGH";
                    }
                } else if ((ai_koc >= 100000)
                        || (ai_koc >= 1000 && ai_hl <= 1)
                        || (ai_sol < 0.5 && ai_hl < 35)) {
                    if (ip.app_area.equals("Banded")
                            || ip.app_area.equals("Spot Treatment")
                            || ip.app_method.equals("Foliar Application")
                            || ip.app_method.equals("Soil Incorporated")
                            || ip.app_rate.equals("LOW")
                            || ip.app_rate.equals("ULTRA LOW")) {
                        ai_psrp = "VERY LOW";
                    } else {
                        ai_psrp = "LOW";
                    }
                } else {
                    if (ip.app_area.equals("Banded")
                            || ip.app_method.equals("Foliar Application")
                            || ip.app_method.equals("Soil Incorporated")
                            || ip.app_rate.equals("LOW")) {
                        ai_psrp = "LOW";
                    } else if (ip.app_area.equals("Spot Treatment")
                            || ip.app_rate.equals("ULTRA LOW")) {
                        ai_psrp = "VERY LOW";
                    } else {
                        ai_psrp = "INTERMEDIATE";
                    }
                }

                //    #Compute pesticide adsorbed runoff potential for each operation pesticide
                String ai_parp;
                if ((ai_hl >= 40 && ai_koc >= 1000)
                        || (ai_hl >= 40 && ai_koc >= 500 && ai_sol <= 0.5)) {
                    if (ip.app_area.equals("Banded")
                            || ip.app_method.equals("Foliar Application")
                            || ip.app_method.equals("Soil Incorporate")
                            || ip.app_rate.equals("LOW")) {
                        ai_parp = "INTERMEDIATE";
                    } else if (ip.app_area.equals("Spot Treatment")
                            || ip.app_rate.equals("ULTRA LOW")) {
                        ai_parp = "LOW";
                    } else {
                        ai_parp = "HIGH";
                    }
                } else if ((ai_hl <= 1) || (ai_hl <= 2 && ai_koc <= 500)
                        || (ai_hl <= 4 && ai_koc <= 900 && ai_sol >= 0.5)
                        || (ai_hl <= 40 && ai_koc <= 500 && ai_sol >= 0.5)
                        || (ai_hl <= 50 && ai_koc <= 900 && ai_sol >= 2)) {
                    if (ip.app_area.equals("Banded")
                            || ip.app_area.equals("Spot Treatment")
                            || ip.app_method.equals("Foliar Application")
                            || ip.app_method.equals("Soil Incorporated")
                            || ip.app_rate.equals("LOW")
                            || ip.app_rate.equals("ULTRA LOW")) {
                        ai_parp = "VERY LOW";
                    } else {
                        ai_parp = "LOW";
                    }
                } else {
                    if (ip.app_area.equals("Banded")
                            || ip.app_method.equals("Foliar Application")
                            || ip.app_method.equals("Soil Incorporated")
                            || ip.app_rate.equals("LOW")) {
                        ai_parp = "VERY LOW";
                    } else {
                        ai_parp = "INTERMEDIATE";
                    }
                }
                Result1 result = new Result1(ip.AoAId, ip.operation_id,
                        ip.op_pest_id, ip.app_rate, ai_name, ai_ph, ai_hl,
                        ai_koc, ai_sol, ai_humtox, ai_humtoxtype, ai_fishtox,
                        ai_fishtoxtype, ai_plp, ai_psrp, ai_parp,
                        ip.app_area, ip.app_method);
                result1.add(result);
            }
        } catch (SQLException se) {
            LOG.info("Did not open database for WQM-4!");
            LOG.info(se.getMessage());
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
        return EXEC_OK;
    }

    @Override
    //writing the results back to JSON
    protected void postProcess() throws Exception {
        JSONArray result1Arr = new JSONArray();
        for (Result1 rs1 : result1) {
            JSONArray tmpArr = new JSONArray();
            tmpArr.put(JSONUtils.dataDesc("AoAId", rs1.AoAId, "Area of Analysis Identifier"));
            tmpArr.put(JSONUtils.dataDesc("operation_id", rs1.operation_id, "Operation ID"));
            tmpArr.put(JSONUtils.dataDesc("op_pest_id", rs1.op_pest_id, "Pesticide Identifier (PC_CODE)"));
            tmpArr.put(JSONUtils.dataDesc("app_rate", rs1.app_rate, "Pesticide Application Rating"));
            tmpArr.put(JSONUtils.dataDesc("ai_name", rs1.ai_name, "Active Ingredient Name (AI_NAME)"));
            tmpArr.put(JSONUtils.dataDesc("ai_ph", rs1.ai_ph, "Active ingredient pH of associate properties (PH)"));
            tmpArr.put(JSONUtils.dataDesc("ai_hl", rs1.ai_hl, "Active ingredient field half life (HL)"));
            tmpArr.put(JSONUtils.dataDesc("ai_koc", rs1.ai_koc, "Active ingredient soil organic carbon sorption coefficient (KOC)"));
            tmpArr.put(JSONUtils.dataDesc("ai_sol", rs1.ai_sol, "Active ingredient solubility in water (SOL)"));
            tmpArr.put(JSONUtils.dataDesc("ai_humtox", rs1.ai_humtox, "Active ingredient human toxicity value – long term"));
            tmpArr.put(JSONUtils.dataDesc("ai_humtoxtype", rs1.ai_humtoxtype, "Active ingredient human toxicity type"));
            tmpArr.put(JSONUtils.dataDesc("ai_fistox", rs1.ai_fishtox, "Active ingredient maximum acceptable toxicant concentration-fish"));
            tmpArr.put(JSONUtils.dataDesc("ai_fishtoxtype", rs1.ai_fishtoxtype, "Active ingredient fish toxicity type"));
            tmpArr.put(JSONUtils.dataDesc("ai_plp", rs1.ai_plp, "Active ingredient pesticide leaching potential"));
            tmpArr.put(JSONUtils.dataDesc("ai_psrp", rs1.ai_psrp, "Active ingredient pesticide soil leaching potential"));
            tmpArr.put(JSONUtils.dataDesc("ai_parp", rs1.ai_parp, "Active ingredient pesticide adsorbed runoff potential"));
            tmpArr.put(JSONUtils.dataDesc("op_app_area", rs1.app_area, "Pesticide application operation area"));
            tmpArr.put(JSONUtils.dataDesc("op_app_method", rs1.app_method, "Pesticide application operation method"));
            result1Arr.put(JSONUtils.dataDesc("pesticide summary", tmpArr, "Pest"));
        }
        putResult("operation", result1Arr);
    }
}