Fixes Issue 22836 : UOM values not translated in Product window
authorShankar Balachandran <shankar.balachandran@openbravo.com>
Mon, 04 Mar 2013 17:44:57 +0530
changeset 19861 f9728bac00aa
parent 19860 a60624fb0f40
child 19862 5dab33c3ffca
Fixes Issue 22836 : UOM values not translated in Product window

When translating identifiers, the value list was retrieved from Session.
Added code to check in the object in case it is not present in the Session.
src/org/openbravo/base/structure/BaseOBObject.java
src/org/openbravo/base/structure/IdentifierProvider.java
--- a/src/org/openbravo/base/structure/BaseOBObject.java	Tue Mar 05 12:53:11 2013 +0100
+++ b/src/org/openbravo/base/structure/BaseOBObject.java	Mon Mar 04 17:44:57 2013 +0530
@@ -22,6 +22,7 @@
 import java.io.Serializable;
 import java.util.List;
 
+import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.exception.OBSecurityException;
 import org.openbravo.base.model.BaseOBObjectDef;
 import org.openbravo.base.model.Entity;
@@ -33,6 +34,7 @@
 import org.openbravo.base.validation.ValidationException;
 import org.openbravo.dal.core.OBContext;
 import org.openbravo.dal.core.OBInterceptor;
+import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.model.ad.system.Language;
 
@@ -113,7 +115,7 @@
     return data[p.getIndexInEntity()];
   }
 
-  private Object getDataValue(Property p, Language language) {
+  private Object getDataValue(Property p, Language language, String id) {
     if (data == null) {
       // nothing set in this case anyway
       return null;
@@ -132,8 +134,28 @@
             dataTrl = trl.get(0);
           }
         } catch (Throwable t) {
-          // Log error but do not fail here, continue using base language
-          log.debug("Error looking for translation of " + p + ". Using base value.", t);
+          // Log error but do not fail here
+          log.debug("Error looking for translation of " + p + " in Session. Looking in Object.", t);
+
+          try {
+            if (id != null) {
+              // check whether translation is available in the object and not stored in session
+              OBCriteria<BaseOBObject> trlList = OBDal.getInstance().createCriteria(
+                  p.getEntity().getName() + "Trl");
+              String parentProperty = p.getTrlOneToManyProperty().getName();
+              parentProperty = parentProperty.replace("TrlList", "");
+              trlList.add(Restrictions.eq(parentProperty,
+                  OBDal.getInstance().get(p.getEntity().toString(), id)));
+              trlList.add(Restrictions.eq("language", language));
+              if (trlList.count() > 0) {
+                dataTrl = trlList.list().get(0);
+              }
+            }
+          } catch (Throwable t1) {
+            // continue using base language
+            log.debug("Error looking for translation of " + p + ". Using base value", t);
+          }
+
         }
       }
 
@@ -203,10 +225,32 @@
    *           in case property is not readable
    */
   public Object get(String propName, Language language) {
+    return get(propName, language, null);
+  }
+
+  /**
+   * Returns the value of the {@link Property Property} identified by the propName translating it,
+   * if possible, to the language. In case the translated value is not in session, it will look for
+   * the same in the object. This method does security checking. If a security violation occurs then
+   * a OBSecurityException is thrown.
+   * 
+   * @see BaseOBObject#get(String)
+   * 
+   * @param propName
+   *          the name of the {@link Property Property} for which the value is requested
+   * @param language
+   *          language to translate to
+   * @param id
+   *          uuid of the referenced object
+   * @return value of the property
+   * @throws OBSecurityException
+   *           in case property is not readable
+   */
+  public Object get(String propName, Language language, String id) {
     final Property p = getEntity().getProperty(propName);
     checkDerivedReadable(p);
     if (language != null) {
-      return getDataValue(p, language);
+      return getDataValue(p, language, id);
     } else {
       return getDataValue(p);
     }
--- a/src/org/openbravo/base/structure/IdentifierProvider.java	Tue Mar 05 12:53:11 2013 +0100
+++ b/src/org/openbravo/base/structure/IdentifierProvider.java	Mon Mar 04 17:44:57 2013 +0530
@@ -118,7 +118,7 @@
         // Assign displayColumnProperty to apply formatting if needed
         property = displayColumnProperty;
       } else if (property.isTranslatable()) {
-        value = ((BaseOBObject) dob).get(identifier.getName(), language);
+        value = ((BaseOBObject) dob).get(identifier.getName(), language, (String) dob.get("id"));
       } else if (!property.isPrimitive() && identifyDeep) {
         if (dob.get(property.getName()) != null) {
           value = ((BaseOBObject) dob.get(property.getName())).getIdentifier();