KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > impl > user > UserModuleImpl


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.impl.user;
10
11 import org.apache.log4j.Logger;
12 import org.jboss.portal.common.util.Tools;
13 import org.jboss.portal.core.model.User;
14 import org.jboss.portal.core.modules.AbstractModule;
15 import org.jboss.portal.core.modules.ModuleException;
16 import org.jboss.portal.core.modules.UserModule;
17 import org.hibernate.Session;
18 import org.hibernate.Query;
19 import org.hibernate.HibernateException;
20 import org.hibernate.SessionFactory;
21
22 import javax.naming.InitialContext JavaDoc;
23 import javax.naming.NamingException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26
27 /**
28  * @author <a HREF="mailto:julien@jboss.org">Julien Viet </a>
29  * @version $Revision: 1.4 $
30  * @jmx.mbean
31  * @jboss.xmbean
32  * @portal.core
33  */

34 public class UserModuleImpl
35       extends AbstractModule
36       implements UserModule
37 {
38
39    private final Logger log = Logger.getLogger(getClass());
40
41    public UserModuleImpl()
42    {
43    }
44
45    public User findUserByUserName(String JavaDoc userName) throws ModuleException
46    {
47       if(userName != null)
48       {
49          try
50          {
51             Session session = getSession();
52             Query query = session.createQuery("from UserImpl as u where u.userName=:userName");
53             query.setString("userName", userName);
54             UserImpl user = (UserImpl) query.uniqueResult();
55             if(user == null)
56             {
57                throw new ModuleException("No such user " + userName);
58             }
59             return user;
60          }
61          catch(HibernateException e)
62          {
63             String JavaDoc message = "Cannot find user by name " + userName;
64             log.error(message, e);
65             throw new ModuleException(message, e);
66          }
67       }
68       else
69       {
70          throw new IllegalArgumentException JavaDoc("user name cannot be null");
71       }
72    }
73
74    public User findUserByID(Integer JavaDoc id) throws ModuleException
75    {
76       if(id != null)
77       {
78          try
79          {
80             Session session = getSession();
81             UserImpl user = (UserImpl) session.get(UserImpl.class, id);
82             if(user == null)
83             {
84                throw new ModuleException("No user found for " + id);
85             }
86             return user;
87          }
88          catch(HibernateException e)
89          {
90             String JavaDoc message = "Cannot find user by id " + id;
91             log.error(message, e);
92             throw new ModuleException(message, e);
93          }
94       }
95       else
96       {
97          throw new IllegalArgumentException JavaDoc("id cannot be null");
98       }
99    }
100
101    public User createUser(String JavaDoc name, String JavaDoc password, String JavaDoc realEmail) throws ModuleException
102    {
103       if(name != null)
104       {
105          try
106          {
107             UserImpl user = new UserImpl(name);
108             user.setPassword(password);
109             user.setRealEmail(realEmail);
110             Session session = getSession();
111             session.save(user);
112             return user;
113          }
114          catch(HibernateException e)
115          {
116             String JavaDoc message = "Cannot create user " + name;
117             log.error(message, e);
118             throw new ModuleException(message, e);
119          }
120       }
121       else
122       {
123          throw new IllegalArgumentException JavaDoc("name cannot be null");
124       }
125    }
126
127    public void removeUser(Integer JavaDoc id) throws ModuleException
128    {
129       if(id != null)
130       {
131          try
132          {
133             Session session = getSession();
134             int num = ((org.hibernate.classic.Session)session).delete("from UserImpl as u where u.ID=" + id.toString());
135             if(num == 0)
136             {
137                throw new ModuleException("No such user " + id);
138             }
139          }
140          catch(HibernateException e)
141          {
142             String JavaDoc message = "Cannot remove user " + id;
143             log.error(message, e);
144             throw new ModuleException(message, e);
145          }
146       }
147       else
148       {
149          throw new IllegalArgumentException JavaDoc("id cannot be null");
150       }
151    }
152
153    public Set JavaDoc findUsers(int offset, int limit) throws ModuleException
154    {
155       try
156       {
157          Session session = getSession();
158          Query query = session.createQuery("from UserImpl");
159          query.setFirstResult(offset);
160          query.setMaxResults(limit);
161          Iterator JavaDoc iterator = query.iterate();
162          Set JavaDoc result = Tools.toSet(iterator);
163          return result;
164       }
165       catch(HibernateException e)
166       {
167          String JavaDoc message = "Cannot find user range [" + offset + "," + limit + "]";
168          log.error(message, e);
169          throw new ModuleException(message, e);
170       }
171    }
172
173    public Set JavaDoc findUsersFilteredByUsername(String JavaDoc filter, int offset, int limit) throws ModuleException
174    {
175       try
176       {
177          Session session = getSession();
178          Query query = session.createQuery("from UserImpl as u where u.userName LIKE '%':filter'%'");
179          query.setString("filter", filter);
180          query.setFirstResult(offset);
181          query.setMaxResults(limit);
182          Iterator JavaDoc iterator = query.iterate();
183          Set JavaDoc result = Tools.toSet(iterator);
184          return result;
185       }
186       catch(HibernateException e)
187       {
188          String JavaDoc message = "Cannot find user range [" + offset + "," + limit + "]";
189          log.error(message, e);
190          throw new ModuleException(message, e);
191       }
192    }
193
194    public int getUserCount() throws ModuleException
195    {
196       try
197       {
198          Session session = getSession();
199          Query query = session.createQuery("select count(u.ID) from UserImpl as u");
200          return ((Integer JavaDoc) query.uniqueResult()).intValue();
201       }
202       catch(HibernateException e)
203       {
204          String JavaDoc message = "Cannot count users";
205          log.error(message, e);
206          throw new ModuleException(message, e);
207       }
208    }
209
210    /**
211     * Return the hibernate session. Testcase can overload this method to provide
212     * their own session.
213     */

214    protected Session getSession()
215    {
216       try
217       {
218          SessionFactory sf = (SessionFactory)new InitialContext JavaDoc().lookup("java:portal/SessionFactory");
219          Session session = sf.getCurrentSession();
220          return session;
221       }
222       catch (NamingException JavaDoc e)
223       {
224          e.printStackTrace(); // FIXME
225
throw new RuntimeException JavaDoc("Cannot get session");
226       }
227    }
228 }
Popular Tags