KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > repository > ForumRepository


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: Apr 23, 2003 / 10:46:05 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.repository;
44
45 import java.text.SimpleDateFormat JavaDoc;
46 import java.util.ArrayList JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Map JavaDoc;
51 import java.util.Set JavaDoc;
52 import java.util.TreeSet JavaDoc;
53
54 import org.apache.log4j.Logger;
55
56 import net.jforum.SessionFacade;
57 import net.jforum.cache.CacheEngine;
58 import net.jforum.cache.Cacheable;
59 import net.jforum.dao.CategoryDAO;
60 import net.jforum.dao.ConfigDAO;
61 import net.jforum.dao.DataAccessDriver;
62 import net.jforum.dao.ForumDAO;
63 import net.jforum.dao.UserDAO;
64 import net.jforum.entities.Category;
65 import net.jforum.entities.Config;
66 import net.jforum.entities.Forum;
67 import net.jforum.entities.LastPostInfo;
68 import net.jforum.entities.MostUsersEverOnline;
69 import net.jforum.entities.Post;
70 import net.jforum.entities.Topic;
71 import net.jforum.entities.User;
72 import net.jforum.exceptions.CategoryNotFoundException;
73 import net.jforum.exceptions.DatabaseException;
74 import net.jforum.security.PermissionControl;
75 import net.jforum.security.SecurityConstants;
76 import net.jforum.util.CategoryOrderComparator;
77 import net.jforum.util.preferences.ConfigKeys;
78 import net.jforum.util.preferences.SystemGlobals;
79
80 /**
81  * Repository for the forums of the System.
82  * This repository acts like a cache system, to avoid repetitive and unnecessary SQL queries
83  * every time we need some info about the forums.
84  * To start the repository, call the method <code>start(ForumModel, CategoryModel)</code>
85  *
86  * @author Rafael Steil
87  * @version $Id: ForumRepository.java,v 1.48 2005/11/16 20:39:57 rafaelsteil Exp $
88  */

89 public class ForumRepository implements Cacheable
90 {
91     private static CacheEngine cache;
92     private static ForumRepository instance;
93     private static Logger logger = Logger.getLogger(ForumRepository.class);
94     
95     private static final String JavaDoc FQN = "forumRepository";
96     private static final String JavaDoc CATEGORIES_SET = "categoriesSet";
97     private static final String JavaDoc RELATION = "relationForums";
98     private static final String JavaDoc FQN_TOTAL_TOPICS = FQN + "/totalTopics";
99     private static final String JavaDoc FQN_MODERATORS = FQN + "/moderators";
100     private static final String JavaDoc TOTAL_MESSAGES = "totalMessages";
101     private static final String JavaDoc MOST_USERS_ONLINE = "mostUsersEverOnline";
102     private static final String JavaDoc LOADED = "loaded";
103     private static final String JavaDoc LAST_USER = "lastUser";
104     private static final String JavaDoc TOTAL_USERS = "totalUsers";
105     
106     /**
107      * @see net.jforum.cache.Cacheable#setCacheEngine(net.jforum.cache.CacheEngine)
108      */

109     public void setCacheEngine(CacheEngine engine)
110     {
111         cache = engine;
112     }
113     
114     /**
115      * Starts the repository.
116      *
117      * @param fm The <code>ForumModel</code> instance which will be
118      * used to retrieve information about the forums.
119      * @param cm The <code>CategoryModel</code> instance which will
120      * be used to retrieve information about the categories.
121      * @throws Exception
122      */

123     public synchronized static void start(ForumDAO fm,
124             CategoryDAO cm,
125             ConfigDAO configModel) throws Exception JavaDoc
126     {
127         instance = new ForumRepository();
128         
129         if (cache.get(FQN, LOADED) == null) {
130             instance.loadCategories(cm);
131             instance.loadForums(fm);
132             instance.loadMostUsersEverOnline(configModel);
133             instance.loadUsersInfo();
134             
135             Integer JavaDoc i = (Integer JavaDoc)cache.get(FQN, TOTAL_MESSAGES);
136             
137             if (i == null) {
138                 cache.add(FQN, TOTAL_MESSAGES, new Integer JavaDoc(0));
139             }
140             
141             cache.add(FQN, LOADED, "1");
142         }
143     }
144     
145     /**
146      * Gets a category by its id.
147      * A call to @link #getCategory(int, int) is made, using the
148      * return of <code>SessionFacade.getUserSession().getUserId()</code>
149      * as argument for the "userId" parameter.
150      *
151      * @param categoryId The id of the category to check
152      * @return <code>null</code> if the category is either not
153      * found or access is denied.
154      * @see #getCategory(int, int)
155      */

156     public static Category getCategory(int categoryId)
157     {
158         return getCategory(SessionFacade.getUserSession().getUserId(), categoryId);
159     }
160
161     /**
162      * Gets a category by its id.
163      *
164      * @param userId The user id who is requesting the category
165      * @param categoryId The id of the category to get
166      * @return <code>null</code> if the category is either not
167      * found or access is denied.
168      * @see #getCategory(int)
169      */

170     public static Category getCategory(int userId, int categoryId)
171     {
172         if (!isCategoryAccessible(userId, categoryId)) {
173             return null;
174         }
175         
176         return (Category)cache.get(FQN, Integer.toString(categoryId));
177     }
178     
179     public static Category getCategory(PermissionControl pc, int categoryId)
180     {
181         if (!isCategoryAccessible(pc, categoryId)) {
182             return null;
183         }
184         
185         return (Category)cache.get(FQN, Integer.toString(categoryId));
186     }
187     
188     public static Category retrieveCategory(int categoryId)
189     {
190         return (Category)cache.get(FQN, Integer.toString(categoryId));
191     }
192     
193     /**
194      * Check is some category is accessible.
195      *
196      * @param userId The user's id who is trying to get the category
197      * @param categoryId The category's id to check for access rights
198      * @return <code>true</code> if access to the category is allowed.
199      */

200     public static boolean isCategoryAccessible(int userId, int categoryId)
201     {
202         return isCategoryAccessible(SecurityRepository.get(userId), categoryId);
203     }
204     
205     /**
206      * Check if some category is accessible.
207      *
208      * @param categoryId The category id to check for access rights
209      * @return <code>true</code> if access to the category is allowed.
210      */

211     public static boolean isCategoryAccessible(int categoryId)
212     {
213         return isCategoryAccessible(SessionFacade.getUserSession().getUserId(), categoryId);
214     }
215     
216     /**
217      * Check is some category is accessible.
218      *
219      * @param pc The <code>PermissionControl</code> instance containing
220      * all security info related to the user.
221      * @param categoryId the category's id to check for access rights
222      * @return <code>true</code> if access to the category is allowed.
223      */

224     public static boolean isCategoryAccessible(PermissionControl pc, int categoryId)
225     {
226         return pc.canAccess(SecurityConstants.PERM_CATEGORY, Integer.toString(categoryId));
227     }
228     
229     /**
230      * Gets all categories from the cache.
231      *
232      * @return <code>List</code> with the categories. Each entry is a <code>Category</code> object.
233      */

234     public static List JavaDoc getAllCategories(int userId)
235     {
236         PermissionControl pc = SecurityRepository.get(userId);
237         List JavaDoc l = new ArrayList JavaDoc();
238         
239         Set JavaDoc categoriesSet = (Set JavaDoc)cache.get(FQN, CATEGORIES_SET);
240         
241         if (categoriesSet == null) {
242             logger.warn("Categories set returned null from the cache. Trying to reload");
243             
244             try {
245                 ForumRepository.instance.loadCategories(DataAccessDriver.getInstance().newCategoryDAO());
246                 ForumRepository.instance.loadForums(DataAccessDriver.getInstance().newForumDAO());
247             }
248             catch (Exception JavaDoc e) {
249                 throw new CategoryNotFoundException("Failed to get the category", e);
250             }
251             
252             categoriesSet = (Set JavaDoc)cache.get(FQN, CATEGORIES_SET);
253             
254             if (categoriesSet == null) {
255                 throw new CategoryNotFoundException("Could not find all categories. There must be a problem "
256                         + "with the cache");
257             }
258         }
259         
260         for (Iterator JavaDoc iter = categoriesSet.iterator(); iter.hasNext(); ) {
261             Category c = (Category)iter.next();
262             
263             if (isCategoryAccessible(pc, c.getId())) {
264                 l.add(c);
265             }
266         }
267         
268         return l;
269     }
270
271     /**
272      * Get all categories.
273      * A call to @link #getAllCategories(int) is made, passing
274      * the return of <code>SessionFacade.getUserSession().getUserId()</code>
275      * as the value for the "userId" argument.
276      *
277      * @return <code>List</code> with the categories. Each entry is a <code>Category</code> object.
278      * @see #getAllCategories(int)
279      */

280     public static List JavaDoc getAllCategories()
281     {
282         return getAllCategories(SessionFacade.getUserSession().getUserId());
283     }
284     
285     private static Category findCategoryByOrder(int order)
286     {
287         for (Iterator JavaDoc iter = ((Set JavaDoc)cache.get(FQN, CATEGORIES_SET)).iterator(); iter.hasNext(); ) {
288             Category c = (Category)iter.next();
289             if (c.getOrder() == order) {
290                 return c;
291             }
292         }
293         
294         return null;
295     }
296
297     /**
298      * Updates some category.
299      * This method only updated the "name" and "order" fields.
300      *
301      * @param c The category to update. The method will search for a category
302      * with the same id and update its data.
303      */

304     public synchronized static void reloadCategory(Category c)
305     {
306         Category current = (Category)cache.get(FQN, Integer.toString(c.getId()));
307         Category currentAtOrder = findCategoryByOrder(c.getOrder());
308         
309         Set JavaDoc tmpSet = new TreeSet JavaDoc(new CategoryOrderComparator());
310         tmpSet.addAll((Set JavaDoc)cache.get(FQN, CATEGORIES_SET));
311         
312         if (currentAtOrder != null) {
313             tmpSet.remove(currentAtOrder);
314             cache.remove(FQN, Integer.toString(currentAtOrder.getId()));
315         }
316         
317         tmpSet.add(c);
318         cache.add(FQN, Integer.toString(c.getId()), c);
319         
320         if (currentAtOrder != null && c.getId() != currentAtOrder.getId()) {
321             tmpSet.remove(current);
322             currentAtOrder.setOrder(current.getOrder());
323             tmpSet.add(currentAtOrder);
324             
325             cache.add(FQN, Integer.toString(currentAtOrder.getId()), currentAtOrder);
326         }
327         
328         cache.add(FQN, CATEGORIES_SET, tmpSet);
329     }
330     
331     /**
332      * Refreshes a category entry in the cache.
333      *
334      * @param c The category to refresh
335      */

336     public synchronized static void refreshCategory(Category c)
337     {
338         cache.add(FQN, Integer.toString(c.getId()), c);
339         Set JavaDoc s = (Set JavaDoc)cache.get(FQN, CATEGORIES_SET);
340         s.remove(c);
341         s.add(c);
342         cache.add(FQN, CATEGORIES_SET, s);
343     }
344     
345     public synchronized static void refreshForum(Forum forum)
346     {
347         Category c = retrieveCategory(forum.getCategoryId());
348         c.addForum(forum);
349         refreshCategory(c);
350     }
351     
352     /**
353      * Remove a category from the cache
354      * @param c The category to remove. The instance should have the
355      * category id at least
356      */

357     public synchronized static void removeCategory(Category c)
358     {
359         cache.remove(FQN, Integer.toString(c.getId()));
360         
361         Set JavaDoc s = (Set JavaDoc)cache.get(FQN, CATEGORIES_SET);
362         s.remove(c);
363         cache.add(FQN, CATEGORIES_SET, s);
364         
365         Map JavaDoc m = (Map JavaDoc)cache.get(FQN, RELATION);
366         for (Iterator JavaDoc iter = m.values().iterator(); iter.hasNext(); ) {
367             if (Integer.parseInt((String JavaDoc)iter.next()) == c.getId()) {
368                 iter.remove();
369             }
370         }
371         
372         cache.add(FQN, RELATION, m);
373     }
374     
375     /**
376      * Adds a new category to the cache.
377      * @param c The category instance to insert in the cache.
378      */

379     public synchronized static void addCategory(Category c)
380     {
381         String JavaDoc categoryId = Integer.toString(c.getId());
382         cache.add(FQN, categoryId, c);
383         
384         Set JavaDoc s = (Set JavaDoc)cache.get(FQN, CATEGORIES_SET);
385         
386         if (s == null) {
387             s = new TreeSet JavaDoc(new CategoryOrderComparator());
388         }
389         
390         s.add(c);
391         cache.add(FQN, CATEGORIES_SET, s);
392         
393         Map JavaDoc relation = (Map JavaDoc)cache.get(FQN, RELATION);
394         if (relation == null) {
395             relation = new HashMap JavaDoc();
396         }
397         
398         for (Iterator JavaDoc iter = c.getForums().iterator(); iter.hasNext(); ) {
399             Forum f = (Forum)iter.next();
400             relation.put(Integer.toString(f.getId()), categoryId);
401         }
402         
403         cache.add(FQN, RELATION, relation);
404     }
405     
406     /**
407      * Gets a specific forum from the cache.
408      *
409      * @param forumId The forum's ID to get
410      * @return <code>net.jforum.Forum</code> object instance or <code>null</code>
411      * if the forum was not found or is not accessible to the user.
412      */

413     public static Forum getForum(int forumId)
414     {
415         String JavaDoc categoryId = (String JavaDoc)((Map JavaDoc)cache.get(FQN, RELATION)).get(Integer.toString(forumId));
416         
417         if (categoryId != null) {
418             Category category = (Category)cache.get(FQN, categoryId);
419             
420             if (isCategoryAccessible(category.getId())) {
421                 return category.getForum(forumId);
422             }
423         }
424         
425         return null;
426     }
427     
428     public static boolean isForumAccessible(int forumId)
429     {
430         return isForumAccessible(SessionFacade.getUserSession().getUserId(), forumId);
431     }
432     
433     public static boolean isForumAccessible(int userId, int forumId)
434     {
435         int categoryId = Integer.parseInt((String JavaDoc)((Map JavaDoc)cache.get(FQN, RELATION)).get(Integer.toString(forumId)));
436         return isForumAccessible(userId, categoryId, forumId);
437     }
438     
439     public static boolean isForumAccessible(int userId, int categoryId, int forumId)
440     {
441         return ((Category)cache.get(FQN, Integer.toString(categoryId))).getForum(userId, forumId) != null;
442     }
443     
444     /**
445      * Adds a new forum to the cache repository.
446      *
447      * @param forum The forum to add
448      */

449     public synchronized static void addForum(Forum forum)
450     {
451         String JavaDoc categoryId = Integer.toString(forum.getCategoryId());
452
453         Category c = (Category)cache.get(FQN, categoryId);
454         c.addForum(forum);
455         cache.add(FQN, categoryId, c);
456         
457         Map JavaDoc m = (Map JavaDoc)cache.get(FQN, RELATION);
458         m.put(Integer.toString(forum.getId()), categoryId);
459         cache.add(FQN, RELATION, m);
460         
461         Set JavaDoc s = (Set JavaDoc)cache.get(FQN, CATEGORIES_SET);
462         cache.add(FQN, CATEGORIES_SET, s);
463     }
464     
465     /**
466      * Removes a forum from the cache.
467      *
468      * @param forum The forum instance to remove.
469      */

470     public synchronized static void removeForum(Forum forum)
471     {
472         String JavaDoc id = Integer.toString(forum.getId());
473         Map JavaDoc m = (Map JavaDoc)cache.get(FQN, RELATION);
474         m.remove(id);
475         cache.add(FQN, RELATION, m);
476
477         id = Integer.toString(forum.getCategoryId());
478         
479         Category c = (Category)cache.get(FQN, id);
480         c.removeForum(forum.getId());
481         cache.add(FQN, id, c);
482         
483         Set JavaDoc s = (Set JavaDoc)cache.get(FQN, CATEGORIES_SET);
484         cache.add(FQN, CATEGORIES_SET, s);
485     }
486     
487     /**
488      * Reloads a forum.
489      * The forum should already be in the cache and <b>SHOULD NOT</b>
490      * have its order changed. If the forum's order was changed,
491      * then you <b>MUST CALL</b> @link Category#changeForumOrder(Forum) <b>BEFORE</b>
492      * calling this method.
493      *
494      * @param forum The forum to reload its information
495      * @throws Exception
496      */

497     public static synchronized void reloadForum(int forumId) throws Exception JavaDoc
498     {
499         Forum f = DataAccessDriver.getInstance().newForumDAO().selectById(forumId);
500         
501         if (((Map JavaDoc)cache.get(FQN, RELATION)).containsKey(Integer.toString(forumId))) {
502             String JavaDoc id = Integer.toString(f.getCategoryId());
503             Category c = (Category)cache.get(FQN, id);
504             
505             f.setLastPostInfo(null);
506             f.setLastPostInfo(ForumRepository.getLastPostInfo(f));
507             c.reloadForum(f);
508             
509             cache.add(FQN, id, c);
510             Set JavaDoc s = (Set JavaDoc)cache.get(FQN, CATEGORIES_SET);
511             cache.add(FQN, CATEGORIES_SET, s);
512         }
513         
514         getTotalMessages(true);
515     }
516     
517     public static synchronized void updateForumStats(Topic t, User u, Post p)
518     {
519         String JavaDoc f = Integer.toString(t.getForumId());
520         
521         if (((Map JavaDoc)cache.get(FQN, RELATION)).containsKey(f)) {
522             Forum forum = getForum(t.getForumId());
523
524             SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc(SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT));
525         
526             LastPostInfo lpi = forum.getLastPostInfo();
527             
528             if (lpi == null) {
529                 lpi = new LastPostInfo();
530             }
531             
532             lpi.setPostId(p.getId());
533             lpi.setPostDate(df.format(p.getTime()));
534             lpi.setPostTimeMillis(p.getTime().getTime());
535             lpi.setTopicId(t.getId());
536             lpi.setTopicReplies(t.getTotalReplies());
537             lpi.setUserId(u.getId());
538             lpi.setUsername(u.getUsername());
539             
540             forum.setLastPostInfo(lpi);
541             
542             if (t.getTotalReplies() == 0) {
543                 forum.setTotalTopics(forum.getTotalTopics() + 1);
544             }
545             else {
546                 forum.setTotalPosts(forum.getTotalPosts() + 1);
547             }
548             
549             Category c = retrieveCategory(forum.getCategoryId());
550             c.reloadForum(forum);
551             
552             refreshCategory(c);
553         }
554     }
555     
556     /**
557      * Gets information about the last message posted in some forum.
558      * @param forum The forum to retrieve information
559      * @return
560      */

561     public static LastPostInfo getLastPostInfo(Forum forum) throws Exception JavaDoc
562     {
563         LastPostInfo lpi = forum.getLastPostInfo();
564         
565         if (lpi == null || !forum.getLastPostInfo().hasInfo()) {
566             lpi = DataAccessDriver.getInstance().newForumDAO().getLastPostInfo(forum.getId());
567             forum.setLastPostInfo(lpi);
568         }
569         
570         return lpi;
571     }
572     
573     /**
574      * Gets information about the last message posted in some forum.
575      *
576      * @param forumId The forum's id to retrieve information
577      * @return
578      * @throws Exception
579      */

580     public static LastPostInfo getLastPostInfo(int forumId) throws Exception JavaDoc
581     {
582         return getLastPostInfo(getForum(forumId));
583     }
584
585     /**
586      * Gets information about the moderators of some forum.
587      * @param forumId The forum to retrieve information
588      * @return
589      */

590     public static List JavaDoc getModeratorList(int forumId)
591     {
592         List JavaDoc l = (List JavaDoc)cache.get(FQN_MODERATORS, Integer.toString(forumId));
593         
594         if (l == null) {
595             synchronized (FQN_MODERATORS) {
596                 try {
597                     l = DataAccessDriver.getInstance().newForumDAO().getModeratorList(forumId);
598                     cache.add(FQN_MODERATORS, Integer.toString(forumId), l);
599                 }
600                 catch (Exception JavaDoc e) {
601                     throw new DatabaseException(e);
602                 }
603             }
604         }
605         
606         return l;
607     }
608     
609     public static void clearModeratorList()
610     {
611         cache.remove(FQN_MODERATORS);
612     }
613     
614     public static User lastRegisteredUser()
615     {
616         return (User)cache.get(FQN, LAST_USER);
617     }
618     
619     public static void setLastRegisteredUser(User user)
620     {
621         cache.add(FQN, LAST_USER, user);
622     }
623     
624     public static Integer JavaDoc totalUsers()
625     {
626         return (Integer JavaDoc)cache.get(FQN, TOTAL_USERS);
627     }
628     
629     public static void incrementTotalUsers()
630     {
631         Integer JavaDoc i = (Integer JavaDoc)cache.get(FQN, TOTAL_USERS);
632         
633         if (i == null) {
634             i = new Integer JavaDoc(0);
635         }
636         
637         cache.add(FQN,TOTAL_USERS, new Integer JavaDoc(i.intValue() + 1));
638     }
639     
640     /**
641      * Gets the number of topics in some forum.
642      *
643      * @param forumId The forum's id to retrieve the number of topics
644      * @param fromDb If <code>true</code>, a query to the database will be made
645      * to get the number of topics. If <code>false</code>, the cached information
646      * will be returned
647      * @return The number of topics
648      * @throws Exception
649      * @see #getTotalTopics(int)
650      */

651     public static int getTotalTopics(int forumId, boolean fromDb) throws Exception JavaDoc
652     {
653         Integer JavaDoc i = (Integer JavaDoc)cache.get(FQN_TOTAL_TOPICS, Integer.toString(forumId));
654         int total = (i != null ? i.intValue() : 0);
655         
656         if (fromDb || total == -1) {
657             total = DataAccessDriver.getInstance().newForumDAO().getTotalTopics(forumId);
658             cache.add(FQN_TOTAL_TOPICS, Integer.toString(forumId), new Integer JavaDoc(total));
659         }
660         
661         return total;
662     }
663     
664     /**
665      * Gets the number of topics in some forum.
666      * @param forumId The forum's id to retrieve the number of topics
667      * @return The number of topics
668      * @throws Exception
669      * @see #getTotalTopics(int, boolean)
670      */

671     public static int getTotalTopics(int forumId) throws Exception JavaDoc
672     {
673         return ForumRepository.getTotalTopics(forumId, false);
674     }
675     
676     /**
677      * Gets the number of messages in the entire board.
678      * @return
679      * @throws Exception
680      * @see #getTotalMessages(boolean)
681      */

682     public static int getTotalMessages() throws Exception JavaDoc
683     {
684         return getTotalMessages(false);
685     }
686
687     /**
688      * Gets the number of messags in the entire board.
689      *
690      * @param fromDb If <code>true</code>, a query to the database will
691      * be made, to retrieve the desired information. If <code>false</code>, the
692      * data will be fetched from the cache.
693      * @return The number of messages posted in the board.
694      * @throws Exception
695      * @see #getTotalMessages()
696      */

697     public static int getTotalMessages(boolean fromDb) throws Exception JavaDoc
698     {
699         int total = ((Integer JavaDoc)cache.get(FQN, TOTAL_MESSAGES)).intValue();
700         
701         if (fromDb || total == 0) {
702             total = DataAccessDriver.getInstance().newForumDAO().getTotalMessages();
703             cache.add(FQN, TOTAL_MESSAGES, new Integer JavaDoc(total));
704         }
705         
706         return total;
707     }
708     
709     public static synchronized void incrementTotalMessages()
710     {
711         int total = ((Integer JavaDoc)cache.get(FQN, TOTAL_MESSAGES)).intValue();
712         cache.add(FQN, TOTAL_MESSAGES, new Integer JavaDoc(total + 1));
713     }
714     
715     /**
716      * Gets the number of most online users ever
717      * @return
718      */

719     public static MostUsersEverOnline getMostUsersEverOnline()
720     {
721         return (MostUsersEverOnline)cache.get(FQN, MOST_USERS_ONLINE);
722     }
723     
724     /**
725      * Update the value of most online users ever.
726      *
727      * @param newValue The new value to store. Generally it
728      * will be a bigger one.
729      * @throws Exception
730      */

731     public static void updateMostUsersEverOnline(MostUsersEverOnline m) throws Exception JavaDoc
732     {
733         ConfigDAO cm = DataAccessDriver.getInstance().newConfigDAO();
734         Config config = cm.selectByName(ConfigKeys.MOST_USERS_EVER_ONLINE);
735         if (config == null) {
736             // Total
737
config = new Config();
738             config.setName(ConfigKeys.MOST_USERS_EVER_ONLINE);
739             config.setValue(Integer.toString(m.getTotal()));
740             
741             cm.insert(config);
742             
743             // Date
744
config.setName(ConfigKeys.MOST_USER_EVER_ONLINE_DATE);
745             config.setValue(Long.toString(m.getTimeInMillis()));
746             
747             cm.insert(config);
748         }
749         else {
750             // Total
751
config.setValue(Integer.toString(m.getTotal()));
752             cm.update(config);
753
754             // Date
755
config.setName(ConfigKeys.MOST_USER_EVER_ONLINE_DATE);
756             config.setValue(Long.toString(m.getTimeInMillis()));
757             cm.update(config);
758         }
759         
760         cache.add(FQN, MOST_USERS_ONLINE, m);
761     }
762     
763     /**
764      * Loads all forums.
765      * @throws Exception
766      */

767     private void loadForums(ForumDAO fm) throws Exception JavaDoc
768     {
769         List JavaDoc l = fm.selectAll();
770         
771         Map JavaDoc m = (Map JavaDoc)cache.get(FQN, RELATION);
772         if (m == null) {
773             m = new HashMap JavaDoc();
774         }
775         
776         int lastId = 0;
777         Category c = null;
778         String JavaDoc catId = null;
779
780         for (Iterator JavaDoc iter = l.iterator(); iter.hasNext(); ) {
781             Forum f = (Forum)iter.next();
782             
783             if (f.getCategoryId() != lastId) {
784                 if (c != null) {
785                     cache.add(FQN, catId, c);
786                 }
787                 
788                 lastId = f.getCategoryId();
789                 catId = Integer.toString(f.getCategoryId());
790                 c = (Category)cache.get(FQN, catId);
791             }
792             
793             if (c == null) {
794                 throw new CategoryNotFoundException("Category for forum #" + f.getId() + " not found");
795             }
796             
797             String JavaDoc forumId = Integer.toString(f.getId());
798             c.addForum(f);
799             m.put(forumId, catId);
800             cache.add(FQN_TOTAL_TOPICS, forumId, new Integer JavaDoc(-1));
801         }
802         
803         if (c != null) {
804             cache.add(FQN, catId, c);
805         }
806         
807         cache.add(FQN, RELATION, m);
808     }
809     
810     private void loadUsersInfo() throws Exception JavaDoc
811     {
812         UserDAO udao = DataAccessDriver.getInstance().newUserDAO();
813         cache.add(FQN, LAST_USER, udao.getLastUserInfo());
814         cache.add(FQN, TOTAL_USERS, new Integer JavaDoc(udao.getTotalUsers()));
815     }
816
817     /**
818      * Loads all categories.
819      * @throws Exception
820      */

821     private void loadCategories(CategoryDAO cm) throws Exception JavaDoc
822     {
823         List JavaDoc categories = cm.selectAll();
824         Set JavaDoc categoriesSet = new TreeSet JavaDoc(new CategoryOrderComparator());
825         
826         for (Iterator JavaDoc iter = categories.iterator(); iter.hasNext(); ) {
827             Category c = (Category)iter.next();
828             
829             cache.add(FQN, Integer.toString(c.getId()), c);
830             categoriesSet.add(c);
831         }
832         
833         cache.add(FQN, CATEGORIES_SET, categoriesSet);
834     }
835     
836     private void loadMostUsersEverOnline(ConfigDAO cm) throws Exception JavaDoc
837     {
838         Config config = cm.selectByName(ConfigKeys.MOST_USERS_EVER_ONLINE);
839         MostUsersEverOnline mostUsersEverOnline = new MostUsersEverOnline();
840         
841         if (config != null) {
842             mostUsersEverOnline.setTotal(Integer.parseInt(config.getValue()));
843             
844             // We're assuming that, if we have one key, the another one
845
// will always exist
846
config = cm.selectByName(ConfigKeys.MOST_USER_EVER_ONLINE_DATE);
847             mostUsersEverOnline.setTimeInMillis(Long.parseLong(config.getValue()));
848         }
849         
850         cache.add(FQN, MOST_USERS_ONLINE, mostUsersEverOnline);
851     }
852 }
853
Popular Tags