V1_0.java [src/java/m/wqm/wqm17_pestipmscores] Revision:   Date:
/*
 * 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.wqm17_pestipmscores;

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

/**
 * WQM-17: Integrated Pest Management Mitigation Scores
 *
 * @author dhawal
 * @author Rumpal Sidhu
 *
 * @version 1.0
 */
@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.")
@Path("m/pesticide_ipm_score/1.0")
@Resource(from = DBResources.class)
public class V1_0 extends ModelDataService {

    private int aoaId;
    private String plan_ipm_level;
    private int pleach_ipm_score;
    private int psolsurf_ipm_score;
    private int padsurf_ipm_score;
    private int pdrift_ipm_score;

    @Override
    protected void preProcess() throws ServiceException, JSONException {
        aoaId = parameter().getInt("AoAId", 0);
        plan_ipm_level = parameter().getString("plan_ipm_level", "err");
    }

    @Override
    protected void doProcess() throws SQLException, ServiceException {
        try (Connection conn = resources().getJDBC(WQM_ID);) {
            compute(conn, plan_ipm_level);
        }
    }

    @Override
    protected void postProcess() throws JSONException {

        results().put("AoAId", aoaId, "Area of Analysis Identifier");
        results().put("pleach_ipm_score", pleach_ipm_score, "pleach_ipm_score");
        results().put("psolsurf_ipm_score", psolsurf_ipm_score, "psolsurf_ipm_score");
        results().put("padsurf_ipm_score", padsurf_ipm_score, "padsurf_ipm_score");
        results().put("pdrift_ipm_score", pdrift_ipm_score, "pdrift_ipm_score");

    }

    public void compute(Connection connection, String ipmLevel) throws SQLException {
        pleach_ipm_score = computeMitigationScore(connection, ipmLevel, "Pesticide Leaching");
        psolsurf_ipm_score = computeMitigationScore(connection, ipmLevel, "Pesticide Solution Runoff");
        padsurf_ipm_score = computeMitigationScore(connection, ipmLevel, "Pesticide Adsorbed Runoff");
        pdrift_ipm_score = computeMitigationScore(connection, ipmLevel, "Pesticide Drift");
    }

    public int computeMitigationScore(Connection connection, String ipmLevel, String concern) throws SQLException {
        int mitigationScore = 0;
        try (Statement statement = connection.createStatement();
                ResultSet resultSet = statement.executeQuery(DBQueries.WQM17Query01(ipmLevel, concern));) {
            while (resultSet.next()) {
                mitigationScore = resultSet.getInt("ipm_mitigation_score");
            }
        }
        return mitigationScore;
    }
}