related to issue 16464: Handle translations in grid
authorAsier Lostalé <asier.lostale@openbravo.com>
Thu, 21 Jul 2011 10:22:13 +0200
changeset 13227 f8b10899ee01
parent 13226 f7319ac66cd2
child 13228 661e642807ed
related to issue 16464: Handle translations in grid

Using Collection filtering instead of Criteria to improve performance.
src/org/openbravo/base/model/ModelProvider.java
src/org/openbravo/base/model/Property.java
src/org/openbravo/base/structure/BaseOBObject.java
--- a/src/org/openbravo/base/model/ModelProvider.java	Mon Jul 18 12:27:31 2011 +0200
+++ b/src/org/openbravo/base/model/ModelProvider.java	Thu Jul 21 10:22:13 2011 +0200
@@ -230,8 +230,10 @@
 
       // in the second pass set all the referenceProperties
       // and targetEntities
-      // uses global member tablesByTableName
-      setReferenceProperties();
+      // uses global member tablesByTableName.
+      // Obtains list of columns candidate to be translated, to be handled after setting properties
+      // in parent entities.
+      List<Column> translatableColumns = setReferenceProperties();
 
       // add virtual property for the case that the
       // id property is also a reference (a foreign key)
@@ -281,6 +283,9 @@
           }
         }
       }
+
+      setTranslatableColumns(translatableColumns);
+
     } finally {
       log.debug("Closing session and sessionfactory used during model read");
       tx.commit();
@@ -290,6 +295,19 @@
     clearLists();
   }
 
+  private void setTranslatableColumns(List<Column> translatableColumns) {
+    for (Column c : translatableColumns) {
+      final Entity translationEntity = getEntityByTableName(c.getTable().getTableName() + "_Trl");
+
+      Property translationProperty = null;
+      if (translationEntity != null) {
+        translationProperty = translationEntity.getPropertyByColumnName(c.getColumnName());
+      }
+      final Property thisProp = c.getProperty();
+      thisProp.setTranslatable(translationProperty);
+    }
+  }
+
   /**
    * This method uses a normal JDBC connection to retrieve the classes of the references. These
    * classes will be instantiated and if they implement the correct interface, they will be added to
@@ -427,7 +445,7 @@
     }
   }
 
-  private void setReferenceProperties() {
+  private List<Column> setReferenceProperties() {
     log.debug("Setting reference property");
     // uses global member tablesByTableName
 
@@ -474,17 +492,7 @@
       }
     }
 
-    // Looping through all the columns set as translatable, it is required another loop because properties set in the previous one are used now.
-    for (Column c : translatableColumns) {
-      final Entity translationEntity = getEntityByTableName(c.getTable().getTableName() + "_Trl");
-
-      Property translationProperty = null;
-      if (translationEntity != null) {
-        translationProperty = translationEntity.getPropertyByColumnName(c.getColumnName());
-      }
-      final Property thisProp = c.getProperty();
-      thisProp.setTranslatable(translationProperty);
-    }
+    return translatableColumns;
 
   }
 
--- a/src/org/openbravo/base/model/Property.java	Mon Jul 18 12:27:31 2011 +0200
+++ b/src/org/openbravo/base/model/Property.java	Thu Jul 21 10:22:13 2011 +0200
@@ -112,6 +112,7 @@
   private String displayProperty;
 
   private Property trlParentProperty;
+  private Property trlOneToManyProperty;
 
   /**
    * Initializes this Property using the information from the Column.
@@ -1152,6 +1153,7 @@
   void setTranslatable(Property translationProperty) {
     log.debug("Setting translatable for " + this.getEntity().getTableName() + "."
         + this.getColumnName());
+
     if (translationProperty == null) {
       log.warn(this.getEntity().getTableName() + "." + this.getColumnName()
           + " is not translatable: null translationProperty");
@@ -1171,10 +1173,26 @@
       return;
     }
 
+    Property trlPropertyListInBase = null;
+    for (Property p : this.getEntity().getProperties()) {
+      if (p.isOneToMany() && translationProperty.getEntity().equals(p.getTargetEntity())) {
+        trlPropertyListInBase = p;
+        break;
+      }
+    }
+
+    if (trlPropertyListInBase == null) {
+      translatable = false;
+      log.warn(this.getEntity().getTableName() + "." + this.getColumnName()
+          + " is not translatable: not found one to many property to trl table");
+      return;
+    }
+
     for (Property trlParent : translationProperty.getEntity().getParentProperties()) {
       if (pk.equals(trlParent.getReferencedProperty())) {
         this.trlParentProperty = trlParent;
         this.translationProperty = translationProperty;
+        this.trlOneToManyProperty = trlPropertyListInBase;
         translatable = true;
         return;
       }
@@ -1190,4 +1208,8 @@
   public Property getTrlParentProperty() {
     return trlParentProperty;
   }
+
+  public Property getTrlOneToManyProperty() {
+    return trlOneToManyProperty;
+  }
 }
--- a/src/org/openbravo/base/structure/BaseOBObject.java	Mon Jul 18 12:27:31 2011 +0200
+++ b/src/org/openbravo/base/structure/BaseOBObject.java	Thu Jul 21 10:22:13 2011 +0200
@@ -20,8 +20,8 @@
 package org.openbravo.base.structure;
 
 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,7 +33,6 @@
 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;
 
@@ -123,19 +122,18 @@
     if (p.isTranslatable()) {
       if (!hasLookedForTrl) {
         hasLookedForTrl = true;
-        OBContext.setAdminMode(false);
         try {
-          OBCriteria<BaseOBObject> qTrl = OBDal.getInstance().createCriteria(
-              p.getTranslationProperty().getEntity().getName());
-          String id = (String) getId();
-          qTrl.add(Restrictions.eq(p.getTrlParentProperty().getName() + ".id", id));
-          qTrl.add(Restrictions.eq("language", language));
-          if (!qTrl.list().isEmpty()) {
-            // Assuming there is just one translation for the current language
-            dataTrl = qTrl.list().get(0);
+          @SuppressWarnings("unchecked")
+          List<BaseOBObject> trl = OBDal.getInstance().getSession()
+              .createFilter(this.get(p.getTrlOneToManyProperty().getName()), "where language = ?")
+              .setParameter(0, language).list();
+
+          if (!trl.isEmpty()) {
+            dataTrl = trl.get(0);
           }
-        } finally {
-          OBContext.restorePreviousMode();
+        } 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);
         }
       }