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