📄 jahiasitegroupmanagerdbservice.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//package org.jahia.services.usermanager;import java.sql.*;import java.util.Vector;import java.util.Hashtable;import org.jahia.services.JahiaService;import org.jahia.services.database.JahiaDBPoolService;import org.jahia.services.sites.JahiaSitesService;import org.jahia.services.usermanager.JahiaGroup;import org.jahia.exceptions.JahiaException;import org.jahia.registries.ServicesRegistry;import org.jahia.utils.JahiaConsole;import org.jahia.utils.JahiaTools;import org.jahia.data.JahiaDOMObject;import org.jahia.data.JahiaDBDOMObject;/** * DB implementation of the Manage groups Grouphip Service in a multi site context * * @author Khue Ng */public class JahiaSiteGroupManagerDBService extends JahiaSiteGroupManagerService{ private static final String MSG_INTERNAL_ERROR = new String ("Site Group Manager internal error"); private static JahiaSiteGroupManagerDBService mInstance; private JahiaSitesService mSitesService; private JahiaGroupManagerService mGroupService; private JahiaDBPoolService mDBPoolService; /** * Inner class * */ private class SiteGroupBean { protected String groupname; protected int siteID = -1; protected String groupID; protected SiteGroupBean( String groupname, int siteID, String groupID ){ this.groupname = groupname; this.siteID = siteID; this.groupID = groupID; } } //-------------------------------------------------------------------------- /** * Default constructor. * * @exception JahiaException Raise a JahiaException when during initialization * one of the needed services could not be instanciated. */ protected JahiaSiteGroupManagerDBService () throws JahiaException { ServicesRegistry registry = ServicesRegistry.getInstance(); if (registry != null) { mDBPoolService = registry.getDBPoolService(); if (mDBPoolService == null) { throw new JahiaException (MSG_INTERNAL_ERROR, "Site Group manager could not get the DB Connection Pool Service instance.", JahiaException.SERVICE_ERROR, JahiaException.CRITICAL); } mSitesService = registry.getJahiaSitesService(); if (mSitesService == null){ throw new JahiaException (MSG_INTERNAL_ERROR, "Site Group manager could not get the Site Service instance.", JahiaException.SERVICE_ERROR, JahiaException.CRITICAL); } mGroupService = registry.getJahiaGroupManagerService(); if (mGroupService == null){ throw new JahiaException (MSG_INTERNAL_ERROR, "Site Group manager could not get the User Manager Service instance.", JahiaException.SERVICE_ERROR, JahiaException.CRITICAL); } } else { throw new JahiaException ( MSG_INTERNAL_ERROR, "Site Group could not get the Service Registry instance.", JahiaException.REGISTRY_ERROR, JahiaException.CRITICAL); } } //------------------------------------------------------------------------- /** * Create an new instance of the Site Group Manager Service if the instance do not * exist, or return the existing instance. * * @return Return the instance of the Site Group Manager Service. */ public static synchronized JahiaSiteGroupManagerDBService getInstance () { if (mInstance == null) { try { mInstance = new JahiaSiteGroupManagerDBService (); } catch (JahiaException ex) { JahiaConsole.println ("Site Group Manager", "Could not create an instance of the JahiaSiteGroupManagerDBService class"); } } return mInstance; } //------------------------------------------------------------------------- /** * Create a new association between a group and a site * * @param int siteID, the site identifier * @param JahiaGroup group, the group to add to a site * @author NK */ public synchronized boolean addGroup (int siteID, JahiaGroup grp) throws JahiaException { if ( grp == null ){ return false; } try { StringBuffer query = new StringBuffer("insert into jahia_sites_grps values('"); query.append(JahiaTools.quote(grp.getGroupname())); query.append("',"); query.append(siteID); query.append(",'"); query.append(JahiaTools.quote(grp.getName())); query.append("')"); executeQueryNoResultSet(query.toString()); } catch (JahiaException je) { String errorMsg = "Error in dbAddGroup(int siteID, JahiaGroup grp) : " + je.getMessage(); JahiaConsole.println( "JahiaSiteGroupManagerDBService", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot add site group membership in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } return true; } //------------------------------------------------------------------------- /** * Remove a group's membership from a site * * @param int siteID, the site identifier * @param JahiaGroup grp reference on the group to be removed from the site. * @author NK */ public synchronized boolean removeGroup (int siteID, JahiaGroup grp) throws JahiaException { if ( grp == null ){ return false; } try { StringBuffer query = new StringBuffer("delete from jahia_sites_grps where grpname_sites_grps='"); query.append(JahiaTools.quote(grp.getGroupname())); query.append("' and siteid_sites_grps="); query.append(siteID); executeQueryNoResultSet(query.toString()); } catch (JahiaException je) { String errorMsg = "Error in dbRemoveGroup(int siteID, JahiaGroup grp) : " + je.getMessage(); JahiaConsole.println( "JahiaSiteGroupManagerDBService", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot remove a site group membership in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } return true; } //------------------------------------------------------------------------- /** * Remove a group's membership from all sites, doesn't delete the group * * @param JahiaGroup grp, the user to be removed from the site. * @author Khue Ng */ public synchronized boolean removeGroup (JahiaGroup grp) throws JahiaException { if ( grp == null ){ return false; } try { StringBuffer query = new StringBuffer("delete from jahia_sites_grps where grpname_sites_grps='"); query.append(JahiaTools.quote(grp.getGroupname())); query.append("'"); executeQueryNoResultSet(query.toString()); } catch (JahiaException je) { String errorMsg = "Error in dbRemoveGroup(JahiaGroup grp) : " + je.getMessage(); JahiaConsole.println( "JahiaSiteGroupManagerDBService", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot remove group from all sites in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } return true; } //------------------------------------------------------------------------- /** * Remove all groups of a site ( only the membership, not the groups ) * * @param int siteID, the identifier of the site. * @author Khue Ng */ public synchronized boolean removeGroups (int siteID) throws JahiaException { try { StringBuffer query = new StringBuffer("delete from jahia_sites_grps where siteid_sites_grps="); query.append(siteID); executeQueryNoResultSet(query.toString()); } catch (JahiaException je) { String errorMsg = "Error in dbRemoveGroups(siteID) : " + je.getMessage(); JahiaConsole.println( "JahiaSiteGroupManagerDBService", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot remove all groups of a site in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } return true; } //------------------------------------------------------------------------- /** * This method returns the list of all the groupnames of a site. * * @param int siteID, the site identifier * @return Return an Hashtable of groupname/grpid couples members of this site. * @author Khue Ng */ public Hashtable getGroups (int siteID) throws JahiaException { Hashtable groups = new Hashtable(); Connection dbConn = null; Statement statement = null; try { SiteGroupBean sgBean = null; dbConn = getDBConnection(); statement = dbConn.createStatement();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -