001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/WatchUtil.java,v 1.13 2007/12/17 09:09:41 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.13 $
005: * $Date: 2007/12/17 09:09:41 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Mai Nguyen
040: * @author: Cord cord_sw@lupinex.com
041: */
042: package com.mvnforum.user;
043:
044: import java.util.*;
045:
046: import com.mvnforum.db.*;
047:
048: import net.myvietnam.mvncore.exception.DatabaseException;
049: import net.myvietnam.mvncore.exception.ObjectNotFoundException;
050:
051: public final class WatchUtil {
052:
053: // prevent instantiation
054: private WatchUtil() {
055: }
056:
057: private static boolean isCategoryInWatchs(int categoryID,
058: ArrayList categoryWatchs) {
059: for (int catIndex = 0; catIndex < categoryWatchs.size(); catIndex++) {
060: WatchBean watchBean = (WatchBean) categoryWatchs
061: .get(catIndex);
062: int currentCategoryID = watchBean.getCategoryID();
063: if (currentCategoryID == categoryID) {
064: return true;
065: }
066: }
067: return false;
068: }
069:
070: /**
071: * Optimize the watch collection, note that the thread is not optimized
072: * @param watchBeans
073: * @return the watch collection that has been optimized
074: */
075: static Collection optimize(Collection watchBeans)
076: throws DatabaseException, ObjectNotFoundException {
077:
078: // now check the global watch first
079: Collection globalWatchs = getGlobalWatchs(watchBeans);
080: if (globalWatchs.size() == 1) {
081: return globalWatchs;
082: }
083:
084: ArrayList categoryWatchs = getCategoryWatchs(watchBeans);
085:
086: // next, set it to the category watch beans
087: ArrayList optimizedWatchs = new ArrayList();// MUST use ArrayList for the optimizedWatchs
088: optimizedWatchs.addAll(categoryWatchs);
089:
090: // now remove the redundant forum watch beans
091: ArrayList forumWatchs = getForumWatchs(watchBeans);
092: ForumCache forumCache = ForumCache.getInstance();
093: for (int forumIndex = 0; forumIndex < forumWatchs.size(); forumIndex++) {
094: WatchBean forumWatch = (WatchBean) forumWatchs
095: .get(forumIndex);
096: ForumBean forumBean = forumCache.getBean(forumWatch
097: .getForumID());
098: int categoryID = forumBean.getCategoryID();
099:
100: // now check if the categoryID is in categoryWatchs or not
101: if (isCategoryInWatchs(categoryID, categoryWatchs) == false) {
102: optimizedWatchs.add(forumWatch);
103: }
104: }
105:
106: // finally, add the thread watches (not optimize thread) (why not optimize thread ???)
107: ArrayList threadWatchs = getThreadWatchs(watchBeans);
108: optimizedWatchs.addAll(threadWatchs);
109:
110: return optimizedWatchs;
111: }
112:
113: static ArrayList getGlobalWatchs(Collection watchBeans) {
114:
115: ArrayList globalWatchs = new ArrayList(1);//maximum is 1 global watch
116:
117: for (Iterator iterator = watchBeans.iterator(); iterator
118: .hasNext();) {
119: WatchBean watchBean = (WatchBean) iterator.next();
120: if ((watchBean.getCategoryID() == 0)
121: && (watchBean.getForumID() == 0)
122: && (watchBean.getThreadID() == 0)) {
123: globalWatchs.add(watchBean);
124: }
125: }
126: return globalWatchs;
127: }
128:
129: static ArrayList getCategoryWatchs(Collection watchBeans) {
130:
131: ArrayList categoryWatchs = new ArrayList();
132:
133: for (Iterator iterator = watchBeans.iterator(); iterator
134: .hasNext();) {
135: WatchBean watchBean = (WatchBean) iterator.next();
136: if (watchBean.getCategoryID() != 0) {
137: if ((watchBean.getForumID() == 0)
138: && (watchBean.getThreadID() == 0)) {
139: categoryWatchs.add(watchBean);
140: } else {
141: //TODO: should we delete watch here ???
142: }
143: }
144: }
145: return categoryWatchs;
146: }
147:
148: static ArrayList getForumWatchs(Collection watchBeans) {
149: ArrayList forumWatchs = new ArrayList();
150: Iterator iterator = watchBeans.iterator();
151: while (iterator.hasNext()) {
152: WatchBean watchBean = (WatchBean) iterator.next();
153: if (watchBean.getForumID() != 0) {
154: if ((watchBean.getCategoryID() == 0)
155: && (watchBean.getThreadID() == 0)) {
156: forumWatchs.add(watchBean);
157: } else {
158: //TODO: delete watch here
159: }
160: }
161: }
162: return forumWatchs;
163: }
164:
165: static ArrayList getThreadWatchs(Collection watchBeans) {
166: ArrayList threadWatchs = new ArrayList();
167: Iterator iterator = watchBeans.iterator();
168: while (iterator.hasNext()) {
169: WatchBean watchBean = (WatchBean) iterator.next();
170: if (watchBean.getThreadID() != 0) {
171: if ((watchBean.getCategoryID() == 0)
172: && (watchBean.getForumID() == 0)) {
173: threadWatchs.add(watchBean);
174: } else {
175: //TODO: delete watch here
176: }
177: }
178: }
179: return threadWatchs;
180: }
181:
182: public static boolean isWatched(int memberID, int categoryID,
183: int forumID, int threadID) throws DatabaseException {
184: try {
185: DAOFactory
186: .getWatchDAO()
187: .findByAlternateKey_MemberID_CategoryID_ForumID_ThreadID(
188: memberID, categoryID, forumID, threadID);
189: } catch (ObjectNotFoundException e) {
190: return false;
191: }
192: return true;
193: }
194:
195: public static boolean isGlobalWatched(int memberID)
196: throws DatabaseException {
197: return isWatched(memberID, 0, 0, 0);
198: }
199:
200: public static boolean isCategoryWatched(int memberID,
201: CategoryBean categoryBean) throws DatabaseException,
202: ObjectNotFoundException {
203: int parentCategoryID = categoryBean.getParentCategoryID();
204: if (parentCategoryID != 0) {
205: CategoryBean parentCategoryBean = CategoryCache
206: .getInstance().getBean(parentCategoryID);
207: if (isCategoryWatched(memberID, parentCategoryBean)) {
208: //temporarily comment out until we implement category hierarchy watching
209: //return true;
210: }
211: }
212: return isWatched(memberID, categoryBean.getCategoryID(), 0, 0)
213: || isGlobalWatched(memberID);
214: }
215:
216: public static boolean isForumWatched(int memberID,
217: ForumBean forumBean) throws DatabaseException,
218: ObjectNotFoundException {
219: int categoryID = forumBean.getCategoryID();
220: CategoryBean categoryBean = CategoryCache.getInstance()
221: .getBean(categoryID);
222: return isWatched(memberID, 0, forumBean.getForumID(), 0)
223: || isCategoryWatched(memberID, categoryBean);
224: }
225:
226: public static boolean isThreadWatched(int memberID,
227: ThreadBean threadBean, ForumBean forumBean)
228: throws DatabaseException, ObjectNotFoundException {
229: return isWatched(memberID, 0, 0, threadBean.getThreadID())
230: || isForumWatched(memberID, forumBean);
231: }
232:
233: }
|