details:   https://code.openbravo.com/erp/devel/pi/rev/4eaae49d7b7e
changeset: 30328:4eaae49d7b7e
user:      Inigo Sanchez <inigo.sanchez <at> openbravo.com>
date:      Wed Sep 28 15:50:27 2016 +0200
summary:   Related to issue 34043: Code review improvements

-Improve comments.
-It is take into account special case in addSelect() method: Combo not defined 
properly.

diffstat:

 src/org/openbravo/erpCommon/ad_callouts/SimpleCallout.java |  35 ++++++++-----
 1 files changed, 21 insertions(+), 14 deletions(-)

diffs (107 lines):

diff -r fb555f365d96 -r 4eaae49d7b7e 
src/org/openbravo/erpCommon/ad_callouts/SimpleCallout.java
--- a/src/org/openbravo/erpCommon/ad_callouts/SimpleCallout.java        Tue Sep 
27 15:39:11 2016 +0200
+++ b/src/org/openbravo/erpCommon/ad_callouts/SimpleCallout.java        Wed Sep 
28 15:50:27 2016 +0200
@@ -29,6 +29,7 @@
 import org.codehaus.jettison.json.JSONArray;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
+import org.openbravo.base.exception.OBException;
 import org.openbravo.base.filter.IsIDFilter;
 import org.openbravo.base.filter.RegexFilter;
 import org.openbravo.base.filter.RequestFilter;
@@ -196,7 +197,7 @@
     }
 
     /**
-     * Starts the inclusion of values of a field named param of type select.
+     * Starts the inclusion of values to the combo field with the name passed 
as parameter.
      * 
      * @param param
      *          The name of the combo field to set the values.
@@ -204,19 +205,26 @@
     public void addSelect(String param) {
       if (result.has(param)) {
         try {
-          currentComboResult = 
getComboMap(result.getJSONObject(param).getJSONArray(
-              CalloutConstants.ENTRIES));
+          currentComboResult = getComboMap(result.getJSONObject(param));
         } catch (JSONException e) {
-          log.error("Error getting JSON Object.", e);
+          log.error("Error retrieving combo entries for field " + param, e);
         }
       } else {
         currentComboResult = new LinkedHashMap<String, String>();
       }
 
+      if (currentComboResult == null) {
+        throw new OBException("Could not retrieve entries for combo field with 
name " + param);
+      }
+
       currentElement = param;
     }
 
-    private Map<String, String> getComboMap(JSONArray entries) throws 
JSONException {
+    private Map<String, String> getComboMap(JSONObject comboField) throws 
JSONException {
+      if (!comboField.has(CalloutConstants.ENTRIES)) {
+        return null;
+      }
+      JSONArray entries = comboField.getJSONArray(CalloutConstants.ENTRIES);
       Map<String, String> comboMap = new LinkedHashMap<String, String>();
       for (int i = 0; i < entries.length(); i++) {
         JSONObject item = entries.getJSONObject(i);
@@ -240,22 +248,22 @@
     }
 
     /**
-     * Removes an entry to the current combo.
+     * Removes an entry of the combo field.
      * 
      * @param id
-     *          The id value of the combo to remove the entry.
+     *          The id of the combo entry to be removed.
      */
     public void removeSelectResult(String id) {
       currentComboResult.remove(id);
     }
 
     /**
-     * Adds an entry value to the select field.
+     * Adds an entry value to the combo field.
      * 
      * @param id
-     *          The entry id to add.
+     *          The id of the combo entry to add.
      * @param identifier
-     *          The entry identifier to add.
+     *          The identifier of the combo entry to add
      * @param selected
      *          Whether this entry field is selected or not.
      */
@@ -270,7 +278,7 @@
           result.put(currentElement, valueSelected);
         }
       } catch (JSONException e) {
-        log.error("Error parsing JSON Object.", e);
+        log.error("Error adding combo entry for combo field " + 
currentElement, e);
       }
     }
 
@@ -279,8 +287,7 @@
      */
     public void endSelect() {
       try {
-        // If any entry is marked as selected for currentElement combo
-        if (isValueNotSelectedForCombo()) {
+        if (hasSelectedComboEntry()) {
           JSONObject notSelectedItem = new JSONObject();
           notSelectedItem.put(CalloutConstants.ENTRIES, getComboEntries());
           result.put(currentElement, notSelectedItem);
@@ -292,7 +299,7 @@
       }
     }
 
-    private boolean isValueNotSelectedForCombo() throws JSONException {
+    private boolean hasSelectedComboEntry() throws JSONException {
       return (result.isNull(currentElement) ? true : 
!result.getJSONObject(currentElement).has(
           CalloutConstants.VALUE));
     }

------------------------------------------------------------------------------
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to