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