KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > impl > role > RoleModuleImpl


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.role;
10
11 import org.apache.log4j.Logger;
12 import org.jboss.portal.common.util.Tools;
13 import org.jboss.portal.core.impl.user.UserImpl;
14 import org.jboss.portal.core.model.Role;
15 import org.jboss.portal.core.modules.AbstractModule;
16 import org.jboss.portal.core.modules.ModuleException;
17 import org.jboss.portal.core.modules.RoleModule;
18 import org.hibernate.Session;
19 import org.hibernate.Query;
20 import org.hibernate.HibernateException;
21 import org.hibernate.SessionFactory;
22
23 import javax.naming.InitialContext JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.HashSet JavaDoc;
28
29 /**
30  * @author <a HREF="mailto:julien@jboss.org">Julien Viet </a>
31  * @author <a HREF="mailto:theute@jboss.org">Thomas Heute </a>
32  * @author Roy Russo : roy at jboss dot org
33  * @version $Revision: 1.4 $
34  * @jmx.mbean
35  * @jboss.xmbean
36  * @portal.core
37  */

38 public class RoleModuleImpl
39       extends AbstractModule
40       implements RoleModule
41 {
42
43    private final Logger log = Logger.getLogger(getClass());
44
45    public RoleModuleImpl()
46    {
47    }
48
49    public Role findRoleByName(String JavaDoc name) throws ModuleException
50    {
51       if(name != null)
52       {
53          try
54          {
55             Session session = getSession();
56             Query query = session.createQuery("from RoleImpl as g where g.name=?");
57             query.setString(0, name);
58             RoleImpl role = (RoleImpl) query.uniqueResult();
59             if(role == null)
60             {
61                throw new ModuleException("No such role " + name);
62             }
63             return role;
64          }
65          catch(HibernateException e)
66          {
67             String JavaDoc message = "Cannot find role by name " + name;
68             log.error(message, e);
69             throw new ModuleException(message, e);
70          }
71       }
72       else
73       {
74          throw new IllegalArgumentException JavaDoc("name cannot be null");
75       }
76    }
77
78    public Set JavaDoc findRolesByNames(String JavaDoc[] names) throws ModuleException
79    {
80       if(names != null)
81       {
82          try
83          {
84             Session session = getSession();
85             StringBuffer JavaDoc queryString = new StringBuffer JavaDoc("from RoleImpl as g where g.name=?");
86             for(int i = 1; i < names.length; i++)
87             {
88                queryString.append(" or g.name=?");
89             }
90             Query query = session.createQuery(queryString.toString());
91             for(int i = 0; i < names.length; i++)
92             {
93                query.setString(i, names[i]);
94             }
95             Iterator JavaDoc iterator = query.iterate();
96             Set JavaDoc result = Tools.toSet(iterator);
97             return result;
98          }
99          catch(HibernateException e)
100          {
101             String JavaDoc message = "Cannot find roles";
102             log.error(message, e);
103             throw new ModuleException(message, e);
104          }
105       }
106       else
107       {
108          throw new IllegalArgumentException JavaDoc("name cannot be null");
109       }
110    }
111
112    public Role findRoleByDisplayName(String JavaDoc displayName) throws ModuleException
113    {
114       if(displayName != null)
115       {
116          try
117          {
118             Session session = getSession();
119             Query query = session.createQuery("from RoleImpl as g where g.displayName=?");
120             query.setString(0, displayName);
121             RoleImpl role = (RoleImpl) query.uniqueResult();
122             if(role == null)
123             {
124                throw new ModuleException("No such role " + displayName);
125             }
126             return role;
127          }
128          catch(HibernateException e)
129          {
130             String JavaDoc message = "Cannot find role by display name " + displayName;
131             log.error(message, e);
132             throw new ModuleException(message, e);
133          }
134       }
135       else
136       {
137          throw new IllegalArgumentException JavaDoc("displayname cannot be null");
138       }
139    }
140
141    public Role findRoleByID(Integer JavaDoc id) throws ModuleException
142    {
143       if(id != null)
144       {
145          try
146          {
147             Session session = getSession();
148             RoleImpl role = (RoleImpl) session.get(RoleImpl.class, id);
149             if(role == null)
150             {
151                throw new ModuleException("No role found for " + id);
152             }
153             return role;
154          }
155          catch(HibernateException e)
156          {
157             String JavaDoc message = "Cannot find role by id " + id;
158             log.error(message, e);
159             throw new ModuleException(message, e);
160          }
161       }
162       else
163       {
164          throw new IllegalArgumentException JavaDoc("id cannot be null");
165       }
166    }
167
168    public Role createRole(String JavaDoc name, String JavaDoc displayName) throws ModuleException
169    {
170       if(name != null)
171       {
172          try
173          {
174             RoleImpl role = new RoleImpl(name, displayName);
175             Session session = getSession();
176             session.save(role);
177             return role;
178          }
179          catch(HibernateException e)
180          {
181             String JavaDoc message = "Cannot create role " + name;
182             log.error(message, e);
183             throw new ModuleException(message, e);
184          }
185       }
186       else
187       {
188          throw new IllegalArgumentException JavaDoc("name cannot be null");
189       }
190    }
191
192    public void removeRole(Integer JavaDoc id) throws ModuleException
193    {
194       if(id != null)
195       {
196          try
197          {
198             Session session = getSession();
199 // int num = session.delete("from RoleImpl as g where g.ID=" + id.toString());
200
// if (num == 0)
201
RoleImpl role = (RoleImpl) session.load(RoleImpl.class, id);
202             // User manages the relation.
203
Iterator JavaDoc users = role.getUsers().iterator();
204             while(users.hasNext())
205             {
206                UserImpl user = (UserImpl) users.next();
207                user.getRoles().remove(role);
208             }
209             session.delete(role);
210             session.flush();
211          }
212          catch(HibernateException e)
213          {
214             String JavaDoc message = "Cannot remove role " + id;
215             log.error(message, e);
216             throw new ModuleException(message, e);
217          }
218       }
219       else
220       {
221          throw new IllegalArgumentException JavaDoc("id cannot be null");
222       }
223    }
224
225    public int getRolesCount() throws ModuleException
226    {
227       try
228       {
229          Session session = getSession();
230          Query query = session.createQuery("select count(g.ID) from RoleImpl as g");
231          return ((Integer JavaDoc) query.uniqueResult()).intValue();
232       }
233       catch(HibernateException e)
234       {
235          String JavaDoc message = "Cannot count roles";
236          log.error(message, e);
237          throw new ModuleException(message, e);
238       }
239    }
240
241    public Set JavaDoc findRoles() throws ModuleException
242    {
243       try
244       {
245          Session session = getSession();
246          Query query = session.createQuery("from RoleImpl");
247          Iterator JavaDoc iterator = query.iterate();
248          Set JavaDoc result = Tools.toSet(iterator);
249          return result;
250       }
251       catch(HibernateException e)
252       {
253          String JavaDoc message = "Cannot find roles";
254          log.error(message, e);
255          throw new ModuleException(message, e);
256       }
257    }
258
259    public Set JavaDoc findRoleMembers(String JavaDoc roleName, int offset, int limit, String JavaDoc userNameFilter) throws ModuleException
260    {
261       if(roleName != null)
262       {
263          try
264          {
265             Session session = getSession();
266
267             UserImpl userimpl = new UserImpl();
268             userimpl.setEnabled(true);
269
270             Query query = null;
271             StringBuffer JavaDoc sQuery = new StringBuffer JavaDoc();
272             sQuery.append("from UserImpl as user left join user.roles role where role.name=:name");
273             if(userNameFilter.trim().length() != 0)
274             {
275                sQuery.append(" AND user.userName LIKE '%':filter'%'");
276                query = session.createQuery(sQuery.toString());
277                query.setString("filter", userNameFilter);
278             }
279             else
280             {
281                query = session.createQuery(sQuery.toString());
282             }
283             query.setString("name", roleName);
284             query.setFirstResult(offset);
285             query.setMaxResults(limit);
286
287             Iterator JavaDoc iterator = query.iterate();
288             Set JavaDoc result = Tools.toSet(iterator);
289
290             Set JavaDoc newResult = new HashSet JavaDoc();
291             Iterator JavaDoc cleaner = result.iterator();
292             while(cleaner.hasNext())
293             {
294                Object JavaDoc[] oArr = (Object JavaDoc[]) cleaner.next();
295                newResult.add(oArr[0]);
296             }
297
298             return newResult;
299          }
300          catch(HibernateException e)
301          {
302             String JavaDoc message = "Cannot find role " + roleName;
303             log.error(message, e);
304             throw new ModuleException(message, e);
305          }
306       }
307       else
308       {
309          throw new IllegalArgumentException JavaDoc("id cannot be null");
310       }
311    }
312
313    /**
314     * Return the hibernate session. Testcase can overload this method to provide
315     * their own session.
316     */

317    protected Session getSession()
318    {
319       try
320       {
321          SessionFactory sf = (SessionFactory)new InitialContext JavaDoc().lookup("java:portal/SessionFactory");
322          Session session = sf.getCurrentSession();
323          return session;
324       }
325       catch (NamingException JavaDoc e)
326       {
327          e.printStackTrace(); // FIXME
328
throw new RuntimeException JavaDoc("Cannot get session");
329       }
330    }
331 }
Popular Tags