1 13 package org.jahia.services.usermanager; 14 15 import org.jahia.data.JahiaDBDOMObject; 16 import org.jahia.data.JahiaDOMObject; 17 import org.jahia.exceptions.JahiaException; 18 import org.jahia.registries.ServicesRegistry; 19 import org.jahia.services.sites.JahiaSitesService; 20 import org.jahia.utils.JahiaTools; 21 22 import java.sql.Connection ; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.sql.Statement ; 26 import java.util.Hashtable ; 27 28 29 34 public class JahiaSiteGroupManagerDBService extends JahiaSiteGroupManagerService { 35 private static org.apache.log4j.Logger logger = 36 org.apache.log4j.Logger.getLogger (JahiaSiteGroupManagerDBService.class); 37 38 private static final String MSG_INTERNAL_ERROR = new String ( 39 "Site Group Manager internal error"); 40 41 private static JahiaSiteGroupManagerDBService mInstance; 42 43 private JahiaSitesService mSitesService; 44 private JahiaGroupManagerService mGroupService; 45 46 47 50 private class SiteGroupBean { 51 52 protected String groupname; 53 protected int siteID = -1; 54 protected String groupID; 55 56 protected SiteGroupBean (String groupname, 57 int siteID, 58 String groupID 59 ) { 60 61 this.groupname = groupname; 62 this.siteID = siteID; 63 this.groupID = groupID; 64 } 65 } 66 67 68 75 protected JahiaSiteGroupManagerDBService () throws JahiaException { 76 77 ServicesRegistry registry = ServicesRegistry.getInstance (); 78 if (registry != null) { 79 mSitesService = registry.getJahiaSitesService (); 80 if (mSitesService == null) { 81 throw new JahiaException (MSG_INTERNAL_ERROR, 82 "Site Group manager could not get the Site Service instance.", 83 JahiaException.SERVICE_ERROR, JahiaException.CRITICAL_SEVERITY); 84 } 85 86 mGroupService = registry.getJahiaGroupManagerService (); 87 if (mGroupService == null) { 88 throw new JahiaException (MSG_INTERNAL_ERROR, 89 "Site Group manager could not get the User Manager Service instance.", 90 JahiaException.SERVICE_ERROR, JahiaException.CRITICAL_SEVERITY); 91 } 92 93 } else { 94 throw new JahiaException (MSG_INTERNAL_ERROR, 95 "Site Group could not get the Service Registry instance.", 96 JahiaException.REGISTRY_ERROR, JahiaException.CRITICAL_SEVERITY); 97 } 98 } 99 100 101 108 public static synchronized JahiaSiteGroupManagerDBService getInstance () { 109 if (mInstance == null) { 110 try { 111 mInstance = new JahiaSiteGroupManagerDBService (); 112 } catch (JahiaException ex) { 113 logger.error ( 114 "Could not create an instance of the JahiaSiteGroupManagerDBService class"); 115 116 } 117 } 118 return mInstance; 119 } 120 121 122 131 public synchronized boolean addGroup (int siteID, JahiaGroup grp) throws JahiaException { 132 133 if (grp == null) { 134 return false; 135 } 136 137 try { 138 139 StringBuffer query = new StringBuffer ("insert into jahia_sites_grps values('"); 140 query.append (JahiaTools.quote (grp.getGroupname ())); 141 query.append ("',"); 142 query.append (siteID); 143 query.append (",'"); 144 query.append (JahiaTools.quote (grp.getName ())); 145 query.append ("')"); 146 147 executeQueryNoResultSet (query.toString ()); 148 149 } catch (JahiaException je) { 150 String errorMsg = "Error in dbAddGroup(int siteID, JahiaGroup grp) : " + je.getMessage (); 151 logger.error (errorMsg + " -> BAILING OUT"); 152 throw new JahiaException ("Cannot add site group membership in the database", 153 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 154 } 155 156 return true; 157 } 158 159 160 169 public synchronized boolean removeGroup (int siteID, JahiaGroup grp) throws JahiaException { 170 171 if (grp == null) { 172 return false; 173 } 174 175 try { 176 177 StringBuffer query = new StringBuffer ( 178 "delete from jahia_sites_grps where grpname_sites_grps='"); 179 query.append (JahiaTools.quote (grp.getGroupname ())); 180 query.append ("' and siteid_sites_grps="); 181 query.append (siteID); 182 183 executeQueryNoResultSet (query.toString ()); 184 185 } catch (JahiaException je) { 186 String errorMsg = "Error in dbRemoveGroup(int siteID, JahiaGroup grp) : " + je.getMessage (); 187 logger.error (errorMsg + " -> BAILING OUT"); 188 throw new JahiaException ("Cannot remove a site group membership in the database", 189 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 190 } 191 192 return true; 193 } 194 195 196 204 public synchronized boolean removeGroup (JahiaGroup grp) throws JahiaException { 205 206 if (grp == null) { 207 return false; 208 } 209 210 try { 211 212 StringBuffer query = new StringBuffer ( 213 "delete from jahia_sites_grps where grpid_sites_grps='"); 214 query.append (JahiaTools.quote (grp.getGroupKey())); 215 query.append ("'"); 216 217 executeQueryNoResultSet (query.toString ()); 218 219 } catch (JahiaException je) { 220 String errorMsg = "Error in dbRemoveGroup(JahiaGroup grp) : " + je.getMessage (); 221 logger.error (errorMsg + " -> BAILING OUT"); 222 throw new JahiaException ("Cannot remove group from all sites in the database", 223 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 224 } 225 226 return true; 227 } 228 229 230 238 public synchronized boolean removeGroups (int siteID) throws JahiaException { 239 240 try { 241 242 StringBuffer query = new StringBuffer ( 243 "delete from jahia_sites_grps where siteid_sites_grps="); 244 query.append (siteID); 245 246 executeQueryNoResultSet (query.toString ()); 247 248 } catch (JahiaException je) { 249 String errorMsg = "Error in dbRemoveGroups(siteID) : " + je.getMessage (); 250 logger.error (errorMsg + " -> BAILING OUT"); 251 throw new JahiaException ("Cannot remove all groups of a site in the database", 252 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 253 } 254 255 return true; 256 } 257 258 259 269 public Hashtable getGroups (int siteID) throws JahiaException { 270 271 Hashtable groups = new Hashtable (); 272 Connection dbConn = null; 273 Statement statement = null; 274 275 try { 276 277 SiteGroupBean sgBean = null; 278 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 279 statement = dbConn.createStatement (); 280 if (statement != null) { 281 282 StringBuffer query = new StringBuffer ( 283 "select * from jahia_sites_grps where siteid_sites_grps="); 284 query.append (siteID); 285 ResultSet rs = statement.executeQuery (query.toString ()); 286 if (rs != null) { 287 while (rs.next ()) { 288 sgBean = getSiteGroupBeanFromResultSet (rs); 289 if (sgBean != null) { 290 groups.put (sgBean.groupname, sgBean.groupID); 291 } 292 } 293 } 294 } 295 296 } catch (SQLException se) { 297 String errorMsg = "Error in getGroups : " + se.getMessage (); 298 logger.error (errorMsg + " -> BAILING OUT"); 299 throw new JahiaException ("Cannot load sites from the database", 300 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 301 } catch (JahiaException je) { 302 String errorMsg = "Error in getGroups(siteID) : " + je.getMessage (); 303 logger.error (errorMsg + " -> BAILING OUT"); 304 throw new JahiaException ("Cannot get site's groups from database", 305 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 306 } finally { 307 308 closeStatement (statement); 309 } 310 311 return groups; 312 } 313 314 315 321 protected SiteGroupBean getSiteGroupBeanFromResultSet (ResultSet rs) throws JahiaException { 322 323 SiteGroupBean sgBean = null; 324 325 if (rs != null) { 326 327 String groupname = null; 328 int siteID = -1; 329 String groupID = null; 330 331 try { 332 333 groupname = rs.getString ("grpname_sites_grps"); 334 siteID = rs.getInt ("siteid_sites_grps"); 335 groupID = rs.getString ("grpid_sites_grps"); 336 337 } catch (SQLException se) { 338 String errorMsg = "Error in getSiteGroupBeanFromResultSet(rs) : " + se.getMessage (); 339 logger.error (errorMsg + " -> BAILING OUT"); 340 throw new JahiaException ("Cannot read data from resultset", 341 errorMsg, JahiaException.DATABASE_ERROR, 342 JahiaException.CRITICAL_SEVERITY); 343 } 344 345 346 sgBean = new SiteGroupBean ( 347 groupname, 348 siteID, 349 groupID 350 ); 351 352 } 353 354 355 return sgBean; 356 } 357 358 359 369 public JahiaDOMObject getGroupMembershipsAsDOM (int siteID) 370 throws JahiaException { 371 372 Connection dbConn = null; 373 Statement statement = null; 374 375 String output = null; 376 JahiaDBDOMObject dom = null; 377 378 try { 379 String sqlQuery = "SELECT * FROM jahia_sites_grps where siteid_sites_grps=" + siteID; 380 381 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 382 statement = dbConn.createStatement (); 383 if (statement != null) { 384 ResultSet rs = statement.executeQuery (sqlQuery); 385 if (rs != null) { 386 dom = new JahiaDBDOMObject (); 387 dom.addTable ("jahia_sites_grps", rs); 388 return dom; 389 } 390 } 391 } catch (SQLException se) { 392 String errorMsg = "Error in getGroupMembershipsAsDOM(int siteID) : " + se.getMessage (); 393 logger.error (errorMsg); 394 throw new JahiaException ("Cannot load data from the database", 395 errorMsg, JahiaException.DATABASE_ERROR, 396 JahiaException.CRITICAL_SEVERITY); 397 } finally { 398 399 closeStatement (statement); 400 } 401 402 return dom; 403 } 404 405 406 417 public JahiaDOMObject getAuthExternalGroupsAsDOM (int siteID) 418 throws JahiaException { 419 420 Connection dbConn = null; 421 Statement statement = null; 422 423 String output = null; 424 JahiaDBDOMObject dom = null; 425 426 try { 427 String sqlQuery = "SELECT * FROM jahia_grps" 428 + " WHERE siteid_jahia_grps<>0 AND siteid_jahia_grps<>" + siteID 429 + " AND name_jahia_grps IN " 430 + "(SELECT grpname_sites_grps from jahia_sites_grps where siteid_sites_grps=" 431 + siteID + ") AND key_jahia_grps IN " 432 + "(SELECT grpid_sites_grps from jahia_sites_grps where siteid_sites_grps=" 433 + siteID + ")"; 434 435 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 436 statement = dbConn.createStatement (); 437 if (statement != null) { 438 ResultSet rs = statement.executeQuery (sqlQuery); 439 if (rs != null) { 440 dom = new JahiaDBDOMObject (); 441 dom.addTable ("jahia_grps", rs); 442 return dom; 443 } 444 } 445 } catch (SQLException se) { 446 String errorMsg = "Error in getAuthExternalGroupsAsDOM(int siteID) : " + se.getMessage (); 447 logger.error (errorMsg); 448 throw new JahiaException ("Cannot load data from the database", 449 errorMsg, JahiaException.DATABASE_ERROR, 450 JahiaException.CRITICAL_SEVERITY); 451 } finally { 452 453 closeStatement (statement); 454 } 455 456 return dom; 457 } 458 459 460 private void executeQueryNoResultSet (String queryStr) throws JahiaException { 462 463 Connection dbConn = null; 464 Statement statement = null; 465 466 try { 467 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 468 statement = dbConn.createStatement (); 469 if (statement != null) { 470 statement.executeUpdate (queryStr); 471 } 472 } catch (SQLException se) { 473 String errorMsg = "Error in executeQueryNoResultSet(String queryStr) : " + se.getMessage (); 474 logger.error (errorMsg + " -> BAILING OUT"); 475 throw new JahiaException ("Cannot execute query" + queryStr, 476 errorMsg, JahiaException.DATABASE_ERROR, JahiaException.CRITICAL_SEVERITY); 477 } finally { 478 479 closeStatement (statement); 480 } 481 482 } 483 484 485 private void closeStatement (Statement statement) { 487 try { 489 if (statement != null) { 490 statement.close (); 491 } 492 } catch (SQLException sqlEx) { 493 JahiaException je = new JahiaException ("Cannot close a statement", 496 "Cannot close a statement", JahiaException.DATABASE_ERROR, 497 JahiaException.WARNING_SEVERITY); 498 } 499 } 500 501 502 } 503 | Popular Tags |