V1_0.java [src/java/m/wqm/pesttechnscores] Revision: 7d708336c092b35776fa7872f8aa87797c5e676b  Date: Sat Nov 14 12:37:03 MST 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("WQM-18: Pesticide Mitigation Technique Scores (PestTechnScores)")
@Description("This services computes scores for applying pesticide management techniques to mitigate hazard potentials for leaching, solution runoff, adsorbed runoff, and drift.")
@Path("m/pest_techn_scores/1.0")

public class V1_0 extends ModelDataService {

    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
    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);
            components.add(new V1_0.Input(AoAid, plan_ipm_technique));
        }
    }

    @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_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.add(new V1_0.Result1(AoAid, pleach_technique_score, psolsurf_technique_score, padsurf_technique_score, pdrift_technique_score));
        } catch (SQLException se) {
            LOG.info("Did not open database for WQM-18!");
            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_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);
    }

    public class Input {

        int AoAId;
        int plan_ipm_technique;

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

    public class Result1 {

        int AoAId;
        int pleach_technique_score;
        int psolsurf_technique_score;
        int padsurf_technique_score;
        int pdrift_technique_score;

        public Result1(int AoAId, int pleach_technique_score, int psolsurf_technique_score, int padsurf_technique_score, int pdrift_technique_score) {
            this.AoAId = AoAId;
            this.pleach_technique_score = pleach_technique_score;
            this.psolsurf_technique_score = psolsurf_technique_score;
            this.padsurf_technique_score = padsurf_technique_score;
            this.pdrift_technique_score = pdrift_technique_score;
        }
    }
}