View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
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       * (non-Javadoc)
35       *
36       * @see org.directdemocracyportal.democracy.service.dao.UserSessionDAO#findBySessionId(java.lang.String)
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       * (non-Javadoc)
55       *
56       * @see org.directdemocracyportal.democracy.service.dao.UserSessionDAO#findByUserId(java.lang.Long)
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       * (non-Javadoc)
74       *
75       * @see org.directdemocracyportal.democracy.service.dao.UserSessionDAO#getActiveSessions()
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       * (non-Javadoc)
86       *
87       * @see org.directdemocracyportal.democracy.service.dao.UserSessionDAO#endActiveSessions()
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  }