Rev 373 | Rev 387 | 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; |
||
344 | jmachado | 4 | import org.apache.log4j.Logger; |
113 | fvelez | 5 | import org.hibernate.Criteria; |
344 | jmachado | 6 | import org.hibernate.HibernateException; |
113 | fvelez | 7 | import org.hibernate.criterion.Criterion; |
186 | fvelez | 8 | import org.hibernate.criterion.Order; |
344 | jmachado | 9 | import static org.hibernate.criterion.Restrictions.*; |
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; |
||
1 | fvelez | 13 | |
344 | jmachado | 14 | import java.io.Serializable; |
15 | import java.util.Date; |
||
16 | import java.util.List; |
||
17 | |||
1 | fvelez | 18 | /** |
19 | * @author Jorge Machado |
||
20 | * @date 28/Fev/2008 |
||
21 | * @time 2:51:06 |
||
22 | * @see pt.estgp.estgweb.domain.dao.impl |
||
23 | */ |
||
215 | jmachado | 24 | public class UserDaoImpl extends UserDao |
1 | fvelez | 25 | { |
179 | fvelez | 26 | |
27 | 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); |
||
28 | |||
1 | fvelez | 29 | public static UserDaoImpl getInstance() |
30 | { |
||
31 | if (myInstance == null) |
||
32 | myInstance = new UserDaoImpl(); |
||
33 | return (UserDaoImpl) myInstance; |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Used by the base DAO classes but here for your modification Load object |
||
38 | * matching the given key and return it. |
||
39 | * |
||
40 | * @param username to load |
||
41 | * @return a user |
||
42 | */ |
||
43 | public final User loadByUsername(1.5.0/docs/api/java/lang/String.html">String username) |
||
44 | { |
||
45 | try |
||
46 | { |
||
47 | 5+0%2Fdocs%2Fapi+List">List list = |
||
48 | createCriteria() |
||
344 | jmachado | 49 | .add(eq("username", username)) |
1 | fvelez | 50 | .setMaxResults(1) |
51 | .list(); |
||
52 | |||
344 | jmachado | 53 | if (list.size() == 0) |
1 | fvelez | 54 | return null; |
55 | return (User) list.get(0); |
||
56 | } |
||
57 | catch (HibernateException e) |
||
58 | { |
||
59 | throw new DaoException(e); |
||
60 | } |
||
61 | } |
||
344 | jmachado | 62 | |
1 | fvelez | 63 | /** |
64 | * Used by the base DAO classes but here for your modification Load object |
||
65 | * matching the given key and return it. |
||
66 | * |
||
67 | * @param email to load |
||
68 | * @return a user |
||
69 | */ |
||
70 | public final User loadByEmail(1.5.0/docs/api/java/lang/String.html">String email) |
||
71 | { |
||
72 | try |
||
73 | { |
||
74 | return (User) createCriteria() |
||
344 | jmachado | 75 | .add(eq("email", email)) |
1 | fvelez | 76 | .uniqueResult(); |
77 | } |
||
78 | catch (HibernateException e) |
||
79 | { |
||
80 | throw new DaoException(e); |
||
81 | } |
||
82 | } |
||
83 | |||
113 | fvelez | 84 | /** |
44 | fvelez | 85 | * Used by the base DAO classes but here for your modification Load object |
86 | * matching the given key and return it. |
||
87 | * |
||
88 | * @param ID to load |
||
89 | * @return a user |
||
90 | */ |
||
113 | fvelez | 91 | public final User loadByID(1.5.0/docs/api/java/lang/String.html">String ID) |
38 | fvelez | 92 | { |
93 | try |
||
94 | { |
||
95 | return (User) createCriteria() |
||
344 | jmachado | 96 | .add(eq("id", ID)) |
38 | fvelez | 97 | .uniqueResult(); |
98 | } |
||
99 | catch (HibernateException e) |
||
100 | { |
||
101 | throw new DaoException(e); |
||
102 | } |
||
103 | } |
||
104 | |||
1 | fvelez | 105 | /** |
106 | * Used by the base DAO classes but here for your modification Load object |
||
107 | * matching the given key and return it. |
||
108 | * |
||
109 | * @param username to load |
||
110 | * @param password to load |
||
111 | * @return a user |
||
112 | */ |
||
113 | 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) |
||
114 | { |
||
115 | try |
||
116 | { |
||
117 | return (User) createCriteria() |
||
344 | jmachado | 118 | .add(eq("username", username)) |
1 | fvelez | 119 | .add(eq("password", jomm.utils.BytesUtils.getDigestMD5Hex(password))) |
120 | .uniqueResult(); |
||
121 | } |
||
122 | catch (HibernateException e) |
||
123 | { |
||
124 | throw new DaoException(e); |
||
125 | } |
||
126 | } |
||
127 | |||
344 | jmachado | 128 | public List<User> loadRoleUsers(1.5.0/docs/api/java/lang/String.html">String role) |
215 | jmachado | 129 | { |
344 | jmachado | 130 | return createCriteria().add(like("roles", "%" + role + "%")).addOrder(Order.asc("name")).list(); |
215 | jmachado | 131 | } |
132 | |||
344 | jmachado | 133 | public List<User> findUsers(1.5.0/docs/api/java/lang/String.html">String textToSearch) |
113 | fvelez | 134 | { |
344 | jmachado | 135 | return findUsers(textToSearch, null); |
179 | fvelez | 136 | } |
137 | |||
344 | jmachado | 138 | 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 | 139 | { |
344 | jmachado | 140 | Criteria criteria; |
141 | if (typeToSearch != null && typeToSearch.trim().length() != 0) |
||
179 | fvelez | 142 | try |
143 | { |
||
144 | criteria = createCriteria(1.5.0/docs/api/java/lang/Class.html">Class.forName(typeToSearch)); |
||
145 | } |
||
146 | catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e) |
||
147 | { |
||
344 | jmachado | 148 | logger.error(e, e); |
149 | return null; |
||
179 | fvelez | 150 | } |
151 | else |
||
344 | jmachado | 152 | criteria = createCriteria(); |
373 | jmachado | 153 | Criterion textSearch = DaoUtils.createSearchQuery(textToSearch, SearchTypeEnum.AllWords, "name", "username"); |
154 | Criterion text2Search = DaoUtils.createSearchQuery(textToSearch, SearchTypeEnum.AllWords, "email"); |
||
344 | jmachado | 155 | Criterion fields = or(textSearch, text2Search); |
156 | return criteria.add(fields) |
||
157 | .addOrder(Order.desc("name")) |
||
158 | .list(); |
||
113 | fvelez | 159 | } |
160 | |||
161 | |||
45 | fvelez | 162 | public 1.5.0/docs/api/java/io/Serializable.html">Serializable save(User obj) |
163 | { |
||
344 | jmachado | 164 | try |
165 | { |
||
45 | fvelez | 166 | obj.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date()); |
167 | return super.save(obj); |
||
168 | } |
||
344 | jmachado | 169 | catch (HibernateException e) |
170 | { |
||
45 | fvelez | 171 | throw new DaoException(e); |
172 | } |
||
173 | |||
174 | } |
||
175 | |||
215 | jmachado | 176 | public int countUsers(1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchType) |
177 | { |
||
344 | jmachado | 178 | Criterion c = DaoUtils.createSearchQuery(query, searchType, "name", "username"); |
215 | jmachado | 179 | return createCriteria() |
180 | .add(c) |
||
181 | .list().size(); |
||
182 | } |
||
183 | |||
184 | |||
185 | public List<User> search(1.5.0/docs/api/java/lang/String.html">String query, SearchTypeEnum searchTypeEnum, int maxResults, int page) |
||
186 | { |
||
187 | Criterion c = DaoUtils.createSearchQuery(query, searchTypeEnum, "name", "username"); |
||
188 | Criteria criteria = createCriteria(); |
||
189 | criteria.add(c) |
||
190 | .addOrder(Order.asc("name")) |
||
191 | .setMaxResults(maxResults) |
||
192 | .setFirstResult(page * maxResults); |
||
193 | return criteria.list(); |
||
194 | } |
||
381 | jmachado | 195 | |
196 | public List<User> findByEmail(1.5.0/docs/api/java/lang/String.html">String email) |
||
197 | { |
||
198 | return createCriteria().add(or(or(or(eq("email",email),eq("outEmail",email)),eq("gmail",email)),eq("msn",email))).list(); |
||
199 | } |
||
1 | fvelez | 200 | } |