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