Coverage Report - org.directdemocracyportal.democracy.service.dao.hibernate.HibernateUserSessionDAO
 
Classes in this File Line Coverage Branch Coverage Complexity
HibernateUserSessionDAO
35%
7/20
16%
1/6
1.6
 
 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  1
         super(UserSession.class);
 31  1
     }
 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  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  
      * (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  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  
      * (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  0
         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  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  
 }