KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > api > usermgr > db > dbUserMgr


1 package com.raptus.owxv3.api.usermgr.db;
2
3 import java.util.*;
4
5 import com.raptus.owxv3.*;
6 import com.raptus.owxv3.api.dataxs.*;
7 import com.raptus.owxv3.api.usermgr.*;
8
9 public class dbUserMgr implements UserMgr
10 {
11     OwxUsersManager usermgr = null;
12     OwxRolesManager rolemgr = null;
13     OwxUserRolesManager userrolesmgr = null;
14  
15     public dbUserMgr()
16     {
17         javax.sql.DataSource JavaDoc ds = com.raptus.owxv3.VModuleManager.getInstance().getDataSource(com.raptus.owxv3.VModuleManager.GLOBALRESOURCES_DS);
18         usermgr = new OwxUsersManager();
19         usermgr.setDataSource(ds);
20         userrolesmgr = new OwxUserRolesManager();
21         userrolesmgr.setDataSource(ds);
22         rolemgr = new OwxRolesManager();
23         rolemgr.setDataSource(ds);
24         LoggingManager.log("Created database user manager!",this);
25     }
26     
27     /**
28      * add a new user
29      *
30      * @param name the name of the user to be added
31      * @param password the password of the user
32      * @param username the login name of the user to be added
33      * @param email the email address of the new user
34      * @param locale the locale of the user
35      * @param addedby the user who added the new user
36      *
37      * @return true if user was successfully added, else returns false
38      */

39     public boolean addUser(String JavaDoc name, String JavaDoc password,String JavaDoc username,String JavaDoc email,String JavaDoc locale, User addedby)
40     {
41         //dbUser user = new dbUser(name, username, password, email, locale);
42
OwxUsers user = new OwxUsers();
43         user.setUserName(username);
44         user.setUserPassword(password);
45         user.setName(name);
46         user.setEmail(email);
47         user.setLocale(locale);
48         
49         try
50         {
51             usermgr.save(user);
52         }
53         catch(java.sql.SQLException JavaDoc ex)
54         {
55             ex.printStackTrace();
56             return false;
57         }
58         return true;
59     }
60     
61     /**
62      * Check if user exists
63      *
64      * @param name the name of the user to be checked
65      *
66      * @return true if an user with this name was found, else return false
67      *
68      */

69     public boolean existUser(String JavaDoc name)
70     {
71         try
72         {
73             OwxUsers user = usermgr.loadByKey(name);
74             if(user == null)
75             {
76                 return false;
77             }
78             return true;
79         }
80         catch(java.sql.SQLException JavaDoc ex)
81         {
82             ex.printStackTrace();
83             return false;
84         }
85     }
86     
87     /**
88      * get a user specified by it's name and password
89      *
90      * @param name the user name
91      * @param password the user password
92      *
93      * @return the User if found, or null if not found
94      *
95      */

96     public User getUser(String JavaDoc name,String JavaDoc password)
97     {
98         try
99         {
100             OwxUsers user = usermgr.loadByKey(name);
101             if(user == null)
102             {
103                 return null;
104             }
105             if(user.getUserPassword().equals(password))
106             {
107                 dbUser usr = new dbUser(user.getName(),
108                     user.getUserName(), user.getUserPassword(),
109                     user.getEmail(), user.getLocale());
110                 return (User)usr;
111             }
112             return null;
113         }
114         catch(java.sql.SQLException JavaDoc ex)
115         {
116             return null;
117         }
118     }
119     
120     /**
121      * Return all available users
122      *
123      * @return an iterator with all users in system
124      *
125      */

126     public Iterator getUsers()
127     {
128         List l = new ArrayList();
129         try
130         {
131             OwxUsers users[] = usermgr.loadAll();
132             if(users == null)
133             {
134                 return l.iterator();
135             }
136
137             for(int i=0;i<users.length;i++)
138             {
139                 dbUser u = new dbUser(users[i].getName(),
140                     users[i].getUserName(), users[i].getUserPassword(),
141                     users[i].getEmail(), users[i].getLocale());
142                 l.add(u);
143             }
144             
145             return l.iterator();
146         }
147         catch(java.sql.SQLException JavaDoc ex)
148         {
149             return l.iterator();
150         }
151     }
152     
153     /**
154      * Return the number of users in system.
155      *
156      * @return the number of users in system
157      *
158      */

159     public int getUsersCount()
160     {
161         try
162         {
163             OwxUsers users[] = usermgr.loadAll();
164             if(users == null)
165             {
166                 return 0;
167             }
168             
169             return users.length;
170         }
171         catch(java.sql.SQLException JavaDoc ex)
172         {
173             return 0;
174         }
175     }
176     
177     /**
178      * remove a user
179      *
180      * @param user the user to be removed
181      * @param removedby the oser who removed the specified user
182      *
183      * @return true if user was removed successfully else return false
184      *
185      */

186     public synchronized boolean removeUser(User user, User removedby)
187     {
188         try
189         {
190             // delete all asociations between this user and it's roles
191
OwxUserRoles our[] = userrolesmgr.loadByWhere("user_name='"+user.getUsername()+"'");
192             if(our != null)
193             {
194                 for(int i=0;i<our.length;i++)
195                 {
196                     userrolesmgr.deleteByKey(our[i].getUserId());
197                 }
198             }
199
200             // remove user
201
usermgr.deleteByKey(user.getUsername());
202             
203             return true;
204         }
205         catch(java.sql.SQLException JavaDoc ex)
206         {
207             return false;
208         }
209     
210     }
211     
212     /**
213      * get a user specified by it's name and password
214      *
215      * @param name the user name
216      *
217      * @return the User if found, or null if not found
218      *
219      */

220     public User getUser(String JavaDoc name)
221     {
222         try
223         {
224             OwxUsers user = usermgr.loadByKey(name);
225             if(user == null)
226             {
227                 return null;
228             }
229             dbUser usr = new dbUser(user.getName(),
230                 user.getUserName(), user.getUserPassword(),
231                 user.getEmail(), user.getLocale());
232             return (User)usr;
233         }
234         catch(java.sql.SQLException JavaDoc ex)
235         {
236             return null;
237         }
238     }
239    
240     /**
241      * update a user
242      *
243      * @param name the name of the user to be updaed
244      * @param password the password of the user
245      * @param username the login name of the user to be updated
246      * @param email the email address of the user
247      * @param locale the locale of the user
248      * @param addedby the user who added the user
249      *
250      * @return true if user was successfully updated, else returns false
251      */

252     public boolean updateUser(String JavaDoc name, String JavaDoc password, String JavaDoc username, String JavaDoc email, String JavaDoc locale, User addedby)
253     {
254         //System.out.println("Setting user name to:"+name);
255
try
256         {
257             OwxUsers user = usermgr.loadByKey(username);
258             if(user == null)
259             {
260                 return false;
261             }
262             
263             user.setName(name);
264             user.setUserPassword(password);
265             user.setEmail(email);
266             user.setLocale(locale);
267             usermgr.save(user);
268             return true;
269         }
270         catch(java.sql.SQLException JavaDoc ex)
271         {
272             return false;
273         }
274     }
275     
276     /**
277      * Add a new role
278      *
279      * @param role the role to be added
280      * @param description the description of the new role
281      *
282      * @return true if role was successfully added, false if not
283      *
284      */

285     public boolean addRole(String JavaDoc role, String JavaDoc description)
286     {
287         OwxRoles srole = new OwxRoles();
288         srole.setRoleName(role);
289         srole.setRoleDesc(description);
290         try
291         {
292             rolemgr.save(srole);
293         }
294         catch(java.sql.SQLException JavaDoc ex)
295         {
296             ex.printStackTrace();
297             return false;
298         }
299         return true;
300     }
301     
302     /**
303      * Update a role
304      *
305      * @param role the role to be updated
306      * @param description the new description
307      *
308      * @return true if role was successfully updated, or false if not
309      *
310      */

311     public boolean updateRole(String JavaDoc role, String JavaDoc description)
312     {
313         try
314         {
315             OwxRoles srole = rolemgr.loadByKey(role);
316             if(srole == null)
317             {
318                 return false;
319             }
320             srole.setRoleDesc(description);
321             rolemgr.save(srole);
322             return true;
323         }
324         catch(java.sql.SQLException JavaDoc ex)
325         {
326             ex.printStackTrace();
327             return false;
328         }
329     }
330     
331     /**
332      * Return a role referenced by it's name
333      *
334      * @param name the name of the role
335      *
336      * @return a Role referenced by its name
337      *
338      */

339     public Role getRole(String JavaDoc name)
340     {
341         try
342         {
343             OwxRoles role = rolemgr.loadByKey(name);
344             if(role == null)
345             {
346                 return null;
347             }
348             dbRole res = new dbRole(role.getRoleName(), role.getRoleDesc());
349             return res;
350         }
351         catch(java.sql.SQLException JavaDoc ex)
352         {
353             ex.printStackTrace();
354             return null;
355         }
356     }
357     
358     /**
359      * Remove a role
360      *
361      * @param role the role to be removed
362      *
363      * @return true if role was removed, false if not
364      *
365      */

366     public boolean removeRole(Role role)
367     {
368         try
369         {
370             OwxUserRoles our[] = userrolesmgr.loadByWhere("role_name='"+role.getName()+"'");
371             if(our != null && our.length>0)
372             {
373                 // role can'tbe removed, users are using it
374

375                 return false;
376             }
377             rolemgr.deleteByKey(role.getName());
378             return true;
379         }
380         catch(java.sql.SQLException JavaDoc ex)
381         {
382             ex.printStackTrace();
383             return false;
384         }
385     }
386     
387     /**
388      * Return the roles
389      *
390      * @return an Iterator with the available roles
391      *
392      */

393     public Iterator getAllRolesAsString()
394     {
395         
396         List l = new ArrayList();
397         try
398         {
399             OwxRoles[] r = rolemgr.loadAll();
400             if(r != null)
401             {
402                 for(int i=0;i<r.length;i++)
403                 {
404                     l.add(r[i].getRoleName());
405                 }
406             }
407         }
408         catch(java.sql.SQLException JavaDoc ex)
409         {
410             ex.printStackTrace();
411         }
412         return l.iterator();
413     }
414     
415     /**
416      * return all roles available in system, az Role objects
417      *
418      * @return all roles available in system, a Role objects
419      */

420     public Iterator getAllRoles()
421     {
422         List l = new ArrayList();
423         try
424         {
425             OwxRoles[] r = rolemgr.loadAll();
426             if(r != null)
427             {
428                 for(int i=0;i<r.length;i++)
429                 {
430                     dbRole rr = new dbRole(r[i].getRoleName(), r[i].getRoleDesc());
431                     l.add(rr);
432                 }
433             }
434         }
435         catch(java.sql.SQLException JavaDoc ex)
436         {
437             ex.printStackTrace();
438         }
439         return l.iterator();
440     }
441     
442     /**
443      * return roles asociated to specified user, as Role objects
444      *
445      * @param u the user for who we require the roles
446      *
447      * @return all roles associated to specified user, as Role objects
448      */

449     public Iterator getRolesForUser(User u)
450     {
451         try
452         {
453             OwxUserRoles roles[] = userrolesmgr.loadByWhere("user_name='"+u.getUsername()+"'");
454             if(roles == null)
455             {
456                 return new ArrayList().iterator();
457             }
458             
459             Vector v = new Vector();
460             for(int i=0;i<roles.length;i++)
461             {
462                 Role r = getRole(roles[i].getRoleName());
463                 v.add(r);
464             }
465             
466             dbRole r = new dbRole(Constants.ROLE_OWXUSER, "owx user role, added by default");
467             return v.iterator();
468         }
469         catch(java.sql.SQLException JavaDoc ex)
470         {
471             return new ArrayList().iterator();
472         }
473     }
474     
475     /**
476      * return roles asociated to specified user, as String objects
477      *
478      * @param u the user for who we require the roles
479      *
480      * @return all roles associated to specified user, as String objects
481      */

482     public Iterator getRolesAsStringForUser(User u)
483     {
484         try
485         {
486             OwxUserRoles roles[] = userrolesmgr.loadByWhere("user_name='"+u.getUsername()+"'");
487             if(roles == null)
488             {
489                 return new ArrayList().iterator();
490             }
491             
492             Vector v = new Vector();
493             for(int i=0;i<roles.length;i++)
494             {
495                 v.add(roles[i].getRoleName());
496             }
497             v.add(Constants.ROLE_OWXUSER);
498              
499             return v.iterator();
500         }
501         catch(java.sql.SQLException JavaDoc ex)
502         {
503             return new ArrayList().iterator();
504         }
505     }
506     
507     /**
508      * Set the roles for the specified user
509      *
510      * @param u the user for who we're setting the roles
511      * @param roles the names of the roles
512      */

513     public void setRolesForUser(User u, String JavaDoc[] roles)
514     {
515         try
516         {
517             // first delete all roles of this user
518
OwxUserRoles our[] = userrolesmgr.loadByWhere("user_name='"+u.getUsername()+"'");
519             if(our !=null)
520             {
521                 for(int i=0;i<our.length;i++)
522                 {
523                     userrolesmgr.deleteByKey(our[i].getUserId());
524                 }
525             }
526             
527             // add new roles
528
if(roles != null)
529             {
530                 for(int i=0;i<roles.length;i++)
531                 {
532                     OwxUserRoles ur = new OwxUserRoles();
533                     ur.setRoleName(roles[i]);
534                     ur.setUserName(u.getUsername());
535                     userrolesmgr.save(ur);
536                 }
537             }
538         }
539         catch(java.sql.SQLException JavaDoc ex)
540         {
541             ex.printStackTrace();
542         }
543     }
544     
545     /**
546      * Set the roles for the specified user
547      *
548      * @param u the user for who we're setting the roles
549      * @param roles the names of the roles
550      *
551      */

552     public void setRolesForUser(User u, List roles)
553     {
554         try
555         {
556             // first delete all roles of this user
557
OwxUserRoles our[] = userrolesmgr.loadByWhere("user_name='"+u.getUsername()+"'");
558             if(our !=null)
559             {
560                 for(int i=0;i<our.length;i++)
561                 {
562                     userrolesmgr.deleteByKey(our[i].getUserId());
563                 }
564             }
565             
566             // add new roles
567
if(roles != null)
568             {
569                 for(int i=0;i<roles.size();i++)
570                 {
571                     OwxUserRoles ur = new OwxUserRoles();
572                     ur.setRoleName((String JavaDoc)roles.get(i));
573                     ur.setUserName(u.getUsername());
574                     userrolesmgr.save(ur);
575                 }
576             }
577         }
578         catch(java.sql.SQLException JavaDoc ex)
579         {
580             ex.printStackTrace();
581         }
582     }
583 }
Popular Tags