[jboss-svn-commits] JBL Code SVN: r7602 - in labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums: . impl ui/action ui/view

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Nov 14 21:52:13 EST 2006


Author: unibrew
Date: 2006-11-14 21:52:06 -0500 (Tue, 14 Nov 2006)
New Revision: 7602

Modified:
   labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ForumsModule.java
   labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java
   labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/action/ModeratorAction.java
   labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/PageNavigator.java
   labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java
   labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/ViewTopic.java
Log:
[JBFORUMS-141] I redesigned pagination to work more efficiently.

Modified: labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ForumsModule.java
===================================================================
--- labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ForumsModule.java	2006-11-15 02:29:09 UTC (rev 7601)
+++ labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ForumsModule.java	2006-11-15 02:52:06 UTC (rev 7602)
@@ -445,7 +445,31 @@
      * @return DOCUMENT_ME
      * @throws ModuleException DOCUMENT_ME
      */
-    List findPostsByTopicIdAscFetchAttachmentsAndPosters(Integer topicID,
+    List findPostsByIdsAscFetchAttachmentsAndPosters( Collection postIds )
+       throws ModuleException;
+
+    /**
+     * DOCUMENT_ME
+     *
+     * @param topicID DOCUMENT_ME
+     * @param start   DOCUMENT_ME
+     * @param limit   DOCUMENT_ME
+     * @return DOCUMENT_ME
+     * @throws ModuleException DOCUMENT_ME
+     */
+    List findPostsByIdsDescFetchAttachmentsAndPosters( Collection postIds )
+       throws ModuleException;
+       
+    /**
+     * DOCUMENT_ME
+     *
+     * @param topicID DOCUMENT_ME
+     * @param start   DOCUMENT_ME
+     * @param limit   DOCUMENT_ME
+     * @return DOCUMENT_ME
+     * @throws ModuleException DOCUMENT_ME
+     */
+    List findPostIdsAsc(Integer topicID,
                                int start,
                                int limit)
        throws ModuleException;
@@ -459,10 +483,11 @@
      * @return DOCUMENT_ME
      * @throws ModuleException DOCUMENT_ME
      */
-    List findPostsByTopicIdDescFetchAttachmentsAndPosters(Integer topicID,
+    List findPostIdsDesc(Integer topicID,
                                 int start,
                                 int limit)
-       throws ModuleException;
+       throws ModuleException;   
+    
 
    /**
     * DOCUMENT_ME

Modified: labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java
===================================================================
--- labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java	2006-11-15 02:29:09 UTC (rev 7601)
+++ labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java	2006-11-15 02:52:06 UTC (rev 7602)
@@ -52,6 +52,7 @@
 import org.jboss.portlet.forums.command.filter.CompositeFilter;
 import org.jboss.portlet.forums.command.filter.ExecuteFilter;
 import org.jboss.portlet.forums.command.result.Result;
+import org.jboss.portlet.forums.ui.JSFUtil;
 
 /**
  * @author <a href="mailto:theute at jboss.org">Thomas Heute </a>
@@ -423,33 +424,52 @@
 
    }
 
-   private List findTopics(Forum forum,
-                           int type,
-                           int start,
-                           int perPage,
-                           String order)
-      throws ModuleException
-   {
-      try
-      {
-         Session session = getSession();
-         Query query =
-            session.createQuery("from TopicImpl as t where t.forum = :forumid and t.type <> :type order by t.lastPostDate " + order);
-         query.setFirstResult(start);
-         query.setMaxResults(perPage);
-         query.setString("forumid", "" + forum.getId());
-         query.setString("type", "" + type);
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find topics";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
+    /**
+     *
+     * @param forum
+     * @param type
+     * @param start
+     * @param perPage
+     * @param order
+     * @return
+     * @throws ModuleException
+     */
+     private List findTopics(Forum forum,
+                             int type,
+                             int start,
+                             int perPage,
+                             String order)
+        throws ModuleException
+     {
+        try
+        {
+           Session session = getSession();
+           Query query =
+              session.createQuery("from TopicImpl as t " +
+                                  "join fetch t.poster " +
+                                  "where t.forum = :forumid " +
+                                  "and t.type = :type " +
+                                  "order by t.lastPostDate " + order);
+           query.setFirstResult(start);
+           query.setMaxResults(perPage);
+           query.setString("forumid", "" + forum.getId());
+           query.setString("type", "" + type);
+           List list = query.list();
+           return list;
+        }
+        catch (HibernateException e)
+        {
+           String message = "Cannot find topics";
+           throw new ModuleException(message, e);
+        } catch (Exception e) {
+            JSFUtil.handleException(e);
+            String message = "Error while using ForumsModule.";
+            throw new ModuleException(message, e);
+        }
+     }
 
 
+
    public List findTopicsAsc(Forum forum,
                              int type,
                              int start,
@@ -1104,9 +1124,7 @@
       }
    }
 
-    private List findPostsByTopicIdFetchAttachmentsAndPosters(Integer topicId,
-                                    int start,
-                                    int limit,
+    private List findPostsByIdsFetchAttachmentsAndPosters(Collection postIds ,
                                     String order)
        throws ModuleException
     {
@@ -1117,14 +1135,9 @@
              session.createQuery(" from PostImpl as p " +
                                  " join fetch p.poster " +
                                  " left outer join fetch p.attachments " +
-                                 " where p.topic=:topicId " +
+                                 " where p.id IN ( :postIds ) " +
                                  " order by p.createDate " + order);
-          query.setString("topicId",topicId.toString());
-          query.setFirstResult(start);
-          if (limit != 0)
-          {
-             query.setMaxResults(limit);
-          }
+          query.setParameterList("postIds",postIds);
           Iterator it = query.list().iterator();
           List list = new LinkedList();
           while (it.hasNext()) {
@@ -1143,20 +1156,62 @@
        }
     }
     
-    public List findPostsByTopicIdAscFetchAttachmentsAndPosters(Integer topicId,
+    public List findPostsByIdsAscFetchAttachmentsAndPosters(Collection postIds)
+       throws ModuleException
+    {
+       return findPostsByIdsFetchAttachmentsAndPosters( postIds , "asc");
+    }
+
+    public List findPostsByIdsDescFetchAttachmentsAndPosters(Collection postIds)
+       throws ModuleException
+    {
+       return findPostsByIdsFetchAttachmentsAndPosters( postIds , "desc");
+    }
+
+    private List findPostIds(Integer topicId,
+                                    int start,
+                                    int limit,
+                                    String order)
+       throws ModuleException
+    {
+       try
+       {
+          Session session = getSession();
+          Query query =
+             session.createQuery("select p.id " +
+                                 "from PostImpl as p " +
+                                 "where p.topic=:topicId " +
+                                 "order by p.createDate " + order);
+          query.setString("topicId", topicId.toString());
+          query.setFirstResult(start);
+          if (limit != 0)
+          {
+             query.setMaxResults(limit);
+          }
+          return query.list();
+       }
+       catch (HibernateException e)
+       {
+          String message = "Cannot find post ids";
+          log.error(message, e);
+          throw new ModuleException(message, e);
+       }
+    }
+    
+    public List findPostIdsAsc(Integer topicId,
                                       int start,
                                       int limit)
        throws ModuleException
     {
-       return findPostsByTopicIdFetchAttachmentsAndPosters(topicId, start, limit, "asc");
+       return findPostIds(topicId, start, limit, "asc");
     }
 
-    public List findPostsByTopicIdDescFetchAttachmentsAndPosters(Integer topicId,
+    public List findPostIdsDesc(Integer topicId,
                                        int start,
                                        int limit)
        throws ModuleException
     {
-       return findPostsByTopicIdFetchAttachmentsAndPosters(topicId, start, limit, "desc");
+       return findPostIds(topicId, start, limit, "desc");
     }
 
    public List findPostsByTopicId(Integer topicId)

Modified: labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/action/ModeratorAction.java
===================================================================
--- labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/action/ModeratorAction.java	2006-11-15 02:29:09 UTC (rev 7601)
+++ labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/action/ModeratorAction.java	2006-11-15 02:52:06 UTC (rev 7602)
@@ -37,6 +37,7 @@
 
 import org.hibernate.Session;
 
+import org.jboss.portal.core.modules.ModuleException;
 import org.jboss.portlet.forums.ForumsModule;
 import org.jboss.portlet.forums.impl.ForumsModuleImpl;
 import org.jboss.portlet.forums.model.Forum;
@@ -420,36 +421,67 @@
 
         checkboxes=new HashMap();
 
+        // ForumsModule is stored as a final variable so that anonymous class could use it.
+        final ForumsModule fm = BaseController.getForumsModule();
+
         //grab the data to be displayed for this page
         if(forumId!=-1)
         {
+            
             //setup the business objects like the forum, topics etc that will be displayed
-            this.forum = BaseController.getForumsModule().findForumById(new Integer(forumId));
-            Object[] topicObjects = this.forum.getTopics().toArray();
-            //setup the pageNavigator for this forum
-            this.pageNavigator = new PageNavigator(
-             topicObjects, //total number of entries to be split up into pages
-             Integer.parseInt(this.userPreferences.getPreference(Constants.TOPICS_FORUM_KEY)), 
-             currentPage //currently selected page being displayed, first page by default
-            );
-            this.page = this.pageNavigator.getPage();
+            this.forum = fm.findForumById(new Integer(forumId));
+                    
         } else {
+            
             // trying to get forumId from topicId read from request
-            int topicId;
             String t = ForumUtil.getParameter(Constants.p_topicId);
+            
             if (t!=null && t.trim().length()>0) {
-                Topic topic = this.getForumsModule().findTopicById(new Integer(t));
+            
+                Topic topic = fm.findTopicById(new Integer(t));
                 this.forum = topic.getForum();
-                Object[] topicObjects = this.forum.getTopics().toArray();
-                //setup the pageNavigator for this forum
-                this.pageNavigator = new PageNavigator(
-                 topicObjects, //total number of entries to be split up into pages
-                 Integer.parseInt(this.userPreferences.getPreference(Constants.TOPICS_FORUM_KEY)), 
-                 currentPage //currently selected page being displayed, first page by default
-                );
-                this.page = this.pageNavigator.getPage();
+                
             }
         }
+
+        int topicCount = this.forum.getTopicCount();
+
+        //setup the pageNavigator for this forum        
+        this.pageNavigator = new PageNavigator(
+            topicCount, //total number of entries to be split up into pages
+            Integer.parseInt(this.userPreferences.getPreference(Constants.TOPICS_FORUM_KEY)), 
+            currentPage //currently selected page being displayed, first page by default
+        ) {
+
+            protected Collection initializePage() {
+            
+                int beginIndex = this.getBeginIndex();
+                int endIndex = this.getEndIndex();
+                int totalEntries = this.getNumberOfEntries();
+                        
+                //reset the page navigation if the start cursor is outside the range (both forward and backward)
+                if(beginIndex >= totalEntries || beginIndex < 0)
+                {
+                    this.setCurrentPage(0); //go to the first page
+                    beginIndex = this.getBeginIndex();
+                    endIndex = this.getEndIndex();
+                }
+                
+                try {
+                    return fm.findTopicsDesc(forum,
+                                             Constants.POST_NORMAL,
+                                             beginIndex,
+                                             endIndex);
+                } catch (ModuleException e) {
+                    JSFUtil.handleException(e);
+                }
+                return new ArrayList();
+                
+            }
+            
+        };
+        
+        this.page = this.pageNavigator.getPage();
     }
     
     private void setWarnBundleMessage(String bundleKey) {

Modified: labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/PageNavigator.java
===================================================================
--- labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/PageNavigator.java	2006-11-15 02:29:09 UTC (rev 7601)
+++ labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/PageNavigator.java	2006-11-15 02:52:06 UTC (rev 7602)
@@ -29,8 +29,9 @@
  * Created on May 11, 2006
  *
  * @author <a href="mailto:sohil.shah at jboss.com">Sohil Shah</a>
+ * @author <a href="mailto:ryszard.kozmik at jboss.com">Ryszard Kozmik</a>
  */
-public class PageNavigator implements Serializable
+public abstract class PageNavigator implements Serializable
 {
     /**
      * 
@@ -38,38 +39,48 @@
     private int totalPages = 0;
     private int pageSize = 0;
     private int currentPage = 0;
-    private Object[] entries = null;
+    private int numberOfEntries=0;
+    private Collection page = null;
     
     /**
      * 
      *
      */
-    public PageNavigator(Object[] entries,int pageSize,int currentPage)
+    public PageNavigator(int numberOfEntries,int pageSize,int currentPage)
     { 
-        if(entries==null)
+        if(numberOfEntries<0)
         {
-            throw new IllegalStateException("PageNavigator cannot be initialized with a null set of entries");
+            throw new IllegalStateException("PageNavigator cannot be initialized for negative number of entries");
         }
         
         this.pageSize = pageSize;
         this.currentPage = currentPage;
-        int totalEntries = 0;
-        if(entries!=null)
-        {
-            this.entries = entries;
-            totalEntries = this.entries.length;
-        }
+        this.numberOfEntries = numberOfEntries;
         
-        
         //calculate the totalNumberofPages that will be made
-        double totalDbl = totalEntries;
         double pageSizeDbl = this.pageSize;
-        double pageCountDbl = totalDbl/pageSizeDbl;
-        this.totalPages = (int)Math.ceil(pageCountDbl);                
+        double pageCountDbl = numberOfEntries/pageSizeDbl;
+        this.totalPages = (int)Math.ceil(pageCountDbl);
+        
+        this.page = initializePage();
     }
     
     /**
+     * Every non-abstract class extending this class will have to implement this method
+     * and initialize page with objects.
+     */
+    protected abstract Collection initializePage();
+    
+    /**
+     *
      * 
+     */
+    public int getNumberOfEntries() {
+        return this.numberOfEntries;
+    }
+    
+    /**
+     * 
      *
      */
     public int getTotalPages()
@@ -109,15 +120,10 @@
         int endIndex = 0;
         
         endIndex = this.getBeginIndex() + this.pageSize;
-        int totalEntries = 0;
-        if(this.entries!=null)
+        if(endIndex >= this.numberOfEntries)
         {
-            totalEntries = this.entries.length;
+            endIndex = this.numberOfEntries;
         }
-        if(endIndex >= totalEntries)
-        {
-            endIndex = totalEntries;
-        }
         
         return endIndex;
     }
@@ -128,31 +134,10 @@
      */
     public Collection getPage()
     {
-        Collection page = new ArrayList();
-        
-        int beginIndex = this.getBeginIndex();
-        int endIndex = this.getEndIndex();
-        int totalEntries = 0;
-        if(this.entries!=null)
-        {
-           totalEntries = this.entries.length;
+        if (this.page==null) {
+            return new ArrayList();
         }
-        
-        //reset the page navigation if the start cursor is outside the range (both forward and backward)
-        if(beginIndex >= totalEntries || beginIndex < 0)
-        {
-            this.setCurrentPage(0); //go to the first page
-            beginIndex = this.getBeginIndex();
-            endIndex = this.getEndIndex();
-        }
-        	            
-        //get the entries for this page	            	            
-        for(int i=beginIndex;i<endIndex;i++)
-        {
-            page.add(this.entries[i]);
-        }
-                
-        return page;
+        return this.page;
     }
     
         
@@ -187,7 +172,7 @@
     {
        int pageAfterAdd = 0;
        
-       double totalDbl = this.entries.length+1;
+       double totalDbl = this.numberOfEntries+1;
        double pageSizeDbl = this.pageSize;
        double pageCountDbl = totalDbl/pageSizeDbl;
        pageAfterAdd = ((int)Math.ceil(pageCountDbl))-1;       

Modified: labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java
===================================================================
--- labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java	2006-11-15 02:29:09 UTC (rev 7601)
+++ labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java	2006-11-15 02:52:06 UTC (rev 7602)
@@ -154,11 +154,11 @@
          stickyThreads = new ArrayList();
          try {
              //ForumsModule fm = this.getForumsModule();
-             stickyThreads = findTopics(forum,
+             stickyThreads = BaseController.getForumsModule().findTopicsDesc(forum,
                              Constants.POST_STICKY,
                              -1,
                              Integer.parseInt(this.userPreferences.getPreference(
-                                                    Constants.TOPICS_FORUM_KEY)),"desc");
+                                                    Constants.TOPICS_FORUM_KEY)));
          } catch (Exception e) {
              JSFUtil.handleException(e);
          }
@@ -183,11 +183,11 @@
          }
          boolean present = false;
          try {
-             stickyThreads = findTopics(forum,
+             stickyThreads = BaseController.getForumsModule().findTopicsDesc(forum,
                              Constants.POST_STICKY,
                              -1,
                              Integer.parseInt(this.userPreferences.getPreference(
-                                                    Constants.TOPICS_FORUM_KEY)),"desc");
+                                                    Constants.TOPICS_FORUM_KEY)));
              if (stickyThreads!=null && stickyThreads.size()>0) {
                  present = true;
              }
@@ -241,57 +241,9 @@
          this.userPreferences = userPreferences;    
      }             
      //-------------------------------------------------------------------------------------------------------------------------------------                
+          
      
      /**
-      *
-      * TODO: THIS METHOD IS IMPORTED FROM FORUMSMODULE AND MUST BE MOVED THERE BACK
-      *       SINCE IT CONTAINS SERIOUS MISTAKE IN FORUMSMODULE, IT IS HERE FOR A MOMENT
-      *       AND WILL BE MOVED AFTER 1.0 RELEASE
-      *
-      * @param forum
-      * @param type
-      * @param start
-      * @param perPage
-      * @param order
-      * @return
-      * @throws ModuleException
-      */
-      private List findTopics(Forum forum,
-                              int type,
-                              int start,
-                              int perPage,
-                              String order)
-         throws ModuleException
-      {
-         try
-         {
-            Session session = this.getForumsModule().getHibernate().getSessionFactory().getCurrentSession();
-            Query query =
-               session.createQuery("from TopicImpl as t " +
-                                   "join fetch t.poster " +
-                                   "where t.forum = :forumid " +
-                                   "and t.type = :type " +
-                                   "order by t.lastPostDate " + order);
-            query.setFirstResult(start);
-            query.setMaxResults(perPage);
-            query.setString("forumid", "" + forum.getId());
-            query.setString("type", "" + type);
-            List list = query.list();
-            return list;
-         }
-         catch (HibernateException e)
-         {
-            String message = "Cannot find topics";
-            throw new ModuleException(message, e);
-         } catch (Exception e) {
-             JSFUtil.handleException(e);
-             String message = "Error while using ForumsModule.";
-             throw new ModuleException(message, e);
-         }
-      }
-     
-     
-     /**
       * 
       * @author sshah
       */
@@ -334,29 +286,58 @@
              forumId = Integer.parseInt(f);
          }
          
+         // ForumsModule is stored as a final variable so that anonymous class could use it.
+         final ForumsModule fm = BaseController.getForumsModule();
+         
          //grab the data to be displayed for this page
          if(forumId!=-1)
          {
              //setup the business objects like the forum, topics etc that will be displayed
-             this.forum = BaseController.getForumsModule().findForumById(new Integer(forumId));
-         
-             Object[] topicObjects = findTopics(forum,
-                                                Constants.POST_NORMAL,
-                                                -1,
-                                                Integer.MAX_VALUE,"desc").toArray();
-                                     
-             //setup the pageNavigator for this forum
-             this.pageNavigator = new PageNavigator(
-              topicObjects, //total number of entries to be split up into pages
-              Integer.parseInt(this.userPreferences.getPreference(Constants.TOPICS_FORUM_KEY)), 
-              0 //currently selected page being displayed, first page by default
-             );
+             this.forum = fm.findForumById(new Integer(forumId));
              
+             int pageNumber = 0;
+             
              if(page!=null && page.trim().length()>0)
              {
                  //setup the page data
-                 this.pageNavigator.setCurrentPage(Integer.parseInt(page));                                              
+                 pageNumber = Integer.parseInt(page);
              }
+                                     
+             //setup the pageNavigator for this forum
+             this.pageNavigator = new PageNavigator(
+                forum.getTopicCount(), //total number of entries to be split up into pages
+                Integer.parseInt(this.userPreferences.getPreference(Constants.TOPICS_FORUM_KEY)), 
+                pageNumber //currently selected page being displayed, first page by default
+             ) {
+             
+                 protected Collection initializePage() {
+                 
+                     int beginIndex = this.getBeginIndex();
+                     int endIndex = this.getEndIndex();
+                     int totalEntries = this.getNumberOfEntries();
+                             
+                     //reset the page navigation if the start cursor is outside the range (both forward and backward)
+                     if(beginIndex >= totalEntries || beginIndex < 0)
+                     {
+                         this.setCurrentPage(0); //go to the first page
+                         beginIndex = this.getBeginIndex();
+                         endIndex = this.getEndIndex();
+                     }
+                     
+                     try {
+                         return fm.findTopicsDesc(forum,
+                                                  Constants.POST_NORMAL,
+                                                  beginIndex,
+                                                  endIndex);
+                     } catch (ModuleException e) {
+                         JSFUtil.handleException(e);
+                     }
+                     return new ArrayList();
+                     
+                 }
+             
+             };
+             
              this.page = this.pageNavigator.getPage();
              
              // Getting sticky topics for this page
@@ -370,20 +351,26 @@
              listOfTopics.addAll(stickies);
              listOfTopics.addAll(announcements);
              listOfTopics.addAll(this.page);
+
+            // Getting sticky topics for this page
+             this.topicLastPosts = fm.findLastPostsOfTopics(listOfTopics);
              
-             // Getting lastPosts for all topics
-             this.topicLastPosts = BaseController.getForumsModule().findLastPostsOfTopics(listOfTopics);
-             
              // setup dummy pageNavigators for all topics being displayed for topic minipaging
              for(Iterator itr=listOfTopics.iterator();itr.hasNext();)
              {
                  Topic cour = (Topic)itr.next();
                  if(cour.getReplies()>0)
                  {
-                     PageNavigator topicNav = new PageNavigator(new Object[cour.getReplies()+1],
+                     PageNavigator topicNav = new PageNavigator(cour.getReplies()+1,
                      Integer.parseInt(this.userPreferences.getPreference(Constants.POSTS_TOPIC_KEY)), //this is user's posts per page preference
                      0 //current page of the navigator
-                     );
+                     ) {
+
+                            protected Collection initializePage() {
+                                return null;
+                            }
+                            
+                        };
                      this.topicNavigator.put(cour.getId(),topicNav);
                  }
              }

Modified: labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/ViewTopic.java
===================================================================
--- labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/ViewTopic.java	2006-11-15 02:29:09 UTC (rev 7601)
+++ labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/ViewTopic.java	2006-11-15 02:52:06 UTC (rev 7602)
@@ -26,6 +26,8 @@
 import java.util.Collection;
 import java.util.Iterator;
 
+import org.jboss.portal.core.modules.ModuleException;
+import org.jboss.portlet.forums.ForumsModule;
 import org.jboss.portlet.forums.model.Category;
 import org.jboss.portlet.forums.model.Topic;
 import org.jboss.portlet.forums.ui.BaseController;
@@ -192,18 +194,17 @@
             topicId = Integer.parseInt(t);
         }        
 
+        // ForumsModule is stored as a final variable so that anonymous class could use it.
+        final ForumsModule fm = BaseController.getForumsModule();
+
         //process the topic information
         if (topicId != -1) {
-            this.topic =
-                    BaseController.getForumsModule().findTopicById(new Integer(topicId));
+            this.topic = fm.findTopicById(new Integer(topicId));
             topic.setViewCount(topic.getViewCount()+1);
-            Collection posts = null;
-            if (this.userPreferences.getPreference(Constants.POST_ORDER_KEY).compareToIgnoreCase("ascending")==0) {
-                posts = this.getForumsModule().findPostsByTopicIdAscFetchAttachmentsAndPosters(topic.getId(),0,Integer.MAX_VALUE);
-            } else {
-                posts = this.getForumsModule().findPostsByTopicIdDescFetchAttachmentsAndPosters(topic.getId(),0,Integer.MAX_VALUE);
-            }
-            if (posts != null && posts.size() > 0) {
+            String postOrder = this.userPreferences.getPreference(Constants.POST_ORDER_KEY);
+            int postCount = topic.getReplies()+1;
+
+            if (postCount > 0) {
                 int currentPage = 0;
                 if (page != null && page.trim().length() > 0) {
                     //setup the page data
@@ -212,13 +213,77 @@
 
                 //setup the pageNavigator
                 //total number of entries to be split up into pages
-                    //currently selected page being displayed, first page by default
+                //currently selected page being displayed, first page by default            
+                if (postOrder.compareToIgnoreCase("ascending")==0) {
                     this.pageNavigator =
-                        new PageNavigator(posts.toArray(), Integer.parseInt(this.userPreferences.getPreference(Constants.POSTS_TOPIC_KEY)),
-                                          currentPage);
+                            new PageNavigator(postCount,
+                                              Integer.parseInt(this.userPreferences.getPreference(Constants.POSTS_TOPIC_KEY)),
+                                              currentPage) {
 
+                                protected Collection initializePage() {
+                                
+                                    int beginIndex = this.getBeginIndex();
+                                    int endIndex = this.getEndIndex();
+                                    int totalEntries = this.getNumberOfEntries();
+                                            
+                                    //reset the page navigation if the start cursor is outside the range (both forward and backward)
+                                    if(beginIndex >= totalEntries || beginIndex < 0)
+                                    {
+                                        this.setCurrentPage(0); //go to the first page
+                                        beginIndex = this.getBeginIndex();
+                                        endIndex = this.getEndIndex();
+                                    }
+                                
+                                    try {
+                                        return fm.findPostIdsAsc(topic.getId(),
+                                                                 beginIndex,
+                                                                 endIndex);
+                                    } catch (ModuleException e) {
+                                        JSFUtil.handleException(e);
+                                    }
+                                    return new ArrayList();
+                                }
+                            };
+                    
+                    Collection pagePostIds = this.pageNavigator.getPage();
+                    this.page = fm.findPostsByIdsAscFetchAttachmentsAndPosters(pagePostIds);
+                
+                } else {
+                
+                    this.pageNavigator =
+                            new PageNavigator(postCount,
+                                              Integer.parseInt(this.userPreferences.getPreference(Constants.POSTS_TOPIC_KEY)),
+                                              currentPage) {
 
-                this.page = this.pageNavigator.getPage();
+                                protected Collection initializePage() {
+                                    
+                                    int beginIndex = this.getBeginIndex();
+                                    int endIndex = this.getEndIndex();
+                                    int totalEntries = this.getNumberOfEntries();
+                                            
+                                    //reset the page navigation if the start cursor is outside the range (both forward and backward)
+                                    if(beginIndex >= totalEntries || beginIndex < 0)
+                                    {
+                                        this.setCurrentPage(0); //go to the first page
+                                        beginIndex = this.getBeginIndex();
+                                        endIndex = this.getEndIndex();
+                                    }
+                                    
+                                    try {
+                                        return fm.findPostIdsDesc(topic.getId(),
+                                                                  beginIndex,
+                                                                  endIndex);
+                                    } catch (ModuleException e) {
+                                        JSFUtil.handleException(e);
+                                    }
+                                    return new ArrayList();
+                                }
+                            };
+
+                    Collection pagePostIds = this.pageNavigator.getPage();
+                    this.page = fm.findPostsByIdsDescFetchAttachmentsAndPosters(pagePostIds);
+
+                }
             }
         }
     }




More information about the jboss-svn-commits mailing list