1 53 54 106 107 package com.Yasna.forum.database; 108 109 import com.Yasna.util.*; 110 import com.Yasna.forum.*; 111 import com.Yasna.forum.util.ClientIP; 112 import com.Yasna.forum.Tasks.EmailWatchManager; 113 import com.Yasna.forum.Tasks.SessionManager; 114 import com.Yasna.forum.Tasks.CleanAccounts; 115 116 import java.sql.*; 117 import java.util.*; 118 import java.io.*; 119 120 123 public class DbForumFactory extends ForumFactory { 124 125 126 private static final String FORUM_COUNT = "SELECT count(*) FROM yazdForum"; 127 private static final String DELETE_FORUM = "DELETE FROM yazdForum WHERE forumID=?"; 128 private static final String DELETE_CATEGORY = "DELETE FROM yazdCategory WHERE categoryID=?"; 129 private static final String DELETE_FORUM_USER_PERMS = 130 "DELETE FROM yazdUserPerm WHERE forumID=?"; 131 private static final String DELETE_FORUM_GROUP_PERMS = 132 "DELETE FROM yazdGroupPerm WHERE forumID=?"; 133 private static final String DELETE_FORUM_PROPERTIES = 134 "DELETE FROM yazdForumProp WHERE forumID=?"; 135 private static final String GET_USER_PERMS = 136 "SELECT DISTINCT permission FROM yazdUserPerm WHERE forumID=? " + 137 "AND userID=?"; 138 private static final String USERS_WITH_PERM = 139 "SELECT DISTINCT userID FROM yazdUserPerm WHERE forumID=? AND permission=?"; 140 private static final String GET_GROUP_PERMS = 141 "SELECT DISTINCT permission from yazdGroupPerm WHERE forumID=? " + 142 "AND groupID=?"; 143 private static final String GROUPS_WITH_PERM = 144 "SELECT DISTINCT groupID FROM yazdGroupPerm WHERE forumID=? AND permission=?"; 145 private static final String ALL_MESSAGES = 146 "SELECT messageID FROM yazdMessage"; 147 private static final String DELETE_MESSAGE = 148 "DELETE FROM yazdMessage WHERE messageID=?"; 149 private static final String INSERT_BLACK_LIST="insert into yazdBlackList(ip,blockcount) values (?,?)"; 150 private static final String DELETE_BLACK_LIST="delete from yazdBlackList where ip=?"; 151 private static final String SELECT_BLACK_LIST="select blockcount from yazdBlackList where ip=?"; 152 private static final String SELECT_ARTICLE_MAP="select threadID from yazdArticleMap where pageKey=? and forumID=?"; 153 private static final String SESSION_LIST = "select sessionID,IP,userID from yazdSessions"; 154 private static final String YESTERDAY_SESSION_COUNT = "select usercount from yazdUserStats where day_dt=?"; 155 156 protected DbCacheManager cacheManager; 157 protected EmailWatchManager watchManager; 158 protected SessionManager sessionManager; 159 160 163 private ProfileManager profileManager; 164 165 168 private DbSearchIndexer searchIndexer; 169 170 173 public DbForumFactory() { 174 watchManager = new EmailWatchManager(this); 175 cacheManager = new DbCacheManager(); 176 177 profileManager = new DbProfileManager(this); 178 searchIndexer = new DbSearchIndexer(this); 179 sessionManager = new SessionManager(); 180 if(Boolean.valueOf(SystemProperty.getProperty("RemoveNotActiveAccounts")).booleanValue()){ 181 new CleanAccounts(); 182 } 183 } 184 185 187 public Forum createForum(String name, String description, 188 boolean moderated, int forumGroupID,boolean article) 189 throws UnauthorizedException, ForumAlreadyExistsException 190 { 191 Forum newForum = null; 192 try { 193 Forum existingForum = getForum(name); 194 195 throw new ForumAlreadyExistsException(); 197 } 198 catch (ForumNotFoundException fnfe) { 199 newForum = new DbForum(name, description, moderated, forumGroupID, this,article); 201 } 202 return newForum; 203 } 204 205 public void deleteForum(Forum forum) throws UnauthorizedException { 206 cacheManager.remove(DbCacheManager.FORUM_CACHE, new Integer (forum.getID())); 208 cacheManager.remove(DbCacheManager.USER_PERMS_CACHE, new Integer (forum.getID())); 209 cacheManager.remove(DbCacheManager.FORUM_ID_CACHE, forum.getName()); 210 211 Iterator threads = forum.threads(); 213 while (threads.hasNext()) { 214 ForumThread thread = (ForumThread)threads.next(); 215 forum.deleteThread(thread); 216 } 217 218 int filterCount = forum.getForumMessageFilters().length; 222 for (int i=filterCount-1; i>=0; i--) { 223 forum.removeForumMessageFilter(i); 224 } 225 226 Connection con = null; 229 PreparedStatement pstmt = null; 230 try { 231 con = DbConnectionManager.getConnection(); 232 pstmt = con.prepareStatement(DELETE_FORUM); 233 pstmt.setInt(1,forum.getID()); 234 pstmt.execute(); 235 pstmt.close(); 236 pstmt = con.prepareStatement(DELETE_FORUM_USER_PERMS); 238 pstmt.setInt(1,forum.getID()); 239 pstmt.execute(); 240 pstmt.close(); 241 pstmt = con.prepareStatement(DELETE_FORUM_GROUP_PERMS); 243 pstmt.setInt(1,forum.getID()); 244 pstmt.execute(); 245 pstmt.close(); 246 pstmt = con.prepareStatement(DELETE_FORUM_PROPERTIES); 248 pstmt.setInt(1,forum.getID()); 249 pstmt.execute(); 250 } 251 catch( Exception sqle ) { 252 System.err.println("Error in DbForumFactory:deleteForum()-" + sqle); 253 } 254 finally { 255 try { pstmt.close(); } 256 catch (Exception e) { e.printStackTrace(); } 257 try { con.close(); } 258 catch (Exception e) { e.printStackTrace(); } 259 } 260 } 261 262 public void deleteCategory(Category category) throws UnauthorizedException { 263 cacheManager.remove(DbCacheManager.CATEGORY_CACHE, new Integer (category.getID())); 265 cacheManager.remove(DbCacheManager.CATEGORY_ID_CACHE, category.getName()); 266 267 Iterator categories = category.forumGroups(); 269 while (categories.hasNext()) { 270 ForumGroup forumGroup = (ForumGroup)categories.next(); 271 category.deleteForumGroup(forumGroup); 272 } 273 274 275 Connection con = null; 277 PreparedStatement pstmt = null; 278 try { 279 con = DbConnectionManager.getConnection(); 280 pstmt = con.prepareStatement(DELETE_CATEGORY); 281 pstmt.setInt(1,category.getID()); 282 pstmt.execute(); 283 pstmt.close(); 284 } 285 catch( Exception sqle ) { 286 System.err.println("Error in DbForumFactory:deleteCategory()-" + sqle); 287 } 288 finally { 289 try { pstmt.close(); } 290 catch (Exception e) { e.printStackTrace(); } 291 try { con.close(); } 292 catch (Exception e) { e.printStackTrace(); } 293 } 294 } 295 296 public Iterator forumsModeration(){ return null; 298 } 299 public Forum getForum(int forumID) 300 throws ForumNotFoundException, UnauthorizedException 301 { 302 if (!cacheManager.isCacheEnabled()) { 304 return new DbForum(forumID, this); 305 } 306 Integer forumIDInteger = new Integer (forumID); 308 DbForum forum = (DbForum)cacheManager.get(DbCacheManager.FORUM_CACHE, forumIDInteger); 309 if(forum == null) { 310 forum = new DbForum(forumID, this); 311 cacheManager.add(DbCacheManager.FORUM_CACHE, forumIDInteger, forum); 312 } 313 return forum; 314 } 315 316 public Forum getForum(String name) 317 throws ForumNotFoundException, UnauthorizedException 318 { 319 if (!cacheManager.isCacheEnabled()) { 321 Forum forum = new DbForum(name, this); 322 return forum; 323 } 324 CacheableInteger forumIDInteger = (CacheableInteger)cacheManager.get( 326 DbCacheManager.FORUM_ID_CACHE, 327 name 328 ); 329 if (forumIDInteger == null) { 331 Forum forum = new DbForum(name, this); 332 forumIDInteger = new CacheableInteger(new Integer (forum.getID())); 333 cacheManager.add(DbCacheManager.FORUM_ID_CACHE, name, forumIDInteger); 334 } 335 return getForum(forumIDInteger.getInteger().intValue()); 336 } 337 338 public int getForumCount() { 339 int forumCount = 0; 340 Connection con = null; 341 PreparedStatement pstmt = null; 342 try { 343 con = DbConnectionManager.getConnection(); 344 pstmt = con.prepareStatement(FORUM_COUNT); 345 ResultSet rs = pstmt.executeQuery(); 346 rs.next(); 347 forumCount = rs.getInt(1); 348 } 349 catch( SQLException sqle ) { 350 System.err.println("DbForumFactory:getForumCount() failed: " + sqle); 351 } 352 finally { 353 try { pstmt.close(); } 354 catch (Exception e) { e.printStackTrace(); } 355 try { con.close(); } 356 catch (Exception e) { e.printStackTrace(); } 357 } 358 return forumCount; 359 } 360 361 public Iterator forums() { 362 return new DbForumFactoryIterator(this); 363 } 364 public Iterator forumsWithArticlesForums(){ 365 return new DbForumFactoryIterator(this); 366 } 367 public Category getCategory(int categoryID) 368 throws CategoryNotFoundException, UnauthorizedException 369 { 370 if (!cacheManager.isCacheEnabled()) { 372 return new DbCategory(categoryID, this); 373 } 374 Integer categoryIDInteger = new Integer (categoryID); 376 DbCategory category = (DbCategory)cacheManager.get(DbCacheManager.CATEGORY_CACHE, categoryIDInteger); 377 if(category == null) { 378 category = new DbCategory(categoryID, this); 379 cacheManager.add(DbCacheManager.CATEGORY_CACHE, categoryIDInteger, category); 380 } 381 return category; 382 } 383 384 public Category getCategory(String name) 385 throws CategoryNotFoundException, UnauthorizedException 386 { 387 if (!cacheManager.isCacheEnabled()) { 389 Category category = new DbCategory(name, this); 390 return category; 391 } 392 CacheableInteger categoryIDInteger = (CacheableInteger)cacheManager.get( 394 DbCacheManager.CATEGORY_ID_CACHE, 395 name 396 ); 397 if (categoryIDInteger == null) { 399 Category category = new DbCategory(name, this); 400 categoryIDInteger = new CacheableInteger(new Integer (category.getID())); 401 cacheManager.add(DbCacheManager.CATEGORY_ID_CACHE, name, categoryIDInteger); 402 } 403 return getCategory(categoryIDInteger.getInteger().intValue()); 404 } 405 406 public Iterator categories() { 407 return new DbCategoryIterator(this); 408 } 409 410 public Category createCategory(String name, String description) 411 throws UnauthorizedException, CategoryAlreadyExistsException 412 { 413 Category newCategory = null; 414 try { 415 Category existingCategory = getCategory(name); 416 417 throw new CategoryAlreadyExistsException(); 419 } 420 catch (CategoryNotFoundException fnfe) { 421 newCategory = new DbCategory(name, description, this); 423 } 424 return newCategory; 425 } 426 427 public ProfileManager getProfileManager() { 428 return profileManager; 429 } 430 431 public SearchIndexer getSearchIndexer() { 432 return searchIndexer; 433 } 434 435 public int[] usersWithPermission(int permissionType) 436 throws UnauthorizedException 437 { 438 int [] users = new int[0]; 439 Connection con = null; 440 PreparedStatement pstmt = null; 441 try { 442 con = DbConnectionManager.getConnection(); 443 pstmt = con.prepareStatement(USERS_WITH_PERM); 444 pstmt.setInt(1,-1); 445 pstmt.setInt(2,permissionType); 446 ResultSet rs = pstmt.executeQuery(); 447 ArrayList userList = new ArrayList(); 448 while (rs.next()) { 449 userList.add(new Integer (rs.getInt("userID"))); 450 } 451 users = new int[userList.size()]; 452 for (int i=0; i<users.length; i++) { 453 users[i] = ((Integer )userList.get(i)).intValue(); 454 } 455 } 456 catch( SQLException sqle ) { 457 sqle.printStackTrace(); 458 } 459 finally { 460 try { pstmt.close(); } 461 catch (Exception e) { e.printStackTrace(); } 462 try { con.close(); } 463 catch (Exception e) { e.printStackTrace(); } 464 } 465 return users; 466 } 467 468 public int[] groupsWithPermission(int permissionType) 469 throws UnauthorizedException 470 { 471 int [] groups = new int[0]; 472 Connection con = null; 473 PreparedStatement pstmt = null; 474 try { 475 con = DbConnectionManager.getConnection(); 476 pstmt = con.prepareStatement(GROUPS_WITH_PERM); 477 pstmt.setInt(1,-1); 478 pstmt.setInt(2,permissionType); 479 ResultSet rs = pstmt.executeQuery(); 480 ArrayList groupList = new ArrayList(); 481 while (rs.next()) { 482 groupList.add(new Integer (rs.getInt("groupID"))); 483 } 484 groups = new int[groupList.size()]; 485 for (int i=0; i<groups.length; i++) { 486 groups[i] = ((Integer )groupList.get(i)).intValue(); 487 } 488 } 489 catch( SQLException sqle ) { 490 System.err.println("Error in DbForum.groupsWithPermission:" + sqle); 491 sqle.printStackTrace(); 492 } 493 finally { 494 try { pstmt.close(); } 495 catch (Exception e) { e.printStackTrace(); } 496 try { con.close(); } 497 catch (Exception e) { e.printStackTrace(); } 498 } 499 return groups; 500 } 501 502 public ForumPermissions getPermissions(Authorization authorization) { 503 int userID = authorization.getUserID(); 504 505 Cache userPermCache = (Cache)getCacheManager().get( 507 DbCacheManager.USER_PERMS_CACHE, 508 new Integer (-1) 509 ); 510 511 if (userPermCache != null) { 514 ForumPermissions permissions = 515 (ForumPermissions)userPermCache.get(new Integer (userID)); 516 if (permissions != null) { 517 return permissions; 518 } 519 } 520 521 boolean isAnonymous = (userID == -1); 524 boolean isUser = !isAnonymous; 525 526 ForumPermissions finalPermissions = ForumPermissions.none(); 527 536 537 538 if (isUser) { 541 ForumPermissions userPermissions = getUserPermissions(userID, -1); 542 finalPermissions = new ForumPermissions(finalPermissions, userPermissions); 544 } 545 ForumPermissions anonyPermissions = null; 547 if (userPermCache != null) { 548 anonyPermissions = (ForumPermissions)userPermCache.get(new Integer (-1)); 549 } 550 if (anonyPermissions == null) { 552 anonyPermissions = getUserPermissions(-1, -1); 553 if (userPermCache != null) { 555 userPermCache.add(new Integer (-1), anonyPermissions); 556 } 557 } 558 finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions); 560 561 if (isUser) { 563 ForumPermissions specialUserPermissions = null; 564 if (userPermCache != null) { 566 specialUserPermissions = (ForumPermissions)userPermCache.get(new Integer (0)); 567 } 568 if (specialUserPermissions == null) { 570 specialUserPermissions = getUserPermissions(0, -1); 571 if (userPermCache != null) { 573 userPermCache.add(new Integer (0), specialUserPermissions); 574 } 575 } 576 finalPermissions = new ForumPermissions(finalPermissions, specialUserPermissions); 578 } 579 580 int [] groups = ((DbProfileManager)getProfileManager()).getUserGroups(userID); 582 for (int i=0; i<groups.length; i++) { 583 ForumPermissions groupPermissions = getGroupPermissions(groups[i], -1); 584 finalPermissions = new ForumPermissions(finalPermissions, groupPermissions); 585 } 586 587 if (isUser && userPermCache != null) { 589 userPermCache.add(new Integer (userID), finalPermissions); 590 } 591 592 return finalPermissions; 593 } 594 595 public boolean hasPermission(int type) { 596 return true; 597 } 598 599 601 604 public DbCacheManager getCacheManager() { 605 return cacheManager; 606 } 607 611 public EmailWatchManager getWatchManager(){ 612 return watchManager; 613 } 614 public SessionManager getSessionManager(){ 615 return sessionManager; 616 } 617 618 631 public void cleanDatabase() { 632 Iterator forums = forums(); 634 while (forums.hasNext()) { 635 Forum forum = (Forum)forums.next(); 636 Iterator threads = forum.threads(); 637 while (threads.hasNext()) { 638 try { 639 ForumThread thread = (ForumThread)threads.next(); 640 Iterator messages = thread.messages(); 641 while (messages.hasNext()) { 642 try { 643 ForumMessage message = (ForumMessage)messages.next(); 644 if ( message.getBody() == null) { 646 thread.deleteMessage(message); 648 } 649 } 650 catch (Exception me) { 651 me.printStackTrace(); 652 } 653 } 654 } 655 catch (Exception te) { 656 te.printStackTrace(); 657 } 658 } 659 } 660 661 713 } 714 715 720 public DbForumThread getThread(int threadID, DbForum forum) throws 721 ForumThreadNotFoundException 722 { 723 if (!cacheManager.isCacheEnabled()) { 725 return new DbForumThread(threadID, forum, this); 726 } 727 Integer threadIDInteger = new Integer (threadID); 729 DbForumThread thread = (DbForumThread)cacheManager.get( 730 DbCacheManager.THREAD_CACHE, 731 threadIDInteger 732 ); 733 if(thread == null) { 734 thread = new DbForumThread(threadID, forum, this); 735 cacheManager.add(DbCacheManager.THREAD_CACHE, threadIDInteger, thread); 737 } 738 return thread; 739 } 740 741 746 public DbForumGroup getForumGroup(int forumGroupID, DbCategory category) throws 747 ForumGroupNotFoundException 748 { 749 if (!cacheManager.isCacheEnabled()) { 751 return new DbForumGroup(forumGroupID, category, this); 752 } 753 Integer forumGroupIDInteger = new Integer (forumGroupID); 755 DbForumGroup forumGroup = (DbForumGroup)cacheManager.get( 756 DbCacheManager.FORUM_GROUP_CACHE, 757 forumGroupIDInteger 758 ); 759 if(forumGroup == null) { 760 forumGroup = new DbForumGroup(forumGroupID, category, this); 761 cacheManager.add(DbCacheManager.FORUM_GROUP_CACHE, forumGroupIDInteger, forumGroup); 762 } 763 return forumGroup; 764 } 765 766 772 protected DbForumMessage getMessage(int messageID) 773 throws ForumMessageNotFoundException 774 { 775 if (!cacheManager.isCacheEnabled()) { 777 return new DbForumMessage(messageID, this); 778 } 779 Integer messageIDInteger = new Integer (messageID); 781 DbForumMessage message = (DbForumMessage)cacheManager.get( 782 DbCacheManager.MESSAGE_CACHE, 783 messageIDInteger 784 ); 785 if(message == null) { 786 message = new DbForumMessage(messageID, this); 788 cacheManager.add(DbCacheManager.MESSAGE_CACHE, messageIDInteger, message); 790 } 791 return message; 792 } 793 794 797 protected void log(String message, Exception e) { 798 System.err.println("Log event : " + message); 799 e.printStackTrace(); 800 } 801 802 805 protected ForumPermissions getUserPermissions(int userID, int forumID) { 806 Connection con = null; 807 PreparedStatement pstmt = null; 808 boolean [] permissions = new boolean[8]; 810 for (int i=0; i<permissions.length; i++) { 811 permissions[i] = false; 812 } 813 try { 814 con = DbConnectionManager.getConnection(); 815 pstmt = con.prepareStatement(GET_USER_PERMS); 816 pstmt.setInt(1, forumID); 817 pstmt.setInt(2, userID); 818 ResultSet rs = pstmt.executeQuery(); 819 while(rs.next()) { 820 int newPerm = rs.getInt("permission"); 821 permissions[newPerm] = true; 822 } 823 } 824 catch( SQLException sqle ) { 825 System.err.println("Error in DbForum.java:" + sqle); 826 sqle.printStackTrace(); 827 } 828 finally { 829 try { pstmt.close(); } 830 catch (Exception e) { e.printStackTrace(); } 831 try { con.close(); } 832 catch (Exception e) { e.printStackTrace(); } 833 } 834 return new ForumPermissions(permissions); 835 } 836 837 840 protected ForumPermissions getGroupPermissions(int groupID, int forumID) { 841 Connection con = null; 842 PreparedStatement pstmt = null; 843 boolean [] permissions = new boolean[8]; 845 for (int i=0; i<permissions.length; i++) { 846 permissions[i] = false; 847 } 848 try { 849 con = DbConnectionManager.getConnection(); 850 pstmt = con.prepareStatement(GET_GROUP_PERMS); 851 pstmt.setInt(1, forumID); 852 pstmt.setInt(2, groupID); 853 ResultSet rs = pstmt.executeQuery(); 854 while(rs.next()) { 855 int newPerm = rs.getInt("permission"); 856 permissions[newPerm] = true; 857 } 858 } 859 catch( SQLException sqle ) { 860 sqle.printStackTrace(); 861 } 862 finally { 863 try { pstmt.close(); } 864 catch (Exception e) { e.printStackTrace(); } 865 try { con.close(); } 866 catch (Exception e) { e.printStackTrace(); } 867 } 868 return new ForumPermissions(permissions); 869 } 870 public Query createQuery(){ 871 return new DbQuery(this); 872 } 873 public void BlackListIP(ClientIP cip,boolean add) throws UnauthorizedException{ 874 Connection con = null; 875 PreparedStatement pstmt = null; 876 try { 877 con = DbConnectionManager.getConnection(); 878 if(add){ 879 pstmt = con.prepareStatement(INSERT_BLACK_LIST); 880 pstmt.setString(1,cip.getRemoteIP()); 881 pstmt.setInt(2,0); 882 }else{ 883 pstmt = con.prepareStatement(DELETE_BLACK_LIST); 884 pstmt.setString(1,cip.getRemoteIP()); 885 } 886 pstmt.executeUpdate(); 887 } 888 catch( SQLException sqle ) { 889 sqle.printStackTrace(); 890 } 891 finally { 892 try { pstmt.close(); } 893 catch (Exception e) { e.printStackTrace(); } 894 try { con.close(); } 895 catch (Exception e) { e.printStackTrace(); } 896 } 897 898 } 899 public boolean isBlackListed(ClientIP cip){ 900 boolean bl=false; 901 902 Connection con = null; 903 PreparedStatement pstmt = null; 904 try { 905 con = DbConnectionManager.getConnection(); 906 pstmt = con.prepareStatement(SELECT_BLACK_LIST); 907 pstmt.setString(1,cip.getRemoteIP()); 908 ResultSet rs = pstmt.executeQuery(); 909 if(rs.next()){ 910 bl = true; 911 } 912 } 913 catch( SQLException sqle ) { 914 sqle.printStackTrace(); 915 } 916 finally { 917 try { pstmt.close(); } 918 catch (Exception e) { e.printStackTrace(); } 919 try { con.close(); } 920 catch (Exception e) { e.printStackTrace(); } 921 } 922 923 return bl; 924 925 } 926 public ForumThread getArticleThread(String pageKey,Forum forum) throws ForumThreadNotFoundException,UnauthorizedException{ 927 Connection con = null; 928 PreparedStatement pstmt = null; 929 ForumThread thread=null; 930 try { 931 con = DbConnectionManager.getConnection(); 932 pstmt = con.prepareStatement(SELECT_ARTICLE_MAP); 933 pstmt.setString(1,pageKey); 934 pstmt.setInt(2,forum.getID()); 935 ResultSet rs = pstmt.executeQuery(); 936 if(rs.next()){ 937 thread=this.getThread(rs.getInt("threadID"),(DbForum)this.getForum(forum.getID())); 938 } else{ 939 throw new ForumThreadNotFoundException(); 940 } 941 }catch( SQLException sqle ) { 942 sqle.printStackTrace(); 943 } 944 catch(ForumNotFoundException e){ 945 946 } 947 finally { 948 try { pstmt.close(); } 949 catch (Exception e) { e.printStackTrace(); } 950 try { con.close(); } 951 catch (Exception e) { e.printStackTrace(); } 952 } 953 return thread; 954 955 } 956 public ThreadType getThreadType(int typeid){ 957 if (!cacheManager.isCacheEnabled()) { 959 return new DbThreadType(typeid); 960 } 961 Integer typeIDInteger = new Integer (typeid); 963 DbThreadType type = (DbThreadType)cacheManager.get( 964 DbCacheManager.THREAD_TYPE_ID_CACHE, 965 typeIDInteger 966 ); 967 if(type == null) { 968 type = new DbThreadType(typeid); 970 cacheManager.add(DbCacheManager.THREAD_TYPE_ID_CACHE, typeIDInteger, type); 972 } 973 return type; 974 975 } 976 public Iterator getThreadTypeIterator(){ 977 return new DbThreadTypeIterator(this); 978 } 979 public Iterator getSessionList(){ 980 Connection con = null; 981 PreparedStatement pstmt = null; 982 ArrayList sessions=new ArrayList(); 983 try { 984 con = DbConnectionManager.getConnection(); 985 pstmt = con.prepareStatement(SESSION_LIST); 986 ResultSet rs = pstmt.executeQuery(); 987 while(rs.next()){ 988 sessions.add( 989 new SessionVO(rs.getString("sessionID"),rs.getString("IP"),rs.getInt("userID")) 990 ); 991 } 992 }catch( SQLException sqle ) { 993 sqle.printStackTrace(); 994 } 995 finally { 996 try { pstmt.close(); } 997 catch (Exception e) { e.printStackTrace(); } 998 try { con.close(); } 999 catch (Exception e) { e.printStackTrace(); } 1000 } 1001 return sessions.iterator(); 1002 1003 } 1004 public int getYesterdayUserCount(){ 1005 Connection con = null; 1006 PreparedStatement pstmt = null; 1007 int usercount=0; 1008 Calendar now = Calendar.getInstance(); 1009 int now_in_minutes = (int)(now.getTimeInMillis()*1.0/(1000.0 * 60.0)); 1010 int now_today = (int)(now_in_minutes * 1.0/(60.0 * 24.0)); 1011 try { 1012 con = DbConnectionManager.getConnection(); 1013 pstmt = con.prepareStatement(YESTERDAY_SESSION_COUNT); 1014 pstmt.setInt(1,now_today-1); 1015 ResultSet rs = pstmt.executeQuery(); 1016 if(rs.next()){ 1017 usercount = rs.getInt("usercount"); 1018 } 1019 }catch( SQLException sqle ) { 1020 sqle.printStackTrace(); 1021 } 1022 finally { 1023 try { pstmt.close(); } 1024 catch (Exception e) { e.printStackTrace(); } 1025 try { con.close(); } 1026 catch (Exception e) { e.printStackTrace(); } 1027 } 1028 return usercount; 1029 1030 } 1031 1032} 1033 | Popular Tags |