V1_0.java [src/java/m/wqm/wqm14_nuttechscores] Revision: default Date:
package m.wqm.wqm14_nuttechscores;
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.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
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-14: Nutrient Technique Scores
*
* @author SrinivasReddy kontham
* @author Rumpal Sidhu
* @author Sandeep Kasavaraju
*
* @version 1.0
*/
@Name("WQM-14: Nutrient Technique Scores (NutTechScores)")
@Description("This service computes scores for techniques applied to "
+ "mitigate nitrogen leaching, nitrogen runoff, and phosphorus "
+ "runoff loss potential")
@Path("m/nut_tech_scores/1.0")
@Resource(from = DBResources.class)
public class V1_0 extends ModelDataService {
private int aoaId;
private ArrayList<Technique> techniqueList = new ArrayList<>();
private TechniqueScore techniqueScore = new TechniqueScore();
@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));
int id = JSONUtils.getIntParam(group, "plan_techn_id", 0);
String discrimType = JSONUtils.getStringParam(group, "plan_techn_discrim_type", null);
String discrim = JSONUtils.getStringParam(group, "plan_techn_discrim", null);
if (discrim != null && discrim.isEmpty()) {
discrim = null;
}
if (discrimType != null && discrimType.isEmpty()) {
discrimType = null;
}
techniqueList.add(new Technique(id, discrimType, discrim));
}
}
@Override
protected void doProcess() throws ServiceException, SQLException {
try (Connection conn = resources().getJDBC(WQM_ID);) {
for (Technique ip : techniqueList) {
techniqueScore.computeScores(conn, ip);
}
}
}
@Override
protected void postProcess() {
results().put("AoAId", aoaId, "Area of Analysis Identifier");
results().put("nleach_techn_score", techniqueScore.nleachTechnScore, "Nutrient Management Technique Mitigation Score for Nitrogen in Ground Water Concern");
results().put("nsurf_techn_score", techniqueScore.nsurfTechnScore, "Nutrient Management Technique Mitigation Score for Nitrogen in Surface Water Concern");
results().put("psurf_techn_score", techniqueScore.psurfTechnScore, "Nutrient Management Technique Mitigation Score for Phosphorus in Surface Water Concern");
results().put("nleach_avoid_techn_score", techniqueScore.nleachAvoidTechnScore, "Nutrient Management Technique Mitigation Score for Avoiding Excess Nitrogen Use or Application and Loss to Ground Water");
results().put("nleach_control_techn_score", techniqueScore.nleachControlTechnScore, "Nutrient Management Technique Mitigation Score for Controlling in-field Nitrogen Losses to Ground Water");
results().put("nleach_trap_techn_score", techniqueScore.nleachTrapTechnScore, "Nutrient Management Technique Mitigation Score for Trapping Excess Nitrogen and keeping it from Ground Water");
results().put("nsurf_avoid_techn_score", techniqueScore.nsurfAvoidTechnScore, "Nutrient Management Technique Mitigation Score for Avoiding Excess Nitrogen use or application and loss to Surface Water");
results().put("nsurf_control_techn_score", techniqueScore.nsurfControlTechnScore, "Nutrient Management Technique Mitigation Score for Controlling in-field Nitrogen Losses to Surface Water Runoff");
results().put("nsurf_trap_techn_score", techniqueScore.nsurfTrapTechnScore, "Nutrient Management Technique Mitigation Score for Trapping Excess Nitrogen and Keeping it from Surface Water");
results().put("psurf_avoid_techn_score", techniqueScore.psurfAvoidTechnScore, "Nutrient Management Technique Mitigation Score for Avoiding Excess Phosphorus Use or Application and Loss to Surface Water");
results().put("psurf_control_techn_score", techniqueScore.psurfControlTechnScore, "Nutrient Management Technique Mitigation Score for Controlling in-field Phosphorus Losses to Surface Water Runoff");
results().put("psurf_trap_techn_score", techniqueScore.psurfTrapTechnScore, "Nutrient Management Technique Mitigation Score for Trapping Excess Phosphorus and Keeping it from Surface Water");
}
static class Technique {
int planTechnId;
String planTechnDiscrimType;
String planTechnDiscrim;
public Technique(int plan_techn_id, String plan_techn_discrim_type, String plan_techn_discrim) {
planTechnId = plan_techn_id;
planTechnDiscrimType = plan_techn_discrim_type;
planTechnDiscrim = plan_techn_discrim;
}
}
static class TechniqueScore {
int nleachTechnScore, nleachAvoidTechnScore, nleachControlTechnScore, nleachTrapTechnScore;
int nsurfTechnScore, nsurfAvoidTechnScore, nsurfControlTechnScore, nsurfTrapTechnScore;
int psurfTechnScore, psurfAvoidTechnScore, psurfControlTechnScore, psurfTrapTechnScore;
public void computeScores(Connection conn, Technique ip) throws SQLException {
//Compute nutrient technique mitigation scores for Nitrogen in Ground Water concern and increment total scores
nleachTechnScore += computeTechniqueScore(conn, ip, "Nitrogen in Ground Water", null);
nleachAvoidTechnScore += computeTechniqueScore(conn, ip, "Nitrogen in Ground Water", "Avoid");
nleachControlTechnScore += computeTechniqueScore(conn, ip, "Nitrogen in Ground Water", "Control");
nleachTrapTechnScore += computeTechniqueScore(conn, ip, "Nitrogen in Ground Water", "Trap");
//Compute nutrient technique mitigation scores for Nitrogen in Surface Water concern and increment total scores
nsurfTechnScore += computeTechniqueScore(conn, ip, "Nitrogen in Surface Water", null);
nsurfAvoidTechnScore += computeTechniqueScore(conn, ip, "Nitrogen in Surface Water", "Avoid");
nsurfControlTechnScore += computeTechniqueScore(conn, ip, "Nitrogen in Surface Water", "Control");
nsurfTrapTechnScore += computeTechniqueScore(conn, ip, "Nitrogen in Surface Water", "Trap");
//Compute nutrient technique mitigation scores for Phosphorus in Surface Water concern and increment total scores
psurfTechnScore += computeTechniqueScore(conn, ip, "Phosphorus in Surface Water", null);
psurfAvoidTechnScore += computeTechniqueScore(conn, ip, "Phosphorus in Surface Water", "Avoid");
psurfControlTechnScore += computeTechniqueScore(conn, ip, "Phosphorus in Surface Water", "Control");
psurfTrapTechnScore += computeTechniqueScore(conn, ip, "Phosphorus in Surface Water", "Trap");
}
public int computeTechniqueScore(Connection conn, Technique ip, String concern, String mode) throws SQLException {
int nut_tech_score = 0;
try (Statement statement = conn.createStatement();) {
String query = DBQueries.WQM14Query01(ip.planTechnId, concern, mode, ip.planTechnDiscrimType, ip.planTechnDiscrim);
try (ResultSet resultset = statement.executeQuery(query);) {
while (resultset.next()) {
nut_tech_score += resultset.getInt("nut_tech_score");
}
}
}
return nut_tech_score;
}
}
}