V1_0.java [src/java/m/wqm/pestpractscores] Revision: 630cd0330c4186ba3f4fa9c59ad3930ae3b38944 Date: Sat Nov 14 12:31:09 MST 2015
package m.wqm.pestpractscores;
/*
* 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.
*/
import csip.ModelDataService;
import static csip.ModelDataService.EXEC_OK;
import csip.ServiceException;
import csip.utils.JSONUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.Path;
import oms3.annotations.Description;
import oms3.annotations.Name;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
*
* @author dhawal
* @author Shaun Case
*/
@Name("WQM-19: Pesticide Mitigation Practice Scores (PestPractScores)")
@Description("This service computes scores for conservation practices applied to mitigate pesticide leaching, solution runoff, adsorbed runoff, and drift.")
@Path("m/pesticide_practice_score/1.0")
public class V1_0 extends ModelDataService {
private String error_msg = "";
private PestPractScores pestPractScores;
@Override
protected void preProcess() throws Exception {
// reading the inputs from the json file into input object and placing it in the arraylist
int AoAId;
this.pestPractScores = null;
JSONArray pestComponents;
try {
AoAId = getIntParam("AoAId");
pestComponents = getJSONArrayParam("pestcomponents");
pestPractScores = new V1_0.PestPractScores(AoAId, pestComponents);
} catch (ServiceException ex) {
this.error_msg = ex.getMessage();
}
}
@Override
protected String process() throws Exception {
try {
if ((this.error_msg.isEmpty()) && (pestPractScores != null)) {
if (!pestPractScores.computeScores()) {
this.error_msg += pestPractScores.getErrorMsg();
}
}
} catch (Exception ex) {
this.error_msg += pestPractScores.getErrorMsg() + " " + ex.getMessage();
LOG.log(Level.SEVERE, this.error_msg);
}
return (this.error_msg.isEmpty() ? EXEC_OK : this.error_msg);
}
@Override
protected void postProcess() throws Exception {
if (pestPractScores.getErrorMsg().isEmpty() && this.error_msg.isEmpty()) {
V1_0.Result result = pestPractScores.getResults();
if (result != null) {
result.putResults();
}
}
}
class PestPractScores {
private String error_msg = "";
private int AoAId;
private int pleach_practice_score;
private int psolsurf_practice_score;
private int padsurf_practice_score;
private int pdrift_practice_score;
private ArrayList<ipmData> ipmList;
private V1_0.Result results = null;
PestPractScores(int AoAId, JSONArray ipms) {
this.pleach_practice_score = this.psolsurf_practice_score = this.padsurf_practice_score = this.pdrift_practice_score = 0;
this.error_msg = "";
this.ipmList = new ArrayList<>();
this.results = null;
this.AoAId = AoAId;
if (null != ipms) {
try {
for (int i = 0; i < ipms.length(); i++) {
Map<String, JSONObject> ipmsData = JSONUtils.preprocess(ipms.getJSONArray(i));
ipmList.add(new ipmData(JSONUtils.getIntParam(ipmsData, "plan_ipm_practice", -1),
JSONUtils.getStringParam(ipmsData, "plan_pract_variant", "err")));
}
} catch (JSONException ex) {
this.error_msg = " Cannot read IPM input variables: " + ex.getMessage() + " ";
}
} else {
this.error_msg = "No input variables: Missing plan_ipm_practice list. ";
}
}
String getErrorMsg() {
return this.error_msg;
}
Result getResults() {
return this.results;
}
boolean computeScores() throws Exception {
boolean ret_val = false;
if ((this.error_msg.isEmpty()) && ((null != this.ipmList) && (!this.ipmList.isEmpty()))) {
//Compute values here.
if (this.error_msg.isEmpty()) {
this.pleach_practice_score = this.psolsurf_practice_score = this.padsurf_practice_score = this.pdrift_practice_score = 0;
for (ipmData ipm : this.ipmList) {
this.pleach_practice_score += getPlPractScore(ipm);
this.psolsurf_practice_score += getPsPractScore(ipm);
this.padsurf_practice_score += getPaPractScore(ipm);
this.pdrift_practice_score += getPdPractScore(ipm);
if (!this.error_msg.isEmpty()) {
break;
}
}
//Create Results here...
if (this.error_msg.isEmpty()) {
results = new V1_0.Result(this.AoAId, this.pleach_practice_score, this.psolsurf_practice_score, this.padsurf_practice_score, this.pdrift_practice_score);
}
}
}
return ret_val;
}
private int getPlPractScore(ipmData ipm) throws ServiceException {
int ret_val = 0;
String query;
if (this.error_msg.isEmpty()) {
try (
Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);) {
String variant = ipm.getVariant();
query = "SELECT ipm_practice_score FROM wqm_ipm_practice_scores WHERE practice_id='" + ipm.getIpm() + "' AND wqm_concern='Pesticide Leaching'";
if (variant != null && !variant.isEmpty()) {
query += " AND practice_variant='" + variant + "'";
}
ResultSet result = statement.executeQuery(query);
if (result.first()) {
ret_val = result.getInt("ipm_practice_score");
}
} catch (SQLException ex) {
this.error_msg += " Cannot compute pl_pract_score: " + ex.getMessage() + " ";
}
}
return ret_val;
}
private int getPsPractScore(ipmData ipm) throws ServiceException {
int ret_val = 0;
String query;
if (this.error_msg.isEmpty()) {
try (
Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);) {
String variant = ipm.getVariant();
query = "SELECT ipm_practice_score FROM wqm_ipm_practice_scores WHERE practice_id='" + ipm.getIpm() + "' AND wqm_concern='Pesticide Solution Runoff'";
if (variant != null && !variant.isEmpty()) {
query += " AND practice_variant='" + variant + "'";
}
ResultSet result = statement.executeQuery(query);
if (result.first()) {
ret_val = result.getInt("ipm_practice_score");
}
} catch (SQLException ex) {
this.error_msg += " Cannot compute ps_pract_score: " + ex.getMessage() + " ";
}
}
return ret_val;
}
private int getPaPractScore(ipmData ipm) {
int ret_val = 0;
String query;
if (this.error_msg.isEmpty()) {
try (
Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);) {
String variant = ipm.getVariant();
query = "SELECT ipm_practice_score FROM wqm_ipm_practice_scores WHERE practice_id='" + ipm.getIpm() + "' AND wqm_concern='Pesticide Adsorbed Runoff'";
if (variant != null && !variant.isEmpty()) {
query += " AND practice_variant='" + variant + "'";
}
ResultSet result = statement.executeQuery(query);
if (result.first()) {
ret_val = result.getInt("ipm_practice_score");
}
} catch (SQLException ex) {
this.error_msg += " Cannot compute pa_pract_score: " + ex.getMessage() + " ";
} catch (ServiceException ex) {
Logger.getLogger(V1_0.class.getName()).log(Level.SEVERE, null, ex);
}
}
return ret_val;
}
private int getPdPractScore(ipmData ipm) throws ServiceException {
int ret_val = 0;
String query;
if (this.error_msg.isEmpty()) {
try (
Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);) {
String variant = ipm.getVariant();
query = "SELECT ipm_practice_score FROM wqm_ipm_practice_scores WHERE practice_id='" + ipm.getIpm() + "' AND wqm_concern='Pesticide Drift'";
if (variant != null && !variant.isEmpty()) {
query += " AND practice_variant='" + variant + "'";
}
ResultSet result = statement.executeQuery(query);
if (result.first()) {
ret_val = result.getInt("ipm_practice_score");
}
} catch (SQLException ex) {
this.error_msg += " Cannot compute pd_pract_score: " + ex.getMessage() + " ";
}
}
return ret_val;
}
class ipmData {
int ipm = 0;
String variant = "";
ipmData(int plan_ipm_practice, String plan_pract_variant) {
this.ipm = plan_ipm_practice;
this.variant = plan_pract_variant;
}
public int getIpm() {
return this.ipm;
}
public String getVariant() {
return this.variant;
}
}
}
class Result {
int AoAId;
int pleach_practice_score;
int psolsurf_practice_score;
int padsurf_practice_score;
int pdrift_practice_score;
Result(int AoAId, int pleach_practice_score, int psolsurf_practice_score, int padsurf_practice_score, int pdrift_practice_score) {
this.AoAId = AoAId;
this.pleach_practice_score = pleach_practice_score;
this.psolsurf_practice_score = psolsurf_practice_score;
this.padsurf_practice_score = padsurf_practice_score;
this.pdrift_practice_score = pdrift_practice_score;
}
void putResults() {
putResult("AoAId", this.AoAId, "Area of Analysis ID");
putResult("pleach_practice_score", this.pleach_practice_score, "pleach_practice_score");
putResult("psolsurf_practice_score", this.psolsurf_practice_score, "psolsurf_practice_score");
putResult("padsurf_practice_score", this.padsurf_practice_score, "padsurf_practice_score");
putResult("pdrift_practice_score", this.pdrift_practice_score, "pdrift_practice_score");
}
}
}