V1_0.java [src/java/m/wqm/pestipmscores] Revision: 369b033f437331d0780ff9df344cfa3cea5f3999  Date: Tue Aug 11 10:50:11 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.pestipmscores;
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.*;

/**
 *
 * @author dhawal
 */
@Name("Integrated Pest Management Mitigation Scores (PestIPMScores)")
@Description("Getting mitigation score for pest management")
//change
@Path("m/pesticide_ipm_score/1.0")

public class V1_0 extends ModelDataService
{
    
    int AoAId=0;
    int pleach_ipm_score=0;
    int psolsurf_ipm_score=0;
    int padsurf_ipm_score=0;
    int pdrift_ipm_score=0;
    
    //JSONArray getArray
    ArrayList<Input> components=new ArrayList<>(); // store the set of all input soilcomponents as objects
    ArrayList<Result1> result1=new ArrayList<>();  // 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 {
        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);
                String plan_ipm_level=JSONUtils.getStringParam(group,"plan_ipm_level","err");

                Input input=new Input(AoAId,plan_ipm_level);
                System.out.println("Identifier"+AoAId);
        components.add(input);
            }
        }

    @Override
        protected String process() throws Exception
        {
            Connection conn = null;
            Statement statement = null;
            Class.forName("org.postgresql.Driver");
            conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "admin");
            conn.setAutoCommit(false);
            statement = conn.createStatement();
            
        for (Input ip : components) {
             
             String query="SELECT ipm_mitigation_score FROM wqm_ipm_scores WHERE ipm_level='"+ip.plan_ipm_level+"' AND wqm_concern='Pesticide Leaching'" ;  
             ResultSet results = statement.executeQuery(query);

                while (results.next())
                {
                   pleach_ipm_score=results.getInt("ipm_mitigation_score");
                   System.out.println("pleach_ipm_score"+pleach_ipm_score);
                }
             query="SELECT ipm_mitigation_score FROM wqm_ipm_scores WHERE ipm_level='"+ip.plan_ipm_level+"' AND wqm_concern='Pesticide Solution Runoff'" ;
                results = statement.executeQuery(query);

                while (results.next())
                {
                   psolsurf_ipm_score=results.getInt("ipm_mitigation_score");
                   System.out.println("psolsurf_ipm_score"+psolsurf_ipm_score);
                }
             query="SELECT ipm_mitigation_score FROM wqm_ipm_scores WHERE ipm_level='"+ip.plan_ipm_level+"' AND wqm_concern='Pesticide Adsorbed Runoff'" ;
                results = statement.executeQuery(query);

                while (results.next())
                {
                   padsurf_ipm_score=results.getInt("ipm_mitigation_score");
                   System.out.println("padsurf_ipm_score"+padsurf_ipm_score);
                }
            query="SELECT ipm_mitigation_score FROM wqm_ipm_scores WHERE ipm_level='"+ip.plan_ipm_level+"' AND wqm_concern='Pesticide Drift'" ;
                results = statement.executeQuery(query);

                while (results.next())
                {
                   pdrift_ipm_score=results.getInt("ipm_mitigation_score");
                   System.out.println("pdrift_ipm_score"+pdrift_ipm_score);
                }  
            Result1 result=new Result1(ip.AoAId,pleach_ipm_score,psolsurf_ipm_score,psolsurf_ipm_score,pdrift_ipm_score);
            result1.add(result);
        }            

            System.out.println("Final result"+(result1.size()));
            return EXEC_OK;       
    }
        @Override
        //writing the results back to JSON
    protected void postProcess() throws Exception 
    {      
                JSONArray result1Arr = new JSONArray();
                 for(m.wqm.pestipmscores.Result1 rs1:result1)
                {    System.out.println("Inside for");
                    JSONArray tmpArr = new JSONArray();
                    tmpArr.put(JSONUtils.dataDesc("AoAId", rs1.AoAId, "Area of Analysis Identifier"));
                    tmpArr.put(JSONUtils.dataDesc("pleach_ipm_score",rs1.pleach_ipm_score,"pleach_ipm_score"));
                    tmpArr.put(JSONUtils.dataDesc("psolsurf_ipm_score",rs1.psolsurf_ipm_score,"psolsurf_ipm_score"));
                    tmpArr.put(JSONUtils.dataDesc("padsurf_ipm_score",rs1.padsurf_ipm_score,"padsurf_ipm_score"));
                    tmpArr.put(JSONUtils.dataDesc("pdrift_ipm_score",rs1.pdrift_ipm_score,"pdrift_ipm_score"));
                    result1Arr.put(JSONUtils.dataDesc("Pest Management Mitigation Scores", tmpArr, "PestIPMscores"));
                }
                
                putResult("operation", result1Arr);    
    }
    
}