1
2
3
4
5 package org.directdemocracyportal.democracy.service.dao.hibernate;
6
7 import java.util.Date;
8 import java.util.List;
9
10 import org.directdemocracyportal.democracy.model.application.UserSession;
11 import org.directdemocracyportal.democracy.service.dao.UserSessionDAO;
12 import org.hibernate.criterion.DetachedCriteria;
13 import org.hibernate.criterion.Restrictions;
14 import org.springframework.dao.DataAccessException;
15 import org.springframework.transaction.annotation.Propagation;
16 import org.springframework.transaction.annotation.Transactional;
17
18 /***
19 * The Class HibernateUserSessionDAO.
20 */
21 @Transactional(propagation = Propagation.MANDATORY)
22 public class HibernateUserSessionDAO extends
23 GenericHibernateDAO<UserSession, Long> implements UserSessionDAO
24 {
25
26 /***
27 * Instantiates a new hibernate user session dao.
28 */
29 public HibernateUserSessionDAO() {
30 super(UserSession.class);
31 }
32
33
34
35
36
37
38 @Transactional(readOnly = true)
39 public UserSession findBySessionId(String sessionId)
40 throws DataAccessException {
41 DetachedCriteria findActiveBySessionId = DetachedCriteria
42 .forClass(UserSession.class);
43 findActiveBySessionId.add(Restrictions.eq("sessionId", sessionId));
44 findActiveBySessionId.add(Restrictions.eq("active", true));
45
46 @SuppressWarnings("unchecked")
47 List<UserSession> result = this.getHibernateTemplate().findByCriteria(
48 findActiveBySessionId);
49
50 return result.isEmpty() ? null : result.get(0);
51 }
52
53
54
55
56
57
58 @Transactional(readOnly = true)
59 public UserSession findByUserId(Long userId) throws DataAccessException {
60 DetachedCriteria findActiveBySessionId = DetachedCriteria
61 .forClass(UserSession.class);
62 findActiveBySessionId.add(Restrictions.eq("user.id", userId));
63 findActiveBySessionId.add(Restrictions.eq("active", true));
64
65 @SuppressWarnings("unchecked")
66 List<UserSession> result = this.getHibernateTemplate().findByCriteria(
67 findActiveBySessionId);
68
69 return result.isEmpty() ? null : result.get(0);
70 }
71
72
73
74
75
76
77 @SuppressWarnings("unchecked")
78 @Transactional(readOnly = true)
79 public List<UserSession> getActiveSessions() throws DataAccessException {
80 return this.getHibernateTemplate().find(
81 "from UserSession where active=1");
82 }
83
84
85
86
87
88
89 public void endActiveSessions() {
90 List<UserSession> sessions = getActiveSessions();
91 for (UserSession session : sessions) {
92 session.setActive(false);
93 session.setEndedAt(new Date());
94 }
95 this.getHibernateTemplate().saveOrUpdateAll(sessions);
96 }
97 }