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

📄 dbgroupmanager.java

📁 一套会员管理系统组件
💻 JAVA
字号:
/**
 * $RCSfile: DbGroupManager.java,v $
 * $Revision: 1.1.1.1 $
 * $Date: 2002/09/09 13:50:56 $
 *
 * New Jive  from Jdon.com. Modified by Frank Chu
 *
 * This software is the proprietary information of CoolServlets, Inc.
 * Use is subject to license terms.
 */

package com.airinbox.member.forum.database;

import java.util.Iterator;
import java.util.ArrayList;
import java.sql.*;
import com.airinbox.member.forum.*;
import com.airinbox.member.util.*;
import com.airinbox.component.authorize.UnauthorizedException;

/**
 * Database implementation of the GroupManager interface.
 */
public class DbGroupManager implements GroupManager {

    /** DATABASE QUERIES **/
    private static final String USER_GROUPS =
        "SELECT groupID from memberGroupUser WHERE userID=?";
    private static final String USER_MESSAGE_COUNT =
        "SELECT count(1) FROM memberMessage,memberForum,memberThread WHERE " +
        "memberMessage.userID=? AND memberForum.forumID=? AND " +
        "memberThread.forumID=memberForum.forumID AND " +
        "memberMessage.threadID=memberThread.threadID";
    private static final String ALL_USER_MESSAGES =
        "SELECT messageID FROM memberMessage WHERE userID=?";
    private static final String DELETE_USER_MESSAGES =
        "UPDATE memberMessage set userID=-1 WHERE userID=?";
    private static final String DELETE_USER_PERMS =
        "DELETE FROM memberUserPerm WHERE userID=?";
    private static final String DELETE_USER_GROUPS =
        "DELETE FROM memberGroupUser WHERE userID=?";
    private static final String DELETE_USER_PROPS =
        "DELETE FROM memberUserProp WHERE userID=?";
    private static final String GROUP_COUNT = "SELECT count(1) FROM memberGroup";
    private static final String DELETE_GROUP_USERS =
        "DELETE FROM memberGroupUser WHERE groupID=?";
    private static final String DELETE_GROUP =
        "DELETE FROM memberGroup WHERE groupID=?";
    private static final String ALL_GROUPS = "SELECT groupID FROM memberGroup";

    private DbForumFactory factory;

    /**
     * Creates a new GroupManager.
     */
    public DbGroupManager(DbForumFactory factory) {
        this.factory = factory;
    }

    //FROM THE GROUPMANAGER INTERFACE//

    public Group createGroup(String name) throws UnauthorizedException,
            GroupAlreadyExistsException
    {
        Group newGroup = null;
        try {
            Group existingGroup = getGroup(name);

            //The group already exists since now exception, so:
            throw new GroupAlreadyExistsException();
        }
        catch (GroupNotFoundException unfe) {
            //The group doesn't already exist so we can create a new group
            newGroup = new DbGroup(name, factory);
        }
        return newGroup;
    }

    public Group getGroup(long groupID) throws GroupNotFoundException {
        return factory.cacheManager.groupCache.get(groupID);
    }

    public Group getGroup(String name) throws GroupNotFoundException {
        return factory.cacheManager.groupCache.get(name);
    }

    public void deleteGroup(Group group) throws UnauthorizedException {
        long groupID = group.getID();
        long [] users = new long[group.getUserCount()];
        Iterator iter = group.users();
        for (int i=0; i<users.length; i++) {
            User user = (User)iter.next();
            users[i] = user.getId();
        }

        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();
            //mark all message by user as anonymous
            pstmt = con.prepareStatement(DELETE_GROUP_USERS);
            pstmt.setLong(1,groupID);
            pstmt.execute();
            pstmt.close();
            //remove all permissions given to user
            pstmt = con.prepareStatement(DELETE_GROUP);
            pstmt.setLong(1,groupID);
            pstmt.execute();
            pstmt.close();
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }

        //Finally, expire all relevant caches
        DatabaseCacheManager cacheManager = factory.getCacheManager();
        cacheManager.groupCache.remove(groupID);
        //Removing a group can change the permissions of all the users in that
        //group. Therefore, remove each user from the user perms cache.
        for (int i=0; i<users.length; i++) {
            cacheManager.userPermsCache.removeUserPerm(users[i]);
        }
    }

    public int getGroupCount() {
        int count = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(GROUP_COUNT);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return count;
    }

    public Iterator groups() {
        LongList groups = new LongList();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(ALL_GROUPS);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                groups.add(rs.getLong(1));
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return new DatabaseObjectIterator(Globals.GROUP, groups.toArray(),
                this);
    }

    public Iterator groups(int startIndex, int numResults) {
        LongList groups = new LongList();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(ALL_GROUPS);
            ResultSet rs = pstmt.executeQuery();
            //Move to start of index
            for (int i=0; i<startIndex; i++) {
                rs.next();
            }
            //Now read in desired number of results
            for (int i=0; i<numResults; i++) {
                if (rs.next()) {
                    groups.add(rs.getLong(1));
                }
                else {
                    break;
                }
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return new DatabaseObjectIterator(Globals.GROUP, groups.toArray(),
                this);
    }

    /**
     * Returns an array of all the groups that the user belongs to.
     */
    protected long[] getUserGroups(long userID) {
        Connection con = null;
        PreparedStatement pstmt = null;
        long [] groups = new long[0];
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(USER_GROUPS);
            pstmt.setLong(1,userID);
            ResultSet rs = pstmt.executeQuery();
            ArrayList groupList = new ArrayList();
            while (rs.next()) {
                groupList.add(new Long(rs.getLong("groupID")));
            }
            groups = new long[groupList.size()];
            for (int i=0; i<groups.length; i++) {
                groups[i] = ((Long)groupList.get(i)).longValue();
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return groups;
    }
}

⌨️ 快捷键说明

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