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

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

/**
 * WQM-13: WQM Concern Treatment Level Threshold Scores
 *
 * @author Srinivas Reddy Kontham
 * @author Shaun Case
 * @author Rumpal Sidhu
 *
 * @version 1.0
 */
@Name("WQM-13: WQM Concern Treatment Level Threshold Scores (WQMThresholdScores)")
@Description("This service computes treatment level threshold scores for each "
        + "of the WQM concerns for an area of analysis. The service uses nutrient "
        + "soil leaching potentials from WQM-5, sediment and nutrient soil runoff "
        + "potentials from WQM-6, hazard ratings from pesticide-related WQM "
        + "concerns from WQM-11, required treatment level from WQM-1, and the "
        + "AoA climate R factor from WQM-12 to calculate the threshold scores.")
@Path("m/thresholdscores/1.0")
@Resource(from = DBResources.class)

public class V1_0 extends ModelDataService {

    private int aoaId;
    private Threshold threshold;

    @Override
    protected void preProcess() throws JSONException, ServiceException {
        aoaId = parameter().getInt("AoAId", 0);
        String nslp = parameter().getString("aoa_nslp", "err");
        String srp = parameter().getString("aoa_srp", "err");
        String leachHuman = parameter().getString("aoa_phr_leach_human", "err");
        String leachMatcfish = parameter().getString("aoa_phr_leach_matcfish", "err");
        String sorunHuman = parameter().getString("aoa_phr_sorun_human", "err");
        String sorunMatcfish = parameter().getString("aoa_phr_sorun_matcfish", "err");
        String adrunHuman = parameter().getString("aoa_phr_adrun_human", "err");
        String adrunStvfish = parameter().getString("aoa_phr_adrun_stvfish", "err");
        String treatmentLevel = parameter().getString("aoa_treatment_level", "err");
        int rfactor = parameter().getInt("aoa_rfactor", -1);
        threshold = new Threshold(nslp, srp, leachHuman, leachMatcfish, sorunHuman, sorunMatcfish, adrunHuman, adrunStvfish, treatmentLevel, rfactor);
    }

    @Override
    protected void doProcess() throws ServiceException, SQLException {
        try (Connection connection = resources().getJDBC(WQM_ID);) {
            threshold.computeThresholdScores(connection);
        }
    }

    @Override
    protected void postProcess() throws JSONException {
        results().put("AoAId", aoaId, "Area of Analysis Identifier");
        results().put("aoa_nleach_threshold", threshold.nleachThreshold, "Nitrogen Leaching Threshold Score");
        results().put("aoa_nrun_threshold", threshold.nrunThreshold, "Nitrogen Runoff Threshold Score");
        results().put("aoa_sedrun_threshold", threshold.sedrunThreshold, "Sediment Runoff Threshold Score");
        results().put("aoa_prun_threshold", threshold.prunThreshold, "Phosphorus Runoff Threshold Score");
        results().put("aoa_pleach_human_threshold", threshold.pleachHumanThreshold, "Pesticide Leaching Threshold Score, Human");
        results().put("aoa_pleach_matcfish_threshold", threshold.pleachMatcfishThreshold, "Pesticide Leaching Threshold Score, Fish");
        results().put("aoa_psorun_human_threshold", threshold.psorunHumanThreshold, "Pesticide Solution Runoff Threshold Score, Human");
        results().put("aoa_psorun_matcfish_threshold", threshold.psorunMatcfishThreshold, "Pesticide Solution Runoff Threshold Score, Fish");
        results().put("aoa_padrun_human_threshold", threshold.padrunHumanThreshold, "Pesticide Adsorbed Runoff Threshold Score, Human");
        results().put("aoa_padrun_stvfish_threshold", threshold.padrunStvfishThreshold, "Pesticide Adsorbed Runoff Threshold Score, Fish");
        results().put("aoa_pdrift_human_threshold", threshold.pdriftHumanThreshold, "Pesticide Drift Threshold Score, Human");
        results().put("aoa_pdrift_fish_threshold", threshold.pdriftFishThreshold, "Pesticide Drift Threshold Score, Fish");
    }

    static class Threshold {

        protected String nslp, srp, leachHuman, leachMatcfish, sorunHuman, sorunMatcfish, adrunHuman, adrunStvfish, treatmentLevel;
        protected int rfactor;

        protected int nleachThreshold, nrunThreshold, sedrunThreshold, prunThreshold, pleachHumanThreshold, pleachMatcfishThreshold,
                psorunHumanThreshold, psorunMatcfishThreshold, padrunHumanThreshold, padrunStvfishThreshold, pdriftHumanThreshold, pdriftFishThreshold;

        public Threshold(String nslp, String srp, String leachHuman, String leachMatcfish, String sorunHuman, String sorunMatcfish,
                String adrunHuman, String adrunStvfish, String treatmentLevel, int rfactor) {
            this.nslp = nslp;
            this.srp = srp;
            this.leachHuman = leachHuman;
            this.leachMatcfish = leachMatcfish;
            this.sorunHuman = sorunHuman;
            this.sorunMatcfish = sorunMatcfish;
            this.adrunHuman = adrunHuman;
            this.adrunStvfish = adrunStvfish;
            this.treatmentLevel = treatmentLevel;
            this.rfactor = rfactor;
        }

        public void computeThresholdScores(Connection conn) throws SQLException {
            nleachThreshold = computeThresholdScore(conn, "Nitrogen in Ground Water", nslp, -1, treatmentLevel);
            nrunThreshold = computeThresholdScore(conn, "Nitrogen in Surface Water", srp, rfactor, treatmentLevel);
            sedrunThreshold = computeThresholdScore(conn, "Sediment in Surface Water", srp, rfactor, treatmentLevel);
            prunThreshold = computeThresholdScore(conn, "Phosphorus in Surface Water", srp, rfactor, treatmentLevel);
            pleachHumanThreshold = computeThresholdScore(conn, "Pesticide (All)", leachHuman, -1, treatmentLevel);
            pleachMatcfishThreshold = computeThresholdScore(conn, "Pesticide (All)", leachMatcfish, -1, treatmentLevel);
            psorunHumanThreshold = computeThresholdScore(conn, "Pesticide (All)", sorunHuman, -1, treatmentLevel);
            psorunMatcfishThreshold = computeThresholdScore(conn, "Pesticide (All)", sorunMatcfish, -1, treatmentLevel);
            padrunHumanThreshold = computeThresholdScore(conn, "Pesticide (All)", adrunHuman, -1, treatmentLevel);
            padrunStvfishThreshold = computeThresholdScore(conn, "Pesticide (All)", adrunStvfish, -1, treatmentLevel);
            pdriftHumanThreshold = pdriftFishThreshold = computeThresholdScore(conn, "Pesticide (All)", "INTERMEDIATE", -1, treatmentLevel);
        }

        public int computeThresholdScore(Connection conn, String concern, String hazardLossRating, int rfactor, String treatmentLevel) throws SQLException {
            int threshold = 0;
            try (Statement statement = conn.createStatement();) {
                String query = DBQueries.WQM13Query01(concern, hazardLossRating, rfactor, treatmentLevel);
                try (ResultSet inputSet = statement.executeQuery(query);) {
                    while (inputSet.next()) {
                        threshold += inputSet.getInt("threshold_treatment_score");
                    }
                }
            }
            return threshold;
        }

    }
}