# HG changeset patch
# User Asier Lostalé <asier.lostale@openbravo.com>
# Date 1443439065 -7200
#      Mon Sep 28 13:17:45 2015 +0200
# Node ID 9e982b12d0c63cc3d443f2785fe2c535a8963e73
# Parent  f54b308139b3eafa671d15a592ec8d31922f2b4e
fixed bug 30948: auxiliary input to default a FK doesn't display identifier

  When an auxiliary input is used to default a FK value, its value was correctly
  calculated but it's identifier was not, this made the UI not to display the
  identifier on NEW, but to correctly set it being visible after save.

  This has been fixed by detecting this special case, when in this situation:

    - Value is calculated in computeAuxiliaryInputs method, including also its
      identifier (obtained from field's ui defintion)
    - Value is not calculated in computeColumnValues method because it has already
      been computed

diff -r f54b308139b3 -r 9e982b12d0c6 modules/org.openbravo.client.application/src/org/openbravo/client/application/window/FormInitializationComponent.java
--- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/FormInitializationComponent.java	Mon Sep 28 09:01:38 2015 +0200
+++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/window/FormInitializationComponent.java	Mon Sep 28 13:17:45 2015 +0200
@@ -623,6 +623,13 @@
     }
     List<String> changedCols = new ArrayList<String>();
     for (String col : allColumns) {
+      if (mode.equals("NEW") && containsIgnoreCase(getAuxiliaryInputNamesList(tab.getId()), col)) {
+        // creating a new record, there is an auxiliary input that has the same name than the
+        // field's column, in this case auxiliary input is used to calculate the default, so there
+        // is no need of calculating it here as it will be done in computeAuxiliaryInputs
+        continue;
+      }
+
       checkNamingCollisionWithAuxiliaryInput(tab, col);
       Field field = columnsOfFields.get(col);
       try {
@@ -932,10 +939,38 @@
       } catch (JSONException e) {
         log.error("Error while computing auxiliary input " + auxIn.getName(), e);
       }
+
       columnValues.put("inp" + Sqlc.TransformaNombreColumna(auxIn.getName()), jsonObj);
       RequestContext.get().setRequestParameter(
           "inp" + Sqlc.TransformaNombreColumna(auxIn.getName()),
           value == null || value.equals("null") ? null : value.toString());
+
+      if (mode.equals("NEW") && containsIgnoreCase(allColumns, auxIn.getName())) {
+        // auxiliary input used to calculate default value for a field, let's obtain the complete
+        // value from ui definition in order to obtain also its identifier
+        try {
+          Field field = null;
+          for (Field f : getADFieldList(tab.getId())) {
+            if (f.getColumn() != null
+                && auxIn.getName().equalsIgnoreCase(f.getColumn().getDBColumnName())) {
+              field = f;
+              break;
+            }
+          }
+          if (field != null) {
+            String columnId = field.getColumn().getId();
+            UIDefinition uiDef = UIDefinitionController.getInstance().getUIDefinition(columnId);
+            JSONObject jsonDefinition = new JSONObject(uiDef.getFieldProperties(field, true));
+            if (jsonDefinition.has("identifier")) {
+              jsonObj.put("identifier", jsonDefinition.get("identifier"));
+            }
+          }
+        } catch (Exception e) {
+          log.error("Error trying to calculate identifier for auxiliary input tab " + tab
+              + " aux input " + auxIn, e);
+        }
+      }
+
       // Now we insert session values for auxiliary inputs
       if (mode.equals("NEW") || mode.equals("EDIT") || mode.equals("SETSESSION")) {
         setSessionValue(tab.getWindow().getId() + "|" + auxIn.getName(), value);
@@ -1893,6 +1928,14 @@
     return cachedStructures.getAuxiliarInputList(tabId);
   }
 
+  private List<String> getAuxiliaryInputNamesList(String tabId) {
+    List<String> result = new ArrayList<String>();
+    for (AuxiliaryInput ai : cachedStructures.getAuxiliarInputList(tabId)) {
+      result.add(ai.getName());
+    }
+    return result;
+  }
+
   private String readParameter(Map<String, Object> parameters, String parameterName) {
     String paramValue = (String) parameters.get(parameterName);
     if (paramValue != null && paramValue.equalsIgnoreCase("null")) {
