KickJava   Java API By Example, From Geeks To Geeks.

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


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
// FH - 18 Jan. 2001 :
14
// 1. Made this class compatible with the Group Java interface
15
// NK - 02 Avr. 2001 :
16
// 1. Added member type ( USERTYPE or GROUPTYPE ) to support group in group
17
// NK - 18 Dec. 2001 :
18
// 1. Added properties to group
19
//
20
///////////////////////////////////////////////////////////////////////////////
21

22 package org.jahia.services.usermanager;
23
24 import org.jahia.exceptions.JahiaException;
25 import org.jahia.exceptions.database.JahiaDatabaseConnectionException;
26 import org.jahia.exceptions.database.JahiaDatabaseException;
27 import org.jahia.registries.ServicesRegistry;
28
29 import java.security.Principal JavaDoc;
30 import java.security.acl.Group JavaDoc;
31 import java.sql.Connection JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Statement JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.Hashtable JavaDoc;
36 import java.util.Properties JavaDoc;
37
38 /**
39  * This class represents the Jahia native database based user group.
40  *
41  * @author Fulco Houkes
42  * @version 1.0
43  */

44 class JahiaDBGroup extends JahiaGroup {
45     /** Group's unique identification number */
46     protected int mID;
47
48     /** User Member type designation * */
49     protected static int mUSERTYPE = 1;
50
51     /** Group Member type designation * */
52     protected static int mGROUPTYPE = 2;
53
54     /** Group home page property * */
55     private static final String JavaDoc mHOMEPAGE_PROP = "group_homepage";
56
57     /** Group additional parameters. */
58     private Properties JavaDoc mProperties = new Properties JavaDoc ();
59
60     //-------------------------------------------------------------------------
61
/**
62      * Instanciate a new JahiaDBGroup object.
63      *
64      * @param name Group's identification name.
65      * @param attributes Group additional attributes. If this parameter is null then the
66      * group will have no additional parameters.
67      *
68      * @throws JahiaException This class need to access the Services Registry and the DB Pool
69      * Service. If any of this services can't be accessed, a
70      * JahiaException is thrown.
71      * @param siteID The site id
72      */

73     protected JahiaDBGroup (int id, String JavaDoc groupname, String JavaDoc groupKey, int siteID,
74                             Hashtable JavaDoc members, Properties JavaDoc properties)
75             throws JahiaException {
76         ServicesRegistry registry = ServicesRegistry.getInstance ();
77         if (registry == null) {
78             throw new JahiaException ("Jahia Internal Error",
79                     "JahiaDBGroup Could not get the Service Registry instance",
80                     JahiaException.SERVICE_ERROR, JahiaException.CRITICAL_SEVERITY);
81         }
82
83         mID = id;
84         mGroupname = groupname;
85         mGroupKey = groupKey;
86         mSiteID = siteID;
87
88         if (members != null) {
89             mMembers = members;
90         }
91
92         if (properties != null) {
93             mProperties = properties;
94         }
95
96     }
97
98
99     //-------------------------------------------------------------------------
100
public boolean addMember (Principal JavaDoc principal) {
101
102         boolean result = false;
103
104         if (principal != null) {
105             if (!isMember (principal)) {
106                 try {
107
108                     String JavaDoc query = null;
109
110                     if (principal instanceof Group JavaDoc) {
111
112                         query = "INSERT INTO jahia_grp_access (id_jahia_member,id_jahia_grps,membertype_grp_access)" +
113                                 " VALUES ('" + ((JahiaGroup) principal).getName () +
114                                 "','" + this.getName () + "'," + mGROUPTYPE + ")";
115                     } else {
116
117                         query = "INSERT INTO jahia_grp_access (id_jahia_member,id_jahia_grps,membertype_grp_access)" +
118                                 " VALUES ('" + ((JahiaUser) principal).getName () +
119                                 "','" + this.getName () + "'," + mUSERTYPE + ")";
120                     }
121
122                     if (makeQuery (query)) {
123                         mMembers.put (principal.getName (), principal);
124                         result = true;
125                         ServicesRegistry.getInstance().getJahiaGroupManagerService().updateCache(this);
126                     }
127                 } catch (JahiaDatabaseConnectionException ex) {
128                     // do nothing .. false will be automatically returned.
129
}
130             }
131         }
132         return result;
133     }
134
135
136     //-------------------------------------------------------------------------
137
/**
138      * Retrieve the group's database unique identification number.
139      *
140      * @return The unique group ID.
141      */

142     public int getGroupID () {
143         return mID;
144     }
145
146     //-------------------------------------------------------------------------
147
/**
148      * Returns the group's home page id.
149      * -1 : undefined
150      *
151      * @return int The group homepage id.
152      */

153     public int getHomepageID () {
154
155         if (mProperties != null) {
156
157             try {
158                 String JavaDoc value = mProperties.getProperty (mHOMEPAGE_PROP);
159                 if (value == null)
160                     return -1;
161                 return Integer.parseInt (value);
162             } catch (Throwable JavaDoc t) {
163                 t.printStackTrace ();
164             }
165         }
166         return -1;
167     }
168
169     //-------------------------------------------------------------------------
170
/**
171      * Set the home page id.
172      *
173      * @param int The group homepage id.
174      *
175      * @return false on error
176      */

177     public boolean setHomepageID (int id) {
178
179         /*
180         if ( !removeProperty(mHOMEPAGE_PROP) )
181             return false;
182         */

183         return setProperty (mHOMEPAGE_PROP, String.valueOf (id));
184     }
185
186     //--------------------------------------------------------------------------
187
public Properties JavaDoc getProperties () {
188         return mProperties;
189     }
190
191
192     //--------------------------------------------------------------------------
193
public String JavaDoc getProperty (String JavaDoc key) {
194
195         if ((mProperties != null) && (key != null)) {
196             return mProperties.getProperty (key);
197         }
198         return null;
199     }
200
201     //--------------------------------------------------------------------------
202
/**
203      * Remove the specified property from the properties list.
204      *
205      * @param key Property's name.
206      *
207      * @return Return true on success or false on any failure.
208      */

209     public synchronized boolean removeProperty (String JavaDoc key) {
210         boolean result = false;
211
212         if ((key != null) && (key.length () > 0)) {
213             JahiaGroupDBUtils utils = JahiaGroupDBUtils.getInstance ();
214             if (utils != null) {
215                 try {
216                     result = utils.removeProperty (key, mID, getProviderName (), getGroupKey ());
217                 }
218                         // general database exeception
219
catch (JahiaDatabaseException ex) {
220                     // false will be returned automaticaly
221
}
222
223                         // database connection failure
224
catch (JahiaDatabaseConnectionException ex) {
225                     // false will be returned automaticaly
226
}
227
228                         // catch all the Jahia exceptions
229
catch (JahiaException ex) {
230                     // false will be returned automaticaly
231
}
232             }
233         }
234
235         if (result) {
236             mProperties.remove (key);
237             ServicesRegistry.getInstance().getJahiaGroupManagerService().updateCache(this);
238         }
239
240         return result;
241     }
242
243     //--------------------------------------------------------------------------
244
/**
245      * Add (or update if not already in the property list) a property key-value
246      * pair in the grp's properties list.
247      *
248      * @param key Property's name.
249      * @param value Property's value.
250      *
251      * @return Return true on success or false on any failure.
252      */

253     public synchronized boolean setProperty (String JavaDoc key, String JavaDoc value) {
254         boolean result = false;
255
256         if ((key != null) && (value != null)) {
257
258             JahiaGroupDBUtils utils = JahiaGroupDBUtils.getInstance ();
259             if (utils != null) {
260                 try {
261                     if (getProperty (key) == null) {
262                         result =
263                                 utils.addProperty (key, value, mID, getProviderName (),
264                                         getGroupKey ());
265                     } else {
266                         result =
267                                 utils.updateProperty (key, value, mID, getProviderName (),
268                                         getGroupKey ());
269                     }
270                 }
271                         // general database exeception
272
catch (JahiaDatabaseException ex) {
273                     // false will be returned automaticaly
274
}
275
276                         // database connection failure
277
catch (JahiaDatabaseConnectionException ex) {
278                     // false will be returned automaticaly
279
}
280
281                         // catch all the Jahia exceptions
282
catch (JahiaException ex) {
283                     // false will be returned automaticaly
284
}
285             }
286
287             if (result) {
288                 mProperties.setProperty (key, value);
289                 ServicesRegistry.getInstance().getJahiaGroupManagerService().updateCache(this);
290             }
291         }
292         return result;
293     }
294
295
296     public boolean equals (Object JavaDoc another) {
297         if (another instanceof Principal JavaDoc) {
298             if (another != null) {
299                 return (getName ().equals (((Principal JavaDoc) another).getName ()));
300             }
301         }
302         return false;
303     }
304
305     //-------------------------------------------------------------------------
306
public int hashCode () {
307         return ("group" + mID).hashCode ();
308     }
309
310
311     //-------------------------------------------------------------------------
312
public boolean removeMember (Principal JavaDoc principal) {
313
314         boolean result = false;
315         if (principal != null) {
316
317             String JavaDoc query = "";
318             if (principal instanceof Group JavaDoc) {
319
320                 query = "DELETE FROM jahia_grp_access WHERE id_jahia_member='" +
321                         ((JahiaDBGroup) principal).getName () +
322                         "' AND id_jahia_grps='" + this.getName () +
323                         "' AND membertype_grp_access=" + mGROUPTYPE;
324             } else {
325
326                 if ((((JahiaUser) principal).isRoot ()) &&
327                         (getGroupname ().equals (
328                                 JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME))) {
329                     return false;
330                 }
331
332                 query = "DELETE FROM jahia_grp_access WHERE id_jahia_member='" +
333                         ((JahiaUser) principal).getName () +
334                         "' AND id_jahia_grps='" + this.getName () +
335                         "' AND membertype_grp_access=" + mUSERTYPE;
336             }
337
338             try {
339                 if (makeQuery (query)) {
340                     mMembers.remove (principal.getName ());
341                     result = true;
342                     ServicesRegistry.getInstance().getJahiaGroupManagerService().updateCache(this);
343                 }
344             } catch (JahiaDatabaseConnectionException ex) {
345                 // do nothing .. false will be automatically returned.
346
}
347         }
348         return result;
349     }
350
351
352     //--------------------------------------------------------------------------
353
public String JavaDoc toString () {
354
355         StringBuffer JavaDoc output = new StringBuffer JavaDoc ("Details of group [" + mGroupname + "] :\n");
356
357         output.append (" - ID : " + Integer.toString (mID) + "\n");
358
359         output.append (" - properties :");
360
361         Enumeration JavaDoc names = mProperties.propertyNames ();
362         String JavaDoc name;
363         if (names.hasMoreElements ()) {
364             output.append ("\n");
365             while (names.hasMoreElements ()) {
366                 name = (String JavaDoc) names.nextElement ();
367                 output.append (
368                         " " + name + " -> [" + (String JavaDoc) mProperties.getProperty (name) + "]\n");
369             }
370         } else {
371             output.append (" -no properties-\n");
372         }
373
374         // Add the user members useranames detail
375
output.append (" - members : ");
376         names = mMembers.keys ();
377         if (names.hasMoreElements ()) {
378             while (names.hasMoreElements ()) {
379                 output.append (names.nextElement () + "/");
380             }
381         } else {
382             output.append (" -no members-\n");
383         }
384
385         return output.toString ();
386     }
387
388
389     //--------------------------------------------------------------------------
390
/**
391      * Make a database query.
392      *
393      * @param query The SQL query to be executed.
394      *
395      * @throws JahiaNullDBConnectionException Throws this exception when not database connection could be
396      * obtained.
397      */

398     private boolean makeQuery (String JavaDoc query)
399             throws JahiaDatabaseConnectionException {
400         boolean result = true;
401         Connection JavaDoc dbConn = null;
402         Statement JavaDoc statement = null;
403
404         try {
405             dbConn = org.jahia.services.database.ConnectionDispenser.getConnection ();
406             if (dbConn == null) {
407                 throw new JahiaDatabaseConnectionException (
408                         "JahiaDBGroup could not get a DB connection");
409             }
410
411             statement = dbConn.createStatement ();
412             synchronized (this) {
413                 statement.executeUpdate (query);
414             }
415         } catch (SQLException JavaDoc sqlEx) {
416             // FIXME -Fulco- : don't know where to log this exception yet.
417
result = false;
418         } finally {
419             try {
420                 if (statement != null) {
421                     statement.close ();
422                 }
423
424             } catch (SQLException JavaDoc sqlEx) {
425                 // FIXME -Fulco- : don't know where to log this exception yet.
426
result = false;
427             }
428         }
429
430         return result;
431     }
432
433     /**
434      * Get the name of the provider of this group.
435      *
436      * @return String representation of the name of the provider of this group
437      */

438     public String JavaDoc getProviderName () {
439         return JahiaGroupManagerDBProvider.PROVIDER_NAME;
440     }
441
442 }
443
Popular Tags