⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbforumfactory.java

📁 这是学习Java必须读懂两套源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            pstmt.setInt(1,-1);
            pstmt.setInt(2,permissionType);
            ResultSet rs = pstmt.executeQuery();
            ArrayList groupList = new ArrayList();
            while (rs.next()) {
                groupList.add(new Integer(rs.getInt("groupID")));
            }
            groups = new int[groupList.size()];
            for (int i=0; i<groups.length; i++) {
                groups[i] = ((Integer)groupList.get(i)).intValue();
            }
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbForum.groupsWithPermission:" + sqle);
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return groups;
    }

    public ForumPermissions getPermissions(Authorization authorization) {
        int userID = authorization.getUserID();

        //Get the user perm cache for this forum
        Cache userPermCache = (Cache)getCacheManager().get(
            DbCacheManager.USER_PERMS_CACHE,
            new Integer(-1)
        );

        //Simple case: if cache is turned on and the user is already cached,
        //we can simply return the cached permissions.
        if (userPermCache != null) {
            ForumPermissions permissions =
                    (ForumPermissions)userPermCache.get(new Integer(userID));
            if (permissions != null) {
                return permissions;
            }
        }

        //Not so simple case: cache is not turned on or the user permissions
        //have not been cached yet.
        boolean isAnonymous = (userID == -1);
        boolean isUser = !isAnonymous;

        ForumPermissions finalPermissions = ForumPermissions.none();
	// check each forum for a specific permission
	Iterator allForums = this.forums();
	Forum forum;
	ForumPermissions forumUserPermissions;
	while (allForums.hasNext()){
		forum = (Forum)allForums.next();
		forumUserPermissions = getUserPermissions(userID,forum.getID());			
            	finalPermissions = new ForumPermissions(finalPermissions, forumUserPermissions);
	}
	

        //Step 1 - Get permissions for the User. This includes anonymous
        //perms, "special user" perms, and the specific perms for the user.
        if (isUser) {
            ForumPermissions userPermissions = getUserPermissions(userID, -1);
            //Combine permissions
            finalPermissions = new ForumPermissions(finalPermissions, userPermissions);
        }
        //Add in anonymous perms.
        ForumPermissions anonyPermissions = null;
        if (userPermCache != null) {
            anonyPermissions = (ForumPermissions)userPermCache.get(new Integer(-1));
        }
        //Otherwise, do our own lookup.
        if (anonyPermissions == null) {
            anonyPermissions = getUserPermissions(-1, -1);
            //Add to cache so it will be there next time.
            if (userPermCache != null) {
                userPermCache.add(new Integer(-1), anonyPermissions);
            }
        }
        //Combine permissions
        finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions);

        //If they are a valid user, figure out "any user" permissions.
        if (isUser) {
            ForumPermissions specialUserPermissions = null;
            //Check for cache
            if (userPermCache != null) {
                specialUserPermissions = (ForumPermissions)userPermCache.get(new Integer(0));
            }
            //Otherwise, do our own lookup.
            if (specialUserPermissions == null) {
                specialUserPermissions = getUserPermissions(0, -1);
                //Add to cache so it will be there next time.
                if (userPermCache != null) {
                    userPermCache.add(new Integer(0), specialUserPermissions);
                }
            }
            //Combine permissions
            finalPermissions = new ForumPermissions(finalPermissions, specialUserPermissions);
        }

        //Step 2 -- get Permissions for all groups the user is in.
        int [] groups = ((DbProfileManager)getProfileManager()).getUserGroups(userID);
        for (int i=0; i<groups.length; i++) {
            ForumPermissions groupPermissions = getGroupPermissions(groups[i], -1);
            finalPermissions = new ForumPermissions(finalPermissions, groupPermissions);
        }

        //Finally, add user to cache so it will be there next time.
        if (isUser && userPermCache != null) {
            userPermCache.add(new Integer(userID), finalPermissions);
        }

        return finalPermissions;
    }

    public boolean hasPermission(int type) {
        return true;
    }

    //OTHER METHODS//

    /**
     * Returns the cache manager object.
     */
    public DbCacheManager getCacheManager() {
        return cacheManager;
    }

    /**
     * Cleans the Jive database of "junk". This is currently defined as: <ul>
     *      <li> Messages with no subject or body.
     *      <li> Messages that do not belong to a thread.</ul>
     *
     * Please be aware that this method will <b>permanently</b> delete forum
     * content. You may want to perform a database backup before calling this
     * method.<p>
     *
     * This method requires two database connections and may take a long time
     * to execute, as it must iterate through ever message record in the
     * database.
     */
    public void cleanDatabase() {
        //Iterate through all forums, threads to delete unwanted messages.
        Iterator forums = forums();
        while (forums.hasNext()) {
            Forum forum = (Forum)forums.next();
            Iterator threads = forum.threads();
            while (threads.hasNext()) {
                try {
                    ForumThread thread = (ForumThread)threads.next();
                    Iterator messages = thread.messages();
                    while (messages.hasNext()) {
                        try {
                            ForumMessage message = (ForumMessage)messages.next();
                            //System.err.println("Evaluating message " + message.getID() + " for deletion");
                            if (/*message.getSubject() == null ||*/ message.getBody() == null) {
                                //System.err.println("Deleting message...");
                                thread.deleteMessage(message);
                            }
                        }
                        catch (Exception me) {
                            me.printStackTrace();
                        }
                    }
                }
                catch (Exception te) {
                    te.printStackTrace();
                }
            }
        }

/*
        //Select all message ID's directly from the message table.
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(ALL_MESSAGES);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()) {
                try {
                    int messageID = rs.getInt(1);
                    //Convert to object
                    ForumMessage message = new DbForumMessage(messageID, this);
                    ForumThread thread = message.getForumThread();
                    if (thread == null) {
                        //manually delete this message from the database. It won't
                        //appear in any search indexes or in any threads, so this
                        //shouldn't have any side effects.
                        Connection con2 = null;
                        PreparedStatement pstmt2 = null;
                        try {
                            con2 = DbConnectionManager.getConnection();
                            pstmt2 = con.prepareStatement(DELETE_MESSAGE);
                            pstmt2.setInt(1, messageID);
                            pstmt2.execute();
                        }
                        catch( SQLException sqle ) {
                            sqle.printStackTrace();
                        }
                        finally {
                            try {  pstmt2.close(); }
                            catch (Exception e) { e.printStackTrace(); }
                            try {  con2.close();   }
                            catch (Exception e) { e.printStackTrace(); }
                        }
                    }
                }
                catch (ForumMessageNotFoundException fmnfe) {
                    fmnfe.printStackTrace();
                }
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
*/
    }

    /**
     * Returns a thread specified by its id. Will return null
     * if the thread is not in the forum. If cache is turned
     * on, it will use it.
     */
    public DbForumThread getThread(int threadID, DbForum forum) throws
            ForumThreadNotFoundException
    {
        //If cache is not enabled, do a new lookup of object
        if (!cacheManager.isCacheEnabled()) {
            return new DbForumThread(threadID, forum, this);
        }
        //Cache is enabled.
        Integer threadIDInteger = new Integer(threadID);
        DbForumThread thread = (DbForumThread)cacheManager.get(
                DbCacheManager.THREAD_CACHE,
                threadIDInteger
        );
        if(thread == null) {
            thread = new DbForumThread(threadID, forum, this);
            cacheManager.add(DbCacheManager.THREAD_CACHE, threadIDInteger, thread);
        }
        return thread;
    }

    /**
     * Returns a message from the thread based on its id. If cache is turned
     * on, it will use it.
     *
     * @param messageID the ID of the message to get from the thread.
     */
    protected DbForumMessage getMessage(int messageID)
            throws ForumMessageNotFoundException
    {
        //If cache is not enabled, do a new lookup of object
        if (!cacheManager.isCacheEnabled()) {
            return new DbForumMessage(messageID, this);
        }
        //Cache is enabled.
        Integer messageIDInteger = new Integer(messageID);
        DbForumMessage message = (DbForumMessage)cacheManager.get(
                DbCacheManager.MESSAGE_CACHE,
                messageIDInteger
        );
        if(message == null) {
            //Load the message
            message = new DbForumMessage(messageID, this);
            //Add it to cache.
            cacheManager.add(DbCacheManager.MESSAGE_CACHE, messageIDInteger, message);
        }
        return message;
    }

    /**
     * Logs events in the system. Very beginnings here....
     */
    protected void log(String message, Exception e) {
        System.err.println("Log event : " + message);
        e.printStackTrace();
    }

    /**
     * Returns the permissions that a particular user has for the forum.
     */
    protected ForumPermissions getUserPermissions(int userID, int forumID) {
        Connection con = null;
        PreparedStatement pstmt = null;
        //Initialize a permissions array with no permissions.
        boolean [] permissions = new boolean[8];
        for (int i=0; i<permissions.length; i++) {
            permissions[i] = false;
        }
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(GET_USER_PERMS);
            pstmt.setInt(1, forumID);
            pstmt.setInt(2, userID);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()) {
                int newPerm = rs.getInt("permission");
                permissions[newPerm] = true;
            }
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbForum.java:" + sqle);
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return new ForumPermissions(permissions);
    }

    /**
     * Returns the permissions that a particular group has for the forum.
     */
    protected ForumPermissions getGroupPermissions(int groupID, int forumID) {
        Connection con = null;
        PreparedStatement pstmt = null;
        //Initialize a permissions array with no permissions.
        boolean [] permissions = new boolean[8];
        for (int i=0; i<permissions.length; i++) {
            permissions[i] = false;
        }
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(GET_GROUP_PERMS);
            pstmt.setInt(1, forumID);
            pstmt.setInt(2, groupID);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()) {
                int newPerm = rs.getInt("permission");
                permissions[newPerm] = true;
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return new ForumPermissions(permissions);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -