Fixed: Makes Forum Articles respond to pagination controls (OFBIZ-11434) (#76)

* Fixed: Makes Forum Articles respond to pagination controls (OFBIZ-11434)
* Improved: Removed commented out code (OFBIZ-11434)
diff --git a/ecommerce/groovyScripts/forum/ShowForum.groovy b/ecommerce/groovyScripts/forum/ShowForum.groovy
new file mode 100644
index 0000000..d04160c
--- /dev/null
+++ b/ecommerce/groovyScripts/forum/ShowForum.groovy
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.ofbiz.ecommerce.forum.ForumEvents
+import org.apache.ofbiz.service.ServiceUtil
+
+Map result = ForumEvents.getForumMessages(request, delegator)
+if (ServiceUtil.isSuccess(result)) {
+    context.put("viewSize",result.get("viewSize"))
+    context.put("forumMessages", result.get("forumMessages"))
+    context.put("viewIndex", result.get("viewIndex"))
+    context.put("listSize", result.get("listSize"))
+    context.put("lowIndex", result.get("lowIndex"))
+    context.put("highIndex", result.get("highIndex"))
+}
diff --git a/ecommerce/src/main/java/org/apache/ofbiz/ecommerce/forum/ForumEvents.java b/ecommerce/src/main/java/org/apache/ofbiz/ecommerce/forum/ForumEvents.java
new file mode 100644
index 0000000..0d60927
--- /dev/null
+++ b/ecommerce/src/main/java/org/apache/ofbiz/ecommerce/forum/ForumEvents.java
@@ -0,0 +1,87 @@
+package org.apache.ofbiz.ecommerce.forum;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.ofbiz.base.util.Debug;
+import org.apache.ofbiz.base.util.UtilMisc;
+import org.apache.ofbiz.base.util.UtilValidate;
+import org.apache.ofbiz.entity.Delegator;
+import org.apache.ofbiz.entity.GenericEntityException;
+import org.apache.ofbiz.entity.GenericValue;
+import org.apache.ofbiz.entity.util.EntityListIterator;
+import org.apache.ofbiz.service.GenericServiceException;
+import org.apache.ofbiz.service.LocalDispatcher;
+import org.apache.ofbiz.service.ServiceUtil;
+
+public class ForumEvents {
+
+    public static final String module = ForumEvents.class.getName();
+
+    public static Map<String, Object> getForumMessages(HttpServletRequest request, Delegator delegator) {
+
+        LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
+        // ========== Create View Indexes
+        int viewIndex = 0;
+        int viewSize = 20;
+
+        if (UtilValidate.isNotEmpty(request.getParameter("VIEW_INDEX"))) {
+            Integer viewIndexInteger = Integer.parseInt(request.getParameter("VIEW_INDEX"));
+            if (viewIndexInteger != null) {
+                viewIndex = viewIndexInteger;
+            }
+        }
+
+        if (UtilValidate.isNotEmpty(request.getParameter("VIEW_SIZE"))) {
+            Integer viewSizeInteger = Integer.parseInt(request.getParameter("VIEW_SIZE"));
+            if (viewSizeInteger != null) {
+                viewSize = viewSizeInteger;
+            }
+        }
+
+        int lowIndex = viewIndex * viewSize;
+        int highIndex = (viewIndex + 1) * viewSize;
+
+        // =========== Set up the PerformFindList
+        GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
+        String forumId = request.getParameter("forumId");
+        Map<String, Object> inputFields = new HashMap<>();
+        inputFields.put("contentIdStart", forumId);
+        inputFields.put("caContentAssocTypeId_fld0", "PUBLISH_LINK");
+        inputFields.put("caContentAssocTypeId_fld1", "RESPONSE");
+
+        Map<String, Object> articlesFound = null;
+
+        try {
+            articlesFound = dispatcher.runSync("performFind", UtilMisc.<String, Object>toMap("entityName",
+                    "ContentAssocViewTo", "inputFields", inputFields, "userLogin", userLogin, "orderBy", "-createdDate"));
+        } catch (GenericServiceException e) {
+            Debug.logError(e, "Cannot get ForumMessages for Forum %s", module, forumId);
+            return ServiceUtil.returnError("Error while searching for Messages, please retry and/or contact Admin.");
+        }
+        int start = viewIndex * viewSize;
+        List<GenericValue> list = null;
+        Integer listSize = 0;
+        try (EntityListIterator it = (EntityListIterator) articlesFound.get("listIt")) {
+            list = it.getPartialList(start + 1, viewSize); // list starts at '1'
+            listSize = it.getResultsSizeAfterPartialList();
+        } catch (ClassCastException | NullPointerException | GenericEntityException e) {
+            Debug.logInfo("Problem getting partial list" + e, module);
+        }
+
+        // create the result map
+        Map<String, Object> result = ServiceUtil.returnSuccess();
+
+        result.put("highIndex", highIndex);
+        result.put("listSize", listSize);
+        result.put("lowIndex", lowIndex);
+        result.put("viewIndex", viewIndex);
+        result.put("viewSize", viewSize);
+        result.put("forumMessages", list);
+
+        return result;
+    }
+}
diff --git a/ecommerce/template/forum/ForumPaging.ftl b/ecommerce/template/forum/ForumPaging.ftl
index 3aefe4d..3f686e4 100644
--- a/ecommerce/template/forum/ForumPaging.ftl
+++ b/ecommerce/template/forum/ForumPaging.ftl
@@ -16,14 +16,15 @@
 specific language governing permissions and limitations
 under the License.
 -->
-
+<#assign viewSize = viewSize?default(20)>
 <#assign viewIndex = viewIndex?default(0)>
 <#assign lowIndex = viewIndex?int * viewSize?int + 1>
 <#assign highIndex = viewIndex?int * viewSize?int + viewSize>
-<#--<br />== viewIndex: ${viewIndex} ==viewSize: ${viewSize} ==lowIndex: ${lowIndex}== highIndex: ${highIndex} == ListSize: ${listSize}-->
+
+<#-- <br />== viewIndex: ${viewIndex} ==viewSize: ${viewSize} ==lowIndex: ${lowIndex}== highIndex: ${highIndex} == ListSize: ${listSize} -->
 <#if forumMessages?has_content && forumMessages?size gt 0>
-  <#assign listSize = forumMessages?size/>
-  <#if highIndex gt listSize><#assign highIndex = listSize></#if>
+    <#assign listSize = listSize?default(forumMessages?size)/>
+    <#if highIndex gt listSize><#assign highIndex = listSize></#if>
 <div class="row">
   <#assign r = listSize / viewSize />
   <#assign viewIndexMax = Static["java.lang.Math"].ceil(r)>
diff --git a/ecommerce/widget/ForumScreens.xml b/ecommerce/widget/ForumScreens.xml
index 2174fec..be9ec25 100644
--- a/ecommerce/widget/ForumScreens.xml
+++ b/ecommerce/widget/ForumScreens.xml
@@ -60,20 +60,11 @@
                 </entity-one>
                 <!-- parameters for the performFind service -->
                 <set field="parameters.forumId" from-field="parameters.forumId" default-value="${parameters.contentId}"/>
-                <set field="viewIndex" from-field="parameters.VIEW_INDEX" type="Integer" default-value="0" />
+                <set field="viewIndex" from-field="parameters.VIEW_INDEX" type="Integer" default-value="0"/>
+                <set field="parameters.viewIndex" from-field="viewIndex"/>
                 <set field="viewSizeDefaultValue" value="${groovy: modelTheme.getDefaultViewSize()}" type="Integer"/>
-                <set field="viewSize" from-field="parameters.VIEW_SIZE" type="Integer" default-value="${viewSizeDefaultValue}"/>
-                <entity-condition entity-name="ContentAssocViewTo" list="forumMessages" >
-                    <condition-list combine="and">
-                        <condition-expr field-name="contentIdStart" from-field="parameters.forumId"/>
-                        <condition-list combine="or">
-                            <condition-expr field-name="caContentAssocTypeId" value="PUBLISH_LINK"/>
-                            <condition-expr field-name="caContentAssocTypeId" value="RESPONSE"/>
-                        </condition-list>
-                    </condition-list>
-                    <order-by field-name="createdDate"/>
-
-                </entity-condition>
+                <set field="parameters.viewSize" from-field="parameters.VIEW_SIZE" type="Integer" default-value="${viewSizeDefaultValue}"/>
+                <script location="component://ecommerce/groovyScripts/forum/ShowForum.groovy"/>
             </actions>
             <widgets>
                 <decorator-screen name="main-decorator" location="${parameters.mainDecoratorLocation}">
@@ -101,7 +92,6 @@
                                 </section>
                                 <platform-specific><html><html-template location="component://ecommerce/template/forum/ForumPaging.ftl"/></html></platform-specific>
                                 <platform-specific><html><html-template location="component://ecommerce/template/forum/ShowForum.ftl"/></html></platform-specific>
-                                <platform-specific><html><html-template location="component://ecommerce/template/forum/ForumPaging.ftl"/></html></platform-specific>
                             </widgets>
                         </section>
                     </decorator-section>