V1_0.java [src/java/m/wqm/wqm18_pesttechnscores] Revision:   Date:
package m.wqm.wqm18_pesttechnscores;

import csip.ModelDataService;
import csip.api.server.ServiceException;
import csip.annotations.Resource;
import java.util.ArrayList;
import javax.ws.rs.Path;
import csip.annotations.Description;
import csip.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import csip.utils.JSONUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import org.codehaus.jettison.json.JSONException;
import wqm.utils.DBQueries;
import wqm.utils.DBResources;
import static wqm.utils.DBResources.WQM_ID;

/**
 * WQM-18: Pesticide Mitigation Technique Scores
 *
 * @author SrinivasReddy kontham
 * @author Rumpal Sidhu
 *
 * @version 1.0
 */
@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")
@Resource(from = DBResources.class)

public class V1_0 extends ModelDataService {

    private ArrayList<Integer> planIpmTechniqueList = new ArrayList<>();
    private int aoaId, pleachTechniqueScore, psolsurfTechniqueScore, padsurfTechniqueScore, pdriftTechniqueScore;

    @Override
    protected void preProcess() throws ServiceException, JSONException {
        aoaId = parameter().getInt("AoAId", 0);
        JSONArray groups = parameter().getJSONArray("technique_list");
        for (int i = 0; i < groups.length(); i++) {
            Map<String, JSONObject> group = JSONUtils.preprocess(groups.getJSONArray(i));
            planIpmTechniqueList.add(JSONUtils.getIntParam(group, "plan_ipm_technique", 0));
        }
    }

    @Override
    protected void doProcess() throws SQLException, ServiceException {
        try (Connection connection = resources().getJDBC(WQM_ID);) {
            for (int technique : planIpmTechniqueList) {
                computeTechniqueScores(connection, technique);
            }
        }
    }

    @Override
    protected void postProcess() throws JSONException {
        results().put("AoAId", aoaId, "Area of Analysis Identifier");
        results().put("pleach_technique_score", pleachTechniqueScore, "Integrated Pest Management Mitigation Technique Score for Pesticide Leaching");
        results().put("psolsurf_technique_score", psolsurfTechniqueScore, "Integrated Pest Management Mitigation Technique Score for Pesticide Solution Runoff");
        results().put("padsurf_technique_score", padsurfTechniqueScore, "Integrated Pest Management Mitigation Technique Score for Pesticide Adsorbed Runoff");
        results().put("pdrift_technique_score", pdriftTechniqueScore, "Integrated Pest Management Mitigation Technique Score for Pesticide Drift");
    }

    private void computeTechniqueScores(Connection connection, int technique) throws SQLException {
        pleachTechniqueScore += computeTechniqueScore(connection, technique, "Pesticide Leaching");
        psolsurfTechniqueScore += computeTechniqueScore(connection, technique, "Pesticide Solution Runoff");
        padsurfTechniqueScore += computeTechniqueScore(connection, technique, "Pesticide Adsorbed Runoff");
        pdriftTechniqueScore += computeTechniqueScore(connection, technique, "Pesticide Drift");
    }

    private int computeTechniqueScore(Connection connection, int technique, String concern) throws SQLException {
        int score = 0;
        try (Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery(DBQueries.WQM18Query01(technique, concern));) {
            while (resultSet.next()) {
                score = resultSet.getInt("ipm_technique_score");
            }
        }
        return score;
    }
}