@@ -7,6 +7,7 @@ |
|
import csip.ServiceException; |
import csip.annotations.Polling; |
+import csip.annotations.Resource; |
import csip.utils.JSONUtils; |
import java.sql.Connection; |
import java.sql.ResultSet; |
@@ -15,14 +16,15 @@ |
import java.util.ArrayList; |
import java.util.Map; |
import java.util.HashMap; |
-import java.util.Iterator; |
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; |
-import wqm.utils.WQMTools; |
+import wqm.utils.DBResources; |
+import static wqm.utils.DBResources.WQM_ID; |
+ |
|
/** |
* |
@@ -32,32 +34,29 @@ |
@Name("WQM-03: Pesticide Product List (PestProdList)") |
@Description("This service returns a list of pesticide products and their active ingredients from the wqm_pesticide_products table of the WQM Data Mart. The list is filtered by criteria provided in the request payload.") |
@Path("m/wqm/pestprodlist/1.0") |
+@Resource(from = DBResources.class) |
@Polling(first = 1000, next = 2000) |
|
public class V1_0 extends csip.ModelDataService { |
|
- private ArrayList<V1_0.searchFilter> filters; |
- private String error_msg; |
- private ArrayList<V1_0.searchResult> searchResults; |
+ private ArrayList<SearchFilter> filters; |
+ |
+ private ArrayList<SearchResult> searchResults; |
|
@Override |
protected void preProcess() throws csip.ServiceException { |
JSONArray filterArray; |
- this.filters = new ArrayList<>(); |
- this.searchResults = new ArrayList<>(); |
- this.error_msg = ""; |
+ filters = new ArrayList<>(); |
+ searchResults = new ArrayList<>(); |
|
filterArray = getJSONArrayParam("filters"); |
|
try { |
for (int i = 0; i < filterArray.length(); i++) { |
- V1_0.searchFilter tFilter = new V1_0.searchFilter(filterArray.getJSONArray(i)); |
- if (!tFilter.getError()) { |
- this.filters.add(tFilter); |
- } else { |
- this.error_msg = tFilter.getErrorMsg(); |
- break; |
- } |
+ SearchFilter tFilter = new SearchFilter(filterArray.getJSONArray(i)); |
+ |
+ filters.add(tFilter); |
+ |
} |
} catch (JSONException ex) { |
throw new csip.ServiceException(ex.getMessage()); |
@@ -65,32 +64,23 @@ |
} |
|
@Override |
- protected String process() throws csip.ServiceException { |
+ protected void doProcess() throws csip.ServiceException { |
String query = "SELECT reg_no, prod_name, type_desc, pc_code, ai_name, pc_pct from wqm_pesticide_products WHERE "; |
int counter = 0; |
- |
- if (this.error_msg.isEmpty()) { |
- for (V1_0.searchFilter aFilter : this.filters) { |
- if (!aFilter.getError()) { |
+ |
+ for (SearchFilter aFilter : filters) { |
+ |
if (counter > 0) { |
query += " AND "; |
} |
- |
query += aFilter.getWhereClause(); |
counter++; |
- } else { |
- this.error_msg += aFilter.getErrorMsg(); |
- break; |
- } |
+ } |
+ |
+ try (Connection conn = getResourceJDBC(WQM_ID); |
+ Statement statement = conn.createStatement()) { |
|
- } |
- |
- if (this.error_msg.isEmpty()) { |
- try ( |
- Connection conn = WQMTools.getConnection("wqm", LOG); |
- Statement statement = conn.createStatement();) { |
- |
- ResultSet results = statement.executeQuery(query); |
+ try( ResultSet results = statement.executeQuery(query)) { |
while (results.next()) { |
Map<String, String> resultMap = new HashMap<>(); |
|
@@ -101,23 +91,21 @@ |
resultMap.put("ai_name", results.getString("ai_name")); |
resultMap.put("pc_pct", results.getString("pc_pct")); |
|
- searchResults.add(new V1_0.searchResult(resultMap)); |
+ searchResults.add(new SearchResult(resultMap)); |
} |
- |
+ } |
} catch (ServiceException | SQLException ex) { |
throw new csip.ServiceException(ex.getMessage()); |
} |
- } |
- } |
- |
- return (this.error_msg.isEmpty() ? EXEC_OK : this.error_msg); |
+ |
+ |
} |
|
@Override |
protected void postProcess() throws JSONException { |
JSONArray resultArr = new JSONArray(); |
|
- for (searchResult result : this.searchResults) { |
+ for (SearchResult result : searchResults) { |
resultArr.put(result.getResultArray()); |
} |
|
@@ -125,24 +113,24 @@ |
|
} |
|
- protected static class searchResult { |
+ protected static class SearchResult { |
|
- private final Map<String, V1_0.searchResult.valueDescription> results = new HashMap<String, V1_0.searchResult.valueDescription>() { |
+ private final Map<String, SearchResult.valueDescription> results = new HashMap<String, SearchResult.valueDescription>() { |
{ |
- put("reg_no", new V1_0.searchResult.valueDescription("", "EPA product registration number (EPA Reg. No.)")); |
- put("prod_name", new V1_0.searchResult.valueDescription("", "Pesticide product name")); |
- put("type_desc", new V1_0.searchResult.valueDescription("", "Pesticide product type)")); |
- put("pc_code", new V1_0.searchResult.valueDescription("", "Pesticide chemical code")); |
- put("ai_name", new V1_0.searchResult.valueDescription("", "Active ingredient name")); |
- put("pc_pct", new V1_0.searchResult.valueDescription("", "Active ingredient percentage")); |
+ put("reg_no", new SearchResult.valueDescription("", "EPA product registration number (EPA Reg. No.)")); |
+ put("prod_name", new SearchResult.valueDescription("", "Pesticide product name")); |
+ put("type_desc", new SearchResult.valueDescription("", "Pesticide product type)")); |
+ put("pc_code", new SearchResult.valueDescription("", "Pesticide chemical code")); |
+ put("ai_name", new SearchResult.valueDescription("", "Active ingredient name")); |
+ put("pc_pct", new SearchResult.valueDescription("", "Active ingredient percentage")); |
} |
}; |
|
- searchResult(Map<String, String> dbResults) { |
- V1_0.searchResult.valueDescription resultValue; |
+ SearchResult(Map<String, String> dbResults) { |
+ SearchResult.valueDescription resultValue; |
|
for (Map.Entry<String, String> result : dbResults.entrySet()) { |
- resultValue = this.results.get(result.getKey()); |
+ resultValue = results.get(result.getKey()); |
if (null != resultValue) { |
resultValue.value = result.getValue(); |
} |
@@ -152,7 +140,7 @@ |
public JSONArray getResultArray() throws JSONException { |
JSONArray ret_val = new JSONArray(); |
|
- for (Map.Entry<String, V1_0.searchResult.valueDescription> result : this.results.entrySet()) { |
+ for (Map.Entry<String, SearchResult.valueDescription> result : results.entrySet()) { |
ret_val.put(JSONUtils.dataDesc(result.getKey(), result.getValue().value, result.getValue().description)); |
} |
|
@@ -172,61 +160,59 @@ |
|
} |
|
- protected static class searchFilter { |
+ protected static class SearchFilter { |
|
- private V1_0.searchFilter.columnName filterName; |
+ private SearchFilter.columnName filterName; |
private String searchValue; |
private boolean beginsWith; |
private boolean contains; |
private boolean endsWith; |
- private String error_msg; |
- |
- searchFilter(JSONArray filterArray) { |
- this.filterName = new V1_0.searchFilter.columnName(); |
+ |
+ SearchFilter(JSONArray filterArray) throws ServiceException { |
+ this.filterName = new SearchFilter.columnName(); |
this.searchValue = ""; |
this.beginsWith = false; |
this.contains = false; |
this.endsWith = false; |
- this.error_msg = ""; |
- |
+ |
if (null != filterArray) { |
try { |
Map<String, JSONObject> filterMap = JSONUtils.preprocess(filterArray); |
if (filterMap.containsKey("pp_filter_name")) { |
- this.filterName.setName(filterMap.get("pp_filter_name").getString("value")); |
+ filterName.setName(filterMap.get("pp_filter_name").getString("value")); |
if (filterMap.containsKey("pp_filter_value")) { |
- this.searchValue = filterMap.get("pp_filter_value").getString("value"); |
+ searchValue = filterMap.get("pp_filter_value").getString("value"); |
if (filterMap.containsKey("begins_with")) { |
- this.beginsWith = filterMap.get("begins_with").getBoolean("value"); |
+ beginsWith = filterMap.get("begins_with").getBoolean("value"); |
if (filterMap.containsKey("contains")) { |
- this.contains = filterMap.get("contains").getBoolean("value"); |
+ contains = filterMap.get("contains").getBoolean("value"); |
if (filterMap.containsKey("ends_with")) { |
- this.endsWith = filterMap.get("ends_with").getBoolean("value"); |
+ endsWith = filterMap.get("ends_with").getBoolean("value"); |
} else { |
- this.error_msg = "Missing 'ends_with' parameter for this filter request. Cannot proceed."; |
+ throw new ServiceException("Missing 'ends_with' parameter for this filter request. Cannot proceed."); |
} |
} else { |
- this.error_msg = "Missing 'contains' parameter for this filter request. Cannot proceed."; |
+ throw new ServiceException("Missing 'contains' parameter for this filter request. Cannot proceed."); |
} |
} else { |
- this.error_msg = "Missing 'begins_with' parameter for this filter request. Cannot proceed."; |
+ throw new ServiceException("Missing 'begins_with' parameter for this filter request. Cannot proceed."); |
} |
} else { |
- this.error_msg = "Missing 'pp_filter_value' parameter for this filter request. Cannot proceed."; |
+ throw new ServiceException("Missing 'pp_filter_value' parameter for this filter request. Cannot proceed."); |
} |
} else { |
- this.error_msg = "Missing 'pp_filter_name' parameter for this filter request. Cannot proceed."; |
+ throw new ServiceException("Missing 'pp_filter_name' parameter for this filter request. Cannot proceed."); |
} |
|
} catch (JSONException ex) { |
- this.error_msg = "Cannot process the input request for this filter: " + ex.getMessage(); |
+ throw new ServiceException("JSONException",ex); |
} |
} else { |
- this.error_msg = "No input values for the filter. Cannot proceed."; |
+ throw new ServiceException("No input values for the filter. Cannot proceed."); |
} |
|
- if (!this.filterName.isValid()) { |
- this.error_msg = "Filter name specified is not a valid column name. Cannot proceed."; |
+ if (!filterName.isValid()) { |
+ throw new ServiceException("Filter name specified is not a valid column name. Cannot proceed."); |
} |
} |
|
@@ -234,27 +220,27 @@ |
String ret_val = "( "; |
int counter = 0; |
|
- if (this.filterName.isValid() && (this.beginsWith || this.contains || this.endsWith)) { |
- ret_val += this.filterName.getName() + " LIKE "; |
+ if (filterName.isValid() && (beginsWith || contains || endsWith)) { |
+ ret_val += filterName.getName() + " LIKE "; |
|
- if (this.beginsWith) { |
- ret_val += " '" + this.searchValue + "%' "; |
+ if (beginsWith) { |
+ ret_val += " '" + searchValue + "%' "; |
counter++; |
} |
|
- if (this.contains) { |
+ if (contains) { |
if (counter > 0) { |
- ret_val += " OR " + this.filterName.getName() + " LIKE "; |
+ ret_val += " OR " + filterName.getName() + " LIKE "; |
} |
- ret_val += " '%" + this.searchValue + "%' "; |
+ ret_val += " '%" + searchValue + "%' "; |
counter++; |
} |
|
- if (this.endsWith) { |
+ if (endsWith) { |
if (counter > 0) { |
- ret_val += " OR " + this.filterName.getName() + " LIKE "; |
+ ret_val += " OR " + filterName.getName() + " LIKE "; |
} |
- ret_val += " '%" + this.searchValue + "' "; |
+ ret_val += " '%" + searchValue + "' "; |
counter++; |
} |
|
@@ -267,13 +253,7 @@ |
return ret_val; |
} |
|
- boolean getError() { |
- return !this.error_msg.isEmpty(); |
- } |
- |
- String getErrorMsg() { |
- return this.error_msg; |
- } |
+ |
|
// Since the column names used in the filter are constrained, this class will help |
// ensure that the contraints are adhered to. |
@@ -283,7 +263,7 @@ |
private int nameIdx; |
|
columnName() { |
- this.nameIdx = -1; |
+ nameIdx = -1; |
} |
|
public boolean setName(String cName) { |
@@ -294,13 +274,13 @@ |
break; |
} |
} |
- return this.isValid(); |
+ return isValid(); |
} |
|
public String getName() { |
String ret_val = ""; |
if ((nameIdx >= 0) && (nameIdx <= 5)) { |
- ret_val = this.names[this.nameIdx]; |
+ ret_val = names[nameIdx]; |
} |
return ret_val; |
} |