All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.appfuse.dao.hibernate.UserDaoHibernate Maven / Gradle / Ivy

There is a newer version: 3.5.0
Show newest version
package org.appfuse.dao.hibernate;

import java.util.List;

import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.appfuse.dao.UserDao;
import org.appfuse.model.User;

/**
 * This class interacts with Spring's HibernateTemplate to save/delete and
 * retrieve User objects.
 *
 * @author Matt Raible
 *   Modified by Dan Kibler
 *   Extended to implement Acegi UserDetailsService interface by David Carter [email protected]
 *   Modified by Bryan Noll to work with 
 *   the new BaseDaoHibernate implementation that uses generics.
*/
public class UserDaoHibernate extends GenericDaoHibernate implements UserDao, UserDetailsService {

    public UserDaoHibernate() {
        super(User.class);
    }

    /**
     * @see org.appfuse.dao.UserDao#getUsers()
     */
    public List getUsers() {
        return getHibernateTemplate().find("from User u order by upper(u.username)");
    }

    /**
     * @see org.appfuse.dao.UserDao#saveUser(org.appfuse.model.User)
     */
    public void saveUser(final User user) {
        log.debug("user's id: " + user.getId());        
        getHibernateTemplate().saveOrUpdate(user);
        // necessary to throw a DataIntegrityViolation and catch it in UserManager
        getHibernateTemplate().flush();
    }
    
    /**
     * Overridden simply to call the saveUser method. This is happenening 
     * because saveUser flushes the session and saveObject of BaseDaoHibernate 
     * does not.
     */
    @Override
    public void save(User user) {
        this.saveUser(user);
    }

    /** 
    * @see org.acegisecurity.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
    */
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List users = getHibernateTemplate().find("from User where username=?", username);
        if (users == null || users.isEmpty()) {
            throw new UsernameNotFoundException("user '" + username + "' not found...");
        } else {
            return (UserDetails) users.get(0);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy