V1_0.java [src/java/m/wqm/pesthazrating] Revision: 7f420495ecf5b257d80c8c039b7e2701f23b0628  Date: Sat Nov 14 12:01:40 MST 2015
/*
 * 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.pesthazrating;
import csip.ModelDataService;
import static csip.ModelDataService.EXEC_OK;
import csip.ServiceException;
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 javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @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")

public class V1_0 extends ModelDataService{
    
  
    private ArrayList<V1_0.Input> components=new ArrayList<>(); // store the set of all input soilcomponents as objects
    private ArrayList<V1_0.Result1> result1=new ArrayList<>();  // store the result as objects
    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;
    
    private String error_msg;
    
    
    @Override
        // reading the inputs from the json file into input object and placing it in the arraylist
        protected void preProcess() throws Exception {
            this.aoa_phr_leach_human="";
            this.aoa_phr_leach_matcfish="";
            this.aoa_phr_sorun_human="";
            this.aoa_phr_sorun_matcfish="";
            this.aoa_phr_adrun_human="";
            this.aoa_phr_adrun_stvfish="";            
            this.error_msg = "";
            
            this.components=new ArrayList<>(); 
            this.result1=new ArrayList<>();  
            
            try{
                JSONArray groups = getJSONArrayParam("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 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 ){
                this.error_msg = "Cannot read input JSON: " + ex.getMessage();
                LOG.warning( this.error_msg );
            }            
        }
        

    @Override
        protected String process() 
        {
            String eat_rating_human=null;
            if ( this.error_msg.isEmpty() ){            
                try(Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
                    Statement statement = conn.createStatement();){
                    
                    for(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_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(query);
                        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_pesticide_hazard_potential WHERE wqm_ilr="+op_pest_isrp_temp+"AND wqm_eat="+ eat_rating_human_temp; 
                        results = statement.executeQuery(query);
                        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_pesticide_hazard_potential WHERE wqm_ilr="+op_pest_iarp_temp+"AND wqm_eat="+ eat_rating_human_temp;
                        results = statement.executeQuery(query);
                        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_pesticide_hazard_potential WHERE wqm_ilr="+op_pest_iarp_temp+"AND wqm_eat="+ eat_rating_stvfish_temp;
                        results = statement.executeQuery(query);
                        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_pesticide_hazard_potential WHERE wqm_ilr="+op_pest_ilp_temp+"AND wqm_eat="+ eat_rating_matcfish_temp;
                        results = statement.executeQuery(query);
                        String op_phr_leach_matcfish="";
                        while(results.next()){
                            op_phr_leach_matcfish=results.getString("wqm_phr");
                        }             
                        query="SELECT wqm_phr FROM wqm_pesticide_hazard_potential WHERE wqm_ilr="+op_pest_isrp_temp+"AND wqm_eat="+ eat_rating_matcfish_temp;
                        results = statement.executeQuery(query);
                        String op_phr_sorun_matcfish="";
                        while(results.next()){
                            op_phr_sorun_matcfish=results.getString("wqm_phr");
                        }
                        result1.add(new 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 ){
                    this.error_msg = "Cannot process your request: " + ex.getMessage();
                    LOG.warning( this.error_msg );
                }
                

            }
        
        return ( this.error_msg.isEmpty()? EXEC_OK : this.error_msg );       
    }
    @Override
    //writing the results back to JSON
    protected void postProcess() throws Exception 
    {
        if ( this.error_msg.isEmpty() ){
            try{
                JSONArray result1Arr = new JSONArray();
                for(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"));
                }

                putResult("operation", result1Arr);
                putResult("aoa_phr_leach_human",aoa_phr_leach_human,"Pesticide Leaching Hazard Rating for Humans in the Area of Analysis");
                putResult("aoa_phr_leach_matcfish",aoa_phr_leach_matcfish,"Pesticide Leaching Hazard Rating for Fish in the Area of Analysis");
                putResult("aoa_phr_sorun_human",aoa_phr_sorun_human,"Pesticide Solution Runoff Hazard Rating for Humans in the Area of Analysis");
                putResult("aoa_phr_sorun_matcfish",aoa_phr_sorun_matcfish,"Pesticide Solution Runoff Hazard Rating for Fish in the Area of Analysis");
                putResult("aoa_phr_adrun_human",aoa_phr_adrun_human,"Pesticide Adsorbed Runoff Hazard Rating for Humans in the Area of Analysis");
                putResult("aoa_phr_adrun_stvfish",aoa_phr_adrun_stvfish,"Pesticide Adsorbed Runoff Hazard Rating for Fish in the Area of Analysis");   
            }
            catch (JSONException ex )
            {
                this.error_msg = "Cannot create output JSON: " + ex.getMessage();
                LOG.warning( this.error_msg );
            }
        }
    }               
    
    // calculate the aoa_nslp 
    void calAoANutSLP(ArrayList<V1_0.Result1> source){
        int phr_leach_high = 1;
        int phr_leach_new = 1;
      //  #Compute pesticide hazard rating for Pesticide Leaching – Human concern
        
        for(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(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(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(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(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(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];
        
    }
    public 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;
        
        }
    
    }
    public 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;
        }
    }

}