V1_0.java [src/java/m/wqm/pesthazrating] Revision: d4e5c230d855e631706b46dd08625538a324d934  Date: Thu Aug 13 16:01:47 MDT 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 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.util.Map;
import java.sql.*;
import java.util.concurrent.*;

/**
 *
 * @ Srinivas
 */
@Name("(PestHazRating)")
@Description("Pesticide Hazard Ratings for an Area of Analysis")
@Path("m/pest_hazrating/1.0")

public class V1_0 extends ModelDataService{
    
  
    ArrayList<Input> components=new ArrayList<>(); // store the set of all input soilcomponents as objects
    ArrayList<Result1> result1=new ArrayList<>();  // store the result as objects
    String aoa_phr_leach_human="";
    String aoa_phr_leach_matcfish="";
    String aoa_phr_sorun_human="";
    String aoa_phr_sorun_matcfish="";
    String aoa_phr_adrun_human="";
    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 Exception {
        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);
                
                Input input=new Input(operation_id,op_pest_id,op_pest_ilp,op_pest_isrp,op_pest_iarp,ai_eathuman,ai_eatmatc,ai_koc);
        components.add(input);
            }
        }

    @Override
        protected String process() throws Exception
        {
            String eat_rating_human=null;
            
            for(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";
                }
                Connection conn = null;
                Statement statement = null;
                Class.forName("org.postgresql.Driver");
                conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/wqm", "postgres", "admin");
                conn.setAutoCommit(false);
                statement = conn.createStatement();
                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+"'";
    //            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_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 res1=new 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);
                result1.add(res1);
            }   
        calAoANutSLP(result1);    
        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("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");   
    }               
    // calculate the aoa_nslp 
    void calAoANutSLP(ArrayList<Result1> source)
    {
        int phr_leach_high = 1;
        int phr_leach_new = 1;
 //       int phr_sorun_high = 1;
 //       int phr_sorun_new = 1;
 //       int phr_adrun_high = 1;
 //       int phr_adrun_new = 1;
        
      //  #Compute pesticide hazard rating for Pesticide Leaching – Human concern
        
        for(Result1 rs1:source)
        {
            if(rs1.op_phr_leach_human.equals("EXTRA HIGH"))
            {
                phr_leach_new=5;
            }
            else if(rs1.op_phr_leach_human.equals("HIGH"))
            {
                phr_leach_new=4;
            }
            else if(rs1.op_phr_leach_human.equals("INTERMEDIATE"))
            {
                phr_leach_new=3;
            }
            else if(rs1.op_phr_leach_human.equals("LOW"))
            {
                phr_leach_new=2;
            }
            else if(rs1.op_phr_leach_human.equals("VERY LOW"))
            {
                phr_leach_new=1;
            }
            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(Result1 rs1:source)
        {
            if(rs1.op_phr_leach_matcfish.equals("EXTRA HIGH"))
            {
                phr_leach_new=5;
            }
            else if(rs1.op_phr_leach_matcfish.equals("HIGH"))
            {
                phr_leach_new=4;
            }
            else if(rs1.op_phr_leach_matcfish.equals("INTERMEDIATE"))
            {
                phr_leach_new=3;
            }
            else if(rs1.op_phr_leach_matcfish.equals("LOW"))
            {
                phr_leach_new=2;
            }
            else if(rs1.op_phr_leach_matcfish.equals("VERY LOW"))
            {
                phr_leach_new=1;
            }
            if(phr_leach_new>phr_leach_high)
            {
                phr_leach_high=phr_leach_new;
            }
        }
   //     String aoa_phr_leach_matcfish_array[]={"","VERY LOW","LOW","INTERMEDIATE","HIGH"};
        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(Result1 rs1:source)
        {
            if(rs1.op_phr_sorun_human.equals("EXTRA HIGH"))
            {
                phr_leach_new=4;
            }
            else if(rs1.op_phr_sorun_human.equals("HIGH"))
            {
                phr_leach_new=3;
            }
            else if(rs1.op_phr_sorun_human.equals("INTERMEDIATE"))
            {
                phr_leach_new=2;
            }
            else if(rs1.op_phr_sorun_human.equals("LOW"))
            {
                phr_leach_new=1;
            }
            
            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(Result1 rs1:source)
        {
            if(rs1.op_phr_sorun_matcfish.equals("EXTRA HIGH"))
            {
                phr_leach_new=4;
            }
            else if(rs1.op_phr_sorun_matcfish.equals("HIGH"))
            {
                phr_leach_new=3;
            }
            else if(rs1.op_phr_sorun_matcfish.equals("INTERMEDIATE"))
            {
                phr_leach_new=2;
            }
            else if(rs1.op_phr_sorun_matcfish.equals("LOW"))
            {
                phr_leach_new=1;
            }
            
            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(Result1 rs1:source)
        {
            if(rs1.op_phr_adrun_human.equals("EXTRA HIGH"))
            {
                phr_leach_new=4;
            }
            else if(rs1.op_phr_adrun_human.equals("HIGH"))
            {
                phr_leach_new=3;
            }
            else if(rs1.op_phr_adrun_human.equals("INTERMEDIATE"))
            {
                phr_leach_new=2;
            }
            else if(rs1.op_phr_adrun_human.equals("LOW"))
            {
                phr_leach_new=1;
            }
            
            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(Result1 rs1:source)
        {
            if(rs1.op_phr_adrun_stvfish.equals("EXTRA HIGH"))
            {
                phr_leach_new=4;
            }
            else if(rs1.op_phr_adrun_stvfish.equals("HIGH"))
            {
                phr_leach_new=3;
            }
            else if(rs1.op_phr_adrun_stvfish.equals("INTERMEDIATE"))
            {
                phr_leach_new=2;
            }
            else if(rs1.op_phr_adrun_stvfish.equals("LOW"))
            {
                phr_leach_new=1;
            }
            
            if(phr_leach_new>phr_leach_high)
            {
                phr_leach_high=phr_leach_new;
            }
        }
 //       String aoa_phr_adrun_stvfish_array[]={"","LOW","INTERMEDIATE","HIGH"};
        aoa_phr_adrun_stvfish=aoa_phr_array[phr_leach_high];
        
    }
}