1 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() { } 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 71 static Collection optimize(Collection watchBeans) 72 throws DatabaseException, ObjectNotFoundException { 73 74 Collection globalWatchs = getGlobalWatchs(watchBeans); 76 if (globalWatchs.size() == 1) { 77 return globalWatchs; 78 } 79 80 ArrayList categoryWatchs = getCategoryWatchs(watchBeans); 81 82 ArrayList optimizedWatchs = new ArrayList(); optimizedWatchs.addAll(categoryWatchs); 85 86 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 if (isCategoryInWatchs(categoryID, categoryWatchs) == false) { 96 optimizedWatchs.add(forumWatch); 97 } 98 } 99 100 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); 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 } 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 } 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 } 162 } 163 } 164 return threadWatchs; 165 } 166 } 167 | Popular Tags |