1 package org.itracker.persistence.dao;
2
3 import org.hibernate.Criteria;
4 import org.hibernate.HibernateException;
5 import org.hibernate.Query;
6 import org.hibernate.criterion.DetachedCriteria;
7 import org.hibernate.criterion.Expression;
8 import org.hibernate.criterion.Order;
9 import org.hibernate.criterion.Restrictions;
10 import org.itracker.model.Permission;
11 import org.itracker.model.PermissionType;
12 import org.itracker.model.User;
13
14 import java.util.*;
15
16
17
18
19 public class UserDAOImpl extends BaseHibernateDAOImpl<User> implements UserDAO {
20
21 public User findByPrimaryKey(Integer userId) {
22 User user;
23
24 try {
25 user = (User) getSession().get(User.class, userId);
26 } catch (HibernateException ex) {
27 throw convertHibernateAccessException(ex);
28 }
29 return user;
30 }
31
32 public User findByLogin(String login) {
33 User user;
34
35 try {
36 Query query = getSession().getNamedQuery("UserByLoginQuery");
37 query.setString("login", login);
38 user = (User) query.uniqueResult();
39 } catch (HibernateException ex) {
40 throw convertHibernateAccessException(ex);
41 }
42 return user;
43 }
44
45 @SuppressWarnings("unchecked")
46 public List<User> findAll() {
47 List<User> users;
48
49 try {
50 Query query = getSession().getNamedQuery("UsersAllQuery");
51 users = query.list();
52 } catch (HibernateException ex) {
53 throw convertHibernateAccessException(ex);
54 }
55 return users;
56 }
57
58 @SuppressWarnings("unchecked")
59 public List<User> findActive() {
60 List<User> users;
61
62 try {
63 Query query = getSession().getNamedQuery("UsersActiveQuery");
64 users = query.list();
65 } catch (HibernateException ex) {
66 throw convertHibernateAccessException(ex);
67 }
68 return users;
69 }
70
71 @SuppressWarnings("unchecked")
72 public List<User> findByStatus(int status) {
73 List<User> users;
74
75 try {
76 Query query = getSession().getNamedQuery("UsersByStatusQuery");
77 query.setInteger("userStatus", status);
78 users = query.list();
79 } catch (HibernateException ex) {
80 throw convertHibernateAccessException(ex);
81 }
82 return users;
83 }
84
85 @SuppressWarnings("unchecked")
86 public List<User> findSuperUsers() {
87 List<User> users;
88
89 try {
90 Query query = getSession().getNamedQuery("UsersSuperQuery");
91 users = query.list();
92 } catch (HibernateException ex) {
93 throw convertHibernateAccessException(ex);
94 }
95 return users;
96 }
97
98 @SuppressWarnings("unchecked")
99 public List<User> findByRegistrationType(int registrationType) {
100 List<User> users;
101
102 try {
103 Query query = getSession().getNamedQuery("UsersByRegistrationTypeQuery");
104 query.setInteger("registrationType", registrationType);
105 users = query.list();
106 } catch (HibernateException ex) {
107 throw convertHibernateAccessException(ex);
108 }
109 return users;
110 }
111
112
113
114
115
116
117
118
119
120
121 @SuppressWarnings("unchecked")
122 public Map<Integer, Set<PermissionType>> getUsersMapOfProjectsAndPermissionTypes(User user) {
123
124
125 final Map<Integer, Set<PermissionType>> permissionsByProjectId =
126 new HashMap<Integer, Set<PermissionType>>();
127
128 try {
129
130 User../../../../org/itracker/model/User.html#User">User userBean = (User) getSession().load(User.class, user.getId());
131
132 Criteria criteria = getSession().createCriteria(Permission.class);
133 criteria.add(Expression.eq("user", userBean));
134 criteria.addOrder(Order.asc("project"));
135
136 List<Permission> permissionsList = criteria.list();
137
138 for (int i = 0; i < permissionsList.size(); i++) {
139 Permission permission = permissionsList.get(i);
140
141
142 final Integer projectId = (permission.getProject() == null)
143 ? null : permission.getProject().getId();
144
145 Set<PermissionType> projectPermissions = permissionsByProjectId.get(projectId);
146
147 if (projectPermissions == null) {
148
149 projectPermissions = new HashSet<PermissionType>();
150 permissionsByProjectId.put(projectId, projectPermissions);
151 }
152
153 PermissionType permissionType = permission.getPermissionType();
154 projectPermissions.add(permissionType);
155 }
156 } catch (HibernateException ex) {
157 throw convertHibernateAccessException(ex);
158 }
159 return permissionsByProjectId;
160 }
161
162
163 @SuppressWarnings("unchecked")
164 public List<User> findUsersForProjectByAllPermissionTypeList(Integer projectID, Integer[] permissionTypes) {
165 PermissionType[] permissions = null;
166 if (null != permissionTypes) {
167 permissions = new PermissionType[permissionTypes.length];
168 int c = 0;
169 for (int p : permissionTypes) {
170 permissions[c++] = PermissionType.valueOf(p);
171 }
172 }
173 return findUsersForProjectByAllPermissionTypeList(projectID, permissions);
174 }
175
176 @Override
177 public List<User> findUsersForProjectByAllPermissionTypeList(Integer projectID, PermissionType[] permissionTypes) {
178 List<User> users = new ArrayList<User>();
179
180 try {
181
182 DetachedCriteria userCriteria = DetachedCriteria.forClass(User.class);
183 userCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
184 DetachedCriteria permissionCriteria = userCriteria.createCriteria("permissions");
185
186 permissionCriteria.add(Restrictions.in("permissionType", permissionTypes));
187 permissionCriteria.add(Restrictions.eq("project.id", projectID));
188
189 List<User> userList = userCriteria.getExecutableCriteria(getSession()).list();
190
191 for (User user : userList) {
192 if (hasAllPermissions(user.getPermissions(), permissionTypes)) {
193 users.add(user);
194 }
195 }
196
197 } catch (HibernateException ex) {
198 throw convertHibernateAccessException(ex);
199 }
200
201 return users;
202
203 }
204
205 private boolean hasAllPermissions(Collection<Permission> permissions, PermissionType[] required) {
206 if (null == required || required.length == 0) {
207 return true;
208 }
209 if (null == permissions || permissions.isEmpty()) {
210 return false;
211 }
212
213 Collection<PermissionType> requiredPermissions = Arrays.asList(required);
214 Collection<PermissionType> userPermissionTypes = new HashSet<>(permissions.size());
215
216 Iterator<Permission> permsIt = permissions.iterator();
217 while (permsIt.hasNext()) {
218 userPermissionTypes.add(permsIt.next().getPermissionType());
219 }
220
221 return userPermissionTypes.containsAll(requiredPermissions);
222 }
223
224 }