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 public class ServicesBO {
20
21 private static Logger logger = Logger.getLogger(ServicesBO.class);
22
23 public boolean changePassword(String login, String hash, String password) {
24
25 boolean change = false;
26 try {
27
28 if (checkRecoveryPassword(login, hash)) {
29
30
31 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
32 session.beginTransaction();
33 Query query = session.createQuery("from UserTO where email = :email ");
34 query.setParameter("email", login);
35 List<UserTO> list = query.list();
36 if (!list.isEmpty()) {
37 UserTO userTO = list.get(0);
38 userTO.setRecoveryPasswordHash(null);
39 userTO.setRecoveryPasswordTimestamp(null);
40 password = SecurityUtil.encryptPassword(password);
41 userTO.setPassword(password);
42
43 session.update(userTO);
44 session.getTransaction().commit();
45 change = true;
46 }
47
48 }
49 } catch (Exception e) {
50 logger.error(e);
51 }
52 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 boolean check = false;
71
72 try {
73
74
75 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
76 session.beginTransaction();
77 Query query = session.createQuery("from UserTO where email = :email ");
78 query.setParameter("email", login);
79 List<UserTO> list = query.list();
80 if (!list.isEmpty()) {
81 UserTO userTO = list.get(0);
82 if (userTO.getRecoveryPasswordHash().equals(hash)
83 && new Date().before(userTO.getRecoveryPasswordTimestamp())) {
84 check = true;
85 }
86 }
87 session.getTransaction().commit();
88 } catch (Exception e) {
89 logger.error(e);
90 }
91
92 return check;
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106 public boolean recoveryPassword(String email) {
107
108 boolean recoveredPassword = false;
109
110 try {
111
112
113 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
114 session.beginTransaction();
115 Query query = session.createQuery("from UserTO where email = :email ");
116 query.setParameter("email", email);
117 List<UserTO> list = query.list();
118
119 if (!list.isEmpty()) {
120 UserTO userTO = list.get(0);
121
122
123 Random randomGenerator = new Random();
124 int randomInt = randomGenerator.nextInt(1000000000);
125 userTO.setRecoveryPasswordHash(SecurityUtil.encryptPassword(String.valueOf(randomInt)));
126 Date incrementedDate = DateUtils.addHours(new Date(), 24);
127 userTO.setRecoveryPasswordTimestamp(incrementedDate);
128
129
130 SendEmailUtil.sendRecoveryMail(userTO);
131
132 session.update(userTO);
133
134 session.getTransaction().commit();
135
136 recoveredPassword = true;
137
138 } else {
139
140 logger.info("recoveryPassword: User Not found [" + email + "]");
141 }
142
143 } catch (Exception e) {
144 logger.error(e);
145 }
146
147 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 boolean saved = false;
172
173 try {
174
175 logger.debug("createUSer(" + sex + ", " + alias + ", " + name + ", " + surname + ", " + phone + ", "
176 + email + ", " + password + "," + level + "[" + laboralTimesheet + "],[" + nolaboralTimesheet
177 + "])");
178
179 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
180
181 session.beginTransaction();
182
183 UserTO userTO = new UserTO();
184 userTO.setAlias(alias);
185 userTO.setEmail(email);
186 userTO.setName(name);
187 userTO.setSurname(surname);
188 password = SecurityUtil.encryptPassword(password);
189 userTO.setPassword(password);
190 userTO.setPhone(phone);
191 userTO.setSex(sex);
192 userTO.setLevel(level);
193
194
195 session.save(userTO);
196
197 if (laboralTimesheet != null && !laboralTimesheet.isEmpty()) {
198 if (!laboralTimesheet.contains(",")) {
199 UserPreferencesTO userPreferencesTO = new UserPreferencesTO();
200 userPreferencesTO.setIdUser(userTO.getId());
201 userPreferencesTO.setName("LT");
202 userPreferencesTO.setValue(laboralTimesheet);
203 session.save(userPreferencesTO);
204 } else {
205 StringTokenizer st = new StringTokenizer(laboralTimesheet, ",");
206 while (st.hasMoreElements()) {
207 UserPreferencesTO userPreferencesTO = new UserPreferencesTO();
208 userPreferencesTO.setIdUser(userTO.getId());
209 userPreferencesTO.setName("LT");
210 userPreferencesTO.setValue((String) st.nextElement());
211 session.save(userPreferencesTO);
212 }
213 }
214
215 }
216
217 if (nolaboralTimesheet != null && !nolaboralTimesheet.isEmpty()) {
218
219 if (!nolaboralTimesheet.contains(",")) {
220 UserPreferencesTO userPreferencesTO = new UserPreferencesTO();
221 userPreferencesTO.setIdUser(userTO.getId());
222 userPreferencesTO.setName("NLT");
223 userPreferencesTO.setValue(nolaboralTimesheet);
224 session.save(userPreferencesTO);
225 } else {
226 StringTokenizer st = new StringTokenizer(nolaboralTimesheet, ",");
227 while (st.hasMoreElements()) {
228 UserPreferencesTO userPreferencesTO = new UserPreferencesTO();
229 userPreferencesTO.setIdUser(userTO.getId());
230 userPreferencesTO.setName("NLT");
231 userPreferencesTO.setValue((String) st.nextElement());
232 session.save(userPreferencesTO);
233 }
234 }
235
236 }
237
238 session.getTransaction().commit();
239
240 saved = true;
241
242 } catch (Exception e) {
243 logger.error(e);
244 }
245
246 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 boolean saved = false;
253
254 try {
255
256 logger.debug("createUSer(" + id + "," + sex + ", " + alias + ", " + name + ", " + surname + ", " + phone
257 + ", " + email + ", " + password + "," + level + "[" + laboralTimesheet + "],["
258 + nolaboralTimesheet + "])");
259
260 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
261
262 session.beginTransaction();
263
264
265 UserTO userTO = new UserTO();
266 userTO.setId(Long.valueOf(id));
267 userTO.setAlias(alias);
268 userTO.setEmail(email);
269 userTO.setName(name);
270 userTO.setSurname(surname);
271 password = SecurityUtil.encryptPassword(password);
272 userTO.setPassword(password);
273 userTO.setPhone(phone);
274 userTO.setSex(sex);
275 userTO.setLevel(level);
276
277
278 session.update(userTO);
279
280
281 String hql = "delete from UserPreferencesTO where idUser= :idUser";
282 session.createQuery(hql).setLong("idUser", Long.valueOf(id)).executeUpdate();
283
284
285 if (laboralTimesheet != null && !laboralTimesheet.isEmpty()) {
286 if (!laboralTimesheet.contains(",")) {
287 UserPreferencesTO userPreferencesTO = new UserPreferencesTO();
288 userPreferencesTO.setIdUser(userTO.getId());
289 userPreferencesTO.setName("LT");
290 userPreferencesTO.setValue(laboralTimesheet);
291 session.save(userPreferencesTO);
292 } else {
293 StringTokenizer st = new StringTokenizer(laboralTimesheet, ",");
294 while (st.hasMoreElements()) {
295 UserPreferencesTO userPreferencesTO = new UserPreferencesTO();
296 userPreferencesTO.setIdUser(userTO.getId());
297 userPreferencesTO.setName("LT");
298 userPreferencesTO.setValue((String) st.nextElement());
299 session.save(userPreferencesTO);
300 }
301 }
302
303 }
304
305 if (nolaboralTimesheet != null && !nolaboralTimesheet.isEmpty()) {
306
307 if (!nolaboralTimesheet.contains(",")) {
308 UserPreferencesTO userPreferencesTO = new UserPreferencesTO();
309 userPreferencesTO.setIdUser(userTO.getId());
310 userPreferencesTO.setName("NLT");
311 userPreferencesTO.setValue(nolaboralTimesheet);
312 session.save(userPreferencesTO);
313 } else {
314 StringTokenizer st = new StringTokenizer(nolaboralTimesheet, ",");
315 while (st.hasMoreElements()) {
316 UserPreferencesTO userPreferencesTO = new UserPreferencesTO();
317 userPreferencesTO.setIdUser(userTO.getId());
318 userPreferencesTO.setName("NLT");
319 userPreferencesTO.setValue((String) st.nextElement());
320 session.save(userPreferencesTO);
321 }
322 }
323
324 }
325
326 session.getTransaction().commit();
327
328 saved = true;
329
330 } catch (Exception e) {
331 logger.error(e);
332 }
333
334 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 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 }
359
360 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 UserTO userTOResponse = null;
379
380 try {
381
382 session.beginTransaction();
383
384 Query query = session.createQuery("from UserTO where email = :email ");
385 query.setParameter("email", email);
386 List<UserTO> list = query.list();
387
388 if (!list.isEmpty()) {
389 userTOResponse = list.get(0);
390
391 if (userTOResponse.getLoginAttempts() < 0
392 || !userTOResponse.getPassword().equals(SecurityUtil.encryptPassword(password))) {
393
394 userTOResponse.setLoginAttempts(userTOResponse.getLoginAttempts() - 1);
395
396 } else {
397 userTOResponse.setLoginAttempts(3);
398 }
399
400 session.save(userTOResponse);
401 session.getTransaction().commit();
402
403 }
404
405 } catch (Exception e) {
406 logger.error(e);
407 }
408
409 return userTOResponse;
410 }
411
412 }