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

📄 dbgroup.java

📁 一套完整的工商12315的源程序jsp部分在12315里,后台JAVA部分在gs12315src里,没有打包数据库.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * Database implementation of the Group interface.
 *
 * @see Group
 */

package com.gs.db.dbimp;

import com.gs.db.*;
import com.gs.util.Cacheable;
import com.gs.util.CacheSizes;

import java.util.Iterator;
import java.util.ArrayList;
import java.sql.*;

public class DbGroup implements Group, Cacheable  {

     /** DATABASE QUERIES **/
    private static final String ADD_ADMIN =
        "INSERT INTO gsGroupUser(groupid,userid,administrator) VALUES(?,?,1)";
    private static final String REMOVE_ADMIN =
        "DELETE FROM gsGroupUser WHERE groupid=? AND userid=? AND administrator=1";
    private static final String ADD_USER =
        "INSERT INTO gsGroupUser(groupid,userid,administrator) VALUES(?,?,0)";
    private static final String REMOVE_USER =
        "DELETE FROM gsGroupUser WHERE groupid=? AND userid=?";
    private static final String ADMIN_TEST =
        "SELECT userid FROM gsGroupUser WHERE groupid=? AND userid=? AND " +
        "administrator=1";
    private static final String MEMBER_TEST =
        "SELECT userid FROM gsGroupUser WHERE groupid=? AND userid=?";
    private static final String ADMIN_COUNT =
        "SELECT count(*) FROM gsGroupUser WHERE groupid=? " +
        "AND administrator=1";
    private static final String MEMBER_COUNT =
        "SELECT DISTINCT count(userid) FROM gsGroupUser " +
        "WHERE groupid=?";
    private static final String LOAD_ADMINS =
        "SELECT userid FROM gsGroupUser WHERE administrator=1 AND groupid=?";
    private static final String LOAD_USERS =
        "SELECT userid FROM gsGroupUser WHERE groupid=?";
    private static final String LOAD_GROUP_BY_ID =
        "SELECT * FROM gsGroup WHERE groupid=?";
    private static final String LOAD_GROUP_BY_NAME =
        "SELECT * FROM gsGroup WHERE myname=?";

    private static final String INSERT_GROUP =          //new
        "INSERT INTO gsGroup(myname,description,groupid,djjg, priority) VALUES(?,?,?,?,?)";
    private static final String SAVE_GROUP =
        "UPDATE gsGroup SET myname=?, description=?, djjg=?, priority=? WHERE groupid=?";


    private int id;
    private String name = null;
    private String description = "";
    private String djjg = "-1";        //default value: -1 means not in any Unit
    private int priority = 0;

    private ProfileManager profileManager;

    private DbIofficeFactory factory;

    /**
     * Creates a new group.
     *
     * @param the name of the group.
     * @param profileManager a ProfileManager that can be used to perform user
     *    and group operations.
     */
    protected DbGroup(String name, DbIofficeFactory factory) {
        this.name = name;
        this.factory = factory;
        this.profileManager = factory.getProfileManager();
        this.id = Integer.parseInt(SequenceAction.getId("group"));
        insertIntoDb();
    }
    /**
     * Creates a new group associated with a unit
     */
    protected DbGroup(String name, DbIofficeFactory factory, Unit unit, int priority) {
        this.name = name;
        this.factory = factory;
        this.profileManager = factory.getProfileManager();
        this.id = Integer.parseInt(SequenceAction.getId("group"));
        this.djjg = unit.getID()+"";
        this.priority = priority;
        insertIntoDb();
    }

    /**
     * Loads a group from the database based on its id.
     *
     * @param id the id of the group to load.
     * @param profileManager a ProfileManager that can be used to perform user
     *    and group operations.
     */
    protected DbGroup(int id, DbIofficeFactory factory)
            throws GroupNotFoundException
    {
        this.id = id;
        this.factory = factory;
        this.profileManager = factory.getProfileManager();
        loadFromDb();
    }

    /**
     * Loads a group from the database based on its name. The implementation
     * of this method is rather hackish since it includes a fake parameter just
     * so that it can have a different method signature than the first
     * constructor. Even so, this methodology makes this class behave more like
     * our other classes, so we're gleefully leaving it this way. :)
     *
     * @param name the name of the group to load.
     * @param fake a fake paramater that can always be null.
     * @param profileManager a ProfileManager that can be used to perform user
     *    and group operations.
     */
    protected DbGroup(String name, Object fake, DbIofficeFactory factory)
            throws GroupNotFoundException
    {
        this.name = name;
        this.factory = factory;
        this.profileManager = factory.getProfileManager();
        loadFromDb();
    }

     /**
     * Returns the id of the group.
     *
     * @return the id of the group.
     */
    public int getID() {
        return id;
    }

     /**
     * Returns the name of the group. For example, 'XYZ Admins'.
     *
     * @return the name of the group.
     */
    public String getName() {
        return name;
    }
    /**
     * Sets the name of the group. For example, 'XYZ Admins'.<p>
     *
     * This method is restricted to those with group administration permission.
     *
     * @param name the name for the group.
     * @throws UnauthorizedException if does not have group admin permissions.
     */
     public void setName(String name) throws UnauthorizedException {
        this.name = name;
        saveToDb();
    }

    /**
     * Returns the description of the group. The description often summarizes
     * a group's function, such as 'Administrators of the XYZ forum'.
     *
     * @return the description of the group.
     */
    public String getDescription() {
        return description;
    }

    /**
     * Sets the description of the group.
     *
     * The description often summarizes a group's function, such as
     * 'Administrators '.<p>
     *
     * This method is restricted to those with group administration permission.
     *
     * @param name the description of the group.
     * @throws UnauthorizedException if does not have group admin permissions.
     */
    public void setDescription(String description)
            throws UnauthorizedException
    {
        this.description = description;
        saveToDb();
    }

    /**
     * Adds a member to the group.<p>
     *
     * This method is restricted to those with group administration permission.
     *
     * @param user the User to add to the group.
     * @throws UnauthorizedException if does not have group admin permissions.
     */
    public void setPriority(int priority) throws UnauthorizedException
    {
    	this.priority = priority;
    	saveToDb();
    }

    /**
     * Removes a member from the group. If the User is not in the group, this
     * method does nothing.<p>
     *
     * This method is restricted to those with group administration permission.
     *
     * @param user the User to remove from the group.
     * @throws UnauthorizedException if does not have group admin permissions.
     */
    public int getPriority()
    {
    	return getPriorityInUnit();
    }

    /**
     * Break the association with with a unit
     *
     *@thows UnauthorizedException if it doesnot have system-admin permission
     * @see Unit
     */
    public void unlinkUnit() throws UnauthorizedException
    {
    	this.djjg = "-1";
    	saveToDb();
    }

    /**
     * Establish the association with a unit
     *
     *@thows UnauthorizedException if it doesnot have system-admin permission
     * @see Unit
     */
    public void linkToUnit(Unit unit) throws UnauthorizedException
    {
    	this.djjg = unit.getID()+"";
    	saveToDb();
    }

    /**
     * Adds a member to the group.<p>
     *
     * This method is restricted to those with group administration permission.
     *
     * @param user the User to add to the group.
     * @throws UnauthorizedException if does not have group admin permissions.
     */
    public void addMember(User user) throws UnauthorizedException {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(ADD_USER);
            pstmt.setInt(1, id);
            pstmt.setInt(2, user.getID());
            pstmt.execute();
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }

        //Now, remove the user from the USER_PERM_CACHE since being in the
        //group could affect their permissions.
        DbCacheManager cacheManager = factory.getCacheManager();
        cacheManager.removeUserPerm(new Integer(user.getID()));
    }

    /**
     * Removes a member from the group. If the User is not in the group, this
     * method does nothing.<p>
     *
     * This method is restricted to those with group administration permission.
     *
     * @param user the User to remove from the group.
     * @throws UnauthorizedException if does not have group admin permissions.
     */
    public void removeMember(User user) throws UnauthorizedException {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(REMOVE_USER);
            pstmt.setInt(1, id);
            pstmt.setInt(2, user.getID());
            pstmt.execute();
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }

        //Now, remove the user from the USER_PERM_CACHE since being in the
        //group could affect their permissions.
        DbCacheManager cacheManager = factory.getCacheManager();
        cacheManager.removeUserPerm(new Integer(user.getID()));
    }


    /**
     * Returns true if if the User is a member of the group.
     *
     * @return true if the User is a member of the group.
     */
    public boolean isMember(User user) {
        boolean answer = false;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(MEMBER_TEST);
            pstmt.setInt(1, id);
            pstmt.setInt(2, user.getID());
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                answer = true;
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return answer;
    }



    /**
     * Returns the number of group members.
     *
     * @return the number of group members.
     */
    public int getMemberCount() {
        int count = 0;
        boolean answer = false;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(MEMBER_COUNT);
            pstmt.setInt(1, id);
            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;
    }



    /**
     * An iterator for all the users that are members of the group.
     *
     * @return an Iterator for all members of the group.
     */
    public Iterator members() {
        ArrayList admins = new ArrayList();
        //Load list of group admins from db.
        Connection con = null;
        PreparedStatement pstmt = null;

        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_USERS);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            User user = null;
            while (rs.next()) {
                try {
                    user = profileManager.getUser(rs.getInt("userid"));
                }
                catch (UserNotFoundException unfe) {
                    unfe.printStackTrace(System.out);
                }
                if(!user.isDisabled())
                {

⌨️ 快捷键说明

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