Related with bug 25811: Show warn message if bad datasource request is issued
authorAugusto Mauch <augusto.mauch@openbravo.com>
Wed, 19 Nov 2014 19:04:06 +0100
changeset 25292 c5de0b748930
parent 25291 ef187dad9b4c
child 25293 3221fd5a8661
Related with bug 25811: Show warn message if bad datasource request is issued

Datasource requests that contain the _targetRecordId parameter but not the _directNavigation parameter are known to have bad perfomance. A check has been added in DefaultJsonDataService to detect them.
modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java
--- a/modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java	Wed Nov 19 18:37:44 2014 +0100
+++ b/modules/org.openbravo.service.json/src/org/openbravo/service/json/DefaultJsonDataService.java	Wed Nov 19 19:04:06 2014 +0100
@@ -388,12 +388,8 @@
     if ((StringUtils.isEmpty(startRowStr) || StringUtils.isEmpty(endRowStr))
         && !isIDCriteria(criteria) && !parameters.containsKey("exportAs")) {
       // pagination is not set, this is most likely a bug
-      String paramMsg = "";
-      for (String paramKey : parameters.keySet()) {
-        paramMsg += paramKey + ":" + parameters.get(paramKey) + "\n";
-      }
       log.warn("Fetching data without pagination, this can cause perfomance issues. Parameters: "
-          + paramMsg);
+          + convertParameterToString(parameters));
 
       if (parameters.containsKey(JsonConstants.TAB_PARAMETER)
           || parameters.containsKey(SelectorConstants.DS_REQUEST_SELECTOR_ID_PARAMETER)) {
@@ -407,6 +403,12 @@
         && "true".equals(parameters.get("_directNavigation"))
         && parameters.containsKey(JsonConstants.TARGETRECORDID_PARAMETER);
 
+    if (parameters.containsKey(JsonConstants.TARGETRECORDID_PARAMETER)
+        && !"true".equals(parameters.get("_directNavigation"))) {
+      log.warn("Datasource request with targetRecordId but without directNavigation detected. This type of requests should be avoided because they result in a query that performs poorly. Parameters: "
+          + convertParameterToString(parameters));
+    }
+
     if (!directNavigation) {
       // set the where/org filter parameters and the @ parameters
       for (String key : parameters.keySet()) {
@@ -499,6 +501,15 @@
     return queryService;
   }
 
+  // Given a map of parameters, returns a string with the pairs key:value
+  private String convertParameterToString(Map<String, String> parameters) {
+    String paramMsg = "";
+    for (String paramKey : parameters.keySet()) {
+      paramMsg += paramKey + ":" + parameters.get(paramKey) + "\n";
+    }
+    return paramMsg;
+  }
+
   private void addWritableAttribute(List<JSONObject> jsonObjects) throws JSONException {
     for (JSONObject jsonObject : jsonObjects) {
       if (!jsonObject.has("client") || !jsonObject.has("organization")) {