Rev 45 | Rev 179 | 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; |
||
4 | import org.hibernate.HibernateException; |
||
5 | import org.hibernate.Query; |
||
6 | import org.hibernate.Hibernate; |
||
113 | fvelez | 7 | import org.hibernate.Criteria; |
8 | import static org.hibernate.criterion.Restrictions.*; |
||
9 | import org.hibernate.criterion.Criterion; |
||
10 | import org.hibernate.criterion.SimpleExpression; |
||
1 | fvelez | 11 | |
12 | |||
13 | import java.util.List; |
||
45 | fvelez | 14 | import java.util.Date; |
15 | import java.io.Serializable; |
||
1 | fvelez | 16 | |
17 | import pt.estgp.estgweb.domain.User; |
||
45 | fvelez | 18 | import pt.estgp.estgweb.domain.Announcement; |
1 | fvelez | 19 | |
20 | /** |
||
21 | * @author Jorge Machado |
||
22 | * @date 28/Fev/2008 |
||
23 | * @time 2:51:06 |
||
24 | * @see pt.estgp.estgweb.domain.dao.impl |
||
25 | */ |
||
45 | fvelez | 26 | public class UserDaoImpl<Announcement> extends UserDao |
1 | fvelez | 27 | { |
28 | public static UserDaoImpl getInstance() |
||
29 | { |
||
30 | if (myInstance == null) |
||
31 | myInstance = new UserDaoImpl(); |
||
32 | return (UserDaoImpl) myInstance; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Used by the base DAO classes but here for your modification Load object |
||
37 | * matching the given key and return it. |
||
38 | * |
||
39 | * @param username to load |
||
40 | * @return a user |
||
41 | */ |
||
42 | public final User loadByUsername(1.5.0/docs/api/java/lang/String.html">String username) |
||
43 | { |
||
44 | try |
||
45 | { |
||
46 | 5+0%2Fdocs%2Fapi+List">List list = |
||
47 | createCriteria() |
||
48 | .add(eq("username",username)) |
||
49 | .setMaxResults(1) |
||
50 | .list(); |
||
51 | |||
52 | if(list.size() == 0) |
||
53 | return null; |
||
54 | return (User) list.get(0); |
||
55 | } |
||
56 | catch (HibernateException e) |
||
57 | { |
||
58 | throw new DaoException(e); |
||
59 | } |
||
60 | } |
||
61 | /** |
||
62 | * Used by the base DAO classes but here for your modification Load object |
||
63 | * matching the given key and return it. |
||
64 | * |
||
65 | * @param email to load |
||
66 | * @return a user |
||
67 | */ |
||
68 | public final User loadByEmail(1.5.0/docs/api/java/lang/String.html">String email) |
||
69 | { |
||
70 | try |
||
71 | { |
||
72 | return (User) createCriteria() |
||
73 | .add(eq("email",email)) |
||
74 | .uniqueResult(); |
||
75 | } |
||
76 | catch (HibernateException e) |
||
77 | { |
||
78 | throw new DaoException(e); |
||
79 | } |
||
80 | } |
||
81 | |||
113 | fvelez | 82 | /** |
44 | fvelez | 83 | * Used by the base DAO classes but here for your modification Load object |
84 | * matching the given key and return it. |
||
85 | * |
||
86 | * @param ID to load |
||
87 | * @return a user |
||
88 | */ |
||
113 | fvelez | 89 | public final User loadByID(1.5.0/docs/api/java/lang/String.html">String ID) |
38 | fvelez | 90 | { |
91 | try |
||
92 | { |
||
93 | return (User) createCriteria() |
||
94 | .add(eq("id",ID)) |
||
95 | .uniqueResult(); |
||
96 | } |
||
97 | catch (HibernateException e) |
||
98 | { |
||
99 | throw new DaoException(e); |
||
100 | } |
||
101 | } |
||
102 | |||
1 | fvelez | 103 | /** |
104 | * Used by the base DAO classes but here for your modification Load object |
||
105 | * matching the given key and return it. |
||
106 | * |
||
107 | * @param username to load |
||
108 | * @param password to load |
||
109 | * @return a user |
||
110 | */ |
||
111 | 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) |
||
112 | { |
||
113 | try |
||
114 | { |
||
115 | return (User) createCriteria() |
||
116 | .add(eq("username",username)) |
||
117 | .add(eq("password", jomm.utils.BytesUtils.getDigestMD5Hex(password))) |
||
118 | .uniqueResult(); |
||
119 | } |
||
120 | catch (HibernateException e) |
||
121 | { |
||
122 | throw new DaoException(e); |
||
123 | } |
||
124 | } |
||
125 | |||
113 | fvelez | 126 | public List<User> findUsers (1.5.0/docs/api/java/lang/String.html">String textToSearch) |
127 | { |
||
128 | Criteria criteria = createCriteria(); |
||
129 | Criterion name= or(like("name","%"+textToSearch+"%"),like("name","%"+textToSearch+"%")); |
||
130 | Criterion username = or(like("username", "%" + textToSearch + "%"), like("username", "%" + textToSearch + "%")); |
||
131 | Criterion email = or(like("email", "%" + textToSearch + "%"), like("email", "%" + textToSearch + "%")); |
||
132 | criteria.add(or(or(name,username),email)); |
||
133 | return criteria.list(); |
||
134 | } |
||
135 | |||
136 | |||
137 | |||
45 | fvelez | 138 | public 1.5.0/docs/api/java/io/Serializable.html">Serializable save(User obj) |
139 | { |
||
140 | try { |
||
141 | obj.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
||
142 | return super.save(obj); |
||
143 | } |
||
144 | catch (HibernateException e) { |
||
145 | throw new DaoException(e); |
||
146 | } |
||
147 | |||
148 | } |
||
149 | |||
1 | fvelez | 150 | } |