Displaying differences for changeset
 
display as  

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

@@ -1,6 +1,5 @@
 package m.wqm.soilpestlosspot;
 
-
 import csip.ModelDataService;
 import csip.ServiceException;
 import csip.annotations.Polling;
@@ -26,7 +25,6 @@
  * @author RUMPAL SIDHU
  * @author Shaun Case
  */
-
 @Name("WQM-10: Soil Pesticide Interaction Loss Potentials (SoilPestLossPot)")
 @Description("This service computes soil pesticide interaction loss potentials for leaching, solution runoff, and adsorbed runoff.")
 @Path("m/soilpestlosspot/1.0")
@@ -34,12 +32,8 @@
 
 public class V1_0 extends ModelDataService {
 
-    
-    private Statement statement;
-    private Connection conn;
     private V1_0.AoA aoa;
     private String error_msg;
-    
 
     //Response
     private ArrayList<V1_0.Result> result;
@@ -47,14 +41,11 @@
     @Override
     protected void preProcess() throws Exception {
         this.error_msg = "";
-        this.statement = null;
-        this.conn = null;
-        try{ 
-            aoa = new V1_0.AoA(  getStringParam("AoAId"),getStringParam("aoa_pslp"),getStringParam("aoa_ssrp"),
-                            getStringParam("aoa_sarp"),getStringParam("aoa_rain_prob"), getJSONArrayParam("operationlist") );
-            error_msg = aoa.getErrorMsg();        
-        }
-        catch (ServiceException ex) {
+        try {
+            aoa = new V1_0.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() + ". ";
         }
     }
@@ -62,39 +53,21 @@
     @Override
     protected String process() throws Exception {
         ResultSet resultSet;
-        try {
-            conn = wqm.utils.WQMTools.getConnection("wqm", LOG );
-            conn.setAutoCommit(false);
-            statement = conn.createStatement(TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY);            
-            if (error_msg.isEmpty() ){
-                if ( aoa.calculate() ) {
-                    result = aoa.getResults();
-                } else {
-                    error_msg = aoa.getErrorMsg();
-                }
-            }           
-        } 
-        catch (ServiceException | SQLException ex) {            
-            LOG.info(ex.getMessage());
-            error_msg += ex.getMessage();
-        } 
-        finally{
-            if ( this.statement != null ){
-                this.statement.close();
-                this.statement = null;
+
+        if (error_msg.isEmpty()) {
+            if (aoa.calculate()) {
+                result = aoa.getResults();
+            } else {
+                error_msg = aoa.getErrorMsg();
             }
 
-            if ( this.conn != null ){
-                this.conn.close();
-                this.conn = null;
-            }
         }
-        
-        if ( result == null ){
+
+        if (result == null) {
             error_msg += "  No return data found, cannot validate input or results.  Please check your input parameters.";
-        }                     
-        
-        return ( error_msg.isEmpty()? EXEC_OK: error_msg );
+        }
+
+        return (error_msg.isEmpty() ? EXEC_OK : error_msg);
     }
 
     @Override
@@ -102,47 +75,47 @@
     protected void postProcess() throws Exception {
         JSONArray resultArr = new JSONArray();
 
-        if (result != null){            
+        if (result != null) {
             String currentOperationId = "";
-            Collections.sort( result ); 
-            
+            Collections.sort(result);
+
             //Put out the AoA top line
-            putResult( "AoAId", result.get(0).getAoaId(), "Area of Analysis Identifier");
+            putResult("AoAId", result.get(0).getAoaId(), "Area of Analysis Identifier");
             JSONArray tmpArr = new JSONArray();
             JSONArray tmpArr2 = new JSONArray();
             for (V1_0.Result rs1 : result) {
-                if ( !rs1.getOperationId().equals(currentOperationId) ) {
-                    if ( !currentOperationId.isEmpty() ){
-                        currentOperationId = rs1.getOperationId();                                        
+                if (!rs1.getOperationId().equals(currentOperationId)) {
+                    if (!currentOperationId.isEmpty()) {
+                        currentOperationId = rs1.getOperationId();
                         //resultArr.put(JSONUtils.dataDesc("operation summary", tmpArr2, "Summary of this operation"));
-                        tmpArr.put( JSONUtils.dataDesc("pesticideList", tmpArr2, null) ); 
+                        tmpArr.put(JSONUtils.dataDesc("pesticideList", tmpArr2, null));
 
-                        tmpArr2 = new JSONArray();                     
+                        tmpArr2 = new JSONArray();
+                        tmpArr.put(JSONUtils.dataDesc("operation_id", rs1.getOperationId(), "Pesticide Application Operation Identifier"));
+                    } else {
+                        tmpArr2 = new JSONArray();
+                        currentOperationId = rs1.getOperationId();
                         tmpArr.put(JSONUtils.dataDesc("operation_id", rs1.getOperationId(), "Pesticide Application Operation Identifier"));
                     }
-                    else {
-                        tmpArr2 = new JSONArray();
-                        currentOperationId = rs1.getOperationId();  
-                        tmpArr.put(JSONUtils.dataDesc("operation_id", rs1.getOperationId(), "Pesticide Application Operation Identifier"));                        
-                    }
                 }
-                 
-                JSONArray tmpArr3 = new JSONArray();                    
+
+                JSONArray tmpArr3 = new JSONArray();
                 tmpArr3.put(JSONUtils.dataDesc("op_pesticide_id", rs1.getOpPesticideId(), "Pesticide identifier, EPA Pesticide Chemical Code (PC_CODE)"));
                 tmpArr3.put(JSONUtils.dataDesc("op_pest_ilp", rs1.getOpPestIlp(), "Operation Soil Pesticide Interaction Leaching Potential"));
                 tmpArr3.put(JSONUtils.dataDesc("op_pest_isrp", rs1.getOpPestIsrp(), "Operation Soil Pesticide Interaction Solution Runoff Potential"));
                 tmpArr3.put(JSONUtils.dataDesc("op_pest_iarp", rs1.getOpPestIarp(), "Operation Soil Pesticide Interaction Adsorbed Runoff Potential"));
-                
-                tmpArr2.put( tmpArr3 );
+
+                tmpArr2.put(tmpArr3);
             }
-            
-            tmpArr.put( JSONUtils.dataDesc("pesticideList", tmpArr2, "List of Pesticides") );
-            resultArr.put( tmpArr  );
+
+            tmpArr.put(JSONUtils.dataDesc("pesticideList", tmpArr2, "List of Pesticides"));
+            resultArr.put(tmpArr);
             putResult("operationList", resultArr);
         }
     }
-    
-    class AoA{
+
+    class AoA {
+
         private final String AoAId;
         private final String aoa_pslp;
         private final String aoa_ssrp;
@@ -158,20 +131,19 @@
             this.aoa_pslp = aoa_pslp;
             this.aoa_ssrp = aoa_ssrp;
             this.aoa_sarp = aoa_sarp;
-            this.aoa_rain_prob = aoa_rain_prob;            
+            this.aoa_rain_prob = aoa_rain_prob;
             pesticideOperationList = new ArrayList<>();
-            
+
             //  Create pesticide operation list here...
-            try{
+            try {
                 for (int i = 0; i < operationList.length(); i++) {
-                    Map<String, JSONObject> operation = JSONUtils.preprocess(operationList.getJSONArray(i));  
-                    
-                    pesticideOperationList.add( new pesticideOperation( JSONUtils.getStringParam(operation, "operation_id", "err"),this, JSONUtils.getJSONArrayParam(operation, "pesticidelist") ) );                    
+                    Map<String, JSONObject> operation = JSONUtils.preprocess(operationList.getJSONArray(i));
+
+                    pesticideOperationList.add(new pesticideOperation(JSONUtils.getStringParam(operation, "operation_id", "err"), this, JSONUtils.getJSONArrayParam(operation, "pesticidelist")));
                 }
+            } catch (JSONException ex) {
+                this.error_msg += ex.getMessage();
             }
-            catch( JSONException ex){
-                this.error_msg += ex.getMessage();                
-            }            
         }
 
         //Get Methods
@@ -194,124 +166,124 @@
         public String getAoa_rain_prob() {
             return this.aoa_rain_prob;
         }
-        
-        public String getErrorMsg(){
+
+        public String getErrorMsg() {
             return this.error_msg;
         }
-                
-        
-        public Boolean calculate(){
+
+        public Boolean calculate() throws ServiceException {
             Boolean ret_val = true;
-            
-            for( pesticideOperation pesticides : pesticideOperationList ){
-                if ( !pesticides.calculate() ){
+
+            for (pesticideOperation pesticides : pesticideOperationList) {
+                if (!pesticides.calculate()) {
                     ret_val = false;
                     this.error_msg = pesticides.getError();
                     break;
                 }
             }
-            
+
             return ret_val;
         }
-        
-        public ArrayList<V1_0.Result> getResults(){
+
+        public ArrayList<V1_0.Result> getResults() {
             this.results = new ArrayList<>();
-            
-            for( pesticideOperation pOperation : pesticideOperationList ){
-                this.results = pOperation.getResult( this.results );
+
+            for (pesticideOperation pOperation : pesticideOperationList) {
+                this.results = pOperation.getResult(this.results);
             }
-                
+
             return this.results;
         }
     }
-    
+
     class pesticideOperation {
+
         private final String operation_id;
         private final AoA aoa;
         private ArrayList<pesticide> pesticideList;
         private String error_msg = "";
-        
-        pesticideOperation( String operation_id, AoA aoa, JSONArray pesticides ) {
+
+        pesticideOperation(String operation_id, AoA aoa, JSONArray pesticides) {
             this.operation_id = operation_id;
             this.aoa = aoa;
             pesticideList = new ArrayList<>();
-            
-            try{     
-            
+
+            try {
+
                 for (int i = 0; i < pesticides.length(); i++) {
                     Map<String, JSONObject> pesticidesData = JSONUtils.preprocess(pesticides.getJSONArray(i));
-                    pesticideList.add( new pesticide(   JSONUtils.getStringParam(pesticidesData, "op_pesticide_id", "err"), 
-                                                        JSONUtils.getStringParam(pesticidesData, "ai_plp", "err"),
-                                                        JSONUtils.getStringParam(pesticidesData, "ai_psrp", "err"),
-                                                        JSONUtils.getStringParam(pesticidesData, "ai_parp", "err") ) );                                
+                    pesticideList.add(new pesticide(JSONUtils.getStringParam(pesticidesData, "op_pesticide_id", "err"),
+                            JSONUtils.getStringParam(pesticidesData, "ai_plp", "err"),
+                            JSONUtils.getStringParam(pesticidesData, "ai_psrp", "err"),
+                            JSONUtils.getStringParam(pesticidesData, "ai_parp", "err")));
                 }
-            }
-            catch( JSONException ex ){
+            } catch (JSONException ex) {
                 this.error_msg = ex.getMessage();
             }
         }
-        
-        String getError(){
+
+        String getError() {
             return this.error_msg;
         }
-        
-        Boolean calculate(){
+
+        Boolean calculate() throws ServiceException {
             Boolean ret_val = true;
-            
-            for ( pesticide pst: pesticideList ){
-                String tResult = pst.getILP() + " " + pst.getISRP() +  " " + pst.getIARP();
-                if ( ( tResult.contains("ERROR") ) || (tResult.contains("err")) ){
+
+            for (pesticide pst : pesticideList) {
+                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();
                     break;
-                }  
+                }
             }
-            
+
             return ret_val;
         }
-        
-        ArrayList<V1_0.Result> getResult( ArrayList<V1_0.Result> results ){
-            
-            for( pesticideOperation.pesticide pesticide : this.pesticideList ){
-                results.add( pesticide.getResult() );
+
+        ArrayList<V1_0.Result> getResult(ArrayList<V1_0.Result> results) {
+
+            for (pesticideOperation.pesticide pesticide : this.pesticideList) {
+                results.add(pesticide.getResult());
             }
-            
+
             return results;
         }
-        
-        class pesticide{
+
+        class pesticide {
+
             private final String op_pesticide_id;
             private final String ai_plp;
             private final String ai_psrp;
             private final String ai_parp;
             private String error_msg = "";
             private String op_pest_ilp = "ERROR";
-            private String op_pest_isrp = "ERROR";            
+            private String op_pest_isrp = "ERROR";
             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);                                                          
+            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);
             }
-            
-            String getILP(){
-                if ("ERROR".equals(op_pest_ilp) ){
-                //Calculate it
+
+            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{
+                    try (
+                            Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
+                            Statement statement = conn.createStatement(TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY);) {
                         resultSet = statement.executeQuery(query);
-                        if ( resultSet.first() ) {
+                        if (resultSet.first()) {
                             op_pest_ilp = resultSet.getString("wqm_ilp");
-                        }
-                        else {
+                        } else {
                             this.error_msg += "For pesticide: " + this.op_pesticide_id + ", Cannot find that ai_plp and aoa_pslp combination in the database.";
                         }
-                        
-                        if ( "LOW".equals(aoa.getAoa_rain_prob()) ) {
+
+                        if ("LOW".equals(aoa.getAoa_rain_prob())) {
                             switch (op_pest_ilp) {
                                 case "HIGH":
                                     op_pest_ilp = "INTERMEDIATE";
@@ -323,32 +295,32 @@
                                     op_pest_ilp = "VERY LOW";
                                     break;
                             }
-                        }                                                
-                    }
-                    catch( SQLException se ){
-                        this.error_msg += se.getMessage();                        
+                        }
+                    } catch (SQLException se) {
+                        this.error_msg += se.getMessage();
                     }
                 }
-                
+
                 return op_pest_ilp;
             }
-            
-            String getISRP(){
-                if ("ERROR".equals(op_pest_isrp) ){
-                //Calculate it
+
+            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{
+                    try (
+                            Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
+                            Statement statement = conn.createStatement(TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY);) {
                         resultSet = statement.executeQuery(query);
                         op_pest_isrp = "err";
-                        if  (resultSet.first()) {
+                        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.";
                         }
-                        else {
-                            this.error_msg += "For pesticide: " + this.op_pesticide_id + ", Cannot find that ai_psrp and aoa_ssrp combination in the database.";
-                        }                        
-                        
-                        if ( "LOW".equals(aoa.getAoa_rain_prob()) ) {
+
+                        if ("LOW".equals(aoa.getAoa_rain_prob())) {
                             switch (op_pest_isrp) {
                                 case "HIGH":
                                     op_pest_isrp = "INTERMEDIATE";
@@ -356,39 +328,39 @@
                                 case "INTERMEDIATE":
                                     op_pest_isrp = "LOW";
                                     break;
-                                                                        
+
                                 /*case "LOW":
-                                    op_pest_isrp = "LOW";
-                                    break;\
-                                */
+                                 op_pest_isrp = "LOW";
+                                 break;\
+                                 */
                             }
                         }
-                    }
-                    catch( SQLException se ){
+                    } catch (SQLException se) {
                         this.error_msg += se.getMessage();
                     }
-                   
+
                 }
-                                
+
                 return op_pest_isrp;
             }
-            
-            String getIARP(){
-                if ("ERROR".equals(op_pest_iarp) ){
-                //Calculate it
+
+            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{
+                    try (
+                            Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
+                            Statement statement = conn.createStatement(TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY);) {
                         resultSet = statement.executeQuery(query);
                         op_pest_iarp = "err";
-                        if ( resultSet.first() ) {
+                        if (resultSet.first()) {
                             op_pest_iarp = resultSet.getString("wqm_iarp");
-                        }
-                        else {
+                        } else {
                             this.error_msg += "For pesticide: " + this.op_pesticide_id + ", Cannot find that ai_parp and aoa_sarp combination in the database.";
                         }
-                        
-                        if ( "LOW".equals( aoa.getAoa_rain_prob() )) {
+
+                        if ("LOW".equals(aoa.getAoa_rain_prob())) {
                             switch (op_pest_iarp) {
                                 case "HIGH":
                                     op_pest_iarp = "INTERMEDIATE";
@@ -396,78 +368,77 @@
                                 case "INTERMEDIATE":
                                     op_pest_iarp = "LOW";
                                     break;
-                                    
+
                                 /*case "LOW":
-                                    op_pest_iarp = "VERY LOW";
-                                    break;                                    
-                                */
+                                 op_pest_iarp = "VERY LOW";
+                                 break;                                    
+                                 */
                             }
                         }
-                    }
-                    catch( SQLException Se ){
+                    } catch (SQLException Se) {
                         this.error_msg += Se.getMessage();
                     }
                 }
-                                
+
                 return op_pest_iarp;
             }
-            
-            String getErrorMsg(){
+
+            String getErrorMsg() {
                 return this.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 );
-                return result;            
-            }            
-        }                
+
+            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);
+                return result;
+            }
+        }
     }
-    class Result implements Comparable<Result>{
 
-           private final String AoAId;
-           private final String operation_id;
-           private final String op_pesticide_id;
-           private final String op_pest_ilp;
-           private final String op_pest_isrp;
-           private final String op_pest_iarp;
+    class Result implements Comparable<Result> {
 
-           Result(String AoAId, String operation_id, String op_pesticide_id, String op_pest_ilp, String op_pest_isrp, String op_pest_iarp) {
-               this.AoAId = AoAId;
-               this.operation_id = operation_id;
-               this.op_pesticide_id = op_pesticide_id;
-               this.op_pest_ilp = op_pest_ilp;
-               this.op_pest_isrp = op_pest_isrp;
-               this.op_pest_iarp = op_pest_iarp;
-           }
+        private final String AoAId;
+        private final String operation_id;
+        private final String op_pesticide_id;
+        private final String op_pest_ilp;
+        private final String op_pest_isrp;
+        private final String op_pest_iarp;
 
-           public String getAoaId() {
-               return this.AoAId;
-           }
+        Result(String AoAId, String operation_id, String op_pesticide_id, String op_pest_ilp, String op_pest_isrp, String op_pest_iarp) {
+            this.AoAId = AoAId;
+            this.operation_id = operation_id;
+            this.op_pesticide_id = op_pesticide_id;
+            this.op_pest_ilp = op_pest_ilp;
+            this.op_pest_isrp = op_pest_isrp;
+            this.op_pest_iarp = op_pest_iarp;
+        }
 
-           public String getOperationId() {
-               return this.operation_id;
-           }
+        public String getAoaId() {
+            return this.AoAId;
+        }
 
-           public String getOpPesticideId() {
-               return this.op_pesticide_id;
-           }
+        public String getOperationId() {
+            return this.operation_id;
+        }
 
-           public String getOpPestIlp() {
-               return this.op_pest_ilp;
-           }
+        public String getOpPesticideId() {
+            return this.op_pesticide_id;
+        }
 
-           public String getOpPestIsrp() {
-               return this.op_pest_isrp;
-           }
+        public String getOpPestIlp() {
+            return this.op_pest_ilp;
+        }
 
-           public String getOpPestIarp() {
-               return this.op_pest_iarp;
-           }
+        public String getOpPestIsrp() {
+            return this.op_pest_isrp;
+        }
 
-           @Override
-           public int compareTo( Result rs ){           
-               return this.getOperationId().compareTo(rs.getOperationId());
-           }        
-    }              
-}  
+        public String getOpPestIarp() {
+            return this.op_pest_iarp;
+        }
 
+        @Override
+        public int compareTo(Result rs) {
+            return this.getOperationId().compareTo(rs.getOperationId());
+        }
+    }
+}