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

📄 jahiagroupmanagerdbservice.java

📁 java 写的一个新闻发布系统
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
//                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .//////// NK - 02 Avr. 2001 ://   1. Added member type ( USERTYPE or GROUPTYPE ) to support group in group// NK - 12 Avr. 2001 ://   1. Added Multi site support// NK - 18 Dec. 2001 ://   1. Added properties to grouppackage org.jahia.services.usermanager;import java.sql.*;import java.util.*;import org.jahia.data.JahiaDBDOMObject;import org.jahia.data.JahiaDOMObject;import org.jahia.utils.DBRowDataFilter;import org.jahia.exceptions.JahiaException;import org.jahia.registries.ServicesRegistry;import org.jahia.services.acl.JahiaACLManagerService;import org.jahia.services.database.JahiaDBPoolService;import org.jahia.services.database.JahiaIncrementorsDBService;import org.jahia.services.sites.JahiaSite;import org.jahia.services.sites.JahiaSiteTools;import org.jahia.utils.JahiaConsole;/* * */public class JahiaGroupManagerDBService extends JahiaGroupManagerService{    private final String MSG_INTERNAL_ERROR = new String ("Group Manager internal error");    // references to needed services.    private JahiaDBPoolService          mDBPoolService = null;    private JahiaIncrementorsDBService  mIncrementorService = null;    private static JahiaGroupManagerDBService mGroupManagerDBService;    private Hashtable  mGroupCache;    /** User Member type designation **/    protected static int mUSERTYPE = 1;    /** Group Member type designation **/    protected static int mGROUPTYPE = 2;    //--------------------------------------------------------------------------    /**     * Default constructor.     *     * @exception   JahiaException Raise a JahiaException when during initialization     *              one of the needed services could not be instanciated.     */    protected JahiaGroupManagerDBService () throws JahiaException    {        mGroupCache = new Hashtable();        ServicesRegistry registry = ServicesRegistry.getInstance();        if (registry != null)        {            mDBPoolService = registry.getDBPoolService();            if (mDBPoolService == null) {                throw new JahiaException (MSG_INTERNAL_ERROR, "Group manager could not get the DB Connection Pool Service instance.",                                            JahiaException.SERVICE_ERROR, JahiaException.CRITICAL);            }            mIncrementorService = registry.getJahiaIncrementorsDBService();            if (mIncrementorService == null) {                throw new JahiaException (MSG_INTERNAL_ERROR, "Group manager could not get the Incrementors DB Service instance.",                                          JahiaException.SERVICE_ERROR, JahiaException.CRITICAL);            }        } else {            throw new JahiaException (MSG_INTERNAL_ERROR, "Group manager could not get the Service Registry instance.",                                      JahiaException.REGISTRY_ERROR, JahiaException.CRITICAL);        }    }    //-------------------------------------------------------------------------    /**     * Create an new instance of the User Manager Service if the instance do not     * exist, or return the existing instance.     *     * @return Return the instance of the User Manager Service.     */    public static JahiaGroupManagerDBService getInstance ()    {        if (mGroupManagerDBService == null)        {            try {                mGroupManagerDBService = new JahiaGroupManagerDBService ();            }            catch (JahiaException ex) {                JahiaConsole.println ("Group Manager",                    "Could not create an instance of the JahiaGroupManagerDBService class");            }        }        return mGroupManagerDBService;    }    //-------------------------------------------------------------------------    /**     * Create a new group in the system.     *     * @param int siteID the site owner of this user     * @param groupname   Group's unique identification name     *     * @return  Retrun a reference on a group object on success, or if the groupname     *          already exists or another error occured, null is returned.     */    public synchronized JahiaGroup createGroup (int siteID, String name, Properties properties)    {        // try to avoid a NullPointerException        if (!isNameValid (name)) {            return null;        }        // Check if the group already exists        if (groupExists (siteID, name)) {            return null;        }        // Get the next available group ID        int groupID;        try {            groupID = ServicesRegistry.getInstance().getJahiaIncrementorsDBService().autoIncrement("jahia_grps");        }        catch (JahiaException ex) {            return null;        }        catch (NullPointerException ex)        {            toConsole ("Could not get the Jahia Incrementor Service !!");            return null;        }        // Create the group        JahiaDBGroup group = null;        String groupKey = name + ":" + String.valueOf(siteID);        try {            group = new JahiaDBGroup (groupID, name, groupKey, siteID, null, properties);        }        catch (JahiaException ex) {            toConsole ("Could not create group ["+name+"] in createGroup()");            return null;        }        // add the grp into the cache if the user could be added into the database.        if (addGroupIntoDB (groupID, name, groupKey, siteID, properties))        {            mGroupCache.put (group.getName(), group);            toConsole ("Group ["+name+"] was added into the database and in the cache");        }        else        {            toConsole ("Could not add the group ["+name+"] in the database!!");            group = null;        }        return group;    }    //-------------------------------------------------------------------------    /**     * Lookup the group information from the underlaying system (DB, LDAP, ... )     * Try to lookup the group into the cache, if it's not in the cache, then     * load it into the cahce from the database.     *     * @param int     *      siteID the site id     * @param name     *      Group's unique identification name.     *     * @return     *      Return a reference on a the specified group name. Return null     *      if the group doesn't exist or when any error occured.     */    public JahiaGroup lookupGroup (int siteID, String name)    {        // bad name -> bad results ;)        if (name == null) {            return null;        }        JahiaGroup group = null;        // First try to find the group in the cache.        Enumeration enum = mGroupCache.elements ();        while (enum.hasMoreElements()) {            group = (JahiaGroup)enum.nextElement();            if (group.getSiteID() == siteID                    && group.getGroupname().equals(name) ){                return group;            }        }        // if the group was not found in the cache, load it from the        // database.        group = lookupGroupInDB (siteID, name);        // if the group could be loaded from the database, add it into        // the cache.        if (group != null) {            mGroupCache.put (group.getName(), group);        }        return group;    }    //-------------------------------------------------------------------------    /**     * Lookup the group information from the underlaying system (DB, LDAP, ... )     * Try to lookup the group into the cache, if it's not in the cache, then     * load it into the cahce from the database.     *     * @param String     *      groupKey Group's unique identification key.     *     * @return     *      Return a reference on a the specified group name. Return null     *      if the group doesn't exist or when any error occured.     */    public JahiaGroup lookupGroup (String groupKey)    {        JahiaDBGroup group = null;        if ( mGroupCache.get (groupKey) != null ){            group = (JahiaDBGroup)mGroupCache.get (groupKey);        }        if (group == null)        {            //toDebug ("Could not find the group in the cache ... load it from DB");            group = lookupGroupInDB (groupKey);            if (group != null) {                //toDebug ("Group ["+group.getName()+"] loaded from DB");                mGroupCache.put (groupKey, group);            }        }        return group;    }    //-------------------------------------------------------------------------    /**     * Delete a group from the system. Updates the database automatically, and     * signal the ACL Manager that the group no longer exists.     *     * @param group  Reference to a JahiaGroup object.     *     * @return  Return true on success, or false on any failure.     */    public synchronized boolean deleteGroup (JahiaGroup group)    {        if (group == null) {            return false;        }        // cannot remove the super admin group        if ( ( group.getSiteID() == 0 ) && ( group.getGroupname().equals(ADMINISTRATORS_GROUPNAME) ) ){            return false;        }        // It's not allowed to remover the admin, guest and users group !        /*        if ((group.getGroupname().equals(ADMINISTRATORS_GROUPNAME)) ||            (group.getGroupname().equals(USERS_GROUPNAME)) ||            (group.getGroupname().equals(GUEST_GROUPNAME)))        {            return false;        }        */        JahiaACLManagerService aclService = null;        // Get the ACL Manager Service.        try        {            aclService = ServicesRegistry.getInstance().getJahiaACLManagerService();            if (aclService == null)            {                toConsole ("ACL Manager Service instance is null !!");                return false;            }        }        catch (NullPointerException ex)        {            toConsole ("Could not get the ACL Manager Service !!");            return false;        }        // delete the group from the database and from the cache.        boolean result = false;        if (deleteGroupFromDB (group))        {            // remove the gfroup from the cache            mGroupCache.remove (group.getName());            // invalidate the group in the ACL manager            aclService.removeGroupFromAllACLs (group);            // invalidate the group            group = null;            result = true;        }        return result;    }    //-------------------------------------------------------------------------    /**     * This function checks on a gived site if the groupname has already been     * assigned to another group.     *     * @param int siteID the site id     * @param groupname   String representing the unique group name.     *     * @return  Return true if the specified username has not been assigned yet,     *          return false on any failure.     */    public boolean groupExists (int siteID, String name)    {        return (lookupGroup (siteID, name) != null);    }    //-------------------------------------------------------------------------    /**     * Remove the specified user from all the membership lists of all the groups.     *     * @param  user  Reference on an existing user.     *     * @return  Return true on success, or false on any failure.     */    public synchronized boolean removeUserFromAllGroups (JahiaUser user)    {        // try to avoid a NullPointerException        if (user == null) {            return false;        }        boolean result = false;        // remove all the users from the database        String query = "DELETE FROM jahia_grp_access WHERE id_jahia_member='"+                        ((JahiaUser)user).getName() + "'";        result = makeQuery (query);        if (result)        {            // remove the user from all the groups in the cache.            Enumeration enum = mGroupCache.keys();            while (enum.hasMoreElements())            {                JahiaGroup group = (JahiaGroup)mGroupCache.get(enum.nextElement());                group.removeMember (user);            }        }        return result;    }    //-------------------------------------------------------------------------    /**     * Return a <code>Vector</code) of <code>String</code> representing all the     * group names.     *     * @return  Return a vector of strings containing all the group names.     */    public Vector getGroupnameList ()    {        Vector result = new Vector();        // Get a database connection

⌨️ 快捷键说明

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