V1_0.java [src/java/m/wqm/pesttechnscores] Revision: 01ac35d4e4cdc146c443fdfda7c11afae9c09612  Date: Tue Aug 11 15:42:56 MDT 2015
package m.wqm.pesttechnscores;

/**
 *
 * @author SrinivasReddy kontham
 */
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.*;

@Name("(pesttechnscores)")
@Description("Pesticide Mitigation Technique Scores")
@Path("m/pest_techn_scores/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
            int pleach_technique_score;
            int psolsurf_technique_score;
            int padsurf_technique_score;
            int pdrift_technique_score;
            int AoAid;
    @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("components");
            for(int i=0;i<groups.length();i++)
            {
                Map<String, JSONObject> group = JSONUtils.preprocess(groups.getJSONArray(i));

                
                AoAid = JSONUtils.getIntParam(group, "AoAId", 0);
                int plan_ipm_technique=JSONUtils.getIntParam(group, "plan_ipm_technique", 0);
                Input input=new Input(AoAid,plan_ipm_technique);
                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_technique_score FROM wqm_ipm_technique_scores WHERE ipm_technique_id="+ip.plan_ipm_technique+"AND wqm_concern="+"'"+"Pesticide Leaching"+"'";
                ResultSet results = statement.executeQuery(query);
       // #Compute IPM technique mitigation score for Pesticide Leaching and increment total score
                while (results.next())
                {
                   int pl_techn_score=results.getInt("ipm_technique_score");
                   pleach_technique_score=pleach_technique_score+pl_techn_score;
                }
                query="SELECT ipm_technique_score FROM wqm_ipm_technique_scores WHERE ipm_technique_id="+ip.plan_ipm_technique+"AND wqm_concern="+"'"+"Pesticide Solution Runoff"+"'";
                results = statement.executeQuery(query);
       // #Compute IPM technique mitigation score for Pesticide Solution Runoff and increment total score
                while (results.next())
                {
                   int ps_techn_score=results.getInt("ipm_technique_score");
                   psolsurf_technique_score=psolsurf_technique_score+ps_techn_score;
                }
                query="SELECT ipm_technique_score FROM wqm_ipm_technique_scores WHERE ipm_technique_id="+ip.plan_ipm_technique+"AND wqm_concern="+"'"+"Pesticide Adsorbed Runoff"+"'";
                results = statement.executeQuery(query);
       // #Compute IPM technique mitigation score for Pesticide Absorbed Runoff and increment total score
                while (results.next())
                {
                   int pa_techn_score=results.getInt("ipm_technique_score");
                   padsurf_technique_score=padsurf_technique_score+pa_techn_score;
                }
                query="SELECT ipm_technique_score FROM wqm_ipm_technique_scores WHERE ipm_technique_id="+ip.plan_ipm_technique+"AND wqm_concern="+"'"+"Pesticide Drift"+"'";
                results = statement.executeQuery(query);
       // #Compute IPM technique mitigation score for Pesticide Drift and increment total score
                while (results.next())
                {
                   int pd_techn_score=results.getInt("ipm_technique_score");
                   pdrift_technique_score=pdrift_technique_score+pd_techn_score;
                }
               
            }
            Result1 result=new Result1(AoAid,pleach_technique_score,psolsurf_technique_score,padsurf_technique_score,pdrift_technique_score);
            result1.add(result);
            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("pleach_technique_score", rs1.pleach_technique_score, "IPM Mitigation Technique Score for Pesticide Leaching"));
                    tmpArr.put(JSONUtils.dataDesc("psolsurf_technique_score", rs1.psolsurf_technique_score, "IPM Mitigation Technique Score for Pesticide Solution Runoff"));
                    tmpArr.put(JSONUtils.dataDesc("padsurf_technique_score", rs1.padsurf_technique_score, "IPM Mitigation Technique Score for Pesticide Adsorbed Runoff"));
                    tmpArr.put(JSONUtils.dataDesc("pdrift_technique_score", rs1.pdrift_technique_score, "IPM Mitigation Technique Score for Pesticide Drift"));
                    result1Arr.put(JSONUtils.dataDesc("Pesticide Mitigation Technique Scores", tmpArr, "(PestTechnScores)"));
                }
                
                putResult("operation", result1Arr);    
    }
    

}