dbgroup.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 824 行 · 第 1/2 页

JAVA
824
字号
            catch( SQLException sqle ) {                sqle.printStackTrace();            }            finally {                try {  pstmt.close();   }                catch (Exception e) { e.printStackTrace(); }                try {  con.close();   }                catch (Exception e) { e.printStackTrace(); }            }            // Add to cache            factory.cacheManager.groupMemberCache.put(id + ",admin," + userID, bool);        }        return bool.booleanValue();    }    public boolean isMember(User user) {        long userID = user.getID();        Boolean bool = null;        bool = (Boolean)factory.cacheManager.groupMemberCache.get(                id + ",member," + userID);        if (bool == null) {            bool = new Boolean(false);            Connection con = null;            PreparedStatement pstmt = null;            try {                con = ConnectionManager.getConnection();                pstmt = con.prepareStatement(MEMBER_TEST);                pstmt.setLong(1, id);                pstmt.setLong(2, userID);                ResultSet rs = pstmt.executeQuery();                // If there is a result, then the user is a member of the group.                if (rs.next()) {                    bool = new Boolean(true);                }            }            catch( SQLException sqle ) {                sqle.printStackTrace();            }            finally {                try {  pstmt.close();   }                catch (Exception e) { e.printStackTrace(); }                try {  con.close();   }                catch (Exception e) { e.printStackTrace(); }            }            // Add to cache            factory.cacheManager.groupMemberCache.put(id + ",member," + userID, bool);        }        return bool.booleanValue();    }    public int getAdministratorCount() {        // If the admin list is null, load it.        if (adminList == null) {            administrators();        }        return adminList.length;    }    public int getMemberCount() {        // If the member list is null, load it.        if (memberList == null) {            members();        }        return memberList.length;    }    public Iterator members() {        if (memberList == null) {            Connection con = null;            PreparedStatement pstmt = null;            LongList members = new LongList();            try {                con = ConnectionManager.getConnection();                pstmt = con.prepareStatement(LOAD_MEMBERS);                pstmt.setLong(1, id);                ResultSet rs = pstmt.executeQuery();                while (rs.next()) {                    members.add(rs.getLong(1));                }                memberList = members.toArray();                // Re-add group to cache.                factory.cacheManager.groupCache.put(new Long(id), this);            }            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(JiveGlobals.USER, memberList,                factory.getUserManager());    }    public Iterator administrators() {        if (adminList == null) {            Connection con = null;            PreparedStatement pstmt = null;            LongList admins = new LongList();            try {                con = ConnectionManager.getConnection();                pstmt = con.prepareStatement(LOAD_ADMINS);                pstmt.setLong(1, id);                ResultSet rs = pstmt.executeQuery();                while (rs.next()) {                    admins.add(rs.getLong(1));                }                adminList = admins.toArray();                // Re-add group to cache.                factory.cacheManager.groupCache.put(new Long(id), this);            }            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(JiveGlobals.USER, adminList,                factory.getUserManager());    }    public ForumPermissions getPermissions(Authorization authorization) {        long userID = authorization.getUserID();        try {            User user = factory.getUserManager().getUser(userID);            if (isAdministrator(user)) {                return new ForumPermissions(false, false, false, false,                        true, false, false, false, false, false, false, false);            }        }        catch (Exception e) { }        return ForumPermissions.none();    }    public boolean hasPermission(int type) {        return true;    }    //FROM THE CACHEABLE INTERFACE//    public int getCachedSize() {        // Approximate the size of the object in bytes by calculating the size        // of each field.        int size = 0;        size += CacheSizes.sizeOfObject();              // overhead of object        size += CacheSizes.sizeOfLong();                // id        size += CacheSizes.sizeOfString(name);          // name        size += CacheSizes.sizeOfString(description);   // description        size += CacheSizes.sizeOfMap(properties);       // properties        size += CacheSizes.sizeOfObject();              // forum factory ref.        size += CacheSizes.sizeOfObject();              // member list        size += CacheSizes.sizeOfObject();              // admin list        return size;    }    //OTHER METHODS    /**     * Returns a String representation of the Group object using the group name.     *     * @return a String representation of the Group object.     */    public String toString() {        return name;    }    public int hashCode() {        return (int)id;    }    public boolean equals(Object object) {        if (this == object) {            return true;        }        if (object != null && object instanceof DbGroup) {            return id == ((DbGroup)object).getID();        }        else {            return false;        }    }    /**     * Updates the modified date. It accepts a Connection so that it can     * participate in trasactions.     */    protected void updateModifiedDate(long date, Connection con)            throws SQLException    {        this.modifiedDate.setTime(date);        PreparedStatement pstmt = null;        try {            pstmt = con.prepareStatement(UPDATE_MODIFIED_DATE);            pstmt.setString(1, StringUtils.dateToMillis(modifiedDate));            pstmt.setLong(2, id);            pstmt.executeUpdate();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    /**     * Load the group data from the database.     */    private synchronized void loadFromDb() throws GroupNotFoundException {        String query;        if (name == null) {            query = LOAD_GROUP_BY_ID;        }        else {            query = LOAD_GROUP_BY_NAME;        }        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(query);            if (name == null) {                pstmt.setLong(1, id);            }            else {                pstmt.setString(1, name);            }            ResultSet rs = pstmt.executeQuery();            if (!rs.next()) {                if (name == null) {                    throw new GroupNotFoundException("Group with ID " + id + " not found.");                }                else {                    throw new GroupNotFoundException("Group " + name + " not found.");                }            }            this.id = rs.getLong("groupID");            this.name = rs.getString("name");            this.description = rs.getString("description");            // We trim() the dates before trying to parse them because some            // databases pad with extra characters when returning the data.            this.creationDate =                new java.util.Date(Long.parseLong(rs.getString("creationDate").trim()));            this.modifiedDate =                new java.util.Date(Long.parseLong(rs.getString("modifiedDate").trim()));            pstmt.close();            // Load any extended message properties.            properties = new Hashtable();            pstmt = con.prepareStatement(LOAD_PROPERTIES);            pstmt.setLong(1, id);            rs = pstmt.executeQuery();            while(rs.next()) {                // Add in name, value as a new property.                properties.put(rs.getString(1), rs.getString(2));            }        }        catch( SQLException sqle ) {            sqle.printStackTrace();            throw new GroupNotFoundException();        }        catch (NumberFormatException nfe) {            System.err.println("WARNING: There was an error parsing the dates " +                "returned from the database. Ensure that they're being stored " +                "correctly.");            throw new GroupNotFoundException( "Group with id "                    + id + " could not be loaded from the database."            );        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    /**     * Inserts a new record into the database.     */    private void insertIntoDb() {        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(INSERT_GROUP);            pstmt.setString(1, name);            pstmt.setString(2, description);            pstmt.setLong(3, id);            pstmt.setString(4, StringUtils.dateToMillis(creationDate));            pstmt.setString(5, StringUtils.dateToMillis(modifiedDate));            pstmt.executeUpdate();        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    /**     * Saves group data to the db.     */    private synchronized void saveToDb() {        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(SAVE_GROUP);            pstmt.setString(1, name);            pstmt.setString(2, description);            pstmt.setString(3, StringUtils.dateToMillis(creationDate));            pstmt.setString(4, StringUtils.dateToMillis(modifiedDate));            pstmt.setLong(5, id);            pstmt.executeUpdate();        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close(); }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    /**     * Saves properties to the database.     */    private void insertPropertyIntoDb(String name, String value) {        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(INSERT_PROPERTY);            pstmt.setLong(1, id);            pstmt.setString(2, name);            pstmt.setString(3, value);            pstmt.executeUpdate();        }        catch( SQLException sqle ) {           sqle.printStackTrace();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    /**     * Updates a property value in the database.     */    private void updatePropertyInDb(String name, String value) {        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(UPDATE_PROPERTY);            pstmt.setString(1, value);            pstmt.setString(2, name);            pstmt.setLong(3, id);            pstmt.executeUpdate();        }        catch( SQLException sqle ) {           sqle.printStackTrace();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    /**     * Deletes a property from the db.     */    private synchronized void deletePropertyFromDb(String name) {        Connection con = null;        PreparedStatement pstmt = null;        try {           con = ConnectionManager.getConnection();           pstmt = con.prepareStatement(DELETE_PROPERTY);           pstmt.setLong(1, id);           pstmt.setString(2, name);           pstmt.execute();        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }}

⌨️ 快捷键说明

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