Source Code Cross Referenced for DbForumFactory.java in  » Forum » yazd » com » Yasna » forum » database » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Forum » yazd » com.Yasna.forum.database 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * Copyright (C) 2001 Yasna.com. All rights reserved.
0003:         *
0004:         * ===================================================================
0005:         * The Apache Software License, Version 1.1
0006:         *
0007:         * Redistribution and use in source and binary forms, with or without
0008:         * modification, are permitted provided that the following conditions
0009:         * are met:
0010:         *
0011:         * 1. Redistributions of source code must retain the above copyright
0012:         *    notice, this list of conditions and the following disclaimer.
0013:         *
0014:         * 2. Redistributions in binary form must reproduce the above copyright
0015:         *    notice, this list of conditions and the following disclaimer in
0016:         *    the documentation and/or other materials provided with the
0017:         *    distribution.
0018:         *
0019:         * 3. The end-user documentation included with the redistribution,
0020:         *    if any, must include the following acknowledgment:
0021:         *       "This product includes software developed by
0022:         *        Yasna.com (http://www.yasna.com)."
0023:         *    Alternately, this acknowledgment may appear in the software itself,
0024:         *    if and wherever such third-party acknowledgments normally appear.
0025:         *
0026:         * 4. The names "Yazd" and "Yasna.com" must not be used to
0027:         *    endorse or promote products derived from this software without
0028:         *    prior written permission. For written permission, please
0029:         *    contact yazd@yasna.com.
0030:         *
0031:         * 5. Products derived from this software may not be called "Yazd",
0032:         *    nor may "Yazd" appear in their name, without prior written
0033:         *    permission of Yasna.com.
0034:         *
0035:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
0036:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0037:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0038:         * DISCLAIMED.  IN NO EVENT SHALL YASNA.COM OR
0039:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0040:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0041:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
0042:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0043:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0044:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0045:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0046:         * SUCH DAMAGE.
0047:         * ====================================================================
0048:         *
0049:         * This software consists of voluntary contributions made by many
0050:         * individuals on behalf of Yasna.com. For more information
0051:         * on Yasna.com, please see <http://www.yasna.com>.
0052:         */
0053:
0054:        /**
0055:         * Copyright (C) 2000 CoolServlets.com. All rights reserved.
0056:         *
0057:         * ===================================================================
0058:         * The Apache Software License, Version 1.1
0059:         *
0060:         * Redistribution and use in source and binary forms, with or without
0061:         * modification, are permitted provided that the following conditions
0062:         * are met:
0063:         *
0064:         * 1. Redistributions of source code must retain the above copyright
0065:         *    notice, this list of conditions and the following disclaimer.
0066:         *
0067:         * 2. Redistributions in binary form must reproduce the above copyright
0068:         *    notice, this list of conditions and the following disclaimer in
0069:         *    the documentation and/or other materials provided with the
0070:         *    distribution.
0071:         *
0072:         * 3. The end-user documentation included with the redistribution,
0073:         *    if any, must include the following acknowledgment:
0074:         *       "This product includes software developed by
0075:         *        CoolServlets.com (http://www.coolservlets.com)."
0076:         *    Alternately, this acknowledgment may appear in the software itself,
0077:         *    if and wherever such third-party acknowledgments normally appear.
0078:         *
0079:         * 4. The names "Jive" and "CoolServlets.com" must not be used to
0080:         *    endorse or promote products derived from this software without
0081:         *    prior written permission. For written permission, please
0082:         *    contact webmaster@coolservlets.com.
0083:         *
0084:         * 5. Products derived from this software may not be called "Jive",
0085:         *    nor may "Jive" appear in their name, without prior written
0086:         *    permission of CoolServlets.com.
0087:         *
0088:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
0089:         * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0090:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0091:         * DISCLAIMED.  IN NO EVENT SHALL COOLSERVLETS.COM OR
0092:         * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0093:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0094:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
0095:         * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0096:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
0097:         * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
0098:         * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0099:         * SUCH DAMAGE.
0100:         * ====================================================================
0101:         *
0102:         * This software consists of voluntary contributions made by many
0103:         * individuals on behalf of CoolServlets.com. For more information
0104:         * on CoolServlets.com, please see <http://www.coolservlets.com>.
0105:         */package com.Yasna.forum.database;
0106:
0107:        import com.Yasna.util.*;
0108:        import com.Yasna.forum.*;
0109:        import com.Yasna.forum.util.ClientIP;
0110:        import com.Yasna.forum.Tasks.EmailWatchManager;
0111:        import com.Yasna.forum.Tasks.SessionManager;
0112:        import com.Yasna.forum.Tasks.CleanAccounts;
0113:
0114:        import java.sql.*;
0115:        import java.util.*;
0116:        import java.io.*;
0117:
0118:        /**
0119:         * Database implementation of the ForumFactory interface.
0120:         */
0121:        public class DbForumFactory extends ForumFactory {
0122:
0123:            /** DATABASE QUERIES **/
0124:            private static final String FORUM_COUNT = "SELECT count(*) FROM yazdForum";
0125:            private static final String DELETE_FORUM = "DELETE FROM yazdForum WHERE forumID=?";
0126:            private static final String DELETE_CATEGORY = "DELETE FROM yazdCategory WHERE categoryID=?";
0127:            private static final String DELETE_FORUM_USER_PERMS = "DELETE FROM yazdUserPerm WHERE forumID=?";
0128:            private static final String DELETE_FORUM_GROUP_PERMS = "DELETE FROM yazdGroupPerm WHERE forumID=?";
0129:            private static final String DELETE_FORUM_PROPERTIES = "DELETE FROM yazdForumProp WHERE forumID=?";
0130:            private static final String GET_USER_PERMS = "SELECT DISTINCT permission FROM yazdUserPerm WHERE forumID=? "
0131:                    + "AND userID=?";
0132:            private static final String USERS_WITH_PERM = "SELECT DISTINCT userID FROM yazdUserPerm WHERE forumID=? AND permission=?";
0133:            private static final String GET_GROUP_PERMS = "SELECT DISTINCT permission from yazdGroupPerm WHERE forumID=? "
0134:                    + "AND groupID=?";
0135:            private static final String GROUPS_WITH_PERM = "SELECT DISTINCT groupID FROM yazdGroupPerm WHERE forumID=? AND permission=?";
0136:            private static final String ALL_MESSAGES = "SELECT messageID FROM yazdMessage";
0137:            private static final String DELETE_MESSAGE = "DELETE FROM yazdMessage WHERE messageID=?";
0138:            private static final String INSERT_BLACK_LIST = "insert into yazdBlackList(ip,blockcount) values (?,?)";
0139:            private static final String DELETE_BLACK_LIST = "delete from yazdBlackList where ip=?";
0140:            private static final String SELECT_BLACK_LIST = "select blockcount from yazdBlackList where ip=?";
0141:            private static final String SELECT_ARTICLE_MAP = "select threadID from yazdArticleMap where pageKey=? and forumID=?";
0142:            private static final String SESSION_LIST = "select sessionID,IP,userID from yazdSessions";
0143:            private static final String YESTERDAY_SESSION_COUNT = "select usercount from yazdUserStats where day_dt=?";
0144:
0145:            protected DbCacheManager cacheManager;
0146:            protected EmailWatchManager watchManager;
0147:            protected SessionManager sessionManager;
0148:
0149:            /**
0150:             * The profile manager provides access to users and groups.
0151:             */
0152:            private ProfileManager profileManager;
0153:
0154:            /**
0155:             * The search indexer periodically runs to index forum content
0156:             */
0157:            private DbSearchIndexer searchIndexer;
0158:
0159:            /**
0160:             * Creates a new DbForumFactory.
0161:             */
0162:            public DbForumFactory() {
0163:                watchManager = new EmailWatchManager(this );
0164:                cacheManager = new DbCacheManager();
0165:
0166:                profileManager = new DbProfileManager(this );
0167:                searchIndexer = new DbSearchIndexer(this );
0168:                sessionManager = new SessionManager();
0169:                if (Boolean.valueOf(
0170:                        SystemProperty.getProperty("RemoveNotActiveAccounts"))
0171:                        .booleanValue()) {
0172:                    new CleanAccounts();
0173:                }
0174:            }
0175:
0176:            //FROM THE FORUMFACTORY INTERFACE//
0177:
0178:            public Forum createForum(String name, String description,
0179:                    boolean moderated, int forumGroupID, boolean article)
0180:                    throws UnauthorizedException, ForumAlreadyExistsException {
0181:                Forum newForum = null;
0182:                try {
0183:                    Forum existingForum = getForum(name);
0184:
0185:                    //The forum already exists since now exception, so:
0186:                    throw new ForumAlreadyExistsException();
0187:                } catch (ForumNotFoundException fnfe) {
0188:                    //The forum doesn't already exist so we can create a new one
0189:                    newForum = new DbForum(name, description, moderated,
0190:                            forumGroupID, this , article);
0191:                }
0192:                return newForum;
0193:            }
0194:
0195:            public void deleteForum(Forum forum) throws UnauthorizedException {
0196:                //First, remove forum from memory.
0197:                cacheManager.remove(DbCacheManager.FORUM_CACHE, new Integer(
0198:                        forum.getID()));
0199:                cacheManager.remove(DbCacheManager.USER_PERMS_CACHE,
0200:                        new Integer(forum.getID()));
0201:                cacheManager.remove(DbCacheManager.FORUM_ID_CACHE, forum
0202:                        .getName());
0203:
0204:                //Delete all messages and threads in the forum.
0205:                Iterator threads = forum.threads();
0206:                while (threads.hasNext()) {
0207:                    ForumThread thread = (ForumThread) threads.next();
0208:                    forum.deleteThread(thread);
0209:                }
0210:
0211:                //Now, delete all filters associated with the forum. We delete in
0212:                //reverse order since filter indexes will change if we don't delete
0213:                //the last filter entry.
0214:                int filterCount = forum.getForumMessageFilters().length;
0215:                for (int i = filterCount - 1; i >= 0; i--) {
0216:                    forum.removeForumMessageFilter(i);
0217:                }
0218:
0219:                //Finally, delete the forum itself and all permissions and properties
0220:                //associated with it.
0221:                Connection con = null;
0222:                PreparedStatement pstmt = null;
0223:                try {
0224:                    con = DbConnectionManager.getConnection();
0225:                    pstmt = con.prepareStatement(DELETE_FORUM);
0226:                    pstmt.setInt(1, forum.getID());
0227:                    pstmt.execute();
0228:                    pstmt.close();
0229:                    //User perms
0230:                    pstmt = con.prepareStatement(DELETE_FORUM_USER_PERMS);
0231:                    pstmt.setInt(1, forum.getID());
0232:                    pstmt.execute();
0233:                    pstmt.close();
0234:                    //Group perms
0235:                    pstmt = con.prepareStatement(DELETE_FORUM_GROUP_PERMS);
0236:                    pstmt.setInt(1, forum.getID());
0237:                    pstmt.execute();
0238:                    pstmt.close();
0239:                    //Properties
0240:                    pstmt = con.prepareStatement(DELETE_FORUM_PROPERTIES);
0241:                    pstmt.setInt(1, forum.getID());
0242:                    pstmt.execute();
0243:                } catch (Exception sqle) {
0244:                    System.err.println("Error in DbForumFactory:deleteForum()-"
0245:                            + sqle);
0246:                } finally {
0247:                    try {
0248:                        pstmt.close();
0249:                    } catch (Exception e) {
0250:                        e.printStackTrace();
0251:                    }
0252:                    try {
0253:                        con.close();
0254:                    } catch (Exception e) {
0255:                        e.printStackTrace();
0256:                    }
0257:                }
0258:            }
0259:
0260:            public void deleteCategory(Category category)
0261:                    throws UnauthorizedException {
0262:                //First, remove forum from memory.
0263:                cacheManager.remove(DbCacheManager.CATEGORY_CACHE, new Integer(
0264:                        category.getID()));
0265:                cacheManager.remove(DbCacheManager.CATEGORY_ID_CACHE, category
0266:                        .getName());
0267:
0268:                //Delete all forumgroups and forums in the cayegory.
0269:                Iterator categories = category.forumGroups();
0270:                while (categories.hasNext()) {
0271:                    ForumGroup forumGroup = (ForumGroup) categories.next();
0272:                    category.deleteForumGroup(forumGroup);
0273:                }
0274:
0275:                //Finally, delete the category itself
0276:                Connection con = null;
0277:                PreparedStatement pstmt = null;
0278:                try {
0279:                    con = DbConnectionManager.getConnection();
0280:                    pstmt = con.prepareStatement(DELETE_CATEGORY);
0281:                    pstmt.setInt(1, category.getID());
0282:                    pstmt.execute();
0283:                    pstmt.close();
0284:                } catch (Exception sqle) {
0285:                    System.err
0286:                            .println("Error in DbForumFactory:deleteCategory()-"
0287:                                    + sqle);
0288:                } finally {
0289:                    try {
0290:                        pstmt.close();
0291:                    } catch (Exception e) {
0292:                        e.printStackTrace();
0293:                    }
0294:                    try {
0295:                        con.close();
0296:                    } catch (Exception e) {
0297:                        e.printStackTrace();
0298:                    }
0299:                }
0300:            }
0301:
0302:            public Iterator forumsModeration() { //this has to be revisited again
0303:                return null;
0304:            }
0305:
0306:            public Forum getForum(int forumID) throws ForumNotFoundException,
0307:                    UnauthorizedException {
0308:                //If cache is not enabled, do a new lookup of object
0309:                if (!cacheManager.isCacheEnabled()) {
0310:                    return new DbForum(forumID, this );
0311:                }
0312:                //Cache is enabled.
0313:                Integer forumIDInteger = new Integer(forumID);
0314:                DbForum forum = (DbForum) cacheManager.get(
0315:                        DbCacheManager.FORUM_CACHE, forumIDInteger);
0316:                if (forum == null) {
0317:                    forum = new DbForum(forumID, this );
0318:                    cacheManager.add(DbCacheManager.FORUM_CACHE,
0319:                            forumIDInteger, forum);
0320:                }
0321:                return forum;
0322:            }
0323:
0324:            public Forum getForum(String name) throws ForumNotFoundException,
0325:                    UnauthorizedException {
0326:                //If cache is not enabled, do a new lookup of object
0327:                if (!cacheManager.isCacheEnabled()) {
0328:                    Forum forum = new DbForum(name, this );
0329:                    return forum;
0330:                }
0331:                //Cache is enabled.
0332:                CacheableInteger forumIDInteger = (CacheableInteger) cacheManager
0333:                        .get(DbCacheManager.FORUM_ID_CACHE, name);
0334:                //if id wan't found in cache, load it up and put it there.
0335:                if (forumIDInteger == null) {
0336:                    Forum forum = new DbForum(name, this );
0337:                    forumIDInteger = new CacheableInteger(new Integer(forum
0338:                            .getID()));
0339:                    cacheManager.add(DbCacheManager.FORUM_ID_CACHE, name,
0340:                            forumIDInteger);
0341:                }
0342:                return getForum(forumIDInteger.getInteger().intValue());
0343:            }
0344:
0345:            public int getForumCount() {
0346:                int forumCount = 0;
0347:                Connection con = null;
0348:                PreparedStatement pstmt = null;
0349:                try {
0350:                    con = DbConnectionManager.getConnection();
0351:                    pstmt = con.prepareStatement(FORUM_COUNT);
0352:                    ResultSet rs = pstmt.executeQuery();
0353:                    rs.next();
0354:                    forumCount = rs.getInt(1);
0355:                } catch (SQLException sqle) {
0356:                    System.err
0357:                            .println("DbForumFactory:getForumCount() failed: "
0358:                                    + sqle);
0359:                } finally {
0360:                    try {
0361:                        pstmt.close();
0362:                    } catch (Exception e) {
0363:                        e.printStackTrace();
0364:                    }
0365:                    try {
0366:                        con.close();
0367:                    } catch (Exception e) {
0368:                        e.printStackTrace();
0369:                    }
0370:                }
0371:                return forumCount;
0372:            }
0373:
0374:            public Iterator forums() {
0375:                return new DbForumFactoryIterator(this );
0376:            }
0377:
0378:            public Iterator forumsWithArticlesForums() {
0379:                return new DbForumFactoryIterator(this );
0380:            }
0381:
0382:            public Category getCategory(int categoryID)
0383:                    throws CategoryNotFoundException, UnauthorizedException {
0384:                //If cache is not enabled, do a new lookup of object
0385:                if (!cacheManager.isCacheEnabled()) {
0386:                    return new DbCategory(categoryID, this );
0387:                }
0388:                //Cache is enabled.
0389:                Integer categoryIDInteger = new Integer(categoryID);
0390:                DbCategory category = (DbCategory) cacheManager.get(
0391:                        DbCacheManager.CATEGORY_CACHE, categoryIDInteger);
0392:                if (category == null) {
0393:                    category = new DbCategory(categoryID, this );
0394:                    cacheManager.add(DbCacheManager.CATEGORY_CACHE,
0395:                            categoryIDInteger, category);
0396:                }
0397:                return category;
0398:            }
0399:
0400:            public Category getCategory(String name)
0401:                    throws CategoryNotFoundException, UnauthorizedException {
0402:                //If cache is not enabled, do a new lookup of object
0403:                if (!cacheManager.isCacheEnabled()) {
0404:                    Category category = new DbCategory(name, this );
0405:                    return category;
0406:                }
0407:                //Cache is enabled.
0408:                CacheableInteger categoryIDInteger = (CacheableInteger) cacheManager
0409:                        .get(DbCacheManager.CATEGORY_ID_CACHE, name);
0410:                //if id wan't found in cache, load it up and put it there.
0411:                if (categoryIDInteger == null) {
0412:                    Category category = new DbCategory(name, this );
0413:                    categoryIDInteger = new CacheableInteger(new Integer(
0414:                            category.getID()));
0415:                    cacheManager.add(DbCacheManager.CATEGORY_ID_CACHE, name,
0416:                            categoryIDInteger);
0417:                }
0418:                return getCategory(categoryIDInteger.getInteger().intValue());
0419:            }
0420:
0421:            public Iterator categories() {
0422:                return new DbCategoryIterator(this );
0423:            }
0424:
0425:            public Category createCategory(String name, String description)
0426:                    throws UnauthorizedException,
0427:                    CategoryAlreadyExistsException {
0428:                Category newCategory = null;
0429:                try {
0430:                    Category existingCategory = getCategory(name);
0431:
0432:                    //The forum already exists since now exception, so:
0433:                    throw new CategoryAlreadyExistsException();
0434:                } catch (CategoryNotFoundException fnfe) {
0435:                    //The forum doesn't already exist so we can create a new one
0436:                    newCategory = new DbCategory(name, description, this );
0437:                }
0438:                return newCategory;
0439:            }
0440:
0441:            public ProfileManager getProfileManager() {
0442:                return profileManager;
0443:            }
0444:
0445:            public SearchIndexer getSearchIndexer() {
0446:                return searchIndexer;
0447:            }
0448:
0449:            public int[] usersWithPermission(int permissionType)
0450:                    throws UnauthorizedException {
0451:                int[] users = new int[0];
0452:                Connection con = null;
0453:                PreparedStatement pstmt = null;
0454:                try {
0455:                    con = DbConnectionManager.getConnection();
0456:                    pstmt = con.prepareStatement(USERS_WITH_PERM);
0457:                    pstmt.setInt(1, -1);
0458:                    pstmt.setInt(2, permissionType);
0459:                    ResultSet rs = pstmt.executeQuery();
0460:                    ArrayList userList = new ArrayList();
0461:                    while (rs.next()) {
0462:                        userList.add(new Integer(rs.getInt("userID")));
0463:                    }
0464:                    users = new int[userList.size()];
0465:                    for (int i = 0; i < users.length; i++) {
0466:                        users[i] = ((Integer) userList.get(i)).intValue();
0467:                    }
0468:                } catch (SQLException sqle) {
0469:                    sqle.printStackTrace();
0470:                } finally {
0471:                    try {
0472:                        pstmt.close();
0473:                    } catch (Exception e) {
0474:                        e.printStackTrace();
0475:                    }
0476:                    try {
0477:                        con.close();
0478:                    } catch (Exception e) {
0479:                        e.printStackTrace();
0480:                    }
0481:                }
0482:                return users;
0483:            }
0484:
0485:            public int[] groupsWithPermission(int permissionType)
0486:                    throws UnauthorizedException {
0487:                int[] groups = new int[0];
0488:                Connection con = null;
0489:                PreparedStatement pstmt = null;
0490:                try {
0491:                    con = DbConnectionManager.getConnection();
0492:                    pstmt = con.prepareStatement(GROUPS_WITH_PERM);
0493:                    pstmt.setInt(1, -1);
0494:                    pstmt.setInt(2, permissionType);
0495:                    ResultSet rs = pstmt.executeQuery();
0496:                    ArrayList groupList = new ArrayList();
0497:                    while (rs.next()) {
0498:                        groupList.add(new Integer(rs.getInt("groupID")));
0499:                    }
0500:                    groups = new int[groupList.size()];
0501:                    for (int i = 0; i < groups.length; i++) {
0502:                        groups[i] = ((Integer) groupList.get(i)).intValue();
0503:                    }
0504:                } catch (SQLException sqle) {
0505:                    System.err.println("Error in DbForum.groupsWithPermission:"
0506:                            + sqle);
0507:                    sqle.printStackTrace();
0508:                } finally {
0509:                    try {
0510:                        pstmt.close();
0511:                    } catch (Exception e) {
0512:                        e.printStackTrace();
0513:                    }
0514:                    try {
0515:                        con.close();
0516:                    } catch (Exception e) {
0517:                        e.printStackTrace();
0518:                    }
0519:                }
0520:                return groups;
0521:            }
0522:
0523:            public ForumPermissions getPermissions(Authorization authorization) {
0524:                int userID = authorization.getUserID();
0525:
0526:                //Get the user perm cache for this forum
0527:                Cache userPermCache = (Cache) getCacheManager().get(
0528:                        DbCacheManager.USER_PERMS_CACHE, new Integer(-1));
0529:
0530:                //Simple case: if cache is turned on and the user is already cached,
0531:                //we can simply return the cached permissions.
0532:                if (userPermCache != null) {
0533:                    ForumPermissions permissions = (ForumPermissions) userPermCache
0534:                            .get(new Integer(userID));
0535:                    if (permissions != null) {
0536:                        return permissions;
0537:                    }
0538:                }
0539:
0540:                //Not so simple case: cache is not turned on or the user permissions
0541:                //have not been cached yet.
0542:                boolean isAnonymous = (userID == -1);
0543:                boolean isUser = !isAnonymous;
0544:
0545:                ForumPermissions finalPermissions = ForumPermissions.none();
0546:                // check each forum for a specific permission
0547:                /*Iterator allForums = this.forums();
0548:                Forum forum;
0549:                ForumPermissions forumUserPermissions;
0550:                while (allForums.hasNext()){
0551:                	forum = (Forum)allForums.next();
0552:                	forumUserPermissions = getUserPermissions(userID,forum.getID());
0553:                        	finalPermissions = new ForumPermissions(finalPermissions, forumUserPermissions);
0554:                }      */
0555:
0556:                //Step 1 - Get permissions for the User. This includes anonymous
0557:                //perms, "special user" perms, and the specific perms for the user.
0558:                if (isUser) {
0559:                    ForumPermissions userPermissions = getUserPermissions(
0560:                            userID, -1);
0561:                    //Combine permissions
0562:                    finalPermissions = new ForumPermissions(finalPermissions,
0563:                            userPermissions);
0564:                }
0565:                //Add in anonymous perms.
0566:                ForumPermissions anonyPermissions = null;
0567:                if (userPermCache != null) {
0568:                    anonyPermissions = (ForumPermissions) userPermCache
0569:                            .get(new Integer(-1));
0570:                }
0571:                //Otherwise, do our own lookup.
0572:                if (anonyPermissions == null) {
0573:                    anonyPermissions = getUserPermissions(-1, -1);
0574:                    //Add to cache so it will be there next time.
0575:                    if (userPermCache != null) {
0576:                        userPermCache.add(new Integer(-1), anonyPermissions);
0577:                    }
0578:                }
0579:                //Combine permissions
0580:                finalPermissions = new ForumPermissions(finalPermissions,
0581:                        anonyPermissions);
0582:
0583:                //If they are a valid user, figure out "any user" permissions.
0584:                if (isUser) {
0585:                    ForumPermissions specialUserPermissions = null;
0586:                    //Check for cache
0587:                    if (userPermCache != null) {
0588:                        specialUserPermissions = (ForumPermissions) userPermCache
0589:                                .get(new Integer(0));
0590:                    }
0591:                    //Otherwise, do our own lookup.
0592:                    if (specialUserPermissions == null) {
0593:                        specialUserPermissions = getUserPermissions(0, -1);
0594:                        //Add to cache so it will be there next time.
0595:                        if (userPermCache != null) {
0596:                            userPermCache.add(new Integer(0),
0597:                                    specialUserPermissions);
0598:                        }
0599:                    }
0600:                    //Combine permissions
0601:                    finalPermissions = new ForumPermissions(finalPermissions,
0602:                            specialUserPermissions);
0603:                }
0604:
0605:                //Step 2 -- get Permissions for all groups the user is in.
0606:                int[] groups = ((DbProfileManager) getProfileManager())
0607:                        .getUserGroups(userID);
0608:                for (int i = 0; i < groups.length; i++) {
0609:                    ForumPermissions groupPermissions = getGroupPermissions(
0610:                            groups[i], -1);
0611:                    finalPermissions = new ForumPermissions(finalPermissions,
0612:                            groupPermissions);
0613:                }
0614:
0615:                //Finally, add user to cache so it will be there next time.
0616:                if (isUser && userPermCache != null) {
0617:                    userPermCache.add(new Integer(userID), finalPermissions);
0618:                }
0619:
0620:                return finalPermissions;
0621:            }
0622:
0623:            public boolean hasPermission(int type) {
0624:                return true;
0625:            }
0626:
0627:            //OTHER METHODS//
0628:
0629:            /**
0630:             * Returns the cache manager object.
0631:             */
0632:            public DbCacheManager getCacheManager() {
0633:                return cacheManager;
0634:            }
0635:
0636:            /**
0637:             * returns the watch manager. This is only used by this package.
0638:             * @return watchmanager
0639:             */
0640:            public EmailWatchManager getWatchManager() {
0641:                return watchManager;
0642:            }
0643:
0644:            public SessionManager getSessionManager() {
0645:                return sessionManager;
0646:            }
0647:
0648:            /**
0649:             * Cleans the Yazd database of "junk". This is currently defined as: <ul>
0650:             *      <li> Messages with no subject or body.
0651:             *      <li> Messages that do not belong to a thread.</ul>
0652:             *
0653:             * Please be aware that this method will <b>permanently</b> delete forum
0654:             * content. You may want to perform a database backup before calling this
0655:             * method.<p>
0656:             *
0657:             * This method requires two database connections and may take a long time
0658:             * to execute, as it must iterate through ever message record in the
0659:             * database.
0660:             */
0661:            public void cleanDatabase() {
0662:                //Iterate through all forums, threads to delete unwanted messages.
0663:                Iterator forums = forums();
0664:                while (forums.hasNext()) {
0665:                    Forum forum = (Forum) forums.next();
0666:                    Iterator threads = forum.threads();
0667:                    while (threads.hasNext()) {
0668:                        try {
0669:                            ForumThread thread = (ForumThread) threads.next();
0670:                            Iterator messages = thread.messages();
0671:                            while (messages.hasNext()) {
0672:                                try {
0673:                                    ForumMessage message = (ForumMessage) messages
0674:                                            .next();
0675:                                    //System.err.println("Evaluating message " + message.getID() + " for deletion");
0676:                                    if (/*message.getSubject() == null ||*/message
0677:                                            .getBody() == null) {
0678:                                        //System.err.println("Deleting message...");
0679:                                        thread.deleteMessage(message);
0680:                                    }
0681:                                } catch (Exception me) {
0682:                                    me.printStackTrace();
0683:                                }
0684:                            }
0685:                        } catch (Exception te) {
0686:                            te.printStackTrace();
0687:                        }
0688:                    }
0689:                }
0690:
0691:                /*
0692:                 //Select all message ID's directly from the message table.
0693:                 Connection con = null;
0694:                 PreparedStatement pstmt = null;
0695:                 try {
0696:                 con = DbConnectionManager.getConnection();
0697:                 pstmt = con.prepareStatement(ALL_MESSAGES);
0698:                 ResultSet rs = pstmt.executeQuery();
0699:                 while(rs.next()) {
0700:                 try {
0701:                 int messageID = rs.getInt(1);
0702:                 //Convert to object
0703:                 ForumMessage message = new DbForumMessage(messageID, this);
0704:                 ForumThread thread = message.getForumThread();
0705:                 if (thread == null) {
0706:                 //manually delete this message from the database. It won't
0707:                 //appear in any search indexes or in any threads, so this
0708:                 //shouldn't have any side effects.
0709:                 Connection con2 = null;
0710:                 PreparedStatement pstmt2 = null;
0711:                 try {
0712:                 con2 = DbConnectionManager.getConnection();
0713:                 pstmt2 = con.prepareStatement(DELETE_MESSAGE);
0714:                 pstmt2.setInt(1, messageID);
0715:                 pstmt2.execute();
0716:                 }
0717:                 catch( SQLException sqle ) {
0718:                 sqle.printStackTrace();
0719:                 }
0720:                 finally {
0721:                 try {  pstmt2.close(); }
0722:                 catch (Exception e) { e.printStackTrace(); }
0723:                 try {  con2.close();   }
0724:                 catch (Exception e) { e.printStackTrace(); }
0725:                 }
0726:                 }
0727:                 }
0728:                 catch (ForumMessageNotFoundException fmnfe) {
0729:                 fmnfe.printStackTrace();
0730:                 }
0731:                 }
0732:                 }
0733:                 catch( SQLException sqle ) {
0734:                 sqle.printStackTrace();
0735:                 }
0736:                 finally {
0737:                 try {  pstmt.close(); }
0738:                 catch (Exception e) { e.printStackTrace(); }
0739:                 try {  con.close();   }
0740:                 catch (Exception e) { e.printStackTrace(); }
0741:                 }
0742:                 */
0743:            }
0744:
0745:            /**
0746:             * Returns a thread specified by its id. Will return null
0747:             * if the thread is not in the forum. If cache is turned
0748:             * on, it will use it.
0749:             */
0750:            public DbForumThread getThread(int threadID, DbForum forum)
0751:                    throws ForumThreadNotFoundException {
0752:                //If cache is not enabled, do a new lookup of object
0753:                if (!cacheManager.isCacheEnabled()) {
0754:                    return new DbForumThread(threadID, forum, this );
0755:                }
0756:                //Cache is enabled.
0757:                Integer threadIDInteger = new Integer(threadID);
0758:                DbForumThread thread = (DbForumThread) cacheManager.get(
0759:                        DbCacheManager.THREAD_CACHE, threadIDInteger);
0760:                if (thread == null) {
0761:                    thread = new DbForumThread(threadID, forum, this );
0762:                    //System.err.println("Aflatoon adding to cache");
0763:                    cacheManager.add(DbCacheManager.THREAD_CACHE,
0764:                            threadIDInteger, thread);
0765:                }
0766:                return thread;
0767:            }
0768:
0769:            /**
0770:             * Returns a forumgroup specified by its id. Will return null
0771:             * if the forumgroup is not in the category. If cache is turned
0772:             * on, it will use it.
0773:             */
0774:            public DbForumGroup getForumGroup(int forumGroupID,
0775:                    DbCategory category) throws ForumGroupNotFoundException {
0776:                //If cache is not enabled, do a new lookup of object
0777:                if (!cacheManager.isCacheEnabled()) {
0778:                    return new DbForumGroup(forumGroupID, category, this );
0779:                }
0780:                //Cache is enabled.
0781:                Integer forumGroupIDInteger = new Integer(forumGroupID);
0782:                DbForumGroup forumGroup = (DbForumGroup) cacheManager.get(
0783:                        DbCacheManager.FORUM_GROUP_CACHE, forumGroupIDInteger);
0784:                if (forumGroup == null) {
0785:                    forumGroup = new DbForumGroup(forumGroupID, category, this );
0786:                    cacheManager.add(DbCacheManager.FORUM_GROUP_CACHE,
0787:                            forumGroupIDInteger, forumGroup);
0788:                }
0789:                return forumGroup;
0790:            }
0791:
0792:            /**
0793:             * Returns a message from the thread based on its id. If cache is turned
0794:             * on, it will use it.
0795:             *
0796:             * @param messageID the ID of the message to get from the thread.
0797:             */
0798:            protected DbForumMessage getMessage(int messageID)
0799:                    throws ForumMessageNotFoundException {
0800:                //If cache is not enabled, do a new lookup of object
0801:                if (!cacheManager.isCacheEnabled()) {
0802:                    return new DbForumMessage(messageID, this );
0803:                }
0804:                //Cache is enabled.
0805:                Integer messageIDInteger = new Integer(messageID);
0806:                DbForumMessage message = (DbForumMessage) cacheManager.get(
0807:                        DbCacheManager.MESSAGE_CACHE, messageIDInteger);
0808:                if (message == null) {
0809:                    //Load the message
0810:                    message = new DbForumMessage(messageID, this );
0811:                    //Add it to cache.
0812:                    cacheManager.add(DbCacheManager.MESSAGE_CACHE,
0813:                            messageIDInteger, message);
0814:                }
0815:                return message;
0816:            }
0817:
0818:            /**
0819:             * Logs events in the system. Very beginnings here....
0820:             */
0821:            protected void log(String message, Exception e) {
0822:                System.err.println("Log event : " + message);
0823:                e.printStackTrace();
0824:            }
0825:
0826:            /**
0827:             * Returns the permissions that a particular user has for the forum.
0828:             */
0829:            protected ForumPermissions getUserPermissions(int userID,
0830:                    int forumID) {
0831:                Connection con = null;
0832:                PreparedStatement pstmt = null;
0833:                //Initialize a permissions array with no permissions.
0834:                boolean[] permissions = new boolean[8];
0835:                for (int i = 0; i < permissions.length; i++) {
0836:                    permissions[i] = false;
0837:                }
0838:                try {
0839:                    con = DbConnectionManager.getConnection();
0840:                    pstmt = con.prepareStatement(GET_USER_PERMS);
0841:                    pstmt.setInt(1, forumID);
0842:                    pstmt.setInt(2, userID);
0843:                    ResultSet rs = pstmt.executeQuery();
0844:                    while (rs.next()) {
0845:                        int newPerm = rs.getInt("permission");
0846:                        permissions[newPerm] = true;
0847:                    }
0848:                } catch (SQLException sqle) {
0849:                    System.err.println("Error in DbForum.java:" + sqle);
0850:                    sqle.printStackTrace();
0851:                } finally {
0852:                    try {
0853:                        pstmt.close();
0854:                    } catch (Exception e) {
0855:                        e.printStackTrace();
0856:                    }
0857:                    try {
0858:                        con.close();
0859:                    } catch (Exception e) {
0860:                        e.printStackTrace();
0861:                    }
0862:                }
0863:                return new ForumPermissions(permissions);
0864:            }
0865:
0866:            /**
0867:             * Returns the permissions that a particular group has for the forum.
0868:             */
0869:            protected ForumPermissions getGroupPermissions(int groupID,
0870:                    int forumID) {
0871:                Connection con = null;
0872:                PreparedStatement pstmt = null;
0873:                //Initialize a permissions array with no permissions.
0874:                boolean[] permissions = new boolean[8];
0875:                for (int i = 0; i < permissions.length; i++) {
0876:                    permissions[i] = false;
0877:                }
0878:                try {
0879:                    con = DbConnectionManager.getConnection();
0880:                    pstmt = con.prepareStatement(GET_GROUP_PERMS);
0881:                    pstmt.setInt(1, forumID);
0882:                    pstmt.setInt(2, groupID);
0883:                    ResultSet rs = pstmt.executeQuery();
0884:                    while (rs.next()) {
0885:                        int newPerm = rs.getInt("permission");
0886:                        permissions[newPerm] = true;
0887:                    }
0888:                } catch (SQLException sqle) {
0889:                    sqle.printStackTrace();
0890:                } finally {
0891:                    try {
0892:                        pstmt.close();
0893:                    } catch (Exception e) {
0894:                        e.printStackTrace();
0895:                    }
0896:                    try {
0897:                        con.close();
0898:                    } catch (Exception e) {
0899:                        e.printStackTrace();
0900:                    }
0901:                }
0902:                return new ForumPermissions(permissions);
0903:            }
0904:
0905:            public Query createQuery() {
0906:                return new DbQuery(this );
0907:            }
0908:
0909:            public void BlackListIP(ClientIP cip, boolean add)
0910:                    throws UnauthorizedException {
0911:                Connection con = null;
0912:                PreparedStatement pstmt = null;
0913:                try {
0914:                    con = DbConnectionManager.getConnection();
0915:                    if (add) {
0916:                        pstmt = con.prepareStatement(INSERT_BLACK_LIST);
0917:                        pstmt.setString(1, cip.getRemoteIP());
0918:                        pstmt.setInt(2, 0);
0919:                    } else {
0920:                        pstmt = con.prepareStatement(DELETE_BLACK_LIST);
0921:                        pstmt.setString(1, cip.getRemoteIP());
0922:                    }
0923:                    pstmt.executeUpdate();
0924:                } catch (SQLException sqle) {
0925:                    sqle.printStackTrace();
0926:                } finally {
0927:                    try {
0928:                        pstmt.close();
0929:                    } catch (Exception e) {
0930:                        e.printStackTrace();
0931:                    }
0932:                    try {
0933:                        con.close();
0934:                    } catch (Exception e) {
0935:                        e.printStackTrace();
0936:                    }
0937:                }
0938:
0939:            }
0940:
0941:            public boolean isBlackListed(ClientIP cip) {
0942:                boolean bl = false;
0943:
0944:                Connection con = null;
0945:                PreparedStatement pstmt = null;
0946:                try {
0947:                    con = DbConnectionManager.getConnection();
0948:                    pstmt = con.prepareStatement(SELECT_BLACK_LIST);
0949:                    pstmt.setString(1, cip.getRemoteIP());
0950:                    ResultSet rs = pstmt.executeQuery();
0951:                    if (rs.next()) {
0952:                        bl = true;
0953:                    }
0954:                } catch (SQLException sqle) {
0955:                    sqle.printStackTrace();
0956:                } finally {
0957:                    try {
0958:                        pstmt.close();
0959:                    } catch (Exception e) {
0960:                        e.printStackTrace();
0961:                    }
0962:                    try {
0963:                        con.close();
0964:                    } catch (Exception e) {
0965:                        e.printStackTrace();
0966:                    }
0967:                }
0968:
0969:                return bl;
0970:
0971:            }
0972:
0973:            public ForumThread getArticleThread(String pageKey, Forum forum)
0974:                    throws ForumThreadNotFoundException, UnauthorizedException {
0975:                Connection con = null;
0976:                PreparedStatement pstmt = null;
0977:                ForumThread thread = null;
0978:                try {
0979:                    con = DbConnectionManager.getConnection();
0980:                    pstmt = con.prepareStatement(SELECT_ARTICLE_MAP);
0981:                    pstmt.setString(1, pageKey);
0982:                    pstmt.setInt(2, forum.getID());
0983:                    ResultSet rs = pstmt.executeQuery();
0984:                    if (rs.next()) {
0985:                        thread = this .getThread(rs.getInt("threadID"),
0986:                                (DbForum) this .getForum(forum.getID()));
0987:                    } else {
0988:                        throw new ForumThreadNotFoundException();
0989:                    }
0990:                } catch (SQLException sqle) {
0991:                    sqle.printStackTrace();
0992:                } catch (ForumNotFoundException e) {
0993:
0994:                } finally {
0995:                    try {
0996:                        pstmt.close();
0997:                    } catch (Exception e) {
0998:                        e.printStackTrace();
0999:                    }
1000:                    try {
1001:                        con.close();
1002:                    } catch (Exception e) {
1003:                        e.printStackTrace();
1004:                    }
1005:                }
1006:                return thread;
1007:
1008:            }
1009:
1010:            public ThreadType getThreadType(int typeid) {
1011:                //If cache is not enabled, do a new lookup of object
1012:                if (!cacheManager.isCacheEnabled()) {
1013:                    return new DbThreadType(typeid);
1014:                }
1015:                //Cache is enabled.
1016:                Integer typeIDInteger = new Integer(typeid);
1017:                DbThreadType type = (DbThreadType) cacheManager.get(
1018:                        DbCacheManager.THREAD_TYPE_ID_CACHE, typeIDInteger);
1019:                if (type == null) {
1020:                    //Load the message
1021:                    type = new DbThreadType(typeid);
1022:                    //Add it to cache.
1023:                    cacheManager.add(DbCacheManager.THREAD_TYPE_ID_CACHE,
1024:                            typeIDInteger, type);
1025:                }
1026:                return type;
1027:
1028:            }
1029:
1030:            public Iterator getThreadTypeIterator() {
1031:                return new DbThreadTypeIterator(this );
1032:            }
1033:
1034:            public Iterator getSessionList() {
1035:                Connection con = null;
1036:                PreparedStatement pstmt = null;
1037:                ArrayList sessions = new ArrayList();
1038:                try {
1039:                    con = DbConnectionManager.getConnection();
1040:                    pstmt = con.prepareStatement(SESSION_LIST);
1041:                    ResultSet rs = pstmt.executeQuery();
1042:                    while (rs.next()) {
1043:                        sessions.add(new SessionVO(rs.getString("sessionID"),
1044:                                rs.getString("IP"), rs.getInt("userID")));
1045:                    }
1046:                } catch (SQLException sqle) {
1047:                    sqle.printStackTrace();
1048:                } finally {
1049:                    try {
1050:                        pstmt.close();
1051:                    } catch (Exception e) {
1052:                        e.printStackTrace();
1053:                    }
1054:                    try {
1055:                        con.close();
1056:                    } catch (Exception e) {
1057:                        e.printStackTrace();
1058:                    }
1059:                }
1060:                return sessions.iterator();
1061:
1062:            }
1063:
1064:            public int getYesterdayUserCount() {
1065:                Connection con = null;
1066:                PreparedStatement pstmt = null;
1067:                int usercount = 0;
1068:                Calendar now = Calendar.getInstance();
1069:                int now_in_minutes = (int) (now.getTimeInMillis() * 1.0 / (1000.0 * 60.0));
1070:                int now_today = (int) (now_in_minutes * 1.0 / (60.0 * 24.0));
1071:                try {
1072:                    con = DbConnectionManager.getConnection();
1073:                    pstmt = con.prepareStatement(YESTERDAY_SESSION_COUNT);
1074:                    pstmt.setInt(1, now_today - 1);
1075:                    ResultSet rs = pstmt.executeQuery();
1076:                    if (rs.next()) {
1077:                        usercount = rs.getInt("usercount");
1078:                    }
1079:                } catch (SQLException sqle) {
1080:                    sqle.printStackTrace();
1081:                } finally {
1082:                    try {
1083:                        pstmt.close();
1084:                    } catch (Exception e) {
1085:                        e.printStackTrace();
1086:                    }
1087:                    try {
1088:                        con.close();
1089:                    } catch (Exception e) {
1090:                        e.printStackTrace();
1091:                    }
1092:                }
1093:                return usercount;
1094:
1095:            }
1096:
1097:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.