📄 jahiasiteusermanagerdbservice.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.*;import org.jahia.services.JahiaService;import org.jahia.services.database.JahiaDBPoolService;import org.jahia.services.sites.*;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 users memberhip Service in a multi site context * * @author Khue Ng */public class JahiaSiteUserManagerDBService extends JahiaSiteUserManagerService{ private static final String MSG_INTERNAL_ERROR = new String ("Site User Manager internal error"); private static JahiaSiteUserManagerDBService mInstance; /** * Inner class * */ private class SiteUserBean { protected String username; protected int siteID = -1; protected String userID; protected SiteUserBean( String username, int siteID, String userID ){ this.username = username; this.siteID = siteID; this.userID = userID; } } //-------------------------------------------------------------------------- /** * Default constructor. * * @exception JahiaException Raise a JahiaException when during initialization * one of the needed services could not be instanciated. */ protected JahiaSiteUserManagerDBService () throws JahiaException { } //------------------------------------------------------------------------- /** * Create an new instance of the Site User Manager Service if the instance do not * exist, or return the existing instance. * * @return Return the instance of the Site User Manager Service. */ public static synchronized JahiaSiteUserManagerDBService getInstance () { if (mInstance == null) { try { mInstance = new JahiaSiteUserManagerDBService (); } catch (JahiaException ex) { JahiaConsole.println ("Site User Manager", "Could not create an instance of the JahiaSiteUserManagerDBService class"); } } return mInstance; } //------------------------------------------------------------------------- /** * Create a new membership for a user on a gived site * * @param int siteID, the site identifier * @param JahiaUser user, the user to add as member * @author NK */ public synchronized boolean addMember (int siteID, JahiaUser user) throws JahiaException { if ( user == null ){ return false; } try { // add new membership to database StringBuffer query = new StringBuffer("insert into jahia_sites_users values('"); query.append(JahiaTools.quote(user.getUsername())); query.append("',"); query.append(siteID); query.append(",'"); query.append(JahiaTools.quote(user.getName())); query.append("')"); executeQueryNoResultSet(query.toString()); } catch (JahiaException je) { String errorMsg = "Error in dbAddMember(int siteID, JahiaUser user) : " + je.getMessage(); JahiaConsole.println( "JahiaSiteUserManagerDBService", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot add member in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } return true; } //------------------------------------------------------------------------- /** * Remove a user's membership from a site, doesn't delete the user * * @param int siteID, the site identifier * @param JahiaUser user reference on the user to be removed from the site. * @author NK */ public synchronized boolean removeMember (int siteID, JahiaUser user) throws JahiaException { if ( user == null ){ return false; } try { // remove a member of a site from database StringBuffer query = new StringBuffer("delete from jahia_sites_users where username_sites_users='"); query.append(JahiaTools.quote(user.getUsername())); query.append("' and siteid_sites_users="); query.append(siteID); executeQueryNoResultSet(query.toString()); } catch (JahiaException je) { String errorMsg = "Error in dbRemoveMember(int siteID, JahiaUser user) : " + je.getMessage(); JahiaConsole.println( "JahiaSiteUserManagerDBService", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot add member in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } return true; } //------------------------------------------------------------------------- /** * Remove a user's membership from all sites, doesn't delete the user * * @param JahiaUser user, the user to be removed from the site. * @author Khue Ng */ public synchronized boolean removeMember (JahiaUser user) throws JahiaException { if ( user == null ){ return false; } try { StringBuffer query = new StringBuffer("delete from jahia_sites_users where username_sites_users='"); query.append(JahiaTools.quote(user.getUsername())); query.append("'"); executeQueryNoResultSet(query.toString()); } catch (JahiaException je) { String errorMsg = "Error in dbRemoveMember(JahiaUser user) : " + je.getMessage(); JahiaConsole.println( "JahiaSiteUserManagerDBService", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot add member in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } return true; } //------------------------------------------------------------------------- /** * Remove all users of a site ( only the membership, not the user ) * * @param int siteID, the identifier of the site. * @author Khue Ng */ public synchronized boolean removeMembers (int siteID) throws JahiaException { if ( siteID == 0 ){ return false; } try { StringBuffer query = new StringBuffer("delete from jahia_sites_users where siteid_sites_users="); query.append(siteID); executeQueryNoResultSet(query.toString()); } catch (JahiaException je) { String errorMsg = "Error in dbRemoveMember(JahiaUser user) : " + je.getMessage(); JahiaConsole.println( "JahiaSiteUserManagerDBService", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot add member in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } return true; } //------------------------------------------------------------------------- /** * This method returns the list of all the usernames of members of a site. * * @param int siteID, the site identifier * @return Return an Hashtable of username/usrid couples members of this site. * @author Khue Ng */ public Hashtable getMembersMap (int siteID) throws JahiaException { Hashtable members = new Hashtable(); Connection dbConn = null; Statement statement = null; try { SiteUserBean suBean = null; dbConn = getDBConnection(); statement = dbConn.createStatement(); if (statement != null) { StringBuffer query = new StringBuffer("select * from jahia_sites_users where siteid_sites_users="); query.append(siteID); ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery( statement,query.toString() ); if (rs != null) { while (rs.next()) { suBean = getSiteUserBeanFromResultSet(rs); if ( suBean != null ){ members.put(suBean.username,suBean.userID); } } } } } catch (SQLException se) { String errorMsg = "Error in dbGetSites : " + se.getMessage(); JahiaConsole.println( "JahiaSitesPersistance", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot load sites from the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } catch (JahiaException je) { String errorMsg = "Error in dbRemoveMember(JahiaUser user) : " + je.getMessage(); JahiaConsole.println( "JahiaSiteUserManagerDBService", errorMsg + " -> BAILING OUT" ); throw new JahiaException( "Cannot add member in the database", errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL ); } finally { closeDBConnection (dbConn); closeStatement (statement); } return members; } //------------------------------------------------------------------------- /** * This method returns the list of all members of this site. * * @param int siteID, the site identifier * @return Vector of members of this site. * @author Khue Ng */ public Vector getMembers (int siteID) throws JahiaException { Hashtable hash = new Hashtable(); Vector members = new Vector(); Connection dbConn = null; Statement statement = null; try { hash = getMembersMap(siteID); if ( hash != null ){ Enumeration enum = hash.elements(); String usrKey = null; JahiaUser user = null; while ( enum.hasMoreElements() ){ usrKey = (String)enum.nextElement(); user = ServicesRegistry.getInstance() .getJahiaUserManagerService() .lookupUser(usrKey); if ( user != null ){ members.add(user); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -