📄 jahiadbgroup.java
字号:
///////////////////////////////////////////////////////////////////////////////// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .////// FH - 18 Jan. 2001 :// 1. Made this class compatible with the Group Java interface// NK - 02 Avr. 2001 :// 1. Added member type ( USERTYPE or GROUPTYPE ) to support group in group// NK - 18 Dec. 2001 :// 1. Added properties to group/////////////////////////////////////////////////////////////////////////////////package org.jahia.services.usermanager;import java.sql.*;import java.security.acl.Group;import java.security.Principal;import java.util.Hashtable;import java.util.Enumeration;import java.util.Properties;import java.lang.StringBuffer;import org.jahia.utils.JahiaConsole;import org.jahia.services.usermanager.JahiaGroup;import org.jahia.services.usermanager.JahiaUser;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.database.JahiaDatabaseException;import org.jahia.exceptions.database.JahiaDatabaseConnectionException;import org.jahia.services.database.JahiaDBPoolService;import org.jahia.registries.ServicesRegistry;/** * This class represents the Jahia native database based user group. * * @author Fulco Houkes * @version 1.0 */class JahiaDBGroup extends JahiaGroup{ /** Group's unique identification number */ protected int mID; /** Reference to the DB Pool Service */ private JahiaDBPoolService mDBPoolService; /** User Member type designation **/ protected static int mUSERTYPE = 1; /** Group Member type designation **/ protected static int mGROUPTYPE = 2; /** Group home page property **/ private static final String mHOMEPAGE_PROP = "group_homepage"; /** Group additional parameters. */ private Properties mProperties = new Properties(); //------------------------------------------------------------------------- /** Instanciate a new JahiaDBGroup object. * * @param name * Group's identification name. * @param attributes * Group additional attributes. If this parameter is null then the * group will have no additional parameters. * @param siteID * The site id * @exception JahiaException * This class need to access the Services Registry and the DB Pool * Service. If any of this services can't be accessed, a * JahiaException is thrown. */ protected JahiaDBGroup (int id, String groupname, String groupKey, int siteID, Hashtable members,Properties properties) throws JahiaException { ServicesRegistry registry = ServicesRegistry.getInstance(); if (registry != null) { mDBPoolService = registry.getDBPoolService(); if (mDBPoolService == null) { throw new JahiaException ("Jahia Internal Error", "JahiaDBGroup could not get the DB Pool Connection Service instance", JahiaException.SERVICE_ERROR, JahiaException.CRITICAL); } } else { throw new JahiaException ("Jahia Internal Error", "JahiaDBGroup Could not get the Service Registry instance", JahiaException.SERVICE_ERROR, JahiaException.CRITICAL); } mID = id; mGroupname = groupname; mGroupKey = groupKey; mSiteID = siteID; if (members != null) { mMembers = members; } if (properties != null) { mProperties = properties; } } //------------------------------------------------------------------------- public boolean addMember (Principal principal) { boolean result = false; if (principal != null) { if (!isMember (principal)) { try { String query = null; if (principal instanceof Group) { query = "INSERT INTO jahia_grp_access (id_jahia_member,id_jahia_grps,membertype_grp_access)"+ " VALUES ('"+((JahiaDBGroup)principal).getName()+ "','"+this.getName()+"',"+mGROUPTYPE+")"; } else { query = "INSERT INTO jahia_grp_access (id_jahia_member,id_jahia_grps,membertype_grp_access)"+ " VALUES ('"+((JahiaUser)principal).getName()+ "','"+this.getName()+"',"+mUSERTYPE+")"; } if (makeQuery (query)) { mMembers.put (principal.getName(), principal); result = true; } } catch (JahiaDatabaseConnectionException ex) { // do nothing .. false will be automatically returned. } } } return result; } //------------------------------------------------------------------------- /** Retrieve the group's database unique identification number. * * @return * The unique group ID. */ public int getGroupID () { return mID; } //------------------------------------------------------------------------- /** * Returns the group's home page id. * -1 : undefined * * @return int The group homepage id. */ public int getHomepageID (){ if (mProperties != null){ try { String value = mProperties.getProperty(mHOMEPAGE_PROP); if ( value == null ) return -1; return Integer.parseInt(value); } catch (Throwable t ){ t.printStackTrace(); } } return -1; } //------------------------------------------------------------------------- /** * Set the home page id. * * @param int The group homepage id. * @return false on error */ public boolean setHomepageID (int id){ /* if ( !removeProperty(mHOMEPAGE_PROP) ) return false; */ return setProperty(mHOMEPAGE_PROP,String.valueOf(id)); } //-------------------------------------------------------------------------- public Properties getProperties () { return mProperties; } //-------------------------------------------------------------------------- public String getProperty (String key) { if ((mProperties != null) && (key != null)) { return mProperties.getProperty (key); } return null; } //-------------------------------------------------------------------------- /** * Remove the specified property from the properties list. * * @param key Property's name. * * @return Return true on success or false on any failure. */ public synchronized boolean removeProperty (String key) { boolean result = false; if ((key != null) && (key.length() > 0)) { JahiaGroupDBUtils utils = JahiaGroupDBUtils.getInstance(); if (utils != null) { try { result = utils.removeProperty (key, mID); } // general database exeception catch (JahiaDatabaseException ex) { // false will be returned automaticaly } // database connection failure catch (JahiaDatabaseConnectionException ex) { // false will be returned automaticaly } // catch all the Jahia exceptions catch (JahiaException ex) { // false will be returned automaticaly } } } if (result) { mProperties.remove(key); } return result; } //-------------------------------------------------------------------------- /** * Add (or update if not already in the property list) a property key-value * pair in the grp's properties list. * * @param key Property's name. * @param value Property's value. * * @return Return true on success or false on any failure. */ public synchronized boolean setProperty (String key, String value) { boolean result = false; if ((key != null) && (value != null)) { JahiaGroupDBUtils utils = JahiaGroupDBUtils.getInstance(); if (utils != null) { try { if (getProperty (key) == null) { result = utils.addProperty (key, value, mID); } else { result = utils.updateProperty (key, value, mID); } } // general database exeception catch (JahiaDatabaseException ex) { // false will be returned automaticaly } // database connection failure catch (JahiaDatabaseConnectionException ex) { // false will be returned automaticaly } // catch all the Jahia exceptions catch (JahiaException ex) { // false will be returned automaticaly } } if (result) { mProperties.setProperty (key, value); } } return result; } //------------------------------------------------------------------------- public int hashCode () { return mID; } //------------------------------------------------------------------------- public boolean removeMember (Principal principal) { boolean result = false; if (principal != null) { String query = ""; if (principal instanceof Group) { query = "DELETE FROM jahia_grp_access WHERE id_jahia_member='"+ ((JahiaDBGroup)principal).getName()+ "' AND id_jahia_grps='"+this.getName()+ "' AND membertype_grp_access=" + mGROUPTYPE; } else { if ( (((JahiaUser)principal).isRoot()) && (getGroupname().equals(JahiaGroupManagerDBService.ADMINISTRATORS_GROUPNAME)) ) { return false; } query = "DELETE FROM jahia_grp_access WHERE id_jahia_member='"+ ((JahiaUser)principal).getName()+ "' AND id_jahia_grps='"+this.getName()+ "' AND membertype_grp_access=" + mUSERTYPE; } try { if (makeQuery(query)) { mMembers.remove (principal.getName()); result = true; } } catch (JahiaDatabaseConnectionException ex) { // do nothing .. false will be automatically returned. } } return result; } //-------------------------------------------------------------------------- public String toString () { StringBuffer output = new StringBuffer ("Details of group ["+mGroupname+"] :\n"); output.append (" - ID : "+Integer.toString(mID)+"\n"); output.append (" - properties :"); Enumeration names = mProperties.propertyNames(); String name; if (names.hasMoreElements()) { output.append ("\n"); while (names.hasMoreElements()) { name = (String)names.nextElement(); output.append (" "+name+" -> ["+(String)mProperties.getProperty(name)+"]\n"); } } else { output.append (" -no properties-\n"); } // Add the user members useranames detail output.append (" - members : "); names = mMembers.keys(); if (names.hasMoreElements()) { while (names.hasMoreElements()) { output.append(names.nextElement()+"/"); } } else { output.append (" -no members-\n"); } return output.toString(); } //-------------------------------------------------------------------------- /** Make a database query. * * @param query * The SQL query to be executed. * * @exception JahiaNullDBConnectionException * Throws this exception when not database connection could be * obtained. */ private boolean makeQuery (String query) throws JahiaDatabaseConnectionException { boolean result = true; Connection dbConn = null; Statement statement = null; try { dbConn = mDBPoolService.getConnection (76); if (dbConn == null) { throw new JahiaDatabaseConnectionException ("JahiaDBGroup could not get a DB connection"); } statement = dbConn.createStatement(); synchronized (this) { ServicesRegistry.getInstance().getDBPoolService().executeUpdate (statement,query); } } catch (SQLException sqlEx) { // FIXME -Fulco- : don't know where to log this exception yet. result = false; } finally { try { if (statement!=null) { statement.close(); } if (mDBPoolService != null) { mDBPoolService.freeConnection (dbConn); } } catch (SQLException sqlEx) { // FIXME -Fulco- : don't know where to log this exception yet. result = false; } } return result; } /** * Get the name of the provider of this group. * * @return * String representation of the name of the provider of this group */ public String getProviderName() { return "jahia"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -