1 25 package org.nemesis.forum.impl; 26 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.nemesis.forum.Forum; 37 import org.nemesis.forum.Group; 38 import org.nemesis.forum.ProfileManager; 39 import org.nemesis.forum.User; 40 import org.nemesis.forum.exception.GroupAlreadyExistsException; 41 import org.nemesis.forum.exception.GroupNotFoundException; 42 import org.nemesis.forum.exception.UnauthorizedException; 43 import org.nemesis.forum.exception.UserAlreadyExistsException; 44 import org.nemesis.forum.exception.UserNotFoundException; 45 import org.nemesis.forum.util.cache.CacheableInteger; 46 import org.nemesis.forum.util.jdbc.DbConnectionManager; 47 50 public class DbProfileManager implements ProfileManager { 51 static protected Log log = LogFactory.getLog(DbProfileManager.class); 52 53 private static final String USER_GROUPS = "SELECT groupID from yazdGroupUser WHERE userID=?"; 54 private static final String USER_MESSAGE_COUNT = 55 "SELECT count(*) FROM yazdMessage,yazdForum,yazdThread WHERE " 56 + "yazdMessage.userID=? AND yazdForum.forumID=? AND " 57 + "yazdThread.forumID=yazdForum.forumID AND " 58 + "yazdMessage.threadID=yazdThread.threadID"; 59 private static final String USER_COUNT = "SELECT count(*) FROM yazdUser"; 60 private static final String ALL_USER_MESSAGES = "SELECT messageID FROM yazdMessage WHERE userID=?"; 61 private static final String DELETE_USER_MESSAGES = "UPDATE yazdMessage set userID=-1 WHERE userID=?"; 62 private static final String DELETE_USER_PERMS = "DELETE FROM yazdUserPerm WHERE userID=?"; 63 private static final String DELETE_USER_GROUPS = "DELETE FROM yazdGroupUser WHERE userID=?"; 64 private static final String DELETE_USER_PROPS = "DELETE FROM yazdUserProp WHERE userID=?"; 65 private static final String DELETE_USER = "DELETE FROM yazdUser WHERE userID=?"; 66 private static final String GROUP_COUNT = "SELECT count(*) FROM yazdGroup"; 67 private static final String DELETE_GROUP_USERS = "DELETE FROM yazdGroupUser WHERE groupID=?"; 68 private static final String DELETE_GROUP = "DELETE FROM yazdGroup WHERE groupID=?"; 69 70 private User anonymousUser = null; 71 private User specialUser = null; 72 private DbForumFactory factory; 73 74 77 public DbProfileManager(DbForumFactory factory) { 78 this.factory = factory; 79 try { 80 anonymousUser = getUser(-1); 81 specialUser = getUser(0); 82 } catch (UserNotFoundException unfe) { 83 } 84 } 85 86 88 public User createUser(String username, String password, String email) throws UserAlreadyExistsException { 89 User newUser = null; 90 try { 91 User existingUser = getUser(username); 92 93 throw new UserAlreadyExistsException(); 95 } catch (UserNotFoundException unfe) { 96 newUser = new DbUser(username, password, email); 98 } 99 return newUser; 100 } 101 102 public User getUser(int userID) throws UserNotFoundException { 103 DbCacheManager cacheManager = factory.getCacheManager(); 104 if (!cacheManager.isCacheEnabled()) { 106 return new DbUser(userID); 107 } 108 Integer userIDInteger = new Integer (userID); 110 DbUser user = (DbUser) cacheManager.get(DbCacheManager.USER_CACHE, userIDInteger); 111 if (user == null) { 112 user = new DbUser(userID); 113 cacheManager.add(DbCacheManager.USER_CACHE, userIDInteger, user); 114 } 115 return user; 116 } 117 118 public User getUser(String username) throws UserNotFoundException { 119 DbCacheManager cacheManager = factory.getCacheManager(); 120 if (!cacheManager.isCacheEnabled()) { 122 User user = new DbUser(username); 123 return getUser(user.getID()); 124 } 125 CacheableInteger userIDInteger = (CacheableInteger) cacheManager.get(DbCacheManager.USER_ID_CACHE, username); 127 if (userIDInteger == null) { 129 User user = new DbUser(username); 130 userIDInteger = new CacheableInteger(new Integer (user.getID())); 131 cacheManager.add(DbCacheManager.USER_ID_CACHE, username, userIDInteger); 132 } 133 return getUser(userIDInteger.getInteger().intValue()); 134 } 135 136 public User getAnonymousUser() { 137 return anonymousUser; 138 } 139 140 public User getSpecialUser() { 141 return specialUser; 142 } 143 144 public void deleteUser(User user) throws UnauthorizedException { 145 int userID = user.getID(); 146 int[] messages; 147 ArrayList tempMessages = new ArrayList (); 150 Connection con = null; 151 PreparedStatement pstmt = null; 152 try { 153 con = DbConnectionManager.getConnection(); 154 pstmt = con.prepareStatement(ALL_USER_MESSAGES); 155 pstmt.setInt(1, user.getID()); 156 ResultSet rs = pstmt.executeQuery(); 157 while (rs.next()) { 158 tempMessages.add(new Integer (rs.getInt("messageID"))); 159 } 160 } catch (SQLException sqle) { 161 log.error("Error in DbProfileManager:deleteUser()-" , sqle); 162 163 } finally { 164 try { 165 pstmt.close(); 166 } catch (Exception e) { 167 log.error("" , e); 168 } 169 try { 170 con.close(); 171 } catch (Exception e) { 172 log.error("" , e); 173 } 174 } 175 messages = new int[tempMessages.size()]; 177 for (int i = 0; i < messages.length; i++) { 178 messages[i] = ((Integer ) tempMessages.get(i)).intValue(); 179 } 180 181 con = null; 182 pstmt = null; 183 try { 184 con = DbConnectionManager.getConnection(); 185 pstmt = con.prepareStatement(DELETE_USER_MESSAGES); 187 pstmt.setInt(1, userID); 188 pstmt.execute(); 189 pstmt.close(); 190 pstmt = con.prepareStatement(DELETE_USER_PERMS); 192 pstmt.setInt(1, userID); 193 pstmt.execute(); 194 pstmt.close(); 195 pstmt = con.prepareStatement(DELETE_USER_GROUPS); 197 pstmt.setInt(1, userID); 198 pstmt.execute(); 199 pstmt.close(); 200 pstmt = con.prepareStatement(DELETE_USER_PROPS); 202 pstmt.setInt(1, userID); 203 pstmt.execute(); 204 pstmt.close(); 205 pstmt = con.prepareStatement(DELETE_USER); 207 pstmt.setInt(1, userID); 208 pstmt.execute(); 209 } catch (SQLException sqle) { 210 log.error("" , sqle); 211 } finally { 212 try { 213 pstmt.close(); 214 } catch (Exception e) { 215 log.error("" , e); 216 } 217 try { 218 con.close(); 219 } catch (Exception e) { 220 log.error("" , e); 221 } 222 } 223 224 DbCacheManager cacheManager = factory.getCacheManager(); 227 for (int i = 0; i < messages.length; i++) { 228 cacheManager.remove(DbCacheManager.MESSAGE_CACHE, new Integer (messages[i])); 229 } 230 cacheManager.remove(DbCacheManager.USER_ID_CACHE, user.getUsername()); 232 cacheManager.remove(DbCacheManager.USER_CACHE, new Integer (userID)); 233 } 234 235 public Group createGroup(String name) throws UnauthorizedException, GroupAlreadyExistsException { 236 Group newGroup = null; 237 try { 238 Group existingGroup = getGroup(name); 239 240 throw new GroupAlreadyExistsException(); 242 } catch (GroupNotFoundException unfe) { 243 newGroup = new DbGroup(name, factory); 245 } 246 return newGroup; 247 } 248 249 public Group getGroup(int groupID) throws GroupNotFoundException { 250 DbCacheManager cacheManager = factory.getCacheManager(); 251 if (!cacheManager.isCacheEnabled()) { 253 return new DbGroup(groupID, factory); 254 } 255 Integer groupIDInteger = new Integer (groupID); 257 DbGroup group = (DbGroup) cacheManager.get(DbCacheManager.GROUP_CACHE, groupIDInteger); 258 if (group == null) { 259 group = new DbGroup(groupID, factory); 260 cacheManager.add(DbCacheManager.GROUP_CACHE, groupIDInteger, group); 261 } 262 return group; 263 } 264 265 public Group getGroup(String name) throws GroupNotFoundException { 266 DbCacheManager cacheManager = factory.getCacheManager(); 267 if (!cacheManager.isCacheEnabled()) { 269 Group group = new DbGroup(name, null, factory); 270 return getGroup(group.getID()); 271 } 272 CacheableInteger groupIDInteger = (CacheableInteger) cacheManager.get(DbCacheManager.GROUP_ID_CACHE, name); 274 if (groupIDInteger == null) { 276 Group group = new DbGroup(name, null, factory); 277 groupIDInteger = new CacheableInteger(new Integer (group.getID())); 278 cacheManager.add(DbCacheManager.GROUP_ID_CACHE, name, groupIDInteger); 279 } 280 return getGroup(groupIDInteger.getInteger().intValue()); 281 } 282 283 public void deleteGroup(Group group) throws UnauthorizedException { 284 int groupID = group.getID(); 285 int[] members = new int[group.getMemberCount()]; 286 Iterator iter = group.members(); 287 for (int i = 0; i < members.length; i++) { 288 User user = (User) iter.next(); 289 members[i] = user.getID(); 290 } 291 292 Connection con = null; 293 PreparedStatement pstmt = null; 294 try { 295 con = DbConnectionManager.getConnection(); 296 pstmt = con.prepareStatement(DELETE_GROUP_USERS); 298 pstmt.setInt(1, groupID); 299 pstmt.execute(); 300 pstmt.close(); 301 pstmt = con.prepareStatement(DELETE_GROUP); 303 pstmt.setInt(1, groupID); 304 pstmt.execute(); 305 pstmt.close(); 306 } catch (SQLException sqle) { 307 log.error("" , sqle); 308 } finally { 309 try { 310 pstmt.close(); 311 } catch (Exception e) { 312 log.error("" , e); 313 } 314 try { 315 con.close(); 316 } catch (Exception e) { 317 log.error("" , e); 318 } 319 } 320 321 DbCacheManager cacheManager = factory.getCacheManager(); 323 cacheManager.remove(DbCacheManager.GROUP_ID_CACHE, group.getName()); 324 cacheManager.remove(DbCacheManager.GROUP_CACHE, new Integer (groupID)); 325 for (int i = 0; i < members.length; i++) { 328 cacheManager.removeUserPerm(new Integer (members[i])); 329 } 330 } 331 332 public int getUserCount() { 333 int count = 0; 334 Connection con = null; 335 PreparedStatement pstmt = null; 336 try { 337 con = DbConnectionManager.getConnection(); 338 pstmt = con.prepareStatement(USER_COUNT); 339 ResultSet rs = pstmt.executeQuery(); 340 if (rs.next()) { 341 count = rs.getInt(1); 342 } 343 } catch (SQLException sqle) { 344 log.error("" , sqle); 345 } finally { 346 try { 347 pstmt.close(); 348 } catch (Exception e) { 349 log.error("" , e); 350 } 351 try { 352 con.close(); 353 } catch (Exception e) { 354 log.error("" , e); 355 } 356 } 357 return count; 358 } 359 360 public int getGroupCount() { 361 int count = 0; 362 Connection con = null; 363 PreparedStatement pstmt = null; 364 try { 365 con = DbConnectionManager.getConnection(); 366 pstmt = con.prepareStatement(GROUP_COUNT); 367 ResultSet rs = pstmt.executeQuery(); 368 if (rs.next()) { 369 count = rs.getInt(1); 370 } 371 } catch (SQLException sqle) { 372 log.error("" , sqle); 373 } finally { 374 try { 375 pstmt.close(); 376 } catch (Exception e) { 377 log.error("" , e); 378 } 379 try { 380 con.close(); 381 } catch (Exception e) { 382 log.error("" , e); 383 } 384 } 385 return count; 386 } 387 388 public Iterator users() { 389 return new DbUserIterator(this); 390 } 391 392 public Iterator users(int startIndex, int numResults) { 393 return new DbUserIterator(this, startIndex, numResults); 394 } 395 396 public Iterator groups() { 397 return new DbGroupIterator(this); 398 } 399 400 public Iterator groups(int startIndex, int numResults) { 401 return new DbGroupIterator(this, startIndex, numResults); 402 } 403 404 public int userMessageCount(User user, Forum forum) { 405 int count = 0; 406 Connection con = null; 407 PreparedStatement pstmt = null; 408 try { 409 con = DbConnectionManager.getConnection(); 410 pstmt = con.prepareStatement(USER_MESSAGE_COUNT); 411 pstmt.setInt(1, user.getID()); 412 pstmt.setInt(2, forum.getID()); 413 ResultSet rs = pstmt.executeQuery(); 414 if (rs.next()) { 415 count = rs.getInt(1); 416 } 417 } catch (SQLException sqle) { 418 log.error("" , sqle); 419 } finally { 420 try { 421 pstmt.close(); 422 } catch (Exception e) { 423 log.error("" , e); 424 } 425 try { 426 con.close(); 427 } catch (Exception e) { 428 log.error("" , e); 429 } 430 } 431 return count; 432 } 433 434 public Iterator userMessages(User user, Forum forum) { 435 return new DbUserMessagesIterator(factory, user, forum); 436 } 437 438 441 protected int[] getUserGroups(int userID) { 442 Connection con = null; 443 PreparedStatement pstmt = null; 444 int[] groups = new int[0]; 445 try { 446 con = DbConnectionManager.getConnection(); 447 pstmt = con.prepareStatement(USER_GROUPS); 448 pstmt.setInt(1, userID); 449 ResultSet rs = pstmt.executeQuery(); 450 ArrayList groupList = new ArrayList (); 451 while (rs.next()) { 452 groupList.add(new Integer (rs.getInt("groupID"))); 453 } 454 groups = new int[groupList.size()]; 455 for (int i = 0; i < groups.length; i++) { 456 groups[i] = ((Integer ) groupList.get(i)).intValue(); 457 } 458 } catch (SQLException sqle) { 459 log.error("" , sqle); 460 } finally { 461 try { 462 pstmt.close(); 463 } catch (Exception e) { 464 log.error("" , e); 465 } 466 try { 467 con.close(); 468 } catch (Exception e) { 469 log.error("" , e); 470 } 471 } 472 return groups; 473 } 474 } 475 | Popular Tags |