KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > controllers > kernel > impl > simple > SystemUserController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.controllers.kernel.impl.simple;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.apache.log4j.Logger;
32 import org.exolab.castor.jdo.Database;
33 import org.exolab.castor.jdo.OQLQuery;
34 import org.exolab.castor.jdo.QueryResults;
35 import org.infoglue.cms.entities.kernel.BaseEntityVO;
36 import org.infoglue.cms.entities.management.Group;
37 import org.infoglue.cms.entities.management.Role;
38 import org.infoglue.cms.entities.management.SystemUser;
39 import org.infoglue.cms.entities.management.SystemUserVO;
40 import org.infoglue.cms.entities.management.impl.simple.SystemUserImpl;
41 import org.infoglue.cms.exception.Bug;
42 import org.infoglue.cms.exception.ConstraintException;
43 import org.infoglue.cms.exception.SystemException;
44 import org.infoglue.cms.util.CmsPropertyHandler;
45 import org.infoglue.cms.util.ConstraintExceptionBuffer;
46 import org.infoglue.cms.util.PasswordGenerator;
47 import org.infoglue.cms.util.mail.MailServiceFactory;
48
49 /**
50  * SystemUserController.java
51  * Created on 2002-aug-28
52  * @author Stefan Sik, ss@frovi.com
53  *
54  * This class is a helper class for the use case handle SystemUsers
55  *
56  */

57 public class SystemUserController extends BaseController
58 {
59     private final static Logger logger = Logger.getLogger(SystemUserController.class.getName());
60
61     /**
62      * Factory method
63      */

64
65     public static SystemUserController getController()
66     {
67         return new SystemUserController();
68     }
69     
70     /*
71     public static SystemUser getSystemUserWithId(Integer systemUserId, Database db) throws SystemException, Bug
72     {
73         return (SystemUser) getObjectWithId(SystemUserImpl.class, systemUserId, db);
74     }
75     
76     public SystemUserVO getSystemUserVOWithId(Integer systemUserId) throws SystemException, Bug
77     {
78         return (SystemUserVO) getVOWithId(SystemUserImpl.class, systemUserId);
79     }
80     */

81
82     public SystemUserVO getSystemUserVOWithName(String JavaDoc name) throws SystemException, Bug
83     {
84         SystemUserVO systemUserVO = null;
85         Database db = CastorDatabaseService.getDatabase();
86
87         try
88         {
89             beginTransaction(db);
90
91             systemUserVO = getReadOnlySystemUserWithName(name, db).getValueObject();
92
93             commitTransaction(db);
94         }
95         catch (Exception JavaDoc e)
96         {
97             logger.info("An error occurred so we should not complete the transaction:" + e);
98             rollbackTransaction(db);
99             throw new SystemException(e.getMessage());
100         }
101         
102         return systemUserVO;
103     }
104     
105     
106     
107     /**
108      * Get the SystemUser with the userName
109      */

110      
111     public SystemUser getReadOnlySystemUserWithName(String JavaDoc userName, Database db) throws SystemException, Bug
112     {
113         SystemUser systemUser = null;
114         OQLQuery oql;
115         try
116         {
117             oql = db.getOQLQuery( "SELECT u FROM org.infoglue.cms.entities.management.impl.simple.SystemUserImpl u WHERE u.userName = $1");
118             oql.bind(userName);
119             
120             QueryResults results = oql.execute(Database.ReadOnly);
121             
122             if (results.hasMore())
123             {
124                 systemUser = (SystemUser)results.next();
125                 logger.info("found one:" + systemUser.getFirstName());
126             }
127         }
128         catch(Exception JavaDoc e)
129         {
130             throw new SystemException("An error occurred when we tried to fetch " + userName + " Reason:" + e.getMessage(), e);
131         }
132
133         return systemUser;
134     }
135
136
137     /**
138      * Get the SystemUser with the userName
139      */

140      
141     public SystemUser getSystemUserWithName(String JavaDoc userName, Database db) throws SystemException, Bug
142     {
143         SystemUser systemUser = null;
144         OQLQuery oql;
145         try
146         {
147             oql = db.getOQLQuery( "SELECT u FROM org.infoglue.cms.entities.management.impl.simple.SystemUserImpl u WHERE u.userName = $1");
148             oql.bind(userName);
149             
150             QueryResults results = oql.execute();
151             this.logger.info("Fetching entity in read/write mode" + userName);
152
153             if (results.hasMore())
154             {
155                 systemUser = (SystemUser)results.next();
156                 logger.info("found one:" + systemUser.getFirstName());
157             }
158         }
159         catch(Exception JavaDoc e)
160         {
161             throw new SystemException("An error occurred when we tried to fetch " + userName + " Reason:" + e.getMessage(), e);
162         }
163
164         return systemUser;
165     }
166
167
168
169     public SystemUserVO getSystemUserVO(String JavaDoc userName, String JavaDoc password) throws SystemException, Bug
170     {
171         SystemUserVO systemUserVO = null;
172
173         Database db = CastorDatabaseService.getDatabase();
174
175         try
176         {
177             beginTransaction(db);
178
179             systemUserVO = getSystemUserVO(db, userName, password);
180
181             commitTransaction(db);
182         }
183         catch (Exception JavaDoc e)
184         {
185             logger.info("An error occurred so we should not complete the transaction:" + e);
186             rollbackTransaction(db);
187             throw new SystemException(e.getMessage());
188         }
189         
190         return systemUserVO;
191     }
192
193
194     public SystemUserVO getSystemUserVO(Database db, String JavaDoc userName, String JavaDoc password) throws SystemException, Exception JavaDoc
195     {
196         SystemUserVO systemUserVO = null;
197
198         OQLQuery oql = db.getOQLQuery( "SELECT u FROM org.infoglue.cms.entities.management.impl.simple.SystemUserImpl u WHERE u.userName = $1 AND u.password = $2");
199         oql.bind(userName);
200         oql.bind(password);
201         
202         QueryResults results = oql.execute(Database.ReadOnly);
203         
204         if (results.hasMore())
205         {
206             SystemUser systemUser = (SystemUser)results.next();
207             systemUserVO = systemUser.getValueObject();
208             logger.info("found one:" + systemUserVO.getFirstName());
209         }
210
211         results.close();
212         oql.close();
213
214         return systemUserVO;
215     }
216     
217     public SystemUser getSystemUser(Database db, String JavaDoc userName, String JavaDoc password) throws SystemException, Exception JavaDoc
218     {
219         SystemUser systemUser = null;
220         
221         OQLQuery oql = db.getOQLQuery( "SELECT u FROM org.infoglue.cms.entities.management.impl.simple.SystemUserImpl u WHERE u.userName = $1 AND u.password = $2");
222         oql.bind(userName);
223         oql.bind(password);
224         
225         QueryResults results = oql.execute();
226         this.logger.info("Fetching entity in read/write mode" + userName);
227
228         if (results.hasMore())
229         {
230             systemUser = (SystemUser)results.next();
231             logger.info("found one:" + systemUser.getFirstName());
232         }
233
234         results.close();
235         oql.close();
236
237         return systemUser;
238     }
239     
240     public List JavaDoc getSystemUserVOList() throws SystemException, Bug
241     {
242         return getAllVOObjects(SystemUserImpl.class, "userName");
243     }
244
245     public List JavaDoc getSystemUserVOList(Database db) throws SystemException, Bug
246     {
247         return getAllVOObjects(SystemUserImpl.class, "userName", db);
248     }
249
250     
251     public List JavaDoc getFilteredSystemUserVOList(String JavaDoc firstName, String JavaDoc lastName, String JavaDoc userName, String JavaDoc email, String JavaDoc[] roleNames) throws SystemException, Bug
252     {
253         List JavaDoc filteredList = new ArrayList JavaDoc();
254         
255         Database db = CastorDatabaseService.getDatabase();
256
257         try
258         {
259             beginTransaction(db);
260                                 
261             filteredList = getFilteredSystemUserList(firstName, lastName, userName, email, roleNames, db);
262             
263             commitTransaction(db);
264         }
265         catch (Exception JavaDoc e)
266         {
267             logger.info("An error occurred so we should not complete the transaction:" + e);
268             rollbackTransaction(db);
269             throw new SystemException("An error occurred so we should not complete the transaction:" + e, e);
270         }
271         
272         return toVOList(filteredList);
273     }
274
275     public List JavaDoc getFilteredSystemUserList(String JavaDoc firstName, String JavaDoc lastName, String JavaDoc userName, String JavaDoc email, String JavaDoc[] roleNames, Database db) throws SystemException, Bug, Exception JavaDoc
276     {
277         List JavaDoc filteredList = new ArrayList JavaDoc();
278         
279         OQLQuery oql = db.getOQLQuery( "SELECT u FROM org.infoglue.cms.entities.management.impl.simple.SystemUserImpl u");
280         
281         QueryResults results = oql.execute(Database.ReadOnly);
282         
283         while (results.hasMore())
284         {
285             SystemUser extranetUser = (SystemUser)results.next();
286             boolean include = true;
287             
288             if(firstName != null && !firstName.equals("") && extranetUser.getFirstName().toLowerCase().indexOf(firstName.toLowerCase()) == -1)
289                 include = false;
290             
291             if(lastName != null && !lastName.equals("") && extranetUser.getLastName().toLowerCase().indexOf(lastName.toLowerCase()) == -1)
292                 include = false;
293             
294             if(userName != null && !userName.equals("") && extranetUser.getUserName().toLowerCase().indexOf(userName.toLowerCase()) == -1)
295                 include = false;
296             
297             if(email != null && !email.equals("") && extranetUser.getEmail().toLowerCase().indexOf(email.toLowerCase()) == -1)
298                 include = false;
299             
300             boolean hasRoles = true;
301             if(roleNames != null && roleNames.length > 0)
302             {
303                 for(int i=0; i < roleNames.length; i++)
304                 {
305                     String JavaDoc roleName = roleNames[i];
306                     if(roleName != null && !roleName.equals(""))
307                     {
308                         Collection JavaDoc roles = extranetUser.getRoles();
309                         Iterator JavaDoc rolesIterator = roles.iterator();
310                         boolean hasRole = false;
311                         while(rolesIterator.hasNext())
312                         {
313                             Role role = (Role)rolesIterator.next();
314                             if(role.getRoleName().equalsIgnoreCase(roleName))
315                             {
316                                 hasRole = true;
317                                 break;
318                             }
319                         }
320                         
321                         if(!hasRole)
322                         {
323                             hasRoles = false;
324                             break;
325                         }
326                     }
327                 }
328             }
329             
330             if(include && hasRoles)
331                 filteredList.add(extranetUser);
332         }
333         
334         results.close();
335         oql.close();
336
337         return filteredList;
338     }
339
340     /*
341      * CREATE
342      *
343      */

344     public SystemUserVO create(SystemUserVO systemUserVO) throws ConstraintException, SystemException
345     {
346         SystemUser systemUser = new SystemUserImpl();
347         systemUser.setValueObject(systemUserVO);
348         systemUser = (SystemUser) createEntity(systemUser);
349         return systemUser.getValueObject();
350     }
351
352     /*
353      * CREATE
354      *
355      */

356     public SystemUser create(SystemUserVO systemUserVO, Database db) throws ConstraintException, SystemException, Exception JavaDoc
357     {
358         SystemUser systemUser = new SystemUserImpl();
359         systemUser.setValueObject(systemUserVO);
360         systemUser = (SystemUser) createEntity(systemUser, db);
361         return systemUser;
362     }
363
364     /*
365      * DELETE
366      *
367      */

368      
369     public void delete(String JavaDoc userName) throws ConstraintException, SystemException
370     {
371         Database db = CastorDatabaseService.getDatabase();
372         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
373
374         SystemUser systemUser = null;
375
376         beginTransaction(db);
377
378         try
379         {
380             //add validation here if needed
381

382             delete(userName, db);
383             
384             //If any of the validations or setMethods reported an error, we throw them up now before create.
385
ceb.throwIfNotEmpty();
386             
387             commitTransaction(db);
388         }
389         catch(ConstraintException ce)
390         {
391             logger.warn("An error occurred so we should not completes the transaction:" + ce, ce);
392             rollbackTransaction(db);
393             throw ce;
394         }
395         catch(Exception JavaDoc e)
396         {
397             logger.error("An error occurred so we should not completes the transaction:" + e, e);
398             rollbackTransaction(db);
399             throw new SystemException(e.getMessage());
400         }
401
402     }
403
404     /*
405      * DELETE
406      *
407      */

408      
409     public void delete(String JavaDoc userName, Database db) throws ConstraintException, SystemException, Exception JavaDoc
410     {
411         SystemUser systemUser = getSystemUserWithName(userName, db);
412
413         Collection JavaDoc roles = systemUser.getRoles();
414         Iterator JavaDoc rolesIterator = roles.iterator();
415         while(rolesIterator.hasNext())
416         {
417             Role role = (Role)rolesIterator.next();
418             role.getSystemUsers().remove(systemUser);
419         }
420         
421         Collection JavaDoc groups = systemUser.getGroups();
422         Iterator JavaDoc groupsIterator = groups.iterator();
423         while(groupsIterator.hasNext())
424         {
425             Group group = (Group)groupsIterator.next();
426             group.getSystemUsers().remove(systemUser);
427         }
428         
429         db.remove(systemUser);
430     }
431
432
433     public SystemUserVO update(SystemUserVO systemUserVO) throws ConstraintException, SystemException
434     {
435         return (SystemUserVO) updateEntity(SystemUserImpl.class, (BaseEntityVO) systemUserVO);
436     }
437
438
439     public SystemUserVO update(SystemUserVO systemUserVO, String JavaDoc[] roleNames, String JavaDoc[] groupNames) throws ConstraintException, SystemException
440     {
441         Database db = CastorDatabaseService.getDatabase();
442         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
443
444         SystemUser systemUser = null;
445
446         beginTransaction(db);
447
448         try
449         {
450             systemUser = update(systemUserVO, roleNames, groupNames, db);
451             
452             commitTransaction(db);
453         }
454         catch(ConstraintException ce)
455         {
456             logger.warn("An error occurred so we should not completes the transaction:" + ce, ce);
457             rollbackTransaction(db);
458             throw ce;
459         }
460         catch(Exception JavaDoc e)
461         {
462             logger.error("An error occurred so we should not completes the transaction:" + e, e);
463             rollbackTransaction(db);
464             throw new SystemException(e.getMessage());
465         }
466
467         return systemUser.getValueObject();
468     }
469
470     public SystemUser update(SystemUserVO systemUserVO, String JavaDoc[] roleNames, String JavaDoc[] groupNames, Database db) throws ConstraintException, SystemException
471     {
472         SystemUser systemUser = systemUser = getSystemUserWithName(systemUserVO.getUserName(), db);
473         systemUser.getRoles().clear();
474         
475         if(roleNames != null)
476         {
477             for (int i=0; i < roleNames.length; i++)
478             {
479                 Role role = RoleController.getController().getRoleWithName(roleNames[i], db);
480                 systemUser.getRoles().add(role);
481                 role.getSystemUsers().add(systemUser);
482             }
483         }
484         
485         systemUser.getGroups().clear();
486         
487         if(groupNames != null)
488         {
489             for (int i=0; i < groupNames.length; i++)
490             {
491                 Group group = GroupController.getController().getGroupWithName(groupNames[i], db);
492                 systemUser.getGroups().add(group);
493                 group.getSystemUsers().add(systemUser);
494             }
495         }
496         
497         systemUserVO.setPassword(systemUser.getPassword());
498         systemUser.setValueObject(systemUserVO);
499
500         return systemUser;
501     }
502     
503     public void updatePassword(String JavaDoc userName) throws ConstraintException, SystemException
504     {
505         Database db = CastorDatabaseService.getDatabase();
506         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
507
508         beginTransaction(db);
509
510         try
511         {
512             updatePassword(userName, db);
513             
514             commitTransaction(db);
515         }
516         catch(Exception JavaDoc e)
517         {
518             logger.error("An error occurred so we should not completes the transaction:" + e, e);
519             rollbackTransaction(db);
520             throw new SystemException(e.getMessage());
521         }
522     }
523
524     public void updatePassword(String JavaDoc userName, Database db) throws ConstraintException, SystemException
525     {
526         SystemUser systemUser = getSystemUserWithName(userName, db);
527         
528         String JavaDoc newPassword = PasswordGenerator.generate();
529         
530         systemUser.setPassword(newPassword);
531         
532         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
533         sb.append("CMS notification: You or an administrator have requested a new password for your account (" + userName + "). \n");
534         sb.append("\n");
535         sb.append("The new password is '" + newPassword + "'.\n");
536         sb.append("\n");
537         sb.append("Please notify the administrator if this does not work. \n");
538         sb.append("\n");
539         sb.append("-----------------------------------------------------------------------\n");
540         sb.append("This email was automatically generated and the sender is the CMS-system. \n");
541         sb.append("Do not reply to this email. \n");
542         
543         try
544         {
545             String JavaDoc systemEmailSender = CmsPropertyHandler.getSystemEmailSender();
546             if(systemEmailSender == null || systemEmailSender.equalsIgnoreCase(""))
547                 systemEmailSender = "InfoGlueCMS@" + CmsPropertyHandler.getMailSmtpHost();
548     
549             MailServiceFactory.getService().send(systemEmailSender, systemUser.getEmail(), null, "InfoGlue Information - Password changed!!", sb.toString());
550         }
551         catch(Exception JavaDoc e)
552         {
553             logger.error("The notification was not sent. Reason:" + e.getMessage(), e);
554         }
555     }
556
557     public void updatePassword(String JavaDoc userName, String JavaDoc oldPassword, String JavaDoc newPassword) throws ConstraintException, SystemException
558     {
559         Database db = CastorDatabaseService.getDatabase();
560         ConstraintExceptionBuffer ceb = new ConstraintExceptionBuffer();
561
562         beginTransaction(db);
563
564         try
565         {
566             //If any of the validations or setMethods reported an error, we throw them up now before create.
567
ceb.throwIfNotEmpty();
568
569             updatePassword(userName, oldPassword, newPassword, db);
570             
571             commitTransaction(db);
572         }
573         catch(ConstraintException ce)
574         {
575             rollbackTransaction(db);
576             throw ce;
577         }
578         catch(Exception JavaDoc e)
579         {
580             logger.error("An error occurred so we should not completes the transaction:" + e, e);
581             rollbackTransaction(db);
582             throw new SystemException(e.getMessage());
583         }
584     }
585     
586     public void updatePassword(String JavaDoc userName, String JavaDoc oldPassword, String JavaDoc newPassword, Database db) throws ConstraintException, SystemException, Exception JavaDoc
587     {
588         if(newPassword == null)
589             throw new ConstraintException("SystemUser.newPassword", "301");
590
591         SystemUser systemUser = getSystemUser(db, userName, oldPassword);
592         if(systemUser == null)
593             throw new ConstraintException("SystemUser.oldPassword", "310");
594             
595         systemUser.setPassword(newPassword);
596     }
597
598   
599     /**
600      * This is a method that gives the user back an newly initialized ValueObject for this entity that the controller
601      * is handling.
602      */

603
604     public BaseEntityVO getNewVO()
605     {
606         return new SystemUserVO();
607     }
608
609 }
610  
611
Popular Tags