[jboss-svn-commits] JBL Code SVN: r7543 - in labs/jbossforums/branches/forums26/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 21:27:33 EST 2006


Author: unibrew
Date: 2006-11-10 21:27:28 -0500 (Fri, 10 Nov 2006)
New Revision: 7543

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/view/ViewCategory.java
   labs/jbossforums/branches/forums26/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/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-11 02:17:49 UTC (rev 7542)
+++ labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ForumsModule.java	2006-11-11 02:27:28 UTC (rev 7543)
@@ -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: 3217 $
  */
 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
     *
@@ -386,6 +397,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/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-11 02:17:49 UTC (rev 7542)
+++ labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java	2006-11-11 02:27:28 UTC (rev 7543)
@@ -39,6 +39,12 @@
 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;
+
 import org.jboss.portlet.forums.command.ActionCommand;
 import org.jboss.portlet.forums.command.CommandException;
 import org.jboss.portlet.forums.command.filter.CommandFilter;
@@ -208,6 +214,40 @@
       }
    }
 
+    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
    {
@@ -276,6 +316,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
@@ -1119,7 +1179,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    " + 
                                      ")                                  " );
@@ -1177,6 +1237,7 @@
          query.setString("topicId", "" + topic.getId());
          query.setFirstResult(0);
          query.setMaxResults(1);
+         query.setCacheable(true);
          Post lastPost = (Post)query.uniqueResult();
          return lastPost;
       }
@@ -1187,6 +1248,61 @@
       }
    }
 
+    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
    {
       try

Modified: labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/ViewCategory.java
===================================================================
--- labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/ViewCategory.java	2006-11-11 02:17:49 UTC (rev 7542)
+++ labs/jbossforums/branches/forums26/forums/src/main/org/jboss/portlet/forums/ui/view/ViewCategory.java	2006-11-11 02:27:28 UTC (rev 7543)
@@ -55,6 +55,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---------------------------------------------------------------------------------------------
     
@@ -113,6 +114,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-------------------------------------------------------------------------------------------------------------
     
     //-------------------------------------------------------------------------------------------------------------------------------------                
@@ -137,81 +151,78 @@
     /**
      *  this generates the category and its corresponding forums
      */
-    private void execute() throws Exception
-    {                
-        //try to extract categoryId
-        int categoryId = -1;
-        String c = ForumUtil.getParameter(Constants.p_categoryId);
-        if(c!=null && c.trim().length()>0)
+     private void execute() throws Exception
+     {                
+         //try to extract categoryId
+         int categoryId = -1;
+         String c = ForumUtil.getParameter(Constants.p_categoryId);
+         if(c!=null && c.trim().length()>0)
+         {
+             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().findCategoriesFetchForums();
+             if(cour!=null)
+             {
+                 Iterator iterator = cour.iterator();
+                 while (iterator.hasNext())
+                 {
+                    Category currentCategory = (Category)iterator.next();
+                    if (!this.getCategories().contains(currentCategory)) {
+                        this.processCategory(currentCategory);
+                    }
+                 }
+             }
+         }
+         else
+         {
+             //process the specifed category 
+             Category currentCategory = BaseController.getForumsModule().findCategoryByIdFetchForums(new Integer(categoryId));
+             if(currentCategory!=null)
+             {
+                 this.processCategory(currentCategory);
+             }
+         }
+     }
+     
+     /**
+      * 
+      * @return
+      */
+     private void processCategory(Category category) throws Exception
+     {
+        if(category!=null)
         {
-            categoryId = Integer.parseInt(c);
-        }
-        
-        
-        //setup category related data to be displayed
-        if(categoryId==-1)
-        {    
-            //process a default level category
-            Collection cour = BaseController.getForumsModule().findCategories();
-            if(cour!=null)
+            this.getCategories().add(category);
+            
+            //process the forums associated with this category
+            Collection forums = category.getForums();
+            Iterator forumsIterator = forums.iterator();
+            Collection categoryForums = new ArrayList();
+            while (forumsIterator.hasNext())
             {
-                if (!Hibernate.isInitialized(cour)) {
-                    Hibernate.initialize(cour);
-                }
-                Iterator iterator = cour.iterator();
-                while (iterator.hasNext())
-                {
-                   Category currentCategory = (Category)iterator.next();        
-                   this.processCategory(currentCategory);
-                }
-            }
+               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
+               if (currentForum.getStatus() == Constants.FORUM_LOCKED)
+               {
+                  folderImage = ThemeHelper.getInstance().getResourceForumLockedURL();
+                  folderAlt = "Forum_locked"; //bundle key
+               }              
+               this.getForumImages().put(currentForum.getId(),folderImage);
+               this.getForumImageDescriptions().put(currentForum.getId(),folderAlt);                                                                                    
+            }           
+            this.getForums().put(category.getId(),categoryForums);
         }
-        else
-        {
-            //process the specifed category 
-            Category currentCategory = BaseController.getForumsModule().findCategoryById(new Integer(categoryId));
-            if(currentCategory!=null)
-            {
-                this.processCategory(currentCategory);
-            }
-        }
-    }
-    
-    /**
-     * 
-     * @return
-     */
-    private void processCategory(Category category) throws Exception
-    {
-       if(category!=null)
-       {
-           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);
-           }
-           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
-              if (currentForum.getStatus() == Constants.FORUM_LOCKED)
-              {
-                 folderImage = ThemeHelper.getInstance().getResourceForumLockedURL();
-                 folderAlt = "Forum_locked"; //bundle key
-              }              
-              this.getForumImages().put(currentForum.getId(),folderImage);
-              this.getForumImageDescriptions().put(currentForum.getId(),folderAlt);                                                                                    
-           }           
-           this.getForums().put(category.getId(),categoryForums);
-       }
-    }    
+     }    
     //-------------------------------------------------------------------------------------------------------------------------------------    
 }

Modified: labs/jbossforums/branches/forums26/forums/src/resources/portal-forums-war/views/category/viewcategory_body.xhtml
===================================================================
--- labs/jbossforums/branches/forums26/forums/src/resources/portal-forums-war/views/category/viewcategory_body.xhtml	2006-11-11 02:17:49 UTC (rev 7542)
+++ labs/jbossforums/branches/forums26/forums/src/resources/portal-forums-war/views/category/viewcategory_body.xhtml	2006-11-11 02:27:28 UTC (rev 7543)
@@ -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:dateStr((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