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 | |
|
20 | |
|
21 | |
@Transactional(propagation = Propagation.MANDATORY) |
22 | |
public class HibernateUserSessionDAO extends |
23 | |
GenericHibernateDAO<UserSession, Long> implements UserSessionDAO |
24 | |
{ |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
public HibernateUserSessionDAO() { |
30 | 1 | super(UserSession.class); |
31 | 1 | } |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
@Transactional(readOnly = true) |
39 | |
public UserSession findBySessionId(String sessionId) |
40 | |
throws DataAccessException { |
41 | 1 | DetachedCriteria findActiveBySessionId = DetachedCriteria |
42 | |
.forClass(UserSession.class); |
43 | 1 | findActiveBySessionId.add(Restrictions.eq("sessionId", sessionId)); |
44 | 1 | findActiveBySessionId.add(Restrictions.eq("active", true)); |
45 | |
|
46 | |
@SuppressWarnings("unchecked") |
47 | 1 | List<UserSession> result = this.getHibernateTemplate().findByCriteria( |
48 | |
findActiveBySessionId); |
49 | |
|
50 | 1 | 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 | 0 | DetachedCriteria findActiveBySessionId = DetachedCriteria |
61 | |
.forClass(UserSession.class); |
62 | 0 | findActiveBySessionId.add(Restrictions.eq("user.id", userId)); |
63 | 0 | findActiveBySessionId.add(Restrictions.eq("active", true)); |
64 | |
|
65 | |
@SuppressWarnings("unchecked") |
66 | 0 | List<UserSession> result = this.getHibernateTemplate().findByCriteria( |
67 | |
findActiveBySessionId); |
68 | |
|
69 | 0 | 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 | 0 | return this.getHibernateTemplate().find( |
81 | |
"from UserSession where active=1"); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public void endActiveSessions() { |
90 | 0 | List<UserSession> sessions = getActiveSessions(); |
91 | 0 | for (UserSession session : sessions) { |
92 | 0 | session.setActive(false); |
93 | 0 | session.setEndedAt(new Date()); |
94 | 0 | } |
95 | 0 | this.getHibernateTemplate().saveOrUpdateAll(sessions); |
96 | 0 | } |
97 | |
} |