[jboss-svn-commits] JBL Code SVN: r7570 - in labs/jbossforums/branches/forums22/forums/src: main/org/jboss/portlet/forums main/org/jboss/portlet/forums/impl main/org/jboss/portlet/forums/ui/view resources/portal-forums-war/views/forums

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Nov 13 15:06:51 EST 2006


Author: unibrew
Date: 2006-11-13 15:06:45 -0500 (Mon, 13 Nov 2006)
New Revision: 7570

Modified:
   labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ForumsModule.java
   labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java
   labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewCategory.java
   labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java
   labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewJumpbox.java
   labs/jbossforums/branches/forums22/forums/src/resources/portal-forums-war/views/forums/viewforum_body.xhtml
Log:
[JBFORUMS-135] Very big improvement of ForumView efficiency and additionally small improvement of Jumpbox and CategoryView.

Modified: labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ForumsModule.java
===================================================================
--- labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ForumsModule.java	2006-11-13 18:09:38 UTC (rev 7569)
+++ labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ForumsModule.java	2006-11-13 20:06:45 UTC (rev 7570)
@@ -10,6 +10,8 @@
  *****************************************/
 package org.jboss.portlet.forums;
 
+import java.util.Collection;
+
 import org.jboss.portal.core.model.User;
 import org.jboss.portal.core.modules.ModuleException;
 import org.jboss.portal.core.hibernate.HibernateProvider;
@@ -471,6 +473,8 @@
 
    Post findLastPost(Topic topic) throws ModuleException;
    
+   Map findLastPostsOfTopics(Collection topics) throws ModuleException;
+   
    Map findLastPostsOfForums() throws ModuleException;
 
    List findForumWatchByUser(User user) throws ModuleException;

Modified: labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java
===================================================================
--- labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java	2006-11-13 18:09:38 UTC (rev 7569)
+++ labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java	2006-11-13 20:06:45 UTC (rev 7570)
@@ -44,6 +44,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -325,7 +326,16 @@
                                             " from CategoryImpl as c " +
                                             " join fetch c.forums " +
                                             " order by c.order asc");
-          return query.list();
+          List categoriesWithDuplicates = query.list();
+          Iterator it = categoriesWithDuplicates.iterator();
+          List categories = new LinkedList();
+          while (it.hasNext()) {
+              Category category = (Category)it.next();
+              if (!categories.contains(category)) {
+                categories.add(category);
+              }
+          }
+          return categories;
        }
        catch (HibernateException e)
        {
@@ -1246,6 +1256,63 @@
       }
    }
    
+    public Map findLastPostsOfTopics(Collection topics)
+       throws ModuleException
+    {
+       try
+       {
+          Session session = getSession();
+          //Query query =
+             // Old query considered as inefficient
+             //session.createQuery("from PostImpl as p where p.topic  = :topicId order by p.createDate desc");           
+            session.createQuery("select topic.lastPostDate as maxDate , topic.id " + 
+                                "from TopicImpl as topic " +
+                                "where topic.forum.id = :forumId ");
+          Iterator it = topics.iterator();
+          List lastPostDates = new ArrayList(topics.size());
+          List dates = new LinkedList();
+          while(it.hasNext()) {
+              Topic tmpTopic = (Topic)it.next();
+              dates.add(tmpTopic.getLastPostDate());
+              lastPostDates.add(new Object[]{tmpTopic.getLastPostDate(),tmpTopic.getId()});
+          }
+          Query query = session.createQuery("select post.createDate, post " +
+                                      "from PostImpl as post " +
+                                      "join fetch post.poster " +
+                                      "where post.createDate IN (:dates) " +
+                                      "order by post.createDate ");
+          query.setParameterList("dates",dates);
+          List posts = query.list();
+          Map forumPostMap = new HashMap(dates.size());
+          Iterator iterator = lastPostDates.iterator();
+          while (iterator.hasNext()) {
+              Object[] dateTopic = (Object[])iterator.next();
+              int index = Collections.binarySearch(posts,
+                                                         dateTopic,
+                                                         new Comparator() {
+                            public int compare(Object o1, Object o2) {
+                                Object[] datePostPair1 = (Object[])o1;
+                                Object[] datePostPair2 = (Object[])o2;
+                                Date postDate1 = (Date)datePostPair1[0];
+                                Date postDate2 = (Date)datePostPair2[0];
+                                return postDate1.compareTo(postDate2);
+                            }
+                        });
+              if (index==-1) {
+                continue;
+              }
+              Object[] datePostPair = (Object[])posts.get(index);
+              forumPostMap.put(dateTopic[1],datePostPair[1]);
+          }         
+          return forumPostMap;
+       }
+       catch (HibernateException e)
+       {
+          e.printStackTrace();
+          return null;
+       }
+    }
+   
     public Map findLastPostsOfForums()
        throws ModuleException
     {
@@ -1256,7 +1323,6 @@
               session.createQuery("select MAX(topic.lastPostDate) as maxDate , topic.forum.id " + 
                                   "from TopicImpl as topic " +
                                   "group by topic.forum.id ");
-          query.setCacheable(true);
           List createDates = query.list();
           Iterator it = createDates.iterator();
           List dates = new LinkedList();
@@ -1269,7 +1335,6 @@
                                       "where post.createDate IN (:dates) " +
                                       "order by post.createDate ");
           query.setParameterList("dates",dates);
-          query.setCacheable(true);
           List posts = query.list();
           Map forumPostMap = new HashMap(createDates.size());
           Iterator iterator = createDates.iterator();
@@ -1286,7 +1351,7 @@
                                 return postDate1.compareTo(postDate2);
                             }
                         });
-              if (index==-1) {
+              if (index<0) {
                 continue;
               }
               Object[] datePostPair = (Object[])posts.get(index);

Modified: labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewCategory.java
===================================================================
--- labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewCategory.java	2006-11-13 18:09:38 UTC (rev 7569)
+++ labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewCategory.java	2006-11-13 20:06:45 UTC (rev 7570)
@@ -175,9 +175,7 @@
                 while (iterator.hasNext())
                 {
                    Category currentCategory = (Category)iterator.next();
-                   if (!this.getCategories().contains(currentCategory)) {
-                       this.processCategory(currentCategory);
-                   }
+                   this.processCategory(currentCategory);
                 }
             }
         }

Modified: labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java
===================================================================
--- labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java	2006-11-13 18:09:38 UTC (rev 7569)
+++ labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewForum.java	2006-11-13 20:06:45 UTC (rev 7570)
@@ -28,8 +28,11 @@
 import java.util.Map;
 import java.util.HashMap;
 
+import java.util.LinkedList;
+
 import org.hibernate.HibernateException;
 import org.hibernate.Query;
+import org.hibernate.ScrollableResults;
 import org.hibernate.Session;
 
 import org.jboss.portal.common.util.Tools;
@@ -64,6 +67,9 @@
     private PageNavigator pageNavigator = null;
     private Collection page = new ArrayList();
     private Map topicNavigator = new HashMap();
+    private Map topicLastPosts = null;
+    private List stickyThreads = null;
+    private List announcements = null;
             
     //----------------business data being generated for use by the view components like facelets---------------------------------------------------------------------------------------
     /**
@@ -74,6 +80,10 @@
         return this.forum;
     }
     
+    public ForumsModule getModule () throws Exception {
+        return BaseController.getForumsModule();
+    }
+    
     //page navigation related methods on this bean--------------------------------------------------------------------------------------------
     /**
      * 
@@ -98,7 +108,10 @@
      */
     public Collection getAnnouncements()
     {
-        Collection announcements = new ArrayList(); 
+        if (announcements!=null) {
+            return announcements;
+        }
+        announcements = new ArrayList(); 
         try {
             announcements = this.getForumsModule().findAnnouncements(forum);
         } catch (Exception e) {
@@ -113,9 +126,19 @@
      */
     public boolean isAnnouncementsPresent()
     {
+        if (announcements!=null) {
+            if (announcements.size()>0) {
+                return true;
+            } else {
+                return false;
+            }
+        }
         boolean present = false;
         try {
-            present = this.getForumsModule().findAnnouncements(forum).size()>0;
+            announcements= this.getForumsModule().findAnnouncements(forum);
+            if (announcements!=null && announcements.size()>0) {
+                present = true;
+            }
         } catch (Exception e) {
             JSFUtil.handleException(e);
         }
@@ -128,8 +151,13 @@
      */
     public Collection getStickyThreads()
     {
-        Collection stickyThreads = new ArrayList();
-        if (this.pageNavigator.getCurrentPage()!=0) return stickyThreads;
+        if (this.pageNavigator.getCurrentPage()!=0) {
+            return new ArrayList();
+        }
+        if (stickyThreads !=null) {
+            return stickyThreads;
+        }
+        stickyThreads = new ArrayList();
         try {
             //ForumsModule fm = this.getForumsModule();
             stickyThreads = findTopics(forum,
@@ -149,10 +177,19 @@
      */
     public boolean isStickyThreadsPresent()
     {
-        if (this.pageNavigator.getCurrentPage()!=0) return false;
+        if (this.pageNavigator.getCurrentPage()!=0) {
+            return false;
+        }
+        if (stickyThreads !=null) {
+            if (stickyThreads.size()>0) {
+                return true;
+            } else {
+                return false;
+            }
+        }
         boolean present = false;
         try {
-            Collection stickyThreads = findTopics(forum,
+            stickyThreads = findTopics(forum,
                             Constants.POST_STICKY,
                             -1,
                             Integer.parseInt(this.userPreferences.getPreference(
@@ -181,6 +218,19 @@
     {     
         return this.page.size()>0;
     }
+    
+    /**
+     * @return Returns the a Map which contains TopicId:LastPost pairs.
+     */
+    public Map getTopicLastPosts() 
+    {
+        if(this.topicLastPosts==null)
+        {
+            this.topicLastPosts = new HashMap();
+        }
+        return this.topicLastPosts;
+    }    
+    
     //------------user preferences-------------------------------------------------------------------------------------------------------------
     /**
      * @return Returns the userPreferences.
@@ -223,12 +273,17 @@
         {
            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);
+              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);
-           return query.list();
+           List list = query.list();
+           return list;
         }
         catch (HibernateException e)
         {
@@ -290,11 +345,11 @@
         {
             //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(
@@ -308,21 +363,34 @@
                 //setup the page data
                 this.pageNavigator.setCurrentPage(Integer.parseInt(page));	                	                
             }
-            
             this.page = this.pageNavigator.getPage();
             
-            //setup a pageNavigator for each topic being displayed on this page            
-            for(Iterator itr=this.page.iterator();itr.hasNext();)
+            // Getting sticky topics for this page
+            Collection stickies = getStickyThreads();
+            
+            // Getting announcements
+            Collection announcements = getAnnouncements();
+            
+            Collection listOfTopics = new LinkedList();
+            
+            listOfTopics.addAll(stickies);
+            listOfTopics.addAll(announcements);
+            listOfTopics.addAll(this.page);
+            
+            // 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();
-                Collection posts = cour.getPosts();
-                if(posts!=null && !posts.isEmpty())
+                if(cour.getReplies()>0)
                 {
-	                PageNavigator topicNav = new PageNavigator(posts.toArray(),
-	                Integer.parseInt(this.userPreferences.getPreference(Constants.POSTS_TOPIC_KEY)), //this is user's posts per page preference
-	                0 //current page of the navigator
-	                );
-	                this.topicNavigator.put(cour.getId(),topicNav);
+                    PageNavigator topicNav = new PageNavigator(new Object[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
+                    );
+                    this.topicNavigator.put(cour.getId(),topicNav);
                 }
             }
         }

Modified: labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewJumpbox.java
===================================================================
--- labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewJumpbox.java	2006-11-13 18:09:38 UTC (rev 7569)
+++ labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewJumpbox.java	2006-11-13 20:06:45 UTC (rev 7570)
@@ -57,10 +57,7 @@
             }
             try
             {
-                categories = getForumsModule().findCategories();
-                if (categories!= null && !Hibernate.isInitialized(categories)) {
-                    Hibernate.initialize(categories);
-                }
+                categories = getForumsModule().findCategoriesFetchForums();
                 return categories;
             }
             catch(Exception e)

Modified: labs/jbossforums/branches/forums22/forums/src/resources/portal-forums-war/views/forums/viewforum_body.xhtml
===================================================================
--- labs/jbossforums/branches/forums22/forums/src/resources/portal-forums-war/views/forums/viewforum_body.xhtml	2006-11-13 18:09:38 UTC (rev 7569)
+++ labs/jbossforums/branches/forums22/forums/src/resources/portal-forums-war/views/forums/viewforum_body.xhtml	2006-11-13 20:06:45 UTC (rev 7570)
@@ -182,7 +182,7 @@
 	        	        <!-- view newest post image -->            			
 	                    <h:outputLink value="#{forums:outputLink(shared.links['topic'],true)}" style="text-decoration: none;" 
 	                    alt="#{resource.View_latest_post}" title="#{resource.View_latest_post}">
-							<f:param name="t" value="#{topicrow.lastPost.topic.id}"/>
+							<f:param name="t" value="#{topicrow.id}"/>
 					    	<f:verbatim>
 								<img border="0" src="#{forums:themeURL('resourceIconLatestReplyURL')}"/>
 							</f:verbatim>
@@ -238,19 +238,19 @@
 	    				<!-- last post poster on this topic -->
 	    				<c:choose>
 	        				<c:when test="#{forum.anonymous}">
-	        					${topicrow.lastPost.poster.user.userName} 
+	        					${forum.topicLastPosts[topicrow.id].poster.user.userName} 
 	        				</c:when>
 	        				<c:otherwise>				        				    
 	        					<h:outputLink value="#{forums:outputLink(shared.links['profile'],true)}">
-	        						<f:param name="uid" value="#{topicrow.lastPost.poster.user.id}"/>
-	        						<h:outputText value="${topicrow.lastPost.poster.user.userName}"/>
+	        						<f:param name="uid" value="#{forum.topicLastPosts[topicrow.id].poster.user.id}"/>
+	        						<h:outputText value="${forum.topicLastPosts[topicrow.id].poster.user.userName}"/>
 	        					</h:outputLink>
 	        				</c:otherwise>
 	    				</c:choose>	
 	    				<!-- link to the last post in the topic -->		                        
 	    				<h:outputLink value="#{forums:outputLink(shared.links['topic'],true)}" style="text-decoration: none;"
 	    				alt="#{resource.View_latest_post}" title="#{resource.View_latest_post}">
-	    					<f:param name="t" value="#{topicrow.lastPost.topic.id}"/>
+	    					<f:param name="t" value="#{topicrow.id}"/>
 	    				    <f:verbatim>
 	    						<img border="0" src="#{forums:themeURL('resourceIconLatestReplyURL')}"/>
 	    					</f:verbatim>
@@ -283,7 +283,7 @@
 	        	        <!-- view newest post image -->            			
 	                    <h:outputLink value="#{forums:outputLink(shared.links['topic'],true)}" style="text-decoration: none;" 
 	                    alt="#{resource.View_latest_post}" title="#{resource.View_latest_post}">
-							<f:param name="t" value="#{topicrow.lastPost.topic.id}"/>
+							<f:param name="t" value="#{topicrow.id}"/>
 					    	<f:verbatim>
 								<img border="0" src="#{forums:themeURL('resourceIconLatestReplyURL')}"/>
 							</f:verbatim>
@@ -339,19 +339,19 @@
 	    				<!-- last post poster on this topic -->
 	    				<c:choose>
 	        				<c:when test="#{forum.anonymous}">
-	        					${topicrow.lastPost.poster.user.userName} 
+	        					${forum.topicLastPosts[topicrow.id].poster.user.userName} 
 	        				</c:when>
 	        				<c:otherwise>				        				    
 	        					<h:outputLink value="#{forums:outputLink(shared.links['profile'],true)}">
-	        						<f:param name="uid" value="#{topicrow.lastPost.poster.user.id}"/>
-	        						<h:outputText value="${topicrow.lastPost.poster.user.userName}"/>
+	        						<f:param name="uid" value="#{forum.topicLastPosts[topicrow.id].poster.user.id}"/>
+	        						<h:outputText value="${forum.topicLastPosts[topicrow.id].poster.user.userName}"/>
 	        					</h:outputLink>
 	        				</c:otherwise>
 	    				</c:choose>	
 	    				<!-- link to the last post in the topic -->		                        
 	    				<h:outputLink value="#{forums:outputLink(shared.links['topic'],true)}" style="text-decoration: none;"
 	    				alt="#{resource.View_latest_post}" title="#{resource.View_latest_post}">
-	    					<f:param name="t" value="#{topicrow.lastPost.topic.id}"/>
+	    					<f:param name="t" value="#{topicrow.id}"/>
 	    				    <f:verbatim>
 	    						<img border="0" src="#{forums:themeURL('resourceIconLatestReplyURL')}"/>
 	    					</f:verbatim>
@@ -384,7 +384,7 @@
 	        	        <!-- view newest post image -->            			
 	                    <h:outputLink value="#{forums:outputLink(shared.links['topic'],true)}" style="text-decoration: none;" 
 	                    alt="#{resource.View_latest_post}" title="#{resource.View_latest_post}">
-							<f:param name="t" value="#{topicrow.lastPost.topic.id}"/>
+							<f:param name="t" value="#{topicrow.id}"/>
 					    	<f:verbatim>
 								<img border="0" src="#{forums:themeURL('resourceIconLatestReplyURL')}"/>
 							</f:verbatim>
@@ -439,19 +439,19 @@
 	    				<!-- last post poster on this topic -->
 	    				<c:choose>
 	        				<c:when test="#{forum.anonymous}">
-	        					${topicrow.lastPost.poster.user.userName} 
+	        					${forum.topicLastPosts[topicrow.id].poster.user.userName} 
 	        				</c:when>
 	        				<c:otherwise>				        				    
 	        					<h:outputLink value="#{forums:outputLink(shared.links['profile'],true)}">
-	        						<f:param name="uid" value="#{topicrow.lastPost.poster.user.id}"/>
-	        						<h:outputText value="${topicrow.lastPost.poster.user.userName}"/>
+	        						<f:param name="uid" value="#{forum.topicLastPosts[topicrow.id].poster.user.id}"/>
+	        						<h:outputText value="${forum.topicLastPosts[topicrow.id].poster.user.userName}"/>
 	        					</h:outputLink>
 	        				</c:otherwise>
 	    				</c:choose>	
 	    				<!-- link to the last post in the topic -->		                        
 	    				<h:outputLink value="#{forums:outputLink(shared.links['topic'],true)}" style="text-decoration: none;"
 	    				alt="#{resource.View_latest_post}" title="#{resource.View_latest_post}">
-	    					<f:param name="t" value="#{topicrow.lastPost.topic.id}"/>
+	    					<f:param name="t" value="#{topicrow.id}"/>
 	    				    <f:verbatim>
 	    						<img border="0" src="#{forums:themeURL('resourceIconLatestReplyURL')}"/>
 	    					</f:verbatim>
@@ -689,7 +689,7 @@
 	                        </forums:isAllowedOtherwise>
                         </forums:isAllowedChoose>
 	      			</span>	      			
-                    <ui:include src="/views/jumpbox.xhtml"/>	      	    	
+                    <ui:include src="/views/jumpbox.xhtml" />
 	      	</span>
 	      </td>
 	   </tr>




More information about the jboss-svn-commits mailing list