V1_0.java [src/java/m/wqm/pestipmscores] Revision: d687882da0fbc1e482baaf5307c087490a806b42  Date: Sat Nov 14 12:15:09 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.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("WQM-17: Integrated Pest Management Mitigation Scores (PestIPMScores)")
@Description("This service computes scores for the level of integrated pest management (IPM) to be applied to mitigate pesticide leaching, solution runoff, adsorbed runoff, and drift hazard potential.")
//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<V1_0.Input> components = new ArrayList<>(); // store the set of all input soilcomponents as objects
    ArrayList<V1_0.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");
            components.add(new V1_0.Input(AoAId, plan_ipm_level));
        }
    }

    @Override
    protected String process() throws Exception {
        try (
                Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
                Statement statement = conn.createStatement();) {
            conn.setAutoCommit(false);
            for (V1_0.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");
                }
                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");
                }
                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");
                }
                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");
                }
                result1.add(new Result1(ip.AoAId, pleach_ipm_score, psolsurf_ipm_score, psolsurf_ipm_score, pdrift_ipm_score));
            }
        } catch (SQLException se) {
            LOG.info("Did not open database for WQM-17!");
            LOG.info(se.getMessage());
        } 
        
        return EXEC_OK;
    }

    @Override
    //writing the results back to JSON
    protected void postProcess() throws Exception {
        JSONArray result1Arr = new JSONArray();
        for (V1_0.Result1 rs1 : result1) {
            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);
    }

    public class Input {

        int AoAId;
        String plan_ipm_level;

        public Input(int AoAId, String plan_ipm_level) {
            this.AoAId = AoAId;
            this.plan_ipm_level = plan_ipm_level;
        }
    }

    public class Result1 {

        int AoAId;
        int pleach_ipm_score;
        int psolsurf_ipm_score;
        int padsurf_ipm_score;
        int pdrift_ipm_score;

        public Result1(int AoAId, int pleach_ipm_score, int psolsurf_ipm_score, int padsurf_ipm_score, int pdrift_ipm_score) {
            this.AoAId = AoAId;
            this.pleach_ipm_score = pleach_ipm_score;
            this.psolsurf_ipm_score = psolsurf_ipm_score;
            this.padsurf_ipm_score = padsurf_ipm_score;
            this.pdrift_ipm_score = pdrift_ipm_score;
        }
    }

}