Displaying differences for changeset
 
display as  

src/java/m/wqm/rfactor/V1_0.java

@@ -1,11 +1,12 @@
 package m.wqm.rfactor;
 
-import csip.Config;
 import csip.ModelDataService;
 import csip.ServiceException;
+import csip.annotations.Resource;
 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.Collections;
@@ -17,6 +18,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;
 
 /**
  *
@@ -24,16 +27,19 @@
  * @author Shaun Case
  */
 @Name("WQM-12: Climate R Factor for an Area of Analysis (AoARFactor)")
-@Description("This service intersects an area of analysis (AoA) with the Revised Universal Soil Loss Equation (RUSLE2) climate R Factor layer and computes an R Factor representing the AoA.")
+@Description("This service intersects an area of analysis (AoA) with the "
+        + "Revised Universal Soil Loss Equation (RUSLE2) climate R Factor layer "
+        + "and computes an R Factor representing the AoA.")
 @Path("m/wqm/rfactor/1.0")
+@Resource(from = DBResources.class)
 
 public class V1_0 extends ModelDataService {
 
     private String error_msg;
-    private ArrayList<AoA> list;
+    private ArrayList<m.wqm.rfactor.V1_0.AoA> list;
 
     @Override
-    public void preProcess() {
+    public void preProcess() throws ServiceException {
         this.error_msg = "";
         String points;
         String finalpoints;
@@ -69,50 +75,35 @@
                 this.list.add(new AoA(aoaId, finalpoints));
             }
         } catch (ServiceException | JSONException ex) {
-            this.error_msg = "Cannot process the input JSON: " + ex.getMessage();
-            LOG.log(Level.SEVERE, this.error_msg);
+            LOG.log(Level.SEVERE, "Error in processing the request JSON for WQM-12!", ex);
+            throw new ServiceException("Error in processing the request JSON.", ex);
         }
     }
 
     @Override
-    public String process() {
+    public void doProcess() throws ServiceException {
         String rFactor;
         ArrayList<String> rfactorList = new ArrayList<>();
 
-        String confString = Config.getString("rse-db", "r2gis");
         if (this.error_msg.isEmpty()) {
-            try (
-                    Connection conn = wqm.utils.WQMTools.getConnection(confString, LOG);
-                    Statement statement = conn.createStatement();) {
+            try (Connection conn = getResourceJDBC(WQM_ID);
+                    Statement statement = conn.createStatement()) {
 
-                for (AoA list1 : this.list) {
+                for (m.wqm.rfactor.V1_0.AoA list1 : this.list) {
                     String tPoints = list1.getCoordinates();
-                    String gisQuery = "";
-                    switch (confString) {
-                        // ms-sql server 
-                        case "rse-sql":
-                            gisQuery = "SELECT TOP 1 m.co_fips, r2_path, r2_name, r_factor, g.geometry "
-                                    + "FROM r2gis.map_climates AS m, r2gis.cli_geom AS g "
-                                    + "WITH (Index(cli_geom_idx)) "
-                                    + "WHERE "
-                                    + "g.geometry.STIntersects(geometry::STPolyFromText('POLYGON((" + tPoints + "))',0)) = 1 "
-                                    + "AND ((g.co_fips = m.co_fips and g.geometry.STIsValid()=1 and m.ei_rang is null) "
-                                    + "or (g.co_fips = m.co_fips and g.ei_rang = m.ei_rang and m.ei_rang is not null and g.geometry.STIsValid()=1)) "
-                                    + "and g.geometry.STIsValid()=1 and geometry::STPolyFromText('POLYGON((" + tPoints + "))',0).STIsValid()=1;";
-                            break;
-                        // postgresql - postgis
-                        case "r2gis":
-//                            gisQuery = "SELECT m.co_fips, r2_path, r2_name, m.ei_rang, m.r_factor, st_area(st_intersection(ST_PolygonFromText('POLYGON((" + tPoints + "))'), geometry)) / st_area(ST_PolygonFromText('POLYGON((" + tPoints + "))'))*100 as percentAoi,st_area(st_intersection(ST_PolygonFromText('POLYGON((" + tPoints + "))'), geometry))/43560 as sizeAoi FROM cli_geom AS g, map_climates AS m WHERE ST_Intersects(ST_PolygonFromText('POLYGON((" + tPoints + "))'), g.geometry) AND ((g.co_fips = m.co_fips and st_isvalid(geometry)='t' and m.ei_rang is null) or (g.co_fips = m.co_fips and g.ei_rang = m.ei_rang and m.ei_rang is not null and st_isvalid(geometry)='t')) and st_isvalid(geometry)='t' and st_isvalid(ST_PolygonFromText('POLYGON((" + tPoints + "))'))='t';";
-                            // why do the complex percentAoi and sizeAoi calculations for intersecting with climate shapes here?
-                            // this is not returned by the rfactor service !
-                            gisQuery = "SELECT m.co_fips, r2_path, r2_name, m.ei_rang, m.r_factor FROM cli_geom AS g, map_climates AS m WHERE ST_Intersects(ST_PolygonFromText('POLYGON((" + tPoints + "))'), g.geometry) AND ((g.co_fips = m.co_fips and st_isvalid(geometry)='t' and m.ei_rang is null) or (g.co_fips = m.co_fips and g.ei_rang = m.ei_rang and m.ei_rang is not null and st_isvalid(geometry)='t')) and st_isvalid(geometry)='t' and st_isvalid(ST_PolygonFromText('POLYGON((" + tPoints + "))'))='t';";
-                            break;
-                    }
+
+                    String gisQuery = "SELECT TOP 1 m.co_fips, r2_path, r2_name, r_factor, g.geometry "
+                            + "FROM r2gis.map_climates AS m, r2gis.cli_geom AS g "
+                            + "WITH (Index(cli_geom_idx)) "
+                            + "WHERE "
+                            + "g.geometry.STIntersects(geometry::STPolyFromText('POLYGON((" + tPoints + "))',0)) = 1 "
+                            + "AND ((g.co_fips = m.co_fips and g.geometry.STIsValid()=1 and m.ei_rang is null) "
+                            + "or (g.co_fips = m.co_fips and g.ei_rang = m.ei_rang and m.ei_rang is not null and g.geometry.STIsValid()=1)) "
+                            + "and g.geometry.STIsValid()=1 and geometry::STPolyFromText('POLYGON((" + tPoints + "))',0).STIsValid()=1;";
+
                     LOG.info("RFACTOR SQL=" + gisQuery);
                     ResultSet results = statement.executeQuery(gisQuery);
                     while (results.next()) {
-//                        String urlName = wqm.utils.WQMTools.getR2GISFileURL(results.getString("r2_path"), results.getString("r2_name"));
-//                        rFactor = getAndProcessXml(urlName);
                         rFactor = results.getString("r_factor");
 
                         if (null != rFactor) {
@@ -130,15 +121,11 @@
                         list1.setRFactor(rfactorList);
                     }
                 }
-
-               
-            } catch (Exception ex) {
-                this.error_msg = "Cannot process that request: " + ex.getMessage();
-                LOG.log(Level.SEVERE, this.error_msg);
+            } catch (ServiceException | SQLException ex) {
+                LOG.log(Level.SEVERE, "SQL problem for WQM-12!", ex);
+                throw new ServiceException("SQL problem", ex);
             }
         }
-
-        return (this.error_msg.isEmpty() ? EXEC_OK : this.error_msg);
     }
 
     @Override
@@ -147,7 +134,7 @@
         try {
             if (this.error_msg.isEmpty()) {
                 JSONArray finalArr = new JSONArray();
-                for (AoA list1 : this.list) {
+                for (m.wqm.rfactor.V1_0.AoA list1 : this.list) {
                     JSONArray resultArr = new JSONArray();
                     temp = new ArrayList<>();
                     for (int j = 0; j < list1.getRFactor().size(); j++) {
@@ -168,13 +155,12 @@
                 putResult("", finalArr);
             }
         } catch (NumberFormatException | JSONException ex) {
-            this.error_msg = "Could not create result JSON: " + ex.getMessage();
-            LOG.log(Level.SEVERE, this.error_msg);
-            throw new Exception(this.error_msg);
+            LOG.log(Level.SEVERE, "Error in processing the response JSON for WQM-12!", ex);
+            throw new ServiceException("Error in processing the response JSON.", ex);
         }
     }
 
-// rFactor now coming from map_climates table    
+// rFactor now coming from map_climates table
 //    private String getAndProcessXml(String url) {
 //        String ret_val = null;
 //
@@ -207,7 +193,7 @@
 //
 //        return ret_val;
 //    }
-    public class AoA {
+    static class AoA {
 
         private final String aoa_id;
         private final String polygonCoordinates;

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

@@ -1,47 +1,47 @@
 {
-"metainfo": {
-},
-"parameter": [
-    {
-        "name" : "aoas",
-        "value" : [
-            [
-                {
-                    "name" : "aoa_id",
-                    "value" : 1,
-                    "description" : "AoA identifier"
-                },
-                {
-                    "name" : "aoa_geometry",
-                    "value" : [
-                                    [
-                                    [
-                                     -100.70589179999996,
-                                     35.161253299000066
-                                    ],
-                                    [
-                                     -100.70606179999999,
-                                     35.146673099000054   
-                                    ],
-                                    [
-                                      -100.72465179999995,
-                                      35.146673099000054  
-                                    ],
-                                    [
-                                      -100.72455179999997,
-                                      35.16112319900003  
-                                    ],
-                                    [
-                                      -100.70589179999996,
-                                      35.161253299000066
-                                    ]
-                                    ]
+    "metainfo": {
+    },
+    "parameter": [
+        {
+            "name": "aoas",
+            "value": [
+                [
+                    {
+                        "name": "aoa_id",
+                        "value": 1,
+                        "description": "AoA identifier"
+                    },
+                    {
+                        "name": "aoa_geometry",
+                        "value": [
+                            [
+                                [
+                                    -100.70589179999996,
+                                    35.161253299000066
                                 ],
-                    "decription" : "AoA coordinate geometry"
-                }
+                                [
+                                    -100.70606179999999,
+                                    35.146673099000054
+                                ],
+                                [
+                                    -100.72465179999995,
+                                    35.146673099000054
+                                ],
+                                [
+                                    -100.72455179999997,
+                                    35.16112319900003
+                                ],
+                                [
+                                    -100.70589179999996,
+                                    35.161253299000066
+                                ]
+                            ]
+                        ],
+                        "decription": "AoA coordinate geometry"
+                    }
                 ]
-        ],
-        "description" : "List of AoAs"
-    }
-]
+            ],
+            "description": "List of AoAs"
+        }
+    ]
 }
\ No newline at end of file