1 | |
package booking.model.bo; |
2 | |
|
3 | |
import java.util.Date; |
4 | |
import java.util.List; |
5 | |
import java.util.Random; |
6 | |
import java.util.StringTokenizer; |
7 | |
|
8 | |
import org.apache.commons.lang.time.DateUtils; |
9 | |
import org.apache.log4j.Logger; |
10 | |
import org.hibernate.Query; |
11 | |
import org.hibernate.Session; |
12 | |
|
13 | |
import booking.model.entity.UserPreferencesTO; |
14 | |
import booking.model.entity.UserTO; |
15 | |
import booking.model.util.HibernateUtil; |
16 | |
import booking.model.util.SecurityUtil; |
17 | |
import booking.model.util.SendEmailUtil; |
18 | |
|
19 | 6 | public class ServicesBO { |
20 | |
|
21 | 1 | private static Logger logger = Logger.getLogger(ServicesBO.class); |
22 | |
|
23 | |
public boolean changePassword(String login, String hash, String password) { |
24 | |
|
25 | 0 | boolean change = false; |
26 | |
try { |
27 | |
|
28 | 0 | if (checkRecoveryPassword(login, hash)) { |
29 | |
|
30 | |
|
31 | 0 | Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
32 | 0 | session.beginTransaction(); |
33 | 0 | Query query = session.createQuery("from UserTO where email = :email "); |
34 | 0 | query.setParameter("email", login); |
35 | 0 | List<UserTO> list = query.list(); |
36 | 0 | if (!list.isEmpty()) { |
37 | 0 | UserTO userTO = list.get(0); |
38 | 0 | userTO.setRecoveryPasswordHash(null); |
39 | 0 | userTO.setRecoveryPasswordTimestamp(null); |
40 | 0 | password = SecurityUtil.encryptPassword(password); |
41 | 0 | userTO.setPassword(password); |
42 | |
|
43 | 0 | session.update(userTO); |
44 | 0 | session.getTransaction().commit(); |
45 | 0 | change = true; |
46 | |
} |
47 | |
|
48 | |
} |
49 | 0 | } catch (Exception e) { |
50 | 0 | logger.error(e); |
51 | 0 | } |
52 | 0 | return change; |
53 | |
} |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public boolean checkRecoveryPassword(String login, String hash) { |
69 | |
|
70 | 0 | boolean check = false; |
71 | |
|
72 | |
try { |
73 | |
|
74 | |
|
75 | 0 | Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
76 | 0 | session.beginTransaction(); |
77 | 0 | Query query = session.createQuery("from UserTO where email = :email "); |
78 | 0 | query.setParameter("email", login); |
79 | 0 | List<UserTO> list = query.list(); |
80 | 0 | if (!list.isEmpty()) { |
81 | 0 | UserTO userTO = list.get(0); |
82 | 0 | if (userTO.getRecoveryPasswordHash().equals(hash) |
83 | |
&& new Date().before(userTO.getRecoveryPasswordTimestamp())) { |
84 | 0 | check = true; |
85 | |
} |
86 | |
} |
87 | 0 | session.getTransaction().commit(); |
88 | 0 | } catch (Exception e) { |
89 | 0 | logger.error(e); |
90 | 0 | } |
91 | |
|
92 | 0 | return check; |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public boolean recoveryPassword(String email) { |
107 | |
|
108 | 0 | boolean recoveredPassword = false; |
109 | |
|
110 | |
try { |
111 | |
|
112 | |
|
113 | 0 | Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
114 | 0 | session.beginTransaction(); |
115 | 0 | Query query = session.createQuery("from UserTO where email = :email "); |
116 | 0 | query.setParameter("email", email); |
117 | 0 | List<UserTO> list = query.list(); |
118 | |
|
119 | 0 | if (!list.isEmpty()) { |
120 | 0 | UserTO userTO = list.get(0); |
121 | |
|
122 | |
|
123 | 0 | Random randomGenerator = new Random(); |
124 | 0 | int randomInt = randomGenerator.nextInt(1000000000); |
125 | 0 | userTO.setRecoveryPasswordHash(SecurityUtil.encryptPassword(String.valueOf(randomInt))); |
126 | 0 | Date incrementedDate = DateUtils.addHours(new Date(), 24); |
127 | 0 | userTO.setRecoveryPasswordTimestamp(incrementedDate); |
128 | |
|
129 | |
|
130 | 0 | SendEmailUtil.sendRecoveryMail(userTO); |
131 | |
|
132 | 0 | session.update(userTO); |
133 | |
|
134 | 0 | session.getTransaction().commit(); |
135 | |
|
136 | 0 | recoveredPassword = true; |
137 | |
|
138 | 0 | } else { |
139 | |
|
140 | 0 | logger.info("recoveryPassword: User Not found [" + email + "]"); |
141 | |
} |
142 | |
|
143 | 0 | } catch (Exception e) { |
144 | 0 | logger.error(e); |
145 | 0 | } |
146 | |
|
147 | 0 | return recoveredPassword; |
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public boolean createUSer(String sex, String alias, String name, String surname, long phone, String email, |
169 | |
String password, String level, String laboralTimesheet, String nolaboralTimesheet) { |
170 | |
|
171 | 2 | boolean saved = false; |
172 | |
|
173 | |
try { |
174 | |
|
175 | 2 | logger.debug("createUSer(" + sex + ", " + alias + ", " + name + ", " + surname + ", " + phone + ", " |
176 | |
+ email + ", " + password + "," + level + "[" + laboralTimesheet + "],[" + nolaboralTimesheet |
177 | |
+ "])"); |
178 | |
|
179 | 2 | Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
180 | |
|
181 | 2 | session.beginTransaction(); |
182 | |
|
183 | 2 | UserTO userTO = new UserTO(); |
184 | 2 | userTO.setAlias(alias); |
185 | 2 | userTO.setEmail(email); |
186 | 2 | userTO.setName(name); |
187 | 2 | userTO.setSurname(surname); |
188 | 2 | password = SecurityUtil.encryptPassword(password); |
189 | 2 | userTO.setPassword(password); |
190 | 2 | userTO.setPhone(phone); |
191 | 2 | userTO.setSex(sex); |
192 | 2 | userTO.setLevel(level); |
193 | |
|
194 | |
|
195 | 2 | session.save(userTO); |
196 | |
|
197 | 2 | if (laboralTimesheet != null && !laboralTimesheet.isEmpty()) { |
198 | 1 | if (!laboralTimesheet.contains(",")) { |
199 | 1 | UserPreferencesTO userPreferencesTO = new UserPreferencesTO(); |
200 | 1 | userPreferencesTO.setIdUser(userTO.getId()); |
201 | 1 | userPreferencesTO.setName("LT"); |
202 | 1 | userPreferencesTO.setValue(laboralTimesheet); |
203 | 1 | session.save(userPreferencesTO); |
204 | 1 | } else { |
205 | 0 | StringTokenizer st = new StringTokenizer(laboralTimesheet, ","); |
206 | 0 | while (st.hasMoreElements()) { |
207 | 0 | UserPreferencesTO userPreferencesTO = new UserPreferencesTO(); |
208 | 0 | userPreferencesTO.setIdUser(userTO.getId()); |
209 | 0 | userPreferencesTO.setName("LT"); |
210 | 0 | userPreferencesTO.setValue((String) st.nextElement()); |
211 | 0 | session.save(userPreferencesTO); |
212 | 0 | } |
213 | |
} |
214 | |
|
215 | |
} |
216 | |
|
217 | 2 | if (nolaboralTimesheet != null && !nolaboralTimesheet.isEmpty()) { |
218 | |
|
219 | 1 | if (!nolaboralTimesheet.contains(",")) { |
220 | 0 | UserPreferencesTO userPreferencesTO = new UserPreferencesTO(); |
221 | 0 | userPreferencesTO.setIdUser(userTO.getId()); |
222 | 0 | userPreferencesTO.setName("NLT"); |
223 | 0 | userPreferencesTO.setValue(nolaboralTimesheet); |
224 | 0 | session.save(userPreferencesTO); |
225 | 0 | } else { |
226 | 1 | StringTokenizer st = new StringTokenizer(nolaboralTimesheet, ","); |
227 | 4 | while (st.hasMoreElements()) { |
228 | 3 | UserPreferencesTO userPreferencesTO = new UserPreferencesTO(); |
229 | 3 | userPreferencesTO.setIdUser(userTO.getId()); |
230 | 3 | userPreferencesTO.setName("NLT"); |
231 | 3 | userPreferencesTO.setValue((String) st.nextElement()); |
232 | 3 | session.save(userPreferencesTO); |
233 | 3 | } |
234 | |
} |
235 | |
|
236 | |
} |
237 | |
|
238 | 2 | session.getTransaction().commit(); |
239 | |
|
240 | 2 | saved = true; |
241 | |
|
242 | 0 | } catch (Exception e) { |
243 | 0 | logger.error(e); |
244 | 2 | } |
245 | |
|
246 | 2 | return saved; |
247 | |
} |
248 | |
|
249 | |
public boolean updateUser(String id, String sex, String alias, String name, String surname, long phone, |
250 | |
String email, String password, String level, String laboralTimesheet, String nolaboralTimesheet) { |
251 | |
|
252 | 2 | boolean saved = false; |
253 | |
|
254 | |
try { |
255 | |
|
256 | 2 | logger.debug("createUSer(" + id + "," + sex + ", " + alias + ", " + name + ", " + surname + ", " + phone |
257 | |
+ ", " + email + ", " + password + "," + level + "[" + laboralTimesheet + "],[" |
258 | |
+ nolaboralTimesheet + "])"); |
259 | |
|
260 | 2 | Session session = HibernateUtil.getSessionFactory().getCurrentSession(); |
261 | |
|
262 | 2 | session.beginTransaction(); |
263 | |
|
264 | |
|
265 | 2 | UserTO userTO = new UserTO(); |
266 | 2 | userTO.setId(Long.valueOf(id)); |
267 | 2 | userTO.setAlias(alias); |
268 | 2 | userTO.setEmail(email); |
269 | 2 | userTO.setName(name); |
270 | 2 | userTO.setSurname(surname); |
271 | 2 | password = SecurityUtil.encryptPassword(password); |
272 | 2 | userTO.setPassword(password); |
273 | 2 | userTO.setPhone(phone); |
274 | 2 | userTO.setSex(sex); |
275 | 2 | userTO.setLevel(level); |
276 | |
|
277 | |
|
278 | 2 | session.update(userTO); |
279 | |
|
280 | |
|
281 | 2 | String hql = "delete from UserPreferencesTO where idUser= :idUser"; |
282 | 2 | session.createQuery(hql).setLong("idUser", Long.valueOf(id)).executeUpdate(); |
283 | |
|
284 | |
|
285 | 2 | if (laboralTimesheet != null && !laboralTimesheet.isEmpty()) { |
286 | 0 | if (!laboralTimesheet.contains(",")) { |
287 | 0 | UserPreferencesTO userPreferencesTO = new UserPreferencesTO(); |
288 | 0 | userPreferencesTO.setIdUser(userTO.getId()); |
289 | 0 | userPreferencesTO.setName("LT"); |
290 | 0 | userPreferencesTO.setValue(laboralTimesheet); |
291 | 0 | session.save(userPreferencesTO); |
292 | 0 | } else { |
293 | 0 | StringTokenizer st = new StringTokenizer(laboralTimesheet, ","); |
294 | 0 | while (st.hasMoreElements()) { |
295 | 0 | UserPreferencesTO userPreferencesTO = new UserPreferencesTO(); |
296 | 0 | userPreferencesTO.setIdUser(userTO.getId()); |
297 | 0 | userPreferencesTO.setName("LT"); |
298 | 0 | userPreferencesTO.setValue((String) st.nextElement()); |
299 | 0 | session.save(userPreferencesTO); |
300 | 0 | } |
301 | |
} |
302 | |
|
303 | |
} |
304 | |
|
305 | 2 | if (nolaboralTimesheet != null && !nolaboralTimesheet.isEmpty()) { |
306 | |
|
307 | 1 | if (!nolaboralTimesheet.contains(",")) { |
308 | 0 | UserPreferencesTO userPreferencesTO = new UserPreferencesTO(); |
309 | 0 | userPreferencesTO.setIdUser(userTO.getId()); |
310 | 0 | userPreferencesTO.setName("NLT"); |
311 | 0 | userPreferencesTO.setValue(nolaboralTimesheet); |
312 | 0 | session.save(userPreferencesTO); |
313 | 0 | } else { |
314 | 1 | StringTokenizer st = new StringTokenizer(nolaboralTimesheet, ","); |
315 | 3 | while (st.hasMoreElements()) { |
316 | 2 | UserPreferencesTO userPreferencesTO = new UserPreferencesTO(); |
317 | 2 | userPreferencesTO.setIdUser(userTO.getId()); |
318 | 2 | userPreferencesTO.setName("NLT"); |
319 | 2 | userPreferencesTO.setValue((String) st.nextElement()); |
320 | 2 | session.save(userPreferencesTO); |
321 | 2 | } |
322 | |
} |
323 | |
|
324 | |
} |
325 | |
|
326 | 2 | session.getTransaction().commit(); |
327 | |
|
328 | 2 | saved = true; |
329 | |
|
330 | 0 | } catch (Exception e) { |
331 | 0 | logger.error(e); |
332 | 2 | } |
333 | |
|
334 | 2 | return saved; |
335 | |
} |
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
public String getEvents(int sport, String start, String end) { |
348 | |
|
349 | 0 | String events = "[{\"idUser\":\"999" |
350 | |
+ sport |
351 | |
+ "\",\"title\":\"Repeating Event\",\"start\":\"2014-12-09T16:00:00\",\"color\":\"#a94442\",\"background\":\"#f2dede\"},{\"idUser\":\"9991" |
352 | |
+ sport + "\",\"title\":\"Repeating Event\",\"start\":\"2014-12-10T16:00:00\"}]"; |
353 | |
|
354 | |
try { |
355 | |
|
356 | |
} catch (Exception e) { |
357 | |
logger.error(e); |
358 | 0 | } |
359 | |
|
360 | 0 | return events; |
361 | |
} |
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
|
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
public UserTO login(String email, String password, Session session) { |
377 | |
|
378 | 1 | UserTO userTOResponse = null; |
379 | |
|
380 | |
try { |
381 | |
|
382 | 1 | session.beginTransaction(); |
383 | |
|
384 | 1 | Query query = session.createQuery("from UserTO where email = :email "); |
385 | 1 | query.setParameter("email", email); |
386 | 1 | List<UserTO> list = query.list(); |
387 | |
|
388 | 1 | if (!list.isEmpty()) { |
389 | 1 | userTOResponse = list.get(0); |
390 | |
|
391 | 1 | if (userTOResponse.getLoginAttempts() < 0 |
392 | |
|| !userTOResponse.getPassword().equals(SecurityUtil.encryptPassword(password))) { |
393 | |
|
394 | 0 | userTOResponse.setLoginAttempts(userTOResponse.getLoginAttempts() - 1); |
395 | |
|
396 | |
} else { |
397 | 1 | userTOResponse.setLoginAttempts(3); |
398 | |
} |
399 | |
|
400 | 1 | session.save(userTOResponse); |
401 | 1 | session.getTransaction().commit(); |
402 | |
|
403 | |
} |
404 | |
|
405 | 0 | } catch (Exception e) { |
406 | 0 | logger.error(e); |
407 | 1 | } |
408 | |
|
409 | 1 | return userTOResponse; |
410 | |
} |
411 | |
|
412 | |
} |