Coverage Report - br.com.arsmachina.authentication.dao.hibernate.UserDAOImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
UserDAOImpl
0%
0/20
0%
0/4
0
 
 1  
 // Copyright 2008 Thiago H. de Paula Figueiredo
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package br.com.arsmachina.authentication.dao.hibernate;
 16  
 
 17  
 import java.util.List;
 18  
 
 19  
 import org.hibernate.Query;
 20  
 import org.hibernate.Session;
 21  
 import org.hibernate.SessionFactory;
 22  
 
 23  
 import br.com.arsmachina.authentication.controller.PasswordEncrypter;
 24  
 import br.com.arsmachina.authentication.dao.UserDAO;
 25  
 import br.com.arsmachina.authentication.entity.Role;
 26  
 import br.com.arsmachina.authentication.entity.User;
 27  
 import br.com.arsmachina.dao.SortCriterion;
 28  
 import br.com.arsmachina.dao.hibernate.GenericDAOImpl;
 29  
 
 30  
 /**
 31  
  * {@link UserDAO} implementation using Hibernate
 32  
  * 
 33  
  * @author Thiago H. de Paula Figueiredo
 34  
  */
 35  
 public class UserDAOImpl extends GenericDAOImpl<User, Integer> implements UserDAO {
 36  
 
 37  
         private PasswordEncrypter passwordEncrypter;
 38  
 
 39  
         /**
 40  
          * Single constructor of this class.
 41  
          * 
 42  
          * @param sessionFactory a {@link SessionFactory}. It cannot be null.
 43  
          * @param passwordEncrypter a {@link PasswordEncrypter}. It cannot be null.
 44  
          */
 45  
         public UserDAOImpl(SessionFactory sessionFactory, PasswordEncrypter passwordEncrypter) {
 46  
 
 47  0
                 super(sessionFactory);
 48  
 
 49  0
                 if (passwordEncrypter == null) {
 50  0
                         throw new IllegalArgumentException("Parameter passwordEncrypter cannot be null");
 51  
                 }
 52  
 
 53  0
         }
 54  
 
 55  
         /**
 56  
          * Finds the user with a given login and password. The login search is case-insensitive.
 57  
          * 
 58  
          * @see br.com.arsmachina.authentication.dao.UserDAO#findByLoginAndPassword(java.lang.String,
 59  
          * java.lang.String)
 60  
          */
 61  
         public User findByLoginAndPassword(String login, String password) {
 62  
 
 63  0
                 Session session = getSession();
 64  
 
 65  0
                 Query query = session.createQuery("from User where lowercase(login) = :login and "
 66  
                                 + "password = :password");
 67  
 
 68  0
                 query.setParameter("login", login.toLowerCase());
 69  0
                 query.setParameter("password", passwordEncrypter.encrypt(password));
 70  
 
 71  0
                 return (User) query.uniqueResult();
 72  
 
 73  
         }
 74  
 
 75  
         /**
 76  
          * Finds an user by its login. The search is case-insensitive.
 77  
          * 
 78  
          * @see br.com.arsmachina.authentication.dao.UserDAO#findByLogin(java.lang.String)
 79  
          */
 80  
         public User findByLogin(String login) {
 81  
 
 82  0
                 Session session = getSession();
 83  
 
 84  0
                 Query query = session.createQuery("from User where lower(login) = :login");
 85  0
                 query.setParameter("login", login.toLowerCase());
 86  
 
 87  0
                 return (User) query.uniqueResult();
 88  
 
 89  
         }
 90  
 
 91  
         @SuppressWarnings("unchecked")
 92  
         public <T extends Role> List<User> findByRole(Class<T> roleClass) {
 93  
 
 94  0
                 Query query = getSession().createQuery(
 95  
                                 "select distinct(m.user) from Manager m order by m.user.login");
 96  
 
 97  0
                 return query.list();
 98  
 
 99  
         }
 100  
 
 101  
         /**
 102  
          * Returns {@link Constants#ASCENDING_NAME_SORT_CRITERIA}.
 103  
          * 
 104  
          * @see br.com.arsmachina.dao.hibernate.GenericDAOImpl#getDefaultSortCriteria()
 105  
          */
 106  
         @Override
 107  
         public SortCriterion[] getDefaultSortCriteria() {
 108  0
                 return Constants.ASCENDING_NAME_SORT_CRITERIA;
 109  
         }
 110  
 
 111  
         public boolean hasUserWithLogin(String login) {
 112  
 
 113  0
                 Query query = getSession().createQuery(
 114  
                                 "select count (distinct u) from User u where lower(login) = :login");
 115  0
                 query.setParameter("login", login.toLowerCase());
 116  
 
 117  0
                 Long result = (Long) query.uniqueResult();
 118  
 
 119  0
                 return result > 0;
 120  
 
 121  
         }
 122  
 
 123  
 }