1 package org.smartcomps.twister.util.user.priv.dao;
2
3 import net.sf.hibernate.Hibernate;
4 import net.sf.hibernate.HibernateException;
5 import net.sf.hibernate.Query;
6 import net.sf.hibernate.Session;
7 import org.smartcomps.twister.common.persistence.DBSessionException;
8 import org.smartcomps.twister.common.persistence.DBSessionManager;
9 import org.smartcomps.twister.common.persistence.FinderException;
10 import org.smartcomps.twister.common.persistence.PersistentDataAccess;
11 import org.smartcomps.twister.util.user.priv.RoleImpl;
12 import org.smartcomps.twister.util.user.priv.UserImpl;
13
14 import java.util.Collection;
15 import java.util.List;
16
17 public class UserDAO extends PersistentDataAccess {
18 private static String FIND_ROLES_BY_USERNAME =
19 "FROM org.smartcomps.twister.util.user.priv.RoleImpl as role " +
20 "WHERE role.username = ?";
21
22 private static String FIND_ROLES_BY_ROLENAMES =
23 "FROM org.smartcomps.twister.util.user.priv.RoleImpl as role " +
24 "WHERE role.rolename in (:roleNames)";
25
26 private static String FIND_ROLE_BY_ROLENAME =
27 "FROM org.smartcomps.twister.util.user.priv.RoleImpl as role " +
28 "WHERE role.rolename = ?";
29
30 private static String FIND_USERS_IN_ROLE =
31 "SELECT user " +
32 "FROM org.smartcomps.twister.util.user.priv.UserImpl as user, user.roles as role " +
33 "WHERE role.rolename = ?";
34
35 public static Collection findRolesByNames(List names) throws DBSessionException, FinderException {
36 Session session = DBSessionManager.getActiveSession();
37
38 List roles = null;
39 try {
40 Query byProperties = session.createQuery(FIND_ROLES_BY_ROLENAMES);
41
42 byProperties.setParameterList("roleNames", names);
43
44 roles = byProperties.list();
45 } catch (net.sf.hibernate.HibernateException e) {
46 throw new DBSessionException(e);
47 }
48
49 return roles;
50 }
51
52 public static Collection findRolesByUsername(String username) throws DBSessionException {
53 Session session = DBSessionManager.getActiveSession();
54
55 List results = null;
56 try {
57 results = session.find(FIND_ROLES_BY_USERNAME, username, Hibernate.STRING);
58 } catch (HibernateException e) {
59 throw new DBSessionException(e);
60 }
61 return results;
62 }
63
64 public static List findAllUsers() throws DBSessionException {
65 return findAll(UserImpl.class);
66 }
67
68 public static List findAllRoles() throws DBSessionException {
69 return findAll(RoleImpl.class);
70 }
71
72 public static RoleImpl findRoleByRolename(String rolename) throws DBSessionException, FinderException {
73 Session session = DBSessionManager.getActiveSession();
74
75 List results = null;
76 try {
77 results = session.find(FIND_ROLE_BY_ROLENAME, rolename, Hibernate.STRING);
78 } catch (HibernateException e) {
79 throw new DBSessionException(e);
80 }
81 if (results.size() == 0) {
82 throw new FinderException("No role exists for rolename : " + rolename);
83 }
84 return (RoleImpl) results.get(0);
85
86 }
87
88 public static Collection findUsersInRole(String rolename) throws DBSessionException, FinderException {
89 Session session = DBSessionManager.getActiveSession();
90
91 List results = null;
92 try {
93 results = session.find(FIND_USERS_IN_ROLE, rolename, Hibernate.STRING);
94 } catch (HibernateException e) {
95 throw new DBSessionException(e);
96 }
97 if (results.size() == 0) {
98 throw new FinderException("No User exists for rolename : " + rolename);
99 }
100 return results;
101
102 }
103 }
This page was automatically generated by Maven