svn commit: r1738900 - in /ofbiz/trunk: applications/order/src/org/ofbiz/order/order/ framework/base/src/org/ofbiz/base/util/collections/ framework/entity/src/org/ofbiz/entity/util/ framework/widget/src/org/ofbiz/widget/renderer/

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1738900 - in /ofbiz/trunk: applications/order/src/org/ofbiz/order/order/ framework/base/src/org/ofbiz/base/util/collections/ framework/entity/src/org/ofbiz/entity/util/ framework/widget/src/org/ofbiz/widget/renderer/

jleroux@apache.org
Author: jleroux
Date: Wed Apr 13 07:29:11 2016
New Revision: 1738900

URL: http://svn.apache.org/viewvc?rev=1738900&view=rev
Log:
A patch from Gareth Carter for "some files set viewIndex to 1 by default rather than 0" https://issues.apache.org/jira/browse/OFBIZ-6705

Implements a PagedList object (stores list of data items, resultset size, viewSize, viewIndex, lowIndex (called startIndex) and highIndex (called endIndex), also a utils class that returns the PagedList object based on a EntityListIterator, viewSize and viewIndex.
And implements a queryPagedList method in EntityQuery, used as an example in findOrders service Java implementation

Added:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/PagedList.java   (with props)
Modified:
    ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderLookupServices.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityQuery.java
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityUtil.java
    ofbiz/trunk/framework/widget/src/org/ofbiz/widget/renderer/Paginator.java

Modified: ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderLookupServices.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderLookupServices.java?rev=1738900&r1=1738899&r2=1738900&view=diff
==============================================================================
--- ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderLookupServices.java (original)
+++ ofbiz/trunk/applications/order/src/org/ofbiz/order/order/OrderLookupServices.java Wed Apr 13 07:29:11 2016
@@ -35,6 +35,7 @@ import org.ofbiz.base.util.UtilGenerics;
 import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.base.util.UtilProperties;
 import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.collections.PagedList;
 import org.ofbiz.entity.Delegator;
 import org.ofbiz.entity.GenericEntityException;
 import org.ofbiz.entity.GenericValue;
@@ -45,13 +46,13 @@ import org.ofbiz.entity.condition.Entity
 import org.ofbiz.entity.condition.EntityOperator;
 import org.ofbiz.entity.model.DynamicViewEntity;
 import org.ofbiz.entity.model.ModelKeyMap;
-import org.ofbiz.entity.util.EntityListIterator;
 import org.ofbiz.entity.util.EntityQuery;
 import org.ofbiz.security.Security;
 import org.ofbiz.service.DispatchContext;
 import org.ofbiz.service.GenericServiceException;
 import org.ofbiz.service.LocalDispatcher;
 import org.ofbiz.service.ServiceUtil;
+import org.ofbiz.widget.renderer.Paginator;
 
 /**
  * OrderLookupServices
@@ -66,10 +67,9 @@ public class OrderLookupServices {
         Security security = dctx.getSecurity();
 
         GenericValue userLogin = (GenericValue) context.get("userLogin");
-        Integer viewIndex = (Integer) context.get("viewIndex");
-        if (viewIndex == null) viewIndex = 1;
-        Integer viewSize = (Integer) context.get("viewSize");
-        if (viewSize == null) viewSize = UtilProperties.getPropertyAsInteger("widget", "widget.form.defaultViewSize", 20);
+        Integer viewIndex = Paginator.getViewIndex(context, "viewIndex", 1);
+        Integer viewSize = Paginator.getViewSize(context, "viewSize");
+
         String showAll = (String) context.get("showAll");
         String useEntryDate = (String) context.get("useEntryDate");
         Locale locale = (Locale) context.get("locale");
@@ -582,47 +582,29 @@ public class OrderLookupServices {
         int orderCount = 0;
 
         // get the index for the partial list
-        int lowIndex = (((viewIndex.intValue() - 1) * viewSize.intValue()) + 1);
-        int highIndex = viewIndex.intValue() * viewSize.intValue();
+        int lowIndex = 0;
+        int highIndex = 0;
 
         if (cond != null) {
-            EntityListIterator eli = null;
+            PagedList<GenericValue> pagedOrderList = null;
             try {
                 // do the lookup
-                eli = EntityQuery.use(delegator)
+                pagedOrderList = EntityQuery.use(delegator)
                         .select(fieldsToSelect)
                         .from(dve)
                         .where(cond)
                         .orderBy(orderBy)
                         .distinct() // set distinct on so we only get one row per order
-                        .maxRows(highIndex)
                         .cursorScrollInsensitive()
-                        .queryIterator();
-
-                orderCount = eli.getResultsSizeAfterPartialList();
-
-                // get the partial list for this page
-                eli.beforeFirst();
-                if (orderCount > viewSize.intValue()) {
-                    orderList = eli.getPartialList(lowIndex, viewSize.intValue());
-                } else if (orderCount > 0) {
-                    orderList = eli.getCompleteList();
-                }
+                        .queryPagedList(viewIndex - 1, viewSize);
 
-                if (highIndex > orderCount) {
-                    highIndex = orderCount;
-                }
+                orderCount = pagedOrderList.getSize();
+                lowIndex = pagedOrderList.getStartIndex();
+                highIndex = pagedOrderList.getEndIndex();
+                orderList = pagedOrderList.getData();
             } catch (GenericEntityException e) {
                 Debug.logError(e, module);
                 return ServiceUtil.returnError(e.getMessage());
-            } finally {
-                if (eli != null) {
-                    try {
-                        eli.close();
-                    } catch (GenericEntityException e) {
-                        Debug.logWarning(e, e.getMessage(), module);
-                    }
-                }
             }
         }
 

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/PagedList.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/PagedList.java?rev=1738900&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/PagedList.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/PagedList.java Wed Apr 13 07:29:11 2016
@@ -0,0 +1,100 @@
+package org.ofbiz.base.util.collections;
+
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * Stores the result of a subset of data items from a source (such
+ * as EntityListIterator).
+ */
+public class PagedList<E> implements Iterable<E> {
+
+    protected int startIndex;
+    protected int endIndex;
+    protected int size;
+    protected int viewIndex;
+    protected int viewSize;
+    protected List<E> data;
+
+    /**
+     * Default constructor - populates all fields in this class
+     * @param startIndex
+     * @param endIndex
+     * @param size
+     * @param viewIndex
+     * @param viewSize
+     * @param data
+     */
+    public PagedList(int startIndex, int endIndex, int size, int viewIndex, int viewSize, List<E> data) {
+        this.startIndex = startIndex;
+        this.endIndex = endIndex;
+        this.size = size;
+        this.viewIndex = viewIndex;
+        this.viewSize = viewSize;
+        this.data = data;
+    }
+
+    /**
+     * @param viewIndex
+     * @param viewSize
+     * @return an empty PagedList object
+     */
+    public static <E> PagedList<E> empty(int viewIndex, int viewSize) {
+        List<E> emptyList = Collections.emptyList();
+        return new PagedList<E>(0, 0, 0, viewIndex, viewSize, emptyList);
+    }
+
+    /**
+     * @return the start index (for paginator) or known as low index
+     */
+    public int getStartIndex() {
+        return startIndex;
+    }
+
+    /**
+     * @return the end index (for paginator) or known as high index
+     */
+    public int getEndIndex() {
+        return endIndex;
+    }
+
+    /**
+     * @return the size of the full list, this can be the
+     * result of <code>EntityListIterator.getResultsSizeAfterPartialList()</code>
+     */
+    public int getSize() {
+        return size;
+    }
+
+    /**
+     * @return the paged data. Eg - the result from <code>EntityListIterator.getPartialList()</code>
+     */
+    public List<E> getData() {
+        return data;
+    }
+
+    /**
+     * @return the view index supplied by client
+     */
+    public int getViewIndex() {
+        return viewIndex;
+    }
+
+    /**
+     * @return the view size supplied by client
+     */
+    public int getViewSize() {
+        return viewSize;
+    }
+
+    /**
+     * @return an interator object over the data returned in getData() method
+     *         of this class
+     */
+    @Override
+    public Iterator<E> iterator() {
+        return this.data.iterator();
+    }
+
+}

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/PagedList.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/PagedList.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/collections/PagedList.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityQuery.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityQuery.java?rev=1738900&r1=1738899&r2=1738900&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityQuery.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/EntityQuery.java Wed Apr 13 07:29:11 2016
@@ -32,6 +32,7 @@ import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilGenerics;
 import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.collections.PagedList;
 import org.ofbiz.entity.Delegator;
 import org.ofbiz.entity.GenericEntityException;
 import org.ofbiz.entity.GenericValue;
@@ -543,4 +544,25 @@ public class EntityQuery {
             }
         }
     }
+
+    /**
+     * @param viewIndex
+     * @param viewSize
+     * @return PagedList object with a subset of data items
+     * @throws GenericEntityException
+     * @see EntityUtil#getPagedList
+     */
+    public PagedList<GenericValue> queryPagedList(final int viewIndex, final int viewSize) throws GenericEntityException {
+        EntityListIterator genericValueEli = null;
+        try {
+            genericValueEli = queryIterator();
+            return EntityUtil.getPagedList(genericValueEli, viewIndex, viewSize);
+        }
+        finally {
+            if (genericValueEli != null) {
+                genericValueEli.close();
+            }
+        }
+    }
+
 }

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=1738900&r1=1738899&r2=1738900&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 Wed Apr 13 07:29:11 2016
@@ -39,6 +39,7 @@ import org.ofbiz.base.util.UtilGenerics;
 import org.ofbiz.base.util.UtilMisc;
 import org.ofbiz.base.util.UtilProperties;
 import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.collections.PagedList;
 import org.ofbiz.entity.Delegator;
 import org.ofbiz.entity.GenericEntity;
 import org.ofbiz.entity.GenericEntityException;
@@ -561,4 +562,43 @@ public class EntityUtil {
     public static boolean isMultiTenantEnabled() {
         return "Y".equalsIgnoreCase(UtilProperties.getPropertyValue("general", "multitenant"));
     }
+
+    /**
+     * @param viewIndex
+     * @param viewSize
+     * @return the calculated start index based on viewIndex and viewSize
+     * @see EntityUtil#getPagedList
+     */
+    public static int getStartIndexFromViewIndex(int viewIndex, int viewSize) {
+        if (viewIndex == 0) {
+            return 1;
+        }
+        return (viewIndex * viewSize) + 1;
+    }
+
+    /**
+     * @param iter EntityListIterator
+     * @param viewIndex
+     * @param viewSize
+     * @return PagedList object with a subset of data items from EntityListIterator based on viewIndex and viewSize
+     * @throws GenericEntityException
+     * @see org.ofbiz.entity.util.EntityListIterator
+     */
+    public static PagedList<GenericValue> getPagedList(EntityListIterator iter, int viewIndex, int viewSize) throws GenericEntityException {
+        int startIndex = getStartIndexFromViewIndex(viewIndex, viewSize);
+        int endIndex = (startIndex + viewSize) - 1;
+
+        List<GenericValue> dataItems = iter.getPartialList(startIndex, viewSize);
+        if (dataItems.size() < viewIndex) {
+            endIndex = (endIndex - viewSize) + dataItems.size();
+        }
+
+        int size = iter.getResultsSizeAfterPartialList();
+        if (endIndex > size) {
+            endIndex = size;
+        }
+
+        return new PagedList<GenericValue>(startIndex, endIndex, size, viewIndex, viewSize, dataItems);
+    }
+
 }

Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/renderer/Paginator.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/renderer/Paginator.java?rev=1738900&r1=1738899&r2=1738900&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/renderer/Paginator.java (original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/renderer/Paginator.java Wed Apr 13 07:29:11 2016
@@ -23,9 +23,12 @@ import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
 
+import org.apache.commons.collections4.MapUtils;
 import org.ofbiz.base.util.Debug;
 import org.ofbiz.base.util.UtilGenerics;
+import org.ofbiz.base.util.UtilProperties;
 import org.ofbiz.base.util.UtilValidate;
+import org.ofbiz.base.util.collections.PagedList;
 import org.ofbiz.entity.GenericEntityException;
 import org.ofbiz.entity.util.EntityListIterator;
 import org.ofbiz.widget.WidgetWorker;
@@ -74,6 +77,9 @@ public final class Paginator {
                     listSize = (int)resultMap.get("listSize");
                 }
             }
+        } else if (entryList instanceof PagedList) {
+            PagedList<?> pagedList = (PagedList<?>) entryList;
+            listSize = pagedList.getSize();
         }
         if (modelForm.getPaginate(context)) {
             viewIndex = getViewIndex(modelForm, context);
@@ -187,6 +193,8 @@ public final class Paginator {
             iter = (Iterator<?>) obj;
         } else if (obj instanceof List<?>) {
             iter = ((List<?>) obj).listIterator();
+        } else if (obj instanceof PagedList<?>) {
+            iter = ((PagedList<?>) obj).iterator();
         }
 
         // set low and high index
@@ -240,4 +248,38 @@ public final class Paginator {
             return null;
         }
     }
+
+    /**
+     * @param context Map
+     * @param viewIndexName
+     * @return value of viewIndexName in context map (as an int) or return 0 as default
+     */
+    public static Integer getViewIndex(final Map<String, ? extends Object> context, final String viewIndexName) {
+        return getViewIndex(context, viewIndexName, 0);
+    }
+
+    /**
+     * @param context
+     * @param viewIndexName
+     * @param defaultValue
+     * @return value of viewIndexName in context map (as an int) or return defaultValue
+     */
+    public static Integer getViewIndex(final Map<String, ? extends Object> context, final String viewIndexName, final int defaultValue) {
+        return MapUtils.getInteger(context, viewIndexName, defaultValue);
+    }
+
+    /**
+     * @param context
+     * @param viewSizeName
+     * @return value of viewSizeName in context map (as an int) or return
+     *         default value from widget.properties
+     */
+    public static Integer getViewSize(Map<String, ? extends Object> context, String viewSizeName) {
+        int defaultSize = UtilProperties.getPropertyAsInteger("widget", "widget.form.defaultViewSize", 20);
+        if (context.containsKey(viewSizeName)) {
+            return MapUtils.getInteger(context, viewSizeName, defaultSize);
+        }
+        return defaultSize;
+    }
+
 }