Displaying differences for changeset
 
display as  

src/java/m/wqm/soilpestlosspot/V1_0.java

@@ -3,6 +3,7 @@
 import csip.ModelDataService;
 import csip.ServiceException;
 import csip.annotations.Polling;
+import csip.annotations.Resource;
 import csip.utils.JSONUtils;
 import java.sql.Connection;
 import java.sql.ResultSet;
@@ -19,6 +20,8 @@
 import org.codehaus.jettison.json.JSONArray;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
+import wqm.utils.DBResources;
+import static wqm.utils.DBResources.WQM_ID;
 
 /**
  *
@@ -29,45 +32,45 @@
 @Description("This service computes soil pesticide interaction loss potentials for leaching, solution runoff, and adsorbed runoff.")
 @Path("m/soilpestlosspot/1.0")
 @Polling(first = 10000, next = 2000)
-
+@Resource(from = DBResources.class)
 public class V1_0 extends ModelDataService {
 
-    private V1_0.AoA aoa;
+    private AoA aoa;
     private String error_msg;
 
     //Response
-    private ArrayList<V1_0.Result> result;
+    private ArrayList<Result> result;
 
     @Override
     protected void preProcess() throws Exception {
-        this.error_msg = "";
+        error_msg = "";
         try {
-            aoa = new V1_0.AoA(getStringParam("AoAId"), getStringParam("aoa_pslp"), getStringParam("aoa_ssrp"),
+            aoa = new AoA(getStringParam("AoAId"), getStringParam("aoa_pslp"), getStringParam("aoa_ssrp"),
                     getStringParam("aoa_sarp"), getStringParam("aoa_rain_prob"), getJSONArrayParam("operationlist"));
             error_msg = aoa.getErrorMsg();
         } catch (Exception ex) {
-            error_msg += "  " + ex.getMessage() + ". ";
+            throw new ServiceException("PreProcess Exception",ex);
+            
         }
     }
 
     @Override
-    protected String process() throws Exception {
-        ResultSet resultSet;
-
+    protected void doProcess() throws Exception {
+   
         if (error_msg.isEmpty()) {
             if (aoa.calculate()) {
                 result = aoa.getResults();
             } else {
-                error_msg = aoa.getErrorMsg();
-            }
+                throw new ServiceException(aoa.getErrorMsg());
+           }
 
         }
 
         if (result == null) {
-            error_msg += "  No return data found, cannot validate input or results.  Please check your input parameters.";
+            throw new ServiceException("  No return data found, cannot validate input or results.  Please check your input parameters.");
         }
 
-        return (error_msg.isEmpty() ? EXEC_OK : error_msg);
+     
     }
 
     @Override
@@ -83,7 +86,7 @@
             putResult("AoAId", result.get(0).getAoaId(), "Area of Analysis Identifier");
             JSONArray tmpArr = new JSONArray();
             JSONArray tmpArr2 = new JSONArray();
-            for (V1_0.Result rs1 : result) {
+            for (Result rs1 : result) {
                 if (!rs1.getOperationId().equals(currentOperationId)) {
                     if (!currentOperationId.isEmpty()) {
                         currentOperationId = rs1.getOperationId();
@@ -123,9 +126,9 @@
         private final String aoa_rain_prob;
         private ArrayList<pesticideOperation> pesticideOperationList;
         private String error_msg;
-        ArrayList<V1_0.Result> results;
+        ArrayList<Result> results;
 
-        AoA(String AoAId, String aoa_pslp, String aoa_ssrp, String aoa_sarp, String aoa_rain_prob, JSONArray operationList) {
+        AoA(String AoAId, String aoa_pslp, String aoa_ssrp, String aoa_sarp, String aoa_rain_prob, JSONArray operationList) throws ServiceException {
             error_msg = "";
             this.AoAId = AoAId;
             this.aoa_pslp = aoa_pslp;
@@ -142,33 +145,33 @@
                     pesticideOperationList.add(new pesticideOperation(JSONUtils.getStringParam(operation, "operation_id", "err"), this, JSONUtils.getJSONArrayParam(operation, "pesticidelist")));
                 }
             } catch (JSONException ex) {
-                this.error_msg += ex.getMessage();
+                throw new ServiceException("JSONException",ex);
             }
         }
 
         //Get Methods
         public String getAoaId() {
-            return this.AoAId;
+            return AoAId;
         }
 
         public String getAoa_pslp() {
-            return this.aoa_pslp;
+            return aoa_pslp;
         }
 
         public String getAoa_ssrp() {
-            return this.aoa_ssrp;
+            return aoa_ssrp;
         }
 
         public String getAoa_sarp() {
-            return this.aoa_sarp;
+            return aoa_sarp;
         }
 
         public String getAoa_rain_prob() {
-            return this.aoa_rain_prob;
+            return aoa_rain_prob;
         }
 
         public String getErrorMsg() {
-            return this.error_msg;
+            return error_msg;
         }
 
         public Boolean calculate() throws ServiceException {
@@ -177,7 +180,7 @@
             for (pesticideOperation pesticides : pesticideOperationList) {
                 if (!pesticides.calculate()) {
                     ret_val = false;
-                    this.error_msg = pesticides.getError();
+                    error_msg = pesticides.getError();
                     break;
                 }
             }
@@ -185,14 +188,14 @@
             return ret_val;
         }
 
-        public ArrayList<V1_0.Result> getResults() {
-            this.results = new ArrayList<>();
+        public ArrayList<Result> getResults() {
+            results = new ArrayList<>();
 
             for (pesticideOperation pOperation : pesticideOperationList) {
-                this.results = pOperation.getResult(this.results);
+                results = pOperation.getResult(results);
             }
 
-            return this.results;
+            return results;
         }
     }
 
@@ -203,7 +206,7 @@
         private ArrayList<pesticide> pesticideList;
         private String error_msg = "";
 
-        pesticideOperation(String operation_id, AoA aoa, JSONArray pesticides) {
+        pesticideOperation(String operation_id, AoA aoa, JSONArray pesticides) throws ServiceException {
             this.operation_id = operation_id;
             this.aoa = aoa;
             pesticideList = new ArrayList<>();
@@ -218,12 +221,12 @@
                             JSONUtils.getStringParam(pesticidesData, "ai_parp", "err")));
                 }
             } catch (JSONException ex) {
-                this.error_msg = ex.getMessage();
+                throw new ServiceException("JSONException",ex);
             }
         }
 
         String getError() {
-            return this.error_msg;
+            return error_msg;
         }
 
         Boolean calculate() throws ServiceException {
@@ -233,7 +236,7 @@
                 String tResult = pst.getILP() + " " + pst.getISRP() + " " + pst.getIARP();
                 if ((tResult.contains("ERROR")) || (tResult.contains("err"))) {
                     ret_val = false;
-                    this.error_msg += "  For operation: " + this.operation_id + ", " + pst.getErrorMsg();
+                    error_msg += "  For operation: " + operation_id + ", " + pst.getErrorMsg();
                     break;
                 }
             }
@@ -241,9 +244,9 @@
             return ret_val;
         }
 
-        ArrayList<V1_0.Result> getResult(ArrayList<V1_0.Result> results) {
+        ArrayList<Result> getResult(ArrayList<Result> results) {
 
-            for (pesticideOperation.pesticide pesticide : this.pesticideList) {
+            for (pesticideOperation.pesticide pesticide : pesticideList) {
                 results.add(pesticide.getResult());
             }
 
@@ -262,25 +265,23 @@
             private String op_pest_iarp = "ERROR";
 
             pesticide(String op_pesticide_id, String ai_plp, String ai_psrp, String ai_parp) {
-                this.op_pesticide_id = ("err".equals(op_pesticide_id) ? (this.error_msg = "Pesticide Id invalid or missing") : op_pesticide_id);
-                this.ai_plp = ("err".equals(ai_plp) ? (this.error_msg = "Active Ingredient Pesticide Leaching Potential invalid or missing") : ai_plp);
-                this.ai_psrp = ("err".equals(ai_psrp) ? (this.error_msg = "Active Ingredient Pesticide Solution Runoff Potential invalid or missing") : ai_psrp);
-                this.ai_parp = ("err".equals(ai_parp) ? (this.error_msg = "Active Ingredient Pesticide Adsorbed Runoff Potential invalid or missing") : ai_parp);
+                this.op_pesticide_id = ("err".equals(op_pesticide_id) ? (error_msg = "Pesticide Id invalid or missing") : op_pesticide_id);
+                this.ai_plp = ("err".equals(ai_plp) ? (error_msg = "Active Ingredient Pesticide Leaching Potential invalid or missing") : ai_plp);
+                this.ai_psrp = ("err".equals(ai_psrp) ? (error_msg = "Active Ingredient Pesticide Solution Runoff Potential invalid or missing") : ai_psrp);
+                this.ai_parp = ("err".equals(ai_parp) ? (error_msg = "Active Ingredient Pesticide Adsorbed Runoff Potential invalid or missing") : ai_parp);
             }
 
             String getILP() throws ServiceException {
                 if ("ERROR".equals(op_pest_ilp)) {
                     //Calculate it
-                    ResultSet resultSet;
                     String query = "SELECT wqm_ilp FROM wqm_soil_pest_interaction_leaching WHERE wqm_plp='" + ai_plp + "' AND wqm_slp='" + aoa.getAoa_pslp() + "';";
-                    try (
-                            Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
-                            Statement statement = conn.createStatement(TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY);) {
-                        resultSet = statement.executeQuery(query);
+                      try (Connection conn = getResourceJDBC(WQM_ID);
+                            Statement statement = conn.createStatement(TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY)) {
+                       try(ResultSet resultSet = statement.executeQuery(query)){
                         if (resultSet.first()) {
                             op_pest_ilp = resultSet.getString("wqm_ilp");
                         } else {
-                            this.error_msg += "For pesticide: " + this.op_pesticide_id + ", Cannot find that ai_plp and aoa_pslp combination in the database.";
+                            error_msg += "For pesticide: " + op_pesticide_id + ", Cannot find that ai_plp and aoa_pslp combination in the database.";
                         }
 
                         if ("LOW".equals(aoa.getAoa_rain_prob())) {
@@ -296,8 +297,9 @@
                                     break;
                             }
                         }
+                       }
                     } catch (SQLException se) {
-                        this.error_msg += se.getMessage();
+                        throw new ServiceException("SQLException",se);
                     }
                 }
 
@@ -307,17 +309,17 @@
             String getISRP() throws ServiceException {
                 if ("ERROR".equals(op_pest_isrp)) {
                     //Calculate it
-                    ResultSet resultSet;
+                
                     String query = "SELECT wqm_isrp FROM wqm_soil_pest_interaction_solution_runoff WHERE wqm_psrp='" + ai_psrp + "' AND wqm_ssrp ='" + aoa.getAoa_ssrp() + "';";
-                    try (
-                            Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
-                            Statement statement = conn.createStatement(TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY);) {
-                        resultSet = statement.executeQuery(query);
+                   
+                            try (Connection conn = getResourceJDBC(WQM_ID);
+                            Statement statement = conn.createStatement(TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY)) {
+                            try( ResultSet resultSet = statement.executeQuery(query)){
                         op_pest_isrp = "err";
                         if (resultSet.first()) {
                             op_pest_isrp = resultSet.getString("wqm_isrp");
                         } else {
-                            this.error_msg += "For pesticide: " + this.op_pesticide_id + ", Cannot find that ai_psrp and aoa_ssrp combination in the database.";
+                            throw new ServiceException( "For pesticide: " + op_pesticide_id + ", Cannot find that ai_psrp and aoa_ssrp combination in the database.");                      
                         }
 
                         if ("LOW".equals(aoa.getAoa_rain_prob())) {
@@ -335,8 +337,9 @@
                                  */
                             }
                         }
+                            }
                     } catch (SQLException se) {
-                        this.error_msg += se.getMessage();
+                        throw new ServiceException("SQLException",se);
                     }
 
                 }
@@ -347,17 +350,17 @@
             String getIARP() throws ServiceException {
                 if ("ERROR".equals(op_pest_iarp)) {
                     //Calculate it
-                    ResultSet resultSet;
+                    
                     String query = "SELECT wqm_iarp FROM wqm_soil_pest_interaction_adsorbed_runoff WHERE wqm_parp='" + ai_parp + "' AND wqm_sarp='" + aoa.getAoa_sarp() + "';";
-                    try (
-                            Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
-                            Statement statement = conn.createStatement(TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY);) {
-                        resultSet = statement.executeQuery(query);
+                     try (Connection conn = getResourceJDBC(WQM_ID);
+                Statement statement = conn.createStatement(TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY)) {
+                         
+                       try(ResultSet resultSet = statement.executeQuery(query)){
                         op_pest_iarp = "err";
                         if (resultSet.first()) {
                             op_pest_iarp = resultSet.getString("wqm_iarp");
                         } else {
-                            this.error_msg += "For pesticide: " + this.op_pesticide_id + ", Cannot find that ai_parp and aoa_sarp combination in the database.";
+                            error_msg += "For pesticide: " + op_pesticide_id + ", Cannot find that ai_parp and aoa_sarp combination in the database.";
                         }
 
                         if ("LOW".equals(aoa.getAoa_rain_prob())) {
@@ -375,8 +378,9 @@
                                  */
                             }
                         }
-                    } catch (SQLException Se) {
-                        this.error_msg += Se.getMessage();
+                       }
+                    } catch (SQLException se) {
+                       throw new ServiceException("SQLException",se);
                     }
                 }
 
@@ -384,11 +388,11 @@
             }
 
             String getErrorMsg() {
-                return this.error_msg;
+                return error_msg;
             }
 
-            V1_0.Result getResult() {
-                V1_0.Result result = new V1_0.Result(aoa.getAoaId(), operation_id, op_pesticide_id, op_pest_ilp, op_pest_isrp, op_pest_iarp);
+            Result getResult() {
+                Result result = new Result(aoa.getAoaId(), operation_id, op_pesticide_id, op_pest_ilp, op_pest_isrp, op_pest_iarp);
                 return result;
             }
         }
@@ -413,32 +417,32 @@
         }
 
         public String getAoaId() {
-            return this.AoAId;
+            return AoAId;
         }
 
         public String getOperationId() {
-            return this.operation_id;
+            return operation_id;
         }
 
         public String getOpPesticideId() {
-            return this.op_pesticide_id;
+            return op_pesticide_id;
         }
 
         public String getOpPestIlp() {
-            return this.op_pest_ilp;
+            return op_pest_ilp;
         }
 
         public String getOpPestIsrp() {
-            return this.op_pest_isrp;
+            return op_pest_isrp;
         }
 
         public String getOpPestIarp() {
-            return this.op_pest_iarp;
+            return op_pest_iarp;
         }
 
         @Override
         public int compareTo(Result rs) {
-            return this.getOperationId().compareTo(rs.getOperationId());
+            return getOperationId().compareTo(rs.getOperationId());
         }
     }
 }