Displaying differences for changeset
 
display as  

src/java/m/wqm/ipmscoresfull/V1_0.java

@@ -1,101 +1,91 @@
 package m.wqm.ipmscoresfull;
 
 /**
- * @author anvesh
+ * @author anvesh, rumpal
  */
 import csip.ModelDataService;
-import static csip.ModelDataService.EXEC_OK;
 import csip.ServiceException;
+import csip.annotations.Resource;
 import java.util.ArrayList;
 import javax.ws.rs.Path;
 import oms3.annotations.Description;
 import oms3.annotations.Name;
 import org.codehaus.jettison.json.JSONArray;
-import org.codehaus.jettison.json.JSONObject;
 import csip.utils.JSONUtils;
-import java.util.Map;
-import java.sql.*;
-import java.util.concurrent.*;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.logging.Level;
 import org.codehaus.jettison.json.JSONException;
+import wqm.utils.DBResources;
+import static wqm.utils.DBResources.WQM_ID;
 
 @Name("WQM-24: IPM Scores")
-@Description("This service returns all rows and columns from the wqm_ipm_scores table.")
+@Description("This service returns all rows and columns "
+        + "from the wqm_ipm_scores table.")
 @Path("m/ipmscoresfull/1.0")
+@Resource(from = DBResources.class)
 
 public class V1_0 extends ModelDataService {
 
-    int ipm_score_id;
-    int ipm_mitigation_score;
-    String resource_concern;
-    String ipm_level;
-    String wqm_concern;
-    String ipm_level_name;
- 
-    ArrayList<V1_0.Result1> result1;
-    String errorMessage = "";
+    private ArrayList<m.wqm.ipmscoresfull.V1_0.Result1> result1;
 
     @Override
-    // reading the inputs from the json file into input object and placing it in the arraylist
-    protected void preProcess() throws ServiceException, JSONException {
-
-        // No pre-processing required, as we don't expect to find anything useful in the Json   
+    protected void preProcess() {
     }
 
     @Override
-    protected String process() throws Exception {
-        try {
-            Connection conn = wqm.utils.WQMTools.getConnection("wqm", LOG);
-            Statement statement = conn.createStatement();
-            this.result1 = new ArrayList<>();  // store the result as objects
+    protected void doProcess() throws Exception {
+        this.result1 = new ArrayList<>();
 
-            conn.setAutoCommit(false);
-            String query = "SELECT * FROM wqm_ipm_scores"; // We need to return the entire table, so we use the not standard SELECT * 
+        try (Connection connection = getResourceJDBC(WQM_ID);
+                Statement statement = connection.createStatement()) {
+            connection.setAutoCommit(false);
+            String query = "SELECT * FROM wqm.wqm_ipm_scores";
             ResultSet results = statement.executeQuery(query);
             while (results.next()) {
-                this.ipm_score_id = results.getInt("ipm_score_id");
-                this.ipm_mitigation_score = results.getInt("ipm_mitigation_score");
-                this.resource_concern = results.getString("resource_concern");
-                this.ipm_level = results.getString("ipm_level");
-                this.wqm_concern = results.getString("wqm_concern");
-                this.ipm_level_name = results.getString("ipm_level_name");
+                int ipm_score_id = results.getInt("ipm_score_id");
+                int ipm_mitigation_score = results.getInt("ipm_mitigation_score");
+                String resource_concern = results.getString("resource_concern");
+                String ipm_level = results.getString("ipm_level");
+                String wqm_concern = results.getString("wqm_concern");
+                String ipm_level_name = results.getString("ipm_level_name");
 
-
-                this.result1.add(new Result1(ipm_score_id, ipm_mitigation_score, resource_concern, ipm_level, wqm_concern, ipm_level_name));
+                this.result1.add(new m.wqm.ipmscoresfull.V1_0.Result1(ipm_score_id, ipm_mitigation_score,
+                        resource_concern, ipm_level, wqm_concern, ipm_level_name));
 
             }
 
         } catch (SQLException se) {
-            this.errorMessage = "Did not open database for WQM-24!";
-            LOG.info(errorMessage);
-            LOG.info(se.getMessage());
+            LOG.log(Level.SEVERE, "Did not open database for WQM-24!", se);
+            throw new ServiceException("SQL problem", se);
         }
-
-        if (errorMessage.isEmpty()) {
-            return EXEC_OK;
-        } else {
-            return errorMessage;
-        }
-
     }
 
     @Override
     //writing the results back to JSON
     protected void postProcess() throws Exception {
-        JSONArray result1Arr = new JSONArray();
-        for (V1_0.Result1 rs1 : result1) {
-            JSONArray tmpArr = new JSONArray();
-            tmpArr.put(JSONUtils.dataDesc("ipm_score_id", rs1.ipm_score_id, "ipm_score_id"));
-            tmpArr.put(JSONUtils.dataDesc("ipm_mitigation_score", rs1.ipm_mitigation_score, "ipm_mitigation_score"));
-            tmpArr.put(JSONUtils.dataDesc("resource_concern", rs1.resource_concern, "NRCS resource concern"));
-            tmpArr.put(JSONUtils.dataDesc("ipm_level", rs1.ipm_level, "Treatment level of integrated pest management:  I (Basic), II (Intermediate), or III (Advanced)"));
-            tmpArr.put(JSONUtils.dataDesc("wqm_concern", rs1.wqm_concern, "WQM resource concern:  pesticide leaching, pesticide solution runoff, pesticide adsorbed runoff, pesticide drift"));
-            tmpArr.put(JSONUtils.dataDesc("ipm_level_name", rs1.ipm_level_name, "Treatment level of integrated pest management:  Basic, Intermediate, Advanced"));
-            result1Arr.put(JSONUtils.dataDesc("IPM  Scores", tmpArr, "IPMScores"));
+        try {
+            JSONArray result1Arr = new JSONArray();
+            for (m.wqm.ipmscoresfull.V1_0.Result1 rs1 : result1) {
+                JSONArray tmpArr = new JSONArray();
+                tmpArr.put(JSONUtils.dataDesc("ipm_score_id", rs1.ipm_score_id, "ipm_score_id"));
+                tmpArr.put(JSONUtils.dataDesc("ipm_mitigation_score", rs1.ipm_mitigation_score, "ipm_mitigation_score"));
+                tmpArr.put(JSONUtils.dataDesc("resource_concern", rs1.resource_concern, "NRCS resource concern"));
+                tmpArr.put(JSONUtils.dataDesc("ipm_level", rs1.ipm_level, "Treatment level of integrated pest management:  I (Basic), II (Intermediate), or III (Advanced)"));
+                tmpArr.put(JSONUtils.dataDesc("wqm_concern", rs1.wqm_concern, "WQM resource concern:  pesticide leaching, pesticide solution runoff, pesticide adsorbed runoff, pesticide drift"));
+                tmpArr.put(JSONUtils.dataDesc("ipm_level_name", rs1.ipm_level_name, "Treatment level of integrated pest management:  Basic, Intermediate, Advanced"));
+                result1Arr.put(JSONUtils.dataDesc("IPM  Scores", tmpArr, "IPMScores"));
+            }
+            putResult("operation", result1Arr);
+        } catch (JSONException ex) {
+            LOG.log(Level.SEVERE, "Error in processing the response JSON for WQM-24!", ex);
+            throw new ServiceException("Error in processing the response JSON.", ex);
         }
-        putResult("operation", result1Arr);
     }
 
-    public class Result1 {
+    static class Result1 {
 
         int ipm_score_id;
         int ipm_mitigation_score;
@@ -104,7 +94,6 @@
         String wqm_concern;
         String ipm_level_name;
 
-
         public Result1(
                 int ipm_score_id,
                 int ipm_mitigation_score,
@@ -119,7 +108,6 @@
             this.wqm_concern = wqm_concern;
             this.ipm_level_name = ipm_level_name;
 
-
         }
     }
 

src/java/m/wqm/ipmscoresfull/V1_0.json

@@ -1,6 +1,5 @@
 {
-  "metainfo": {},
-  "parameter": [
-
-  ]
+    "metainfo": {},
+    "parameter": [
+    ]
 }
\ No newline at end of file