You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by nm...@apache.org on 2016/03/19 10:01:40 UTC

svn commit: r1735741 - in /ofbiz/trunk/framework: entity/src/org/ofbiz/entity/model/ModelUtil.java entity/src/org/ofbiz/entity/util/EntityUtil.java widget/src/org/ofbiz/widget/model/ModelFormField.java

Author: nmalin
Date: Sat Mar 19 09:01:40 2016
New Revision: 1735741

URL: http://svn.apache.org/viewvc?rev=1735741&view=rev
Log:
For screen engine drop-down, sort with the locale the entity-list if the related entity have a default resource defined.

This work also with view entity. Relate issue OFBIZ-3311

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelUtil.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityUtil.java
    ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormField.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelUtil.java?rev=1735741&r1=1735740&r2=1735741&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelUtil.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/model/ModelUtil.java Sat Mar 19 09:01:40 2016
@@ -19,8 +19,16 @@
 package org.ofbiz.entity.model;
 
 import java.io.File;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
 
 import org.ofbiz.base.util.StringUtil;
+import org.ofbiz.base.util.UtilMisc;
+import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.entity.model.ModelViewEntity.ModelAlias;
 
 /**
  * Generic Entity - General Utilities
@@ -292,4 +300,43 @@ public class ModelUtil {
             return "invalid-" + sqlTypeName + ":" + length + ":" + precision;
         }
     }
+
+    /**
+     * Check is a ModelEntity have a default resource associate to resolve localized value
+     * When the ModelEntity is a ModelViewEntity, check with the field to resolve the related entity
+     * @param modelEntity
+     * @param fieldName
+     * @return
+     */
+    public static boolean isPotentialLocalizedField(ModelEntity modelEntity, String fieldName) {
+        return isPotentialLocalizedFields(modelEntity, UtilMisc.toList(fieldName));
+    }
+
+    /**
+     * Check is a ModelEntity have a default resource associate to resolve localized value
+     * When the ModelEntity is a ModelViewEntity, check with the list fields to resolve these related entities
+     * @param modelEntity
+     * @param fieldName
+     * @return
+     */
+    public static boolean isPotentialLocalizedFields(ModelEntity modelEntity, List<String> fieldNames) {
+        if (modelEntity == null) return false;
+        if (modelEntity instanceof ModelViewEntity) {
+            //  now try to retrieve with the field heading from the real entity linked to the view
+            ModelViewEntity modelViewEntity = (ModelViewEntity) modelEntity;
+            Iterator<ModelAlias> it = modelViewEntity.getAliasesIterator();
+            while (it.hasNext()) {
+                ModelAlias modelAlias = it.next();
+                if (fieldNames.contains(modelAlias.getName())) {
+                    ModelEntity memberModelEntity = modelViewEntity.getMemberModelEntity(modelAlias.getEntityAlias());
+                    if (UtilValidate.isNotEmpty(memberModelEntity.getDefaultResourceName())) {
+                        return true;
+                    }
+                }
+            }
+            return false;
+        } else {
+            return UtilValidate.isNotEmpty(modelEntity.getDefaultResourceName());
+        }
+    }
 }

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityUtil.java?rev=1735741&r1=1735740&r2=1735741&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityUtil.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityUtil.java Sat Mar 19 09:01:40 2016
@@ -21,6 +21,7 @@ package org.ofbiz.entity.util;
 
 import java.io.Serializable;
 import java.sql.Timestamp;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -28,6 +29,7 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 
@@ -333,6 +335,30 @@ public class EntityUtil {
         }
         return result;
     }
+    
+    /**
+     *returns the values in the order specified after with localized value 
+     *
+     *@param values List of GenericValues
+     *@param orderBy The fields of the named entity to order the query by;
+     *      optionally add a " ASC" for ascending or " DESC" for descending
+     *@param locale Locale use to retreive localized value
+     *@return List of GenericValue's in the proper order
+     */
+    public static <T extends GenericEntity> List<T> localizedOrderBy(Collection<T> values, List<String> orderBy, Locale locale) {
+        if (values == null) return null;
+        if (values.isEmpty()) return new ArrayList<>();
+        //force check entity label before order by
+        List<T> localizedValues = new ArrayList<T>();
+        for (T value : values) {
+            T newValue = (T) value.clone();
+            for (String orderByField : orderBy) {
+                newValue.put(orderByField, value.get(orderByField, locale));
+            }
+            localizedValues.add(newValue);
+        }
+        return orderBy(localizedValues, orderBy);
+    }
 
     /**
      *returns the values in the order specified
@@ -344,14 +370,14 @@ public class EntityUtil {
      */
     public static <T extends GenericEntity> List<T> orderBy(Collection<T> values, List<String> orderBy) {
         if (values == null) return null;
-        if (values.size() == 0) return new LinkedList<T>();
+        if (values.isEmpty()) return new ArrayList<T>();
         if (UtilValidate.isEmpty(orderBy)) {
-            List<T> newList = new LinkedList<T>();
+            List<T> newList = new ArrayList<T>();
             newList.addAll(values);
             return newList;
         }
 
-        List<T> result = new LinkedList<T>();
+        List<T> result = new ArrayList<T>();
         result.addAll(values);
         if (Debug.verboseOn()) Debug.logVerbose("Sorting " + values.size() + " values, orderBy=" + orderBy.toString(), module);
         Collections.sort(result, new OrderByList(orderBy));

Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormField.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormField.java?rev=1735741&r1=1735740&r2=1735741&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormField.java (original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/model/ModelFormField.java Sat Mar 19 09:01:40 2016
@@ -62,6 +62,7 @@ import org.ofbiz.entity.GenericValue;
 import org.ofbiz.entity.condition.EntityCondition;
 import org.ofbiz.entity.finder.EntityFinderUtil;
 import org.ofbiz.entity.model.ModelEntity;
+import org.ofbiz.entity.model.ModelUtil;
 import org.ofbiz.entity.util.EntityUtil;
 import org.ofbiz.widget.WidgetWorker;
 import org.ofbiz.widget.model.CommonWidgetModels.AutoEntityParameters;
@@ -1904,16 +1905,24 @@ public class ModelFormField {
 
             try {
                 Locale locale = UtilMisc.ensureLocale(context.get("locale"));
+                ModelEntity modelEntity = delegator.getModelEntity(this.entityName);
+                Boolean localizedOrderBy = UtilValidate.isNotEmpty(this.orderByList)
+                        && ModelUtil.isPotentialLocalizedFields(modelEntity, this.orderByList);
 
                 List<GenericValue> values = null;
-                values = delegator.findList(this.entityName, findCondition, null, this.orderByList, null, this.cache);
+                if (!localizedOrderBy) {
+                    values = delegator.findList(this.entityName, findCondition, null, this.orderByList, null, this.cache);
+                } else {
+                    //if entity has localized label 
+                    values = delegator.findList(this.entityName, findCondition, null, null, null, this.cache);
+                    values = EntityUtil.localizedOrderBy(values, this.orderByList, locale);
+                }
 
                 // filter-by-date if requested
                 if ("true".equals(this.filterByDate)) {
                     values = EntityUtil.filterByDate(values, true);
                 } else if (!"false".equals(this.filterByDate)) {
                     // not explicitly true or false, check to see if has fromDate and thruDate, if so do the filter
-                    ModelEntity modelEntity = delegator.getModelEntity(this.entityName);
                     if (modelEntity != null && modelEntity.isField("fromDate") && modelEntity.isField("thruDate")) {
                         values = EntityUtil.filterByDate(values, true);
                     }