KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > user > WatchUtil


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/WatchUtil.java,v 1.5 2006/04/14 17:05:27 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.5 $
5  * $Date: 2006/04/14 17:05:27 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  * @author: Cord cord_sw@lupinex.com
41  */

42 package com.mvnforum.user;
43
44 import java.util.*;
45
46 import com.mvnforum.db.*;
47 import net.myvietnam.mvncore.exception.DatabaseException;
48 import net.myvietnam.mvncore.exception.ObjectNotFoundException;
49
50 final class WatchUtil {
51
52     private WatchUtil() {// prevent instantiation
53
}
54
55     private static boolean isCategoryInWatchs(int categoryID, ArrayList categoryWatchs) {
56         for (int catIndex = 0; catIndex < categoryWatchs.size(); catIndex++) {
57             WatchBean watchBean = (WatchBean) categoryWatchs.get(catIndex);
58             int currentCategoryID = watchBean.getCategoryID();
59             if (currentCategoryID == categoryID) {
60                 return true;
61             }
62         }
63         return false;
64     }
65
66     /**
67      * Optimize the watch collection, note that the thread is not optimized
68      * @param watchBeans
69      * @return the watch collection that has been optimized
70      */

71     static Collection optimize(Collection watchBeans)
72         throws DatabaseException, ObjectNotFoundException {
73
74         // now check the global watch first
75
Collection globalWatchs = getGlobalWatchs(watchBeans);
76         if (globalWatchs.size() == 1) {
77             return globalWatchs;
78         }
79
80         ArrayList categoryWatchs = getCategoryWatchs(watchBeans);
81
82         // next, set it to the category watch beans
83
ArrayList optimizedWatchs = new ArrayList();// MUST use ArrayList for the optimizedWatchs
84
optimizedWatchs.addAll(categoryWatchs);
85
86         // now remove the redundant forum watch beans
87
ArrayList forumWatchs = getForumWatchs(watchBeans);
88         ForumCache forumCache = ForumCache.getInstance();
89         for (int forumIndex = 0; forumIndex < forumWatchs.size(); forumIndex++) {
90             WatchBean forumWatch = (WatchBean) forumWatchs.get(forumIndex);
91             ForumBean forumBean = forumCache.getBean(forumWatch.getForumID());
92             int categoryID = forumBean.getCategoryID();
93
94             // now check if the categoryID is in categoryWatchs or not
95
if (isCategoryInWatchs(categoryID, categoryWatchs) == false) {
96                 optimizedWatchs.add(forumWatch);
97             }
98         }
99
100         // finally, add the thread watchs (not optimize thread)
101
ArrayList threadWatchs = getThreadWatchs(watchBeans);
102         optimizedWatchs.addAll(threadWatchs);
103
104         return optimizedWatchs;
105     }
106
107     static ArrayList getGlobalWatchs(Collection watchBeans) {
108         ArrayList globalWatchs = new ArrayList(1);//maximum is 1 global watch
109
Iterator iterator = watchBeans.iterator();
110         while (iterator.hasNext()) {
111             WatchBean watchBean = (WatchBean) iterator.next();
112             if ( (watchBean.getCategoryID() == 0) && (watchBean.getForumID() == 0) && (watchBean.getThreadID() == 0)) {
113                 globalWatchs.add(watchBean);
114             }
115         }
116         return globalWatchs;
117     }
118
119     static ArrayList getCategoryWatchs(Collection watchBeans) {
120         ArrayList categoryWatchs = new ArrayList();
121         Iterator iterator = watchBeans.iterator();
122         while (iterator.hasNext()) {
123             WatchBean watchBean = (WatchBean) iterator.next();
124             if (watchBean.getCategoryID() != 0) {
125                 if ( (watchBean.getForumID()==0) && (watchBean.getThreadID()==0) ) {
126                     categoryWatchs.add(watchBean);
127                 } else {
128                     //@todo: delete watch here
129
}
130             }
131         }
132         return categoryWatchs;
133     }
134
135     static ArrayList getForumWatchs(Collection watchBeans) {
136         ArrayList forumWatchs = new ArrayList();
137         Iterator iterator = watchBeans.iterator();
138         while (iterator.hasNext()) {
139             WatchBean watchBean = (WatchBean) iterator.next();
140             if (watchBean.getForumID() != 0) {
141                 if ( (watchBean.getCategoryID()==0) && (watchBean.getThreadID()==0) ) {
142                     forumWatchs.add(watchBean);
143                 } else {
144                     //@todo: delete watch here
145
}
146             }
147         }
148         return forumWatchs;
149     }
150
151     static ArrayList getThreadWatchs(Collection watchBeans) {
152         ArrayList threadWatchs = new ArrayList();
153         Iterator iterator = watchBeans.iterator();
154         while (iterator.hasNext()) {
155             WatchBean watchBean = (WatchBean) iterator.next();
156             if (watchBean.getThreadID() != 0) {
157                 if ( (watchBean.getCategoryID()==0) && (watchBean.getForumID()==0) ) {
158                     threadWatchs.add(watchBean);
159                 } else {
160                     //@todo: delete watch here
161
}
162             }
163         }
164         return threadWatchs;
165     }
166 }
167
Popular Tags