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

📄 acldbutils.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                if (result)                {                    cachedType.remove (targetID);                }             }        }        return result;    }    //-------------------------------------------------------------------------    // FH   4 Apr. 2001    /**     * Remove all the ACL entries associated with the specified ACL ID and     * type.     *     * @param   aclID     *      ACL's indentification number.     * @param   typeID     *      Entry type. See constants defined int <code>ACLInfo</code>.     *     * @return     *      Return <code>true</code> on success.     *     * @exception   JahiaDatabaseException     *      Throws this error on any database failure.     */    public boolean removeACLEntries (int aclID, int typeID)        throws  JahiaDatabaseException    {        StringBuffer query = new StringBuffer ("DELETE FROM ");        query.append (JAHIA_ACL_ENTRIES);        query.append (" WHERE ");        query.append (FIELD_ENTRY_ACL_ID);        query.append ("=");        query.append (aclID);        query.append ("   AND ");        query.append (FIELD_ENTRY_TYPE);        query.append ("=");        query.append (typeID);        boolean result = makeQuery (query.toString());        if (result)        {            Hashtable cachedType = null;            Hashtable cachedACL = (Hashtable)cacheACLEntries.getValue(new Integer(aclID));            if (cachedACL!=null)            {                cachedACL.remove(new Integer(typeID));            }        }        query = null;        return result;    }    //------------------------------------------------------------------------    // FH   4 Apr. 2001    /**     * Return all the ACL entries associated to the specified ACL ID and having     * the specified type.     *     * @param   aclID     *      ACL's indentification number.     * @param   typeID     *      Entry type. See constants defined int <code>ACLInfo</code>.     *     * @return     *      Return <code>true</code> on success.     *     * @exception   JahiaDatabaseException     *      Throws this error on any database failure.     */    public Hashtable getACLEntries (int aclID, int typeID)    {        // it's in cache -> return it!        Hashtable aclEntries = (Hashtable)cacheACLEntries.getValue(new Integer(aclID));        if (aclEntries!=null) {            Hashtable typeEntries = (Hashtable)aclEntries.get(new Integer(typeID));            if (typeEntries!=null)            {                return typeEntries;            }        }        // else it means there is no entry for this aclid/type!        return new Hashtable();    }    // DJ 05/05/02    /**     * Cache all the ACL entries from the database. All of them. this is only     * possible as there shouldn't be millions of entries in the database     * (there is an entry only at points where inheritence is disabled)     * If this ever happens (!?) we have to think of a better cache system.     *     * @exception   JahiaDatabaseException     *      Throws this error on any database failure.     */    private static final int CHUNK_SIZE = 500;    private void cacheAllACLEntries ()        throws  JahiaDatabaseException    {        // Get a database connection        Connection dbConn = getDBConnection ();        if (dbConn == null) {            throw new JahiaDatabaseException (                    "ACL entry loading process could not get a database connection.",                    JahiaDatabaseException.CRITICAL);        }        Statement statement = null;        StringBuffer query = new StringBuffer ();        try        {            // get aclentriesID            statement = dbConn.createStatement();            query.append ("SELECT ");            query.append (FIELD_ENTRY_ACL_ID);            query.append (" FROM ");            query.append (JAHIA_ACL_ENTRIES);            query.append (" ORDER BY ");            query.append (FIELD_ACL_ID);            query.append (" ASC");            ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery (statement, query.toString());            Vector entries = new Vector();            if (rs != null) {                while (rs.next())                {                    entries.add(new Integer(rs.getInt (FIELD_ACL_ID)));                }            }            // cache them, approx. 500 by 500            int ptr=0;            while (ptr < entries.size())            {                query = new StringBuffer();                query.append ("SELECT * FROM ");                query.append (JAHIA_ACL_ENTRIES);                query.append (" WHERE ");                query.append (FIELD_ENTRY_ACL_ID);                query.append (">=");                query.append (((Integer)entries.elementAt(ptr)).toString());                query.append (" AND ");                query.append (FIELD_ENTRY_ACL_ID);                query.append ("<=");                int max = (ptr+CHUNK_SIZE<entries.size()?ptr+CHUNK_SIZE:entries.size()-1);                query.append (((Integer)entries.elementAt(max)).toString());                rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery (statement, query.toString());                if (rs != null) {                    while (rs.next())                    {                        String target = rs.getString (FIELD_ENTRY_TARGET);                        int state     = rs.getInt (FIELD_ENTRY_STATE);                        int tristate  = rs.getInt (FIELD_ENTRY_TRISTATE);                        int id         = rs.getInt (FIELD_ENTRY_ACL_ID);                        int type       = rs.getInt (FIELD_ENTRY_TYPE);                        JahiaACLEntry entry = new JahiaACLEntry (state, tristate);                        Hashtable cachedType = null;                        Hashtable cachedACL = (Hashtable)cacheACLEntries.getValue(new Integer(id));                        if (cachedACL==null)                        {                            cachedACL = new Hashtable();                            cacheACLEntries.setValue(cachedACL, new Integer(id));                        }                        cachedType=(Hashtable)cachedACL.get(new Integer(type));                        if (cachedType == null)                        {                            cachedType = new Hashtable();                            cachedACL.put(new Integer(type), cachedType);                        }                        cachedType.put (target, entry);                    }                }                ptr+=CHUNK_SIZE;            }        }        catch (SQLException ex)        {            throw new JahiaDatabaseException (                    "An SQL exception occrued while accessing an ACL entry.",                    query.toString(), ex, JahiaDatabaseException.ERROR);        }        finally        {            query = null;            closeDBConnection (dbConn);            closeStatement (statement);        }    }    //-------------------------------------------------------------------------    // FH   12 Apr. 2001    /**     * Add all the ACL entries stored in the specified Hashtable into the     * database.     *     * @param   aclID     *      Unique identification number of the related ACL object.     * @param   typeID     *      Entry type. See constants defined int <code>ACLInfo</code>.     * @param   entries     *      Hashtable containing all the entries to store.     *     * @return     *      Return <code>true</code> if the entries could be inserted into the     *      database, or <code>false</code> on any error.     *     * @exception   JahiaDatabaseException     *      Throw this exception when any error occured while accessing the     *      database.     */    public boolean addACLEntries (int aclID, int typeID, Hashtable entries)        throws  JahiaDatabaseException    {        if (entries == null) {            return false;        }        Enumeration keys = entries.keys();        while (keys.hasMoreElements()) {            String key = (String)keys.nextElement();            JahiaACLEntry entry = (JahiaACLEntry)entries.get(key);            addACLEntry (aclID, typeID, key,                         entry.getState(), entry.getTriState());        } // while        return true;    }    /**     * Get the direct child (one level down) from a given parent ACL.     *     * @param parentACL_ID The root parent ACL ID.     * @return An array list of the direct childs from the given parent ACL.     * @throws JahiaDatabaseException     */    public ArrayList getACLDirectChilds(int parentACL_ID)    throws  JahiaDatabaseException    {        ArrayList aclChilds = new ArrayList();        Connection dbConn = getDBConnection ();        if (dbConn == null) {            throw new JahiaDatabaseException (                    "ACL entry loading process could not get a database connection.",                    JahiaDatabaseException.CRITICAL);        }        Statement statement = null;        StringBuffer query = new StringBuffer ();        try {            statement = dbConn.createStatement();            query.append("SELECT ");            query.append(FIELD_ACL_ID);            query.append(" FROM ");            query.append(JAHIA_ACL);            query.append(" WHERE ");            query.append(FIELD_PARENT_ID);            query.append("=");            query.append(parentACL_ID);            ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery(statement, query.toString());            while (rs.next()) {                int child = rs.getInt(FIELD_ACL_ID);                aclChilds.add(new Integer(child));            }        } catch (SQLException ex) {            throw new JahiaDatabaseException (                    "An SQL exception occured while accessing an ACL.",                    query.toString(), ex, JahiaDatabaseException.ERROR);        } finally {            query = null;            closeDBConnection (dbConn);            closeStatement (statement);        }        return aclChilds;    }     /**      * Get all childs from a root parent ACL ID.      * @param parentACL_ID The root parent ACL ID.      * @return an array list OF array list of the direct childs from root parent ID.      * @throws JahiaDatabaseException      */    public ArrayList getACLAllChilds(int parentACL_ID)    throws  JahiaDatabaseException {        ArrayList aclAllChilds = getACLDirectChilds(parentACL_ID);        int childNb = aclAllChilds.size();        for (int i = 0; i < childNb; i++) {            aclAllChilds.add(getACLAllChilds(((Integer)aclAllChilds.get(i)).intValue()));        }        return aclAllChilds;    }    //------------------------------------------------------------------------    // FH   4 Apr. 2001    /**     * Insert the specified ACL into the database.     *     * @param   acl     *      Reference to the ACL to be inserted.     *     * @return     *      Return <code>true</code> if the entries could be inserted into the     *      database, or <code>false</code> on any error.     *     * @exception   JahiaDatabaseException     *      Throw this exception when any error occured while accessing the     *      database.     */    public boolean addACLInDB (JahiaACL acl)        throws  JahiaDatabaseException    {        String parentIDStr = Integer.toString (acl.getParentID ());        StringBuffer query = new StringBuffer ("INSERT INTO ");        query.append (JAHIA_ACL);        query.append (" (");        query.append (FIELD_ACL_ID);        query.append (",");        query.append (FIELD_PARENT_ID);        query.append (",");        query.append (FIELD_INHERITANCE);        query.append (") VALUES (");        query.append (acl.getID());        query.append (",");        query.append (parentIDStr);        query.append (",");        query.append (Integer.toString(acl.getInheritance()));        query.append (")");        boolean result = makeQuery (query.toString());        // Add the user and group entries in the database.        if (result) {            // Add the user entries            result = addACLEntries (acl.getID(), USER_TYPE_ENTRY,                                    acl.getAllUserEntries());            if (result) {                result = addACLEntries (acl.getID(), GROUP_TYPE_ENTRY,                                        acl.getAllGroupEntries());            }            //if (result) {            //    result = AddACLEntries (acl.getID(), IP_TYPE_ENTRY,            //                            acl.getAllIPEntries());            //}        }        query = null;        return result;    }    //------------------------------------------------------------------------    // FH   4 Apr. 2001    /**     * Delete the specified ACL from the database.     *     * @param   acl     *      Reference to the ACL to be inserted.     *     * @return     *      Return <code>true</code> if the entries could be inserted into the     *      database, or <code>false</code> on any error.     *     * @exception   JahiaDatabaseException     *      Throw this exception when any error occured while accessing the     *      database.     */    public boolean deleteACLFromDB (JahiaACL acl)        throws  JahiaDatabaseException    {        // Get a database connection        Connection  dbConn = getDBConnection ();        if (dbConn == null) {            throw new JahiaDatabaseException (                    "ACL deletion process could not get a database connection.",                    JahiaDatabaseException.CRITICAL);        }        boolean     result = false;        Statement   statement = null;        StringBuffer query = new StringBuffer ();        try        {            statement = dbConn.createStatement();            // first remove the entries            query.append ("DELETE FROM ");            query.append (JAHIA_ACL_ENTRIES);            query.append (" WHERE ");            query.append (FIELD_ENTRY_ACL_ID);

⌨️ 快捷键说明

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