Rev 1414 | Rev 1524 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | fvelez | 1 | package pt.estgp.estgweb.domain.dao.impl; |
2 | |||
3 | import jomm.dao.DaoException; |
||
395 | jmachado | 4 | import jomm.utils.DesUtils; |
344 | jmachado | 5 | import org.apache.log4j.Logger; |
113 | fvelez | 6 | import org.hibernate.Criteria; |
344 | jmachado | 7 | import org.hibernate.HibernateException; |
113 | fvelez | 8 | import org.hibernate.criterion.Criterion; |
186 | fvelez | 9 | import org.hibernate.criterion.Order; |
1496 | jmachado | 10 | import pt.estgp.estgweb.domain.Teacher; |
1 | fvelez | 11 | import pt.estgp.estgweb.domain.User; |
215 | jmachado | 12 | import pt.estgp.estgweb.domain.dao.DaoUtils; |
13 | import pt.estgp.estgweb.services.common.SearchTypeEnum; |
||
1414 | jmachado | 14 | import pt.estgp.estgweb.utils.RoleManager; |
1 | fvelez | 15 | |
344 | jmachado | 16 | import java.io.Serializable; |
1136 | jmachado | 17 | import java.util.ArrayList; |
344 | jmachado | 18 | import java.util.Date; |
1200 | jmachado | 19 | import java.util.Iterator; |
344 | jmachado | 20 | import java.util.List; |
21 | |||
684 | jmachado | 22 | import static org.hibernate.criterion.Restrictions.*; |
23 | |||
1 | fvelez | 24 | /** |
25 | * @author Jorge Machado |
||
26 | * @date 28/Fev/2008 |
||
27 | * @time 2:51:06 |
||
28 | * @see pt.estgp.estgweb.domain.dao.impl |
||
29 | */ |
||
215 | jmachado | 30 | public class UserDaoImpl extends UserDao |
1 | fvelez | 31 | { |
179 | fvelez | 32 | |
33 | private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(UserDaoImpl.class); |
||
34 | |||
1 | fvelez | 35 | public static UserDaoImpl getInstance() |
36 | { |
||
37 | if (myInstance == null) |
||
38 | myInstance = new UserDaoImpl(); |
||
39 | return (UserDaoImpl) myInstance; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Used by the base DAO classes but here for your modification Load object |
||
44 | * matching the given key and return it. |
||
45 | * |
||
46 | * @param username to load |
||
47 | * @return a user |
||
48 | */ |
||
49 | public final User loadByUsername(1.5.0/docs/api/java/lang/String.html">String username) |
||
50 | { |
||
51 | try |
||
52 | { |
||
53 | 5+0%2Fdocs%2Fapi+List">List list = |
||
54 | createCriteria() |
||
344 | jmachado | 55 | .add(eq("username", username)) |
1 | fvelez | 56 | .setMaxResults(1) |
57 | .list(); |
||
58 | |||
344 | jmachado | 59 | if (list.size() == 0) |
1 | fvelez | 60 | return null; |
61 | return (User) list.get(0); |
||
62 | } |
||
63 | catch (HibernateException e) |
||
64 | { |
||
65 | throw new DaoException(e); |
||
66 | } |
||
67 | } |
||
344 | jmachado | 68 | |
1 | fvelez | 69 | /** |
70 | * Used by the base DAO classes but here for your modification Load object |
||
71 | * matching the given key and return it. |
||
72 | * |
||
73 | * @param email to load |
||
74 | * @return a user |
||
75 | */ |
||
76 | public final User loadByEmail(1.5.0/docs/api/java/lang/String.html">String email) |
||
77 | { |
||
78 | try |
||
79 | { |
||
80 | return (User) createCriteria() |
||
344 | jmachado | 81 | .add(eq("email", email)) |
1 | fvelez | 82 | .uniqueResult(); |
83 | } |
||
84 | catch (HibernateException e) |
||
85 | { |
||
86 | throw new DaoException(e); |
||
87 | } |
||
88 | } |
||
89 | |||
113 | fvelez | 90 | /** |
44 | fvelez | 91 | * Used by the base DAO classes but here for your modification Load object |
92 | * matching the given key and return it. |
||
93 | * |
||
94 | * @param ID to load |
||
95 | * @return a user |
||
96 | */ |
||
113 | fvelez | 97 | public final User loadByID(1.5.0/docs/api/java/lang/String.html">String ID) |
38 | fvelez | 98 | { |
99 | try |
||
100 | { |
||
101 | return (User) createCriteria() |
||
344 | jmachado | 102 | .add(eq("id", ID)) |
38 | fvelez | 103 | .uniqueResult(); |
104 | } |
||
105 | catch (HibernateException e) |
||
106 | { |
||
107 | throw new DaoException(e); |
||
108 | } |
||
109 | } |
||
110 | |||
1 | fvelez | 111 | /** |
112 | * Used by the base DAO classes but here for your modification Load object |
||
113 | * matching the given key and return it. |
||
114 | * |
||
115 | * @param username to load |
||
116 | * @param password to load |
||
117 | * @return a user |
||
118 | */ |
||
119 | public final User loadByUsernameAndPassword(1.5.0/docs/api/java/lang/String.html">String username, 1.5.0/docs/api/java/lang/String.html">String password) |
||
120 | { |
||
121 | try |
||
122 | { |
||
123 | return (User) createCriteria() |
||
344 | jmachado | 124 | .add(eq("username", username)) |
395 | jmachado | 125 | .add(or( |
126 | eq("password", jomm.utils.BytesUtils.getDigestMD5Hex(password)), |
||
127 | eq("pop3password",DesUtils.getInstance().encrypt(password)) |
||
128 | ) |
||
129 | ) |
||
1 | fvelez | 130 | .uniqueResult(); |
131 | } |
||
132 | catch (HibernateException e) |
||
133 | { |
||
134 | throw new DaoException(e); |
||
135 | } |
||
136 | } |
||
137 | |||
1414 | jmachado | 138 | public List<User> loadRoleUsers(1.5.0/docs/api/java/lang/String.html">String role, int maxResults, int firstResult) |
139 | { |
||
140 | Criteria c = createCriteria() |
||
141 | .add(like("roles","%" + role + RoleManager.ROLE_SEPARATOR +"%")) |
||
142 | .addOrder(Order.asc("name")) |
||
143 | .setMaxResults(maxResults) |
||
144 | .setFirstResult(firstResult); |
||
145 | return c.list(); |
||
146 | } |
||
147 | |||
344 | jmachado | 148 | public List<User> loadRoleUsers(1.5.0/docs/api/java/lang/String.html">String role) |
215 | jmachado | 149 | { |
1136 | jmachado | 150 | if(role == null || role.trim().length() == 0) |
151 | return new ArrayList<User>(); |
||
1200 | jmachado | 152 | List<User> users = createCriteria().add(like("roles", "%" + role + "%")).addOrder(Order.asc("name")).list(); |
153 | Iterator<User> iter = users.iterator(); |
||
154 | while(iter.hasNext()) |
||
155 | { |
||
156 | User u = iter.next(); |
||
157 | if(!u.hasRole(role)) |
||
158 | iter.remove(); |
||
159 | } |
||
1201 | jmachado | 160 | return users; |
215 | jmachado | 161 | } |
162 | |||
1496 | jmachado | 163 | public List<Teacher> loadRoleTeachers(1.5.0/docs/api/java/lang/String.html">String role) |
164 | { |
||
165 | if(role == null || role.trim().length() == 0) |
||
166 | return new ArrayList<Teacher>(); |
||
167 | List<Teacher> users = createCriteria(Teacher.class).add(like("roles", "%" + role + ",%")).addOrder(Order.asc("name")).list(); |
||
168 | return users; |
||
169 | } |
||
170 | |||
171 | |||
172 | |||
387 | jmachado | 173 | public List<User> loadRoleAreaUsers(1.5.0/docs/api/java/lang/String.html">String role, 1.5.0/docs/api/java/lang/String.html">String area) |
174 | { |
||
175 | Criteria c = createCriteria().add(like("roles", "%" + role + "%")).addOrder(Order.asc("name")); |
||
176 | if(area != null && area.trim().length() > 0) |
||
177 | c.add(eq("mainArea",area)); |
||
1202 | jmachado | 178 | List<User> users = c.list(); |
179 | Iterator<User> iter = users.iterator(); |
||
180 | while(iter.hasNext()) |
||
181 | { |
||
182 | User u = iter.next(); |
||
183 | if(!u.hasRole(role)) |
||
184 | iter.remove(); |
||
185 | } |
||
186 | return users; |
||
387 | jmachado | 187 | } |
188 | |||
344 | jmachado | 189 | public List<User> findUsers(1.5.0/docs/api/java/lang/String.html">String textToSearch) |
113 | fvelez | 190 | { |
344 | jmachado | 191 | return findUsers(textToSearch, null); |
179 | fvelez | 192 | } |
193 | |||
344 | jmachado | 194 | public List<User> findUsers(1.5.0/docs/api/java/lang/String.html">String textToSearch, 1.5.0/docs/api/java/lang/String.html">String typeToSearch) |
179 | fvelez | 195 | { |
344 | jmachado | 196 | Criteria criteria; |
197 | if (typeToSearch != null && typeToSearch.trim().length() != 0) |
||
179 | fvelez | 198 | try |
199 | { |
||
200 | criteria = createCriteria(1.5.0/docs/api/java/lang/Class.html">Class.forName(typeToSearch)); |
||
201 | } |
||
202 | catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e) |
||
203 | { |
||
344 | jmachado | 204 | logger.error(e, e); |
205 | return null; |
||
179 | fvelez | 206 | } |
207 | else |
||
344 | jmachado | 208 | criteria = createCriteria(); |
721 | jmachado | 209 | Criterion textSearch = DaoUtils.createSearchQuery(textToSearch, SearchTypeEnum.AllWords, "name", "username"); |
373 | jmachado | 210 | Criterion text2Search = DaoUtils.createSearchQuery(textToSearch, SearchTypeEnum.AllWords, "email"); |
720 | jmachado | 211 | Criterion fields = or(textSearch, text2Search); |
721 | jmachado | 212 | try{ |
213 | 1.5.0/docs/api/java/lang/Integer.html">Integer siges = 1.5.0/docs/api/java/lang/Integer.html">Integer.parseInt(textToSearch); |
||
214 | fields = or(fields,eq("sigesCode",siges)); |
||
215 | }catch(1.5.0/docs/api/java/lang/Exception.html">Exception e){} |
||
216 | |||
217 | |||
344 | jmachado | 218 | return criteria.add(fields) |
219 | .addOrder(Order.desc("name")) |
||
220 | .list(); |
||
113 | fvelez | 221 | } |
222 | |||
223 | |||
45 | fvelez | 224 | public 1.5.0/docs/api/java/io/Serializable.html">Serializable save(User obj) |
225 | { |
||
344 | jmachado | 226 | try |
227 | { |
||
45 | fvelez | 228 | obj.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
229 | return super.save(obj); |
||
230 | } |
||
344 | jmachado | 231 | catch (HibernateException e) |
232 | { |
||
45 | fvelez | 233 | throw new DaoException(e); |
234 | } |
||
235 | |||
236 | } |
||
237 | |||
215 | jmachado | 238 | public int countUsers(1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchType) |
239 | { |
||
344 | jmachado | 240 | Criterion c = DaoUtils.createSearchQuery(query, searchType, "name", "username"); |
215 | jmachado | 241 | return createCriteria() |
242 | .add(c) |
||
243 | .list().size(); |
||
244 | } |
||
245 | |||
246 | |||
247 | public List<User> search(1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchTypeEnum, int maxResults, int page) |
||
248 | { |
||
249 | Criterion c = DaoUtils.createSearchQuery(query, searchTypeEnum, "name", "username"); |
||
250 | Criteria criteria = createCriteria(); |
||
251 | criteria.add(c) |
||
252 | .addOrder(Order.asc("name")) |
||
253 | .setMaxResults(maxResults) |
||
254 | .setFirstResult(page * maxResults); |
||
255 | return criteria.list(); |
||
256 | } |
||
381 | jmachado | 257 | |
258 | public List<User> findByEmail(1.5.0/docs/api/java/lang/String.html">String email) |
||
259 | { |
||
260 | return createCriteria().add(or(or(or(eq("email",email),eq("outEmail",email)),eq("gmail",email)),eq("msn",email))).list(); |
||
261 | } |
||
411 | jmachado | 262 | |
1312 | jmachado | 263 | public User loadByUsernameOrSigesCode(int sigesCode) |
411 | jmachado | 264 | { |
265 | return (User) createCriteria().add(or(eq("sigesCode",sigesCode),eq("username","a" + sigesCode))).uniqueResult(); |
||
266 | } |
||
1312 | jmachado | 267 | |
268 | public User loadBySigesCode(int sigesCode) |
||
269 | { |
||
270 | return (User) createCriteria().add(eq("sigesCode", sigesCode)).uniqueResult(); |
||
271 | } |
||
272 | |||
1317 | jmachado | 273 | public List<User> loadByCertificate(1.5.0/docs/api/java/lang/String.html">String certificate) |
274 | { |
||
275 | return createCriteria() |
||
276 | .add(eq("authenticationCertificate", certificate)) |
||
277 | .add(gt("authenticationCertificateExpire",new 5+0%2Fdocs%2Fapi+Date">Date())) |
||
278 | .list(); |
||
279 | } |
||
280 | |||
684 | jmachado | 281 | |
282 | // Duarte Santos |
||
671 | jmachado | 283 | public List<User> loadRolesUsers(List<String> roles) |
284 | { |
||
1136 | jmachado | 285 | if(roles == null || roles.size() == 0) |
286 | return new ArrayList<User>(); |
||
671 | jmachado | 287 | Criteria criteria = createCriteria(); |
288 | Criterion co = null; |
||
289 | for (1.5.0/docs/api/java/lang/String.html">String role : roles) |
||
290 | { |
||
1136 | jmachado | 291 | if(role.trim().length() == 0) |
292 | continue; |
||
671 | jmachado | 293 | Criterion cor = like("roles", "%" + role + "%"); |
294 | if (co == null) |
||
295 | co = cor; |
||
296 | else |
||
297 | co = or(co, cor); |
||
298 | } |
||
299 | criteria.add(co); |
||
1202 | jmachado | 300 | List<User> users = criteria.list(); |
301 | Iterator<User> iter = users.iterator(); |
||
302 | while(iter.hasNext()) |
||
303 | { |
||
304 | boolean hasIt = false; |
||
305 | User u = iter.next(); |
||
306 | for(1.5.0/docs/api/java/lang/String.html">String role: roles) |
||
307 | { |
||
308 | if(u.hasRole(role)) |
||
309 | { |
||
310 | hasIt = true; |
||
311 | break; |
||
312 | } |
||
313 | } |
||
314 | if(!hasIt) |
||
315 | iter.remove(); |
||
316 | } |
||
317 | return users; |
||
671 | jmachado | 318 | } |
1 | fvelez | 319 | } |