[jboss-svn-commits] JBL Code SVN: r6042 - labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/view

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Aug 31 11:33:07 EDT 2006


Author: unibrew
Date: 2006-08-31 11:33:05 -0400 (Thu, 31 Aug 2006)
New Revision: 6042

Modified:
   labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java
Log:
[JBFORUMS-104] Repairing partially pagination efficience problem and wrongly showed sticky topics.

Modified: labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java
===================================================================
--- labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java	2006-08-31 12:34:58 UTC (rev 6041)
+++ labs/jbossforums/trunk/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java	2006-08-31 15:33:05 UTC (rev 6042)
@@ -28,13 +28,19 @@
 import java.util.Map;
 import java.util.HashMap;
 
+import org.hibernate.HibernateException;
+import org.hibernate.Query;
+import org.hibernate.Session;
+
+import org.jboss.portal.common.util.Tools;
+import org.jboss.portal.core.modules.ModuleException;
+import org.jboss.portlet.forums.ForumsModule;
 import org.jboss.portlet.forums.model.Forum;
 import org.jboss.portlet.forums.model.Topic;
 import org.jboss.portlet.forums.ui.Constants;
 import org.jboss.portlet.forums.ui.BaseController;
 import org.jboss.portlet.forums.ui.JSFUtil;
 import org.jboss.portlet.forums.ui.ForumUtil;
-import org.jboss.portlet.forums.ui.PortalUtil;
 import org.jboss.portlet.forums.ui.action.PreferenceController;
 
 //jsf imports
@@ -42,6 +48,7 @@
 
 /**
  * @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 ViewForum extends BaseController
@@ -91,116 +98,87 @@
     public Collection getAnnouncements()
     {
         Collection announcements = new ArrayList(); 
-        Collection topics = this.forum.getTopics();
-        if(topics!=null)
-        {
-	        for(Iterator itr=topics.iterator();itr.hasNext();)
-	        {
-	            Topic cour = (Topic)itr.next();
-	            if(cour.getType()==Constants.POST_ANNOUNCE)
-	            {
-	                announcements.add(cour);
-	            }
-	        }
+        try {
+            announcements = this.getForumsModule().findAnnouncements(forum);
+        } catch (Exception e) {
+            JSFUtil.handleException(e);
         }
         return announcements;
     }
     
     /**
      * 
-     *
+     * TODO: Make a special method in ForumsModule for that.
      */
     public boolean isAnnouncementsPresent()
     {
-        boolean present = false;      
-        Collection topics = this.forum.getTopics();
-        if(topics!=null)
-        {
-	        for(Iterator itr=topics.iterator();itr.hasNext();)
-	        {
-	            Topic cour = (Topic)itr.next();
-	            if(cour.getType()==Constants.POST_ANNOUNCE)
-	            {
-	                present = true;
-	                break;
-	            }
-	        }
+        boolean present = false;
+        try {
+            present = this.getForumsModule().findAnnouncements(forum).size()>0;
+        } catch (Exception e) {
+            JSFUtil.handleException(e);
         }
         return present;
     }
     
     /**
      * 
-     *
+     * TODO: Make a special method in ForumsModule for that.
      */
     public Collection getStickyThreads()
     {
         Collection stickyThreads = new ArrayList();
-        for(Iterator itr=this.page.iterator();itr.hasNext();)
-        {
-            Topic cour = (Topic)itr.next();
-            if(cour.getType()==Constants.POST_STICKY)
-            {
-                stickyThreads.add(cour);
-            }
+        if (this.pageNavigator.getCurrentPage()!=0) return stickyThreads;
+        try {
+            //ForumsModule fm = this.getForumsModule();
+            stickyThreads = findTopics(forum,
+                            Constants.POST_STICKY,
+                            -1,
+                            Integer.parseInt(this.userPreferences.getPreference(
+                                                   Constants.TOPICS_FORUM_KEY)),"asc");
+        } catch (Exception e) {
+            JSFUtil.handleException(e);
         }
         return stickyThreads;
     }
     
     /**
      * 
-     *
+     * TODO: Make a special method in ForumsModule for that.
      */
     public boolean isStickyThreadsPresent()
     {
-        boolean present = false;      
-        for(Iterator itr=this.page.iterator();itr.hasNext();)
-        {
-            Topic cour = (Topic)itr.next();
-            if(cour.getType()==Constants.POST_STICKY)
-            {
+        if (this.pageNavigator.getCurrentPage()!=0) return false;
+        boolean present = false;
+        try {
+            Collection stickyThreads = findTopics(forum,
+                            Constants.POST_STICKY,
+                            -1,
+                            Integer.parseInt(this.userPreferences.getPreference(
+                                                   Constants.TOPICS_FORUM_KEY)),"asc");
+            if (stickyThreads!=null && stickyThreads.size()>0) {
                 present = true;
-                break;
             }
+        } catch (Exception e) {
+            JSFUtil.handleException(e);
         }
         return present;
     }
     
     /**
      * 
-     *
      */
     public Collection getNormalThreads()
     {
-        Collection normalThreads = new ArrayList();
-        for(Iterator itr=this.page.iterator();itr.hasNext();)
-        {
-            Topic cour = (Topic)itr.next();
-            if(cour.getType()==Constants.POST_NORMAL)
-            {
-                normalThreads.add(cour);
-            }
-        }
-        return normalThreads;
+        return this.page;
     } 
     
     /**
      * 
-     *
      */
     public boolean isNormalThreadsPresent()
-    {
-        boolean present = false;      
-        for(Iterator itr=this.page.iterator();itr.hasNext();)
-        {
-            Topic cour = (Topic)itr.next();
-            if(cour.getType()==Constants.POST_NORMAL)
-            {
-                present = true;
-                break;
-            }
-        }
-        return present;
+    {     
+        return this.page.size()>0;
     }
     //------------user preferences-------------------------------------------------------------------------------------------------------------
     /**
@@ -218,7 +196,53 @@
         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 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);
+           Iterator iterator = query.iterate();
+           return Tools.toList(iterator);
+        }
+        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
      */
@@ -267,21 +291,11 @@
             //setup the business objects like the forum, topics etc that will be displayed
             this.forum = BaseController.getForumsModule().findForumById(new Integer(forumId));
             List allTopics = this.forum.getTopics();
-            Object[] topicObjects = null;
-            if(allTopics!=null)
-            {
-                //filter out any announcements from this array....Announcements appear on all pages regardless
-                Collection topics = new ArrayList();
-                for(Iterator itr=allTopics.iterator();itr.hasNext();)
-                {
-                    Topic cour = (Topic)itr.next();
-                    if(cour.getType()!=Constants.POST_ANNOUNCE)
-                    {
-                        topics.add(cour);
-                    }
-                }
-                topicObjects = topics.toArray();
-            }
+            Object[] topicObjects = findTopics(forum,
+                                               Constants.POST_NORMAL,
+                                               -1,
+                                               Integer.MAX_VALUE,"asc").toArray();
+            
                                     
             //setup the pageNavigator for this forum
             this.pageNavigator = new PageNavigator(




More information about the jboss-svn-commits mailing list