KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > usermanager > JahiaSiteGroupManagerDBService


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

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 JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.util.Hashtable JavaDoc;
27
28
29 /**
30  * DB implementation of the Manage groups Grouphip Service in a multi site context
31  *
32  * @author Khue Ng
33  */

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 JavaDoc MSG_INTERNAL_ERROR = new String JavaDoc (
39             "Site Group Manager internal error");
40
41     private static JahiaSiteGroupManagerDBService mInstance;
42
43     private JahiaSitesService mSitesService;
44     private JahiaGroupManagerService mGroupService;
45
46
47     /**
48      * Inner class
49      */

50     private class SiteGroupBean {
51
52         protected String JavaDoc groupname;
53         protected int siteID = -1;
54         protected String JavaDoc groupID;
55
56         protected SiteGroupBean (String JavaDoc groupname,
57                                  int siteID,
58                                  String JavaDoc groupID
59                                  ) {
60
61             this.groupname = groupname;
62             this.siteID = siteID;
63             this.groupID = groupID;
64         }
65     }
66
67
68     //--------------------------------------------------------------------------
69
/**
70      * Default constructor.
71      *
72      * @throws JahiaException Raise a JahiaException when during initialization
73      * one of the needed services could not be instanciated.
74      */

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     //-------------------------------------------------------------------------
102
/**
103      * Create an new instance of the Site Group Manager Service if the instance do not
104      * exist, or return the existing instance.
105      *
106      * @return Return the instance of the Site Group Manager Service.
107      */

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     //-------------------------------------------------------------------------
123
/**
124      * Create a new association between a group and a site
125      *
126      * @param int siteID, the site identifier
127      * @param JahiaGroup group, the group to add to a site
128      *
129      * @author NK
130      */

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 JavaDoc query = new StringBuffer JavaDoc ("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 JavaDoc 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     //-------------------------------------------------------------------------
161
/**
162      * Remove a group's membership from a site
163      *
164      * @param int siteID, the site identifier
165      * @param JahiaGroup grp reference on the group to be removed from the site.
166      *
167      * @author NK
168      */

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 JavaDoc query = new StringBuffer JavaDoc (
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 JavaDoc 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     //-------------------------------------------------------------------------
197
/**
198      * Remove a group's membership from all sites, doesn't delete the group
199      *
200      * @param JahiaGroup grp, the group to be removed from the site.
201      *
202      * @author Khue Ng
203      */

204     public synchronized boolean removeGroup (JahiaGroup grp) throws JahiaException {
205
206         if (grp == null) {
207             return false;
208         }
209
210         try {
211
212             StringBuffer JavaDoc query = new StringBuffer JavaDoc (
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 JavaDoc 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     //-------------------------------------------------------------------------
231
/**
232      * Remove all groups of a site ( only the membership, not the groups )
233      *
234      * @param int siteID, the identifier of the site.
235      *
236      * @author Khue Ng
237      */

238     public synchronized boolean removeGroups (int siteID) throws JahiaException {
239
240         try {
241
242             StringBuffer JavaDoc query = new StringBuffer JavaDoc (
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 JavaDoc 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     //-------------------------------------------------------------------------
260
/**
261      * This method returns the list of all the groupnames of a site.
262      *
263      * @param int siteID, the site identifier
264      *
265      * @return Return an Hashtable of groupname/grpid couples members of this site.
266      *
267      * @author Khue Ng
268      */

269     public Hashtable JavaDoc getGroups (int siteID) throws JahiaException {
270
271         Hashtable JavaDoc groups = new Hashtable JavaDoc ();
272         Connection JavaDoc dbConn = null;
273         Statement JavaDoc 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 JavaDoc query = new StringBuffer JavaDoc (
283                         "select * from jahia_sites_grps where siteid_sites_grps=");
284                 query.append (siteID);
285                 ResultSet JavaDoc 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 JavaDoc se) {
297             String JavaDoc 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 JavaDoc 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     //-------------------------------------------------------------------------
316
/**
317      * Build a SiteGroupBean from a resultset
318      *
319      * @return SiteGroupBean the site group bean
320      */

321     protected SiteGroupBean getSiteGroupBeanFromResultSet (ResultSet JavaDoc rs) throws JahiaException {
322
323         SiteGroupBean sgBean = null;
324
325         if (rs != null) {
326
327             String JavaDoc groupname = null;
328             int siteID = -1;
329             String JavaDoc 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 JavaDoc se) {
338                 String JavaDoc 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     //--------------------------------------------------------------------------
360
/**
361      * return a DOM document of all groups membership of a site
362      *
363      * @param int the site id
364      *
365      * @return JahiaDOMObject a DOM representation of this object
366      *
367      * @author NK
368      */

369     public JahiaDOMObject getGroupMembershipsAsDOM (int siteID)
370             throws JahiaException {
371
372         Connection JavaDoc dbConn = null;
373         Statement JavaDoc statement = null;
374
375         String JavaDoc output = null;
376         JahiaDBDOMObject dom = null;
377
378         try {
379             String JavaDoc 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 JavaDoc 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 JavaDoc se) {
392             String JavaDoc 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     //--------------------------------------------------------------------------
407
/**
408      * return a DOM document of external groups ( from other sites )
409      * that have membership access on this site
410      *
411      * @param int the site id
412      *
413      * @return JahiaDOMObject a DOM representation of this object
414      *
415      * @author NK
416      */

417     public JahiaDOMObject getAuthExternalGroupsAsDOM (int siteID)
418             throws JahiaException {
419
420         Connection JavaDoc dbConn = null;
421         Statement JavaDoc statement = null;
422
423         String JavaDoc output = null;
424         JahiaDBDOMObject dom = null;
425
426         try {
427             String JavaDoc 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 JavaDoc 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 JavaDoc se) {
446             String JavaDoc 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     //-------------------------------------------------------------------------
461
private void executeQueryNoResultSet (String JavaDoc queryStr) throws JahiaException {
462
463         Connection JavaDoc dbConn = null;
464         Statement JavaDoc 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 JavaDoc se) {
473             String JavaDoc 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     //-------------------------------------------------------------------------
486
private void closeStatement (Statement JavaDoc statement) {
487         // Close the opened statement
488
try {
489             if (statement != null) {
490                 statement.close ();
491             }
492         } catch (SQLException JavaDoc sqlEx) {
493             // just create an exception without raising it, just to notify it
494
// in the logs.
495
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