[jboss-svn-commits] JBL Code SVN: r7541 - 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/category

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Nov 10 20:50:16 EST 2006


Author: unibrew
Date: 2006-11-10 20:50:11 -0500 (Fri, 10 Nov 2006)
New Revision: 7541

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/resources/portal-forums-war/views/category/viewcategory_body.xhtml
Log:
[JBFORUMS-135] Finally main view of Forums is very efficient.

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-11 00:47:16 UTC (rev 7540)
+++ labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ForumsModule.java	2006-11-11 01:50:11 UTC (rev 7541)
@@ -20,10 +20,12 @@
 
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author <a href="mailto:theute at jboss.org">Thomas Heute </a>
  * @author <a href="mailto:boleslaw.dawidowicz at jboss.com">Boleslaw Dawidowicz</a>
+ * @author <a href="mailto:ryszard.kozmik at jboss.com">Ryszard Kozmik</a>
  * @version $Revision: 1018 $
  */
 public interface ForumsModule
@@ -238,6 +240,15 @@
    List findCategories()
       throws ModuleException;
 
+    /**
+     * Get all the categories of forums and fetch forums.
+     *
+     * @return All the categories
+     * @throws ModuleException
+     */
+    List findCategoriesFetchForums()
+       throws ModuleException;
+
    /**
     * DOCUMENT_ME
     *
@@ -387,6 +398,16 @@
    Category findCategoryById(Integer categoryID)
       throws ModuleException;
 
+    /**
+     * DOCUMENT_ME
+     *
+     * @param categoryID DOCUMENT_ME
+     * @return DOCUMENT_ME
+     * @throws ModuleException DOCUMENT_ME
+     */
+   Category findCategoryByIdFetchForums(Integer categoryID)
+      throws ModuleException;
+
    /**
     * DOCUMENT_ME
     *
@@ -449,6 +470,8 @@
    Post findFirstPost(Topic topic) throws ModuleException;
 
    Post findLastPost(Topic topic) 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-11 00:47:16 UTC (rev 7540)
+++ labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java	2006-11-11 01:50:11 UTC (rev 7541)
@@ -11,7 +11,6 @@
 package org.jboss.portlet.forums.impl;
 
 import org.apache.log4j.Logger;
-import org.jboss.portal.common.util.Tools;
 import org.jboss.portal.common.command.result.Result;
 import org.jboss.portal.common.command.CommandException;
 import org.jboss.portal.common.command.filter.CompositeFilter;
@@ -45,8 +44,13 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.Map;
 
+
 /**
  * @author <a href="mailto:theute at jboss.org">Thomas Heute </a>
  * @author <a href="mailto:boleslaw.dawidowicz at jboss.com">Boleslaw Dawidowicz</a>
@@ -208,7 +212,40 @@
          throw new IllegalArgumentException("id cannot be null");
       }
    }
+   
+    public Category findCategoryByIdFetchForums(Integer id)
+       throws ModuleException
+    {
+       if (id != null)
+       {
+          try
+          {
+             Session session = getSession();
+             Query query = session.createQuery(" from CategoryImpl as c " +
+                                                " join fetch c.forums " +
+                                                " where c.id=:categoryId ");
+             query.setParameter("categoryId",id);
+             Category category = (Category)query.uniqueResult();
+             if (category == null)
+             {
+                throw new ModuleException("No category found for " + id);
+             }
 
+             return category;
+          }
+          catch (HibernateException e)
+          {
+             String message = "Cannot find category by id " + id;
+             log.error(message, e);
+             throw new ModuleException(message, e);
+          }
+       }
+       else
+       {
+          throw new IllegalArgumentException("id cannot be null");
+       }
+    }
+
    public Poster findPosterByUserId(String userId)
       throws ModuleException
    {
@@ -277,6 +314,26 @@
          throw new ModuleException(message, e);
       }
    }
+   
+    public List findCategoriesFetchForums()
+       throws ModuleException
+    {
+       try
+       {
+          Session session = getSession();
+          Query query = session.createQuery(" select c " +
+                                            " from CategoryImpl as c " +
+                                            " join fetch c.forums " +
+                                            " order by c.order asc");
+          return query.list();
+       }
+       catch (HibernateException e)
+       {
+          String message = "Cannot find categories";
+          log.error(message, e);
+          throw new ModuleException(message, e);
+       }
+    }
 
    public List findForums()
       throws ModuleException
@@ -1120,7 +1177,7 @@
                                 "from PostImpl as p                 " +
                                 "join fetch p.poster                " + 
                                 "where p.createDate = (             " + 
-                                "   select MAX(topic.lastPostDate)  " + 
+                                "   select DISTINCT MAX(topic.lastPostDate)  " + 
                                 "   from TopicImpl as topic         " + 
                                 "   where topic.forum = :forumId    " + 
                                 ")                                  " );
@@ -1178,6 +1235,7 @@
          query.setString("topicId", "" + topic.getId());
          query.setFirstResult(0);
          query.setMaxResults(1);
+         query.setCacheable(true);
          Post lastPost = (Post)query.uniqueResult();
          return lastPost;
       }
@@ -1187,6 +1245,61 @@
          return null;
       }
    }
+   
+    public Map findLastPostsOfForums()
+       throws ModuleException
+    {
+       try
+       {
+          Session session = getSession();
+          Query query =             
+              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();
+          while(it.hasNext()) {
+              dates.add(((Object[])it.next())[0]);
+          }
+          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);
+          query.setCacheable(true);
+          List posts = query.list();
+          Map forumPostMap = new HashMap(createDates.size());
+          Iterator iterator = createDates.iterator();
+          while (iterator.hasNext()) {
+              Object[] dateForum = (Object[])iterator.next();
+              int index = Collections.binarySearch(posts,
+                                                         dateForum,
+                                                         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(dateForum[1],datePostPair[1]);
+          }         
+          return forumPostMap;
+       }
+       catch (HibernateException e)
+       {
+          e.printStackTrace();
+          return null;
+       }
+    }
 
    public List findForumWatchByUser(User user) throws ModuleException
    {

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-11 00:47:16 UTC (rev 7540)
+++ labs/jbossforums/branches/forums22/forums/src/main/org/jboss/portlet/forums/ui/view/ViewCategory.java	2006-11-11 01:50:11 UTC (rev 7541)
@@ -56,6 +56,7 @@
     private Map forums = null;
     private Map forumImages = null;
     private Map forumImageDescriptions = null;
+    private Map forumLastPosts = null;
     
     //----------------bean configuration supplied by the forums-config.xml---------------------------------------------------------------------------------------------
     
@@ -114,6 +115,19 @@
         }
         return forumImages;
     }
+    
+    /**
+     * @return Returns the a Map which contains ForumId:LastPost pairs.
+     */
+    public Map getForumLastPosts() 
+    {
+        if(this.forumLastPosts==null)
+        {
+            this.forumLastPosts = new HashMap();
+        }
+        return this.forumLastPosts;
+    }
+    
     //------------user preferences-------------------------------------------------------------------------------------------------------------
     
     //-------------------------------------------------------------------------------------------------------------------------------------                
@@ -147,30 +161,30 @@
         {
             categoryId = Integer.parseInt(c);
         }
+
+        this.forumLastPosts = this.getForumsModule().findLastPostsOfForums();
         
-        
         //setup category related data to be displayed
         if(categoryId==-1)
         {    
             //process a default level category
-            Collection cour = BaseController.getForumsModule().findCategories();
+            Collection cour = BaseController.getForumsModule().findCategoriesFetchForums();
             if(cour!=null)
             {
-                if (!Hibernate.isInitialized(cour)) {
-                    Hibernate.initialize(cour);
-                }
                 Iterator iterator = cour.iterator();
                 while (iterator.hasNext())
                 {
-                   Category currentCategory = (Category)iterator.next();	
-                   this.processCategory(currentCategory);
+                   Category currentCategory = (Category)iterator.next();
+                   if (!this.getCategories().contains(currentCategory)) {
+                       this.processCategory(currentCategory);
+                   }
                 }
             }
         }
         else
         {
             //process the specifed category 
-            Category currentCategory = BaseController.getForumsModule().findCategoryById(new Integer(categoryId));
+            Category currentCategory = BaseController.getForumsModule().findCategoryByIdFetchForums(new Integer(categoryId));
             if(currentCategory!=null)
             {
                 this.processCategory(currentCategory);
@@ -189,17 +203,14 @@
            this.getCategories().add(category);
            
            //process the forums associated with this category
-           Collection forums = BaseController.getForumsModule().findForumsByCategoryId(category.getId());
-           if (forums!=null && !Hibernate.isInitialized(forums)) {
-               Hibernate.initialize(forums);
-           }
+           Collection forums = category.getForums();
            Iterator forumsIterator = forums.iterator();
            Collection categoryForums = new ArrayList();
            while (forumsIterator.hasNext())
            {
               Forum currentForum = (Forum)forumsIterator.next();
               categoryForums.add(currentForum);
-                                                                         
+                                                         
               //setup folderLook based on whats specified in the theme
               String folderImage = ThemeHelper.getInstance().getResourceForumURL();
               String folderAlt = "No_new_posts"; //bundle key

Modified: labs/jbossforums/branches/forums22/forums/src/resources/portal-forums-war/views/category/viewcategory_body.xhtml
===================================================================
--- labs/jbossforums/branches/forums22/forums/src/resources/portal-forums-war/views/category/viewcategory_body.xhtml	2006-11-11 00:47:16 UTC (rev 7540)
+++ labs/jbossforums/branches/forums22/forums/src/resources/portal-forums-war/views/category/viewcategory_body.xhtml	2006-11-11 01:50:11 UTC (rev 7541)
@@ -91,22 +91,22 @@
 		        <td class="row2" align="center" valign="middle" height="50" nowrap="nowrap">
 		        	<span class="gensmall">        	    	
 		        	    <c:choose>
-		        	            <c:when test="#{(forums:lastPost(category.module,forumrow))!=null}">
-			        				#{(forums:lastPost(category.module,forumrow)).createDate}
+		        	            <c:when test="#{category.forumLastPosts[forumrow.id]!=null}">
+			        				#{category.forumLastPosts[forumrow.id].createDate}
 			        				<br/>
 			        				<c:choose>
 				        				<c:when test="#{category.anonymous}">
-				        					${(forums:lastPost(category.module,forumrow)).poster.user.userName} 
+				        					${category.forumLastPosts[forumrow.id].poster.user.userName} 
 				        				</c:when>
 				        				<c:otherwise>				        				    
 				        					<h:outputLink value="#{forums:outputLink(shared.links['profile'],true)}">
-				        						<f:param name="uid" value="#{(forums:lastPost(category.module,forumrow)).poster.user.id}"/>
-				        						<h:outputText value="${(forums:lastPost(category.module,forumrow)).poster.user.userName}"/>
+				        						<f:param name="uid" value="#{category.forumLastPosts[forumrow.id].poster.user.id}"/>
+				        						<h:outputText value="${category.forumLastPosts[forumrow.id].poster.user.userName}"/>
 				        					</h:outputLink>
 				        				</c:otherwise>
 			        				</c:choose>			                        
 			        				<h:outputLink value="#{forums:outputLink(shared.links['topic'],true)}" style="text-decoration: none;">
-			        					<f:param name="t" value="#{(forums:lastPost(category.module,forumrow)).topic.id}"/>
+			        					<f:param name="t" value="#{category.forumLastPosts[forumrow.id].topic.id}"/>
 			        				    <f:verbatim>
 			        						<img border="0" src="#{forums:themeURL('resourceIconLatestReplyURL')}"/>
 			        					</f:verbatim>




More information about the jboss-svn-commits mailing list