[jboss-svn-commits] JBL Code SVN: r16872 - labs/jbossforums/branches/multipleforums110P26/forums/src/main/org/jboss/portlet/forums/impl.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Nov 27 19:06:36 EST 2007


Author: sviluppatorefico
Date: 2007-11-27 19:06:35 -0500 (Tue, 27 Nov 2007)
New Revision: 16872

Modified:
   labs/jbossforums/branches/multipleforums110P26/forums/src/main/org/jboss/portlet/forums/impl/CategoryImpl.java
   labs/jbossforums/branches/multipleforums110P26/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java
Log:
[JBFORUMS-245] added multiple forums updates

Modified: labs/jbossforums/branches/multipleforums110P26/forums/src/main/org/jboss/portlet/forums/impl/CategoryImpl.java
===================================================================
--- labs/jbossforums/branches/multipleforums110P26/forums/src/main/org/jboss/portlet/forums/impl/CategoryImpl.java	2007-11-28 00:05:14 UTC (rev 16871)
+++ labs/jbossforums/branches/multipleforums110P26/forums/src/main/org/jboss/portlet/forums/impl/CategoryImpl.java	2007-11-28 00:06:35 UTC (rev 16872)
@@ -17,6 +17,9 @@
 import org.jboss.portlet.forums.model.Category;
 import org.jboss.portlet.forums.model.Forum;
 
+//Luca Stancapiano - import
+import org.jboss.portlet.forums.model.ForumInstance;
+
 /**
  * Category of forums.
  *
@@ -32,6 +35,10 @@
    Category
 {
    private List forums;
+   /* Luca Stancapiano - forumInstance field to tie category to it, so that
+      each forum can to get different categories
+   */
+   private ForumInstance forumInstance;
    private int order;
    private String title;
    private Integer id;
@@ -133,4 +140,29 @@
    {
       this.id = id;
    }
+   
+   /* Luca Stancapiano start - I add accessors to manage ForumInstance field 
+       so Category is tied to the Forum Instance. Now each category belong to a
+       Forum Instance so that we can to get different instances of forums with different
+       categories and different sub objects
+   */
+   /**
+    * @hibernate.many-to-one column="jbp_forum_instance_id"
+    * class="org.jboss.portlet.forums.impl.ForumInstanceImpl"
+    */
+   public ForumInstance getForumInstance()
+   {
+      return forumInstance;
+   }
+
+   /**
+    * DOCUMENT_ME
+    *
+    * @param forum DOCUMENT_ME
+    */
+   public void setForumInstance(ForumInstance forumInstance)
+   {
+      this.forumInstance = forumInstance;
+   }
+   // Luca Stancapiano end
 }
\ No newline at end of file

Modified: labs/jbossforums/branches/multipleforums110P26/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java
===================================================================
--- labs/jbossforums/branches/multipleforums110P26/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java	2007-11-28 00:05:14 UTC (rev 16871)
+++ labs/jbossforums/branches/multipleforums110P26/forums/src/main/org/jboss/portlet/forums/impl/ForumsModuleImpl.java	2007-11-28 00:06:35 UTC (rev 16872)
@@ -37,6 +37,8 @@
 import org.jboss.portlet.forums.model.Attachment;
 import org.jboss.portlet.forums.model.Category;
 import org.jboss.portlet.forums.model.Forum;
+// Luca Stancapiano -- import
+import org.jboss.portlet.forums.model.ForumInstance;
 import org.jboss.portlet.forums.model.ForumWatch;
 import org.jboss.portlet.forums.model.Message;
 import org.jboss.portlet.forums.model.Poll;
@@ -72,7 +74,7 @@
    private int floodInterval;
 
    private String guestUserName;
-   
+
    private String fromAddress;
 
 // private final ResourceCacheInvalidatorInterceptor resourceInterceptor;
@@ -156,7 +158,7 @@
    {
        this.guestUserName = guestUserName;
    }
-   
+
    /**
     * @jmx.managed-attribute
     */
@@ -355,71 +357,83 @@
       }
    }
 
-   public List findCategories()
-      throws ModuleException
-   {
-      try
-      {
-         Session session = getSession();
-         Query query = session.createQuery("from CategoryImpl as c order by c.order asc");
-		 query.setCacheable(true);
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find categories";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
-   
-    public List findCategoriesFetchForums()
-       throws ModuleException
-    {
-       try
-       {
-          Session session = getSession();
-          Query query = session.createQuery(" select c " +
-                                            " from CategoryImpl as c " +
-                                            " left outer join fetch c.forums " +
-                                            " order by c.order asc");
-		  query.setCacheable(true);
-          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)
-       {
-          String message = "Cannot find categories";
-          log.error(message, e);
-          throw new ModuleException(message, e);
-       }
-    }
 
-   public List findForums()
-      throws ModuleException
-   {
-      try
-      {
-         Session session = getSession();
-         Query query = session.createQuery("from ForumImpl as f order by f.order asc");
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find forums";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
+   /* Luca Stancapiano start - findCategories, findCategoriesFetchForums,
+     findForums methods need to a forumInstanceId argument 
+     to take only objects of a specific forum instance 
+   */
 
+	public List findCategories(Integer indexInstance) throws ModuleException {
+		try {
+			Session session = getSession();
+			Query query = session
+					.createQuery("from CategoryImpl as c " +
+							// Luca Stancapiano
+							" where c.forumInstance.id := forumInstanceId " +
+							"order by c.order asc");
+                        query.setCacheable(true);
+			// Luca Stancapiano
+			query.setParameter("forumInstanceId", indexInstance);
+			return query.list();
+		} catch (HibernateException e) {
+			String message = "Cannot find categories";
+			log.error(message, e);
+			throw new ModuleException(message, e);
+		}
+	}
+	
+	public List findCategoriesFetchForums(Integer indexInstance)
+			throws ModuleException {
+		try {
+			Session session = getSession();
+			Query query = session.createQuery(" select c "
+					+ " from CategoryImpl as c "
+					+ " left outer join fetch c.forums "
+                    // Luca Stancapiano
+					+ " where c.forumInstance.id = :forumInstanceId "
+					+ " order by c.order asc");
+
+                        query.setCacheable(true);
+			// Luca Stancapiano
+			query.setParameter("forumInstanceId", indexInstance);
+			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) {
+			String message = "Cannot find categories";
+			log.error(message, e);
+			throw new ModuleException(message, e);
+		}
+	}
+
+	public List findForums(Integer indexInstance) throws ModuleException {
+		try {
+			Session session = getSession();
+			Query query = session
+					.createQuery("from ForumImpl as f " +
+							// Luca Stancapiano
+							"where f.forumInstance.id := forumInstanceId " +
+							"order by f.order asc");
+			
+			// Luca Stancapiano
+			query.setParameter("forumInstanceId", indexInstance);
+			return query.list();
+		} catch (HibernateException e) {
+			String message = "Cannot find forums";
+			log.error(message, e);
+			throw new ModuleException(message, e);
+		}
+	}
+
+	// Luca Stancapiano end
+
    public List findForumsByCategoryId(Integer categoryId)
       throws ModuleException
    {
@@ -460,24 +474,30 @@
       }
    }
 
-   public List findTopics() throws ModuleException
-   {
-      try
-      {
-         Session session = getSession();
-         Query query =
-            session.createQuery("from TopicImpl");
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find topics";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
+   /* Luca Stancapiano - findTopics
+      method need to a forumInstanceId argument 
+      to take only topics of a specific forum instance 
+   */
+   public List findTopics(Integer indexInstance) throws ModuleException {
+		try {
+			Session session = getSession();
+			Query query = session.createQuery("from TopicImpl as t " +
+					// Luca Stancapiano
+					"where t.forum.forumInstance.id := forumInstanceId "
+			);
+			
+			// Luca Stancapiano
+			query.setParameter("forumInstanceId", indexInstance);
+			return query.list();
+		} catch (HibernateException e) {
+			String message = "Cannot find topics";
+			log.error(message, e);
+			throw new ModuleException(message, e);
+		}
 
+	}
 
-   }
+	// Luca Stancapiano end
 
     /**
      *
@@ -610,91 +630,101 @@
       return null;
    }
 
-   public List findTopicsHot(int replies, int limit)
-      throws ModuleException
-   {
-      try
-      {
-         //"SELECT OBJECT(t) FROM topic AS t WHERE t.replies > ?1 ORDER BY t.creationDate DESC LIMIT ?2"
-         Session session = getSession();
-         Query query =
-            session.createQuery("from TopicImpl as t where t.replies > :replies order by t.lastPostDate desc");
-         query.setMaxResults(limit);
-         query.setInteger("replies", replies);
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find topics";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
 
-   public List findTopicsByLatestPosts(int limit)
-      throws ModuleException
-   {
-      try
-      {
-         //SELECT DISTINCT OBJECT(t) FROM topic AS t ORDER BY t.lastPostDate DESC LIMIT ?1"
 
-         Session session = getSession();
-         Query query =
-            session.createQuery("from TopicImpl as t order by t.lastPostDate desc");
-         query.setMaxResults(limit);
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find topics";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
+   /* Luca Stancapiano start - findTopicsHot, findTopicsByLatestPosts, findTopicsHottest,
+      findTopicsMostViewed methods need to a forumInstanceId argument 
+      to take only topics of a specific forum instance 
+   */
 
-   public List findTopicsHottest(Date after, int limit)
-      throws ModuleException
-   {
-      try
-      {
-         //"SELECT OBJECT(t) FROM topic AS t WHERE t.lastPostDate > ?1 ORDER BY t.replies DESC LIMIT ?2"
-         Session session = getSession();
-         Query query =
-            session.createQuery("from TopicImpl as t where t.lastPostDate > :after order by t.replies desc");
-         query.setMaxResults(limit);
-         query.setDate("after", after);
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find topics";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
+	public List findTopicsHot(int replies, int limit, Integer indexInstance) throws ModuleException {
+		try {
+           //"SELECT OBJECT(t) FROM topic AS t WHERE t.replies > ?1 ORDER BY t.creationDate DESC LIMIT ?2"
+			Session session = getSession();
+			Query query = session
+					.createQuery("from TopicImpl as t where t.replies > :replies "+
+							// Luca Stancapiano
+							"and t.forum.forumInstance.id := forumInstanceId "+
+							"order by t.lastPostDate desc");
+			query.setMaxResults(limit);
+			query.setInteger("replies", replies);		
+			// Luca Stancapiano
+			query.setParameter("forumInstanceId", indexInstance);
+			return query.list();
+		} catch (HibernateException e) {
+			String message = "Cannot find topics";
+			log.error(message, e);
+			throw new ModuleException(message, e);
+		}
+	}
+	
+	public List findTopicsByLatestPosts(int limit, Integer indexInstance)
+			throws ModuleException {
+		try {
+			//SELECT DISTINCT OBJECT(t) FROM topic AS t ORDER BY t.lastPostDate DESC LIMIT ?1"
 
-   public List findTopicsMostViewed(Date after, int limit)
-      throws ModuleException
-   {
-      try
-      {
-         //"SELECT OBJECT(t) FROM topic AS t WHERE t.lastPostDate > ?1 ORDER BY t.views DESC LIMIT ?2"
-         Session session = getSession();
-         Query query =
-            session.createQuery("from TopicImpl as t where t.lastPostDate > :after order by t.viewCount desc");
-         query.setMaxResults(limit);
-         query.setDate("after", after);
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find topics";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
+			Session session = getSession();
+			Query query =
+			// Luca Stancapiano
+			session
+					.createQuery("from TopicImpl as t where t.forum.category.forumInstance.id = :forumInstanceId order by t.lastPostDate desc");
+			query.setMaxResults(limit);
+			// Luca Stancapiano
+			query.setParameter("forumInstanceId", indexInstance);
+			return query.list();
+		} catch (HibernateException e) {
+			String message = "Cannot find topics";
+			log.error(message, e);
+			throw new ModuleException(message, e);
+		}
+	}
 
+	public List findTopicsHottest(Date after, int limit, Integer indexInstance) throws ModuleException {
+		try {
+			//"SELECT OBJECT(t) FROM topic AS t WHERE t.lastPostDate > ?1 ORDER BY t.replies DESC LIMIT ?2"
+			Session session = getSession();
+			Query query = session
+					.createQuery("from TopicImpl as t where t.lastPostDate > :after "+
+							// Luca Stancapiano
+							"and t.forum.forumInstance.id := forumInstanceId "+
+							"order by t.replies desc");
+			query.setMaxResults(limit);
+			query.setDate("after", after);
+			// Luca Stancapiano
+			query.setParameter("forumInstanceId", indexInstance);
+			return query.list();
+		} catch (HibernateException e) {
+			String message = "Cannot find topics";
+			log.error(message, e);
+			throw new ModuleException(message, e);
+		}
+	}
 
+	public List findTopicsMostViewed(Date after, int limit, Integer indexInstance)
+			throws ModuleException {
+		try {
+			//"SELECT OBJECT(t) FROM topic AS t WHERE t.lastPostDate > ?1 ORDER BY t.views DESC LIMIT ?2"
+			Session session = getSession();
+			Query query = session
+					.createQuery("from TopicImpl as t where t.lastPostDate > :after " +
+							// Luca Stancapiano
+							"and t.forum.forumInstance.id := forumInstanceId "+
+							"order by t.viewCount desc");
+			query.setMaxResults(limit);
+			query.setDate("after", after);
+			// Luca Stancapiano
+			query.setParameter("forumInstanceId", indexInstance);
+			return query.list();
+		} catch (HibernateException e) {
+			String message = "Cannot find topics";
+			log.error(message, e);
+			throw new ModuleException(message, e);
+		}
+	}
+
+	// Luca Stancapiano end
+
+
    public Post createTopic(Forum forum,
                            Message message,
                            Date creationDate,
@@ -880,42 +910,48 @@
       }
    }
 
-   private int getLastCategoryOrder()
-   {
-      try
-      {
-         Session session = getSession();
-         Query query = session.createQuery("select max(c.order) from CategoryImpl as c");
-         Integer lastCategoryOrder = (Integer)query.uniqueResult();
-         return (lastCategoryOrder != null) ? lastCategoryOrder.intValue() : 0;
-      }
-      catch (HibernateException e)
-      {
-         return 0;
-      }
-   }
 
-   public Category createCategory(String name)
-      throws ModuleException
-   {
-      try
-      {
-         Session session = getSession();
 
-         CategoryImpl category = new CategoryImpl();
-         category.setTitle(name);
-         category.setOrder(getLastCategoryOrder() + 10);
-         session.save(category);
-         return category;
-      }
-      catch (HibernateException e)
-      {
-         String errorMessage = "Cannot create topic";
-         log.error(errorMessage, e);
-         throw new ModuleException(errorMessage, e);
-      }
-   }
+   /* Luca Stancapiano start - getLastCategoryOrder and createCategory
+      methods need to a forumInstanceId argument 
+      to take only categories of a specific forum instance 
+   */
+	private int getLastCategoryOrder(Integer indexInstance) {
+		try {
+			Session session = getSession();
+			// Luca Stancapiano start
+			Query query = session
+					.createQuery("select max(c.order) from CategoryImpl as c where c.forumInstanceId = :indexInstance");
+			query.setParameter("forumInstanceId", indexInstance);
+			// Luca Stancapiano end
+			Integer lastCategoryOrder = (Integer) query.uniqueResult();
+			return (lastCategoryOrder != null) ? lastCategoryOrder.intValue()
+					: 0;
+		} catch (HibernateException e) {
+			return 0;
+		}
+	}
 
+	public Category createCategory(String name, ForumInstance forumInstance)
+			throws ModuleException {
+		try {
+			Session session = getSession();
+
+			CategoryImpl category = new CategoryImpl();
+			category.setTitle(name);
+			category.setOrder(getLastCategoryOrder(forumInstance.getId()) + 10);
+			// Luca Stancapiano
+			category.setForumInstance(forumInstance);
+			session.save(category);
+			return category;
+		} catch (HibernateException e) {
+			String errorMessage = "Cannot create topic";
+			log.error(errorMessage, e);
+			throw new ModuleException(errorMessage, e);
+		}
+	}
+	// Luca Stancapiano end
+
    public void removeCategory(Category category)
       throws ModuleException
    {
@@ -1136,24 +1172,31 @@
       }
    }
 
-   public List findPosts()
-      throws ModuleException
-   {
-      try
-      {
-         Session session = getSession();
-         Query query =
-            session.createQuery("from PostImpl");
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find posts";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
 
+
+   /* Luca Stancapiano start - findPosts
+      method need to a forumInstanceId argument 
+      to take only posts of a specific forum instance 
+    */
+	public List findPosts(Integer indexInstance) throws ModuleException {
+		try {
+			Session session = getSession();
+			Query query = session.createQuery("from PostImpl "+
+
+					// Luca Stancapiano
+					"and t.forum.forumInstance.id := forumInstanceId"
+			);	
+			// Luca Stancapiano
+			query.setParameter("forumInstanceId", indexInstance);
+			return query.list();
+		} catch (HibernateException e) {
+			String message = "Cannot find posts";
+			log.error(message, e);
+			throw new ModuleException(message, e);
+		}
+	}
+	// Luca Stancapiano end
+
    public Post findPostById(Integer id)
       throws ModuleException
    {
@@ -1552,7 +1595,12 @@
        }
     }
 
-    public Map findLastPostsOfForums()
+    /* Luca Stancapiano start - findLastPostsOfForums, findForumWatchByUser,
+       findForumWatchedByUser, findTopicWatchedByUser and findTopicWatches  
+       methods need to a forumInstanceId argument 
+       to take only objects of a specific forum instance 
+    */
+    public Map findLastPostsOfForums(Integer indexInstance)
        throws ModuleException
     {
        try
@@ -1561,8 +1609,12 @@
           Query query =             
               session.createQuery("select MAX(topic.lastPostDate) as maxDate , topic.forum.id " + 
                                   "from TopicImpl as topic " +
+      							  // Luca Stancapiano
+      							  "where topic.forum.category.forumInstance.id = :forumInstanceId " +
                                   "group by topic.forum.id ");
-		  query.setCacheable(true);
+                  query.setCacheable(true);
+		  // Luca Stancapiano
+		  query.setParameter("forumInstanceId", indexInstance);
           List createDates = query.list();
           Iterator it = createDates.iterator();
           List dates = new LinkedList();
@@ -1581,7 +1633,7 @@
                                       "where post.createDate IN (:dates) " +
                                       "order by post.createDate ");
           query.setCacheable(true);
-		  query.setParameterList("dates",dates);
+          query.setParameterList("dates",dates);
           List posts = query.list();
           Map forumPostMap = new HashMap(createDates.size());
           Iterator iterator = createDates.iterator();
@@ -1613,6 +1665,147 @@
        }
     }
 
+   public List findForumWatchByUser(User user, Integer indexInstance) throws ModuleException
+   {
+      try
+      {
+         Session session = getSession();
+	     Query query = session
+			.createQuery("from ForumWatchImpl as f where f.poster.userId = :userId " +
+					// Luca Stancapiano
+					"and f.forum.category.forumInstance.id = :forumInstanceId"
+			);
+	     query.setString("userId", user.getId().toString());
+         // Luca Stancapiano
+	     query.setParameter("forumInstanceId", indexInstance);
+         return query.list();
+      }
+      catch (HibernateException e)
+      {
+         String message = "Cannot find forum watch";
+         log.error(message, e);
+         throw new ModuleException(message, e);
+      }
+   }
+   
+   public List findForumWatchedByUser(User user, Integer indexInstance) throws ModuleException
+   {
+      try
+      {
+         Session session = getSession();
+		 Query query = session.createQuery("select f "
+					+ "from ForumImpl as f " + "where f.id IN ( "
+					+ "    select fw.forum.id " + "    from ForumWatchImpl fw "
+					+ "    where fw.poster.userId = :userId ) " 
+					// Luca Stancapiano
+					+ "and f.category.forumInstance.id = :forumInstanceId ");
+		query.setString("userId", user.getId().toString());
+        // Luca Stancapiano
+	    query.setParameter("forumInstanceId", indexInstance);
+         return query.list();
+      }
+      catch (HibernateException e)
+      {
+         String message = "Cannot find forum watched";
+         log.error(message, e);
+         throw new ModuleException(message, e);
+      }
+   }
+   
+   public List findTopicWatchedByUser(User user, Integer indexInstance) throws ModuleException
+   {
+      try
+      {
+         Session session = getSession();
+		 Query query = session.createQuery("select t "
+					+ "from TopicImpl as t " + "where t.id IN ( "
+					+ "    select tw.topic.id " + "    from TopicWatchImpl tw "
+					+ "    where tw.poster.userId = :userId ) "
+                    // Luca Stancapiano
+					+ "and t.forum.category.forumInstance.id = :forumInstanceId "
+					+ "order by t.lastPostDate desc ");
+		 query.setString("userId", user.getId().toString());
+         // Luca Stancapiano
+	     query.setParameter("forumInstanceId", indexInstance);
+         return query.list();
+      }
+      catch (HibernateException e)
+      {
+         String message = "Cannot find topic watched";
+         log.error(message, e);
+         throw new ModuleException(message, e);
+      }
+   }
+   
+   public List findTopicWatchedByUser(User user,Date datePoint, Integer indexInstance) throws ModuleException
+   {
+      try
+      {
+         Session session = getSession();
+		 Query query = session.createQuery("select t "
+					+ "from TopicImpl as t " + "where t.id IN ( "
+					+ "    select tw.topic.id " + "    from TopicWatchImpl tw "
+					+ "    where tw.poster.userId = :userId ) "
+					+ "and (t.lastPostDate > :datePoint) "
+                    // Luca Stancapiano
+					+ "and t.forum.category.forumInstance.id = :forumInstanceId "
+					+ "order by t.lastPostDate desc ");
+		 query.setString("userId", user.getId().toString());
+		 query.setTimestamp("datePoint", datePoint);
+         // Luca Stancapiano
+	     query.setParameter("forumInstanceId", indexInstance);
+         return query.list();
+      }
+      catch (HibernateException e)
+      {
+         String message = "Cannot find topic watched";
+         log.error(message, e);
+         throw new ModuleException(message, e);
+      }
+   }
+   
+   /**
+    * 
+    * This method returns Map<Integer,TopicWatch> pairs where Integer
+    * key is watched topic id.
+    * 
+    * @param user
+    * @return 
+    * @throws ModuleException
+    */
+   public Map findTopicWatches(User user, Integer indexInstance) throws ModuleException
+   {
+      try
+      {
+         Session session = getSession();
+		 Query query = session.createQuery("select tw.topic.id , tw "
+					+ "from TopicWatchImpl tw "
+					+ "where tw.poster.userId = :userId "
+                    // Luca Stancapiano
+					+ "and t.forum.category.forumInstance.id = :forumInstanceId "
+		 );
+		 query.setString("userId", user.getId().toString());
+         // Luca Stancapiano
+		 query.setParameter("forumInstanceId", indexInstance);
+         List results = query.list();
+         HashMap map = new HashMap(results.size());
+         Iterator it = results.iterator();
+         while (it.hasNext())
+         {
+            Object[] element = (Object[]) it.next();
+            map.put(element[0], element[1]);
+         }
+         return map;
+      }
+      catch (HibernateException e)
+      {
+         String message = "Cannot find topic watches";
+         log.error(message, e);
+         throw new ModuleException(message, e);
+      }
+   }
+   // Luca Stancapiano end
+
     private List findPostsFromForum(Forum forum, int limit, String order)
             throws ModuleException
     {
@@ -1724,129 +1917,6 @@
     {
         return findPosts(limit, "desc");
     }
-    
-   public List findForumWatchByUser(User user) throws ModuleException
-   {
-      try
-      {
-         Session session = getSession();
-         Query query = session.createQuery("from ForumWatchImpl as f where f.poster.userId = :userId");
-         query.setString("userId", user.getId().toString());
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find forum watch";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
-   
-   public List findForumWatchedByUser(User user) throws ModuleException
-   {
-      try
-      {
-         Session session = getSession();
-         Query query = session.createQuery("select f " +
-                                           "from ForumImpl as f " +
-                                           "where f.id IN ( "+
-                                           "    select fw.forum.id " +
-                                           "    from ForumWatchImpl fw " +
-                                           "    where fw.poster.userId = :userId )");
-         query.setString("userId", user.getId().toString());
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find forum watched";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
-   
-   public List findTopicWatchedByUser(User user) throws ModuleException
-   {
-      try
-      {
-         Session session = getSession();
-         Query query = session.createQuery("select t " +
-                                           "from TopicImpl as t " +
-                                           "where t.id IN ( " +
-                                           "    select tw.topic.id " +
-                                           "    from TopicWatchImpl tw " +
-                                           "    where tw.poster.userId = :userId ) " +
-                                           "order by t.lastPostDate desc ");
-         query.setString("userId", user.getId().toString());
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find topic watched";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
-   
-   public List findTopicWatchedByUser(User user,Date datePoint) throws ModuleException
-   {
-      try
-      {
-         Session session = getSession();
-         Query query = session.createQuery("select t " +
-                                           "from TopicImpl as t " +
-                                           "where t.id IN ( " +
-                                           "    select tw.topic.id " +
-                                           "    from TopicWatchImpl tw " +
-                                           "    where tw.poster.userId = :userId ) " +
-                                           "and (t.lastPostDate > :datePoint) " +
-                                           "order by t.lastPostDate desc ");
-         query.setString("userId", user.getId().toString());
-         query.setTimestamp("datePoint", datePoint);
-         return query.list();
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find topic watched";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
-   
-   /**
-    * 
-    * This method returns Map<Integer,TopicWatch> pairs where Integer
-    * key is watched topic id.
-    * 
-    * @param user
-    * @return 
-    * @throws ModuleException
-    */
-   public Map findTopicWatches(User user) throws ModuleException
-   {
-      try
-      {
-         Session session = getSession();
-         Query query = session.createQuery("select tw.topic.id , tw " +
-                                           "from TopicWatchImpl tw " +
-                                           "where tw.poster.userId = :userId ");
-         query.setString("userId", user.getId().toString());
-         List results = query.list();
-         HashMap map = new HashMap(results.size());
-         Iterator it = results.iterator();
-         while (it.hasNext())
-         {
-            Object[] element = (Object[]) it.next();
-            map.put(element[0], element[1]);
-         }
-         return map;
-      }
-      catch (HibernateException e)
-      {
-         String message = "Cannot find topic watches";
-         log.error(message, e);
-         throw new ModuleException(message, e);
-      }
-   }
 
    public void createWatch(Poster poster, Forum forum, int mode) throws ModuleException
    {
@@ -1924,15 +1994,22 @@
     * @return 
     * @throws ModuleException
     */
-   public Map findForumWatches(User user) throws ModuleException
+   /* Luca Stancapiano start - findForumWatches method need to a forumInstanceId argument 
+      to take only forums of a specific forum instance 
+   */
+   public Map findForumWatches(User user, Integer indexInstance) throws ModuleException
    {
       try
       {
          Session session = getSession();
          Query query = session.createQuery("select fw.forum.id , fw " +
-                                           "from ForumWatchImpl fw " +
-                                           "where fw.poster.userId = :userId ");
+                                        "from ForumWatchImpl fw " +
+                                        "where fw.poster.userId = :userId " +
+                                        // Luca Stancapiano
+			                               "and fw.forum.category.forumInstance.id = :forumInstanceId ");
          query.setString("userId", user.getId().toString());
+         // Luca Stancapiano
+		 query.setParameter("forumInstanceId", indexInstance);
          List results = query.list();
          HashMap map = new HashMap(results.size());
          Iterator it = results.iterator();
@@ -1950,6 +2027,7 @@
          throw new ModuleException(message, e);
       }
    }
+   // Luca Stancapiano end
    
    public ForumWatch findForumWatchByUserAndForum(User user,int forumId) throws ModuleException
    {
@@ -2099,5 +2177,59 @@
 	   }*/
 	   return session;
    }
+
+	/* Luca Stancapiano start - I add createForumInstance, removeForumInstance
+      and findForumInstanceById methods to manage ForumInstance object
+   */
+	public ForumInstance createForumInstance(Integer indexInstance, String name)
+			throws ModuleException {
+		try {
+			Session session = getSession();
+
+			ForumInstanceImpl forumInstance = new ForumInstanceImpl();
+			forumInstance.setId(indexInstance);
+			forumInstance.setName(name);
+			session.save(forumInstance);
+			return forumInstance;
+		} catch (HibernateException e) {
+			String errorMessage = "Cannot create forum Instance";
+			log.error(errorMessage, e);
+			throw new ModuleException(errorMessage, e);
+		}
+	}
+
+	public void removeForumInstance(ForumInstance forumInstance)
+			throws ModuleException {
+		Session session = getSession();
+		try {
+			// session.flush();
+			session.delete(forumInstance);
+			// session.flush();
+		} catch (HibernateException e) {
+			String errorMessage = "Cannot delete forum Instance";
+			log.error(errorMessage, e);
+			throw new ModuleException(errorMessage, e);
+		}
+	}
+
+	public ForumInstance findForumInstanceById(Integer id)
+			throws ModuleException {
+		if (id != null) {
+			try {
+				Session session = getSession();
+				ForumInstanceImpl forumInstance = (ForumInstanceImpl) session
+						.get(ForumInstanceImpl.class, id);
+				return forumInstance;
+			} catch (HibernateException e) {
+				String message = "Cannot find forum instance by id " + id;
+				log.error(message, e);
+				throw new ModuleException(message, e);
+			}
+		} else {
+			throw new IllegalArgumentException("id cannot be null");
+		}
+	}
+
+	// Luca Stancapiano end
 }
 




More information about the jboss-svn-commits mailing list