org.appfuse.dao.jpa.UserDaoJpa Maven / Gradle / Ivy
package org.appfuse.dao.jpa;
import org.appfuse.dao.UserDao;
import org.appfuse.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.sql.DataSource;
import java.util.List;
/**
* 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.
*/
@Repository("userDao")
public class UserDaoJpa extends GenericDaoJpa implements UserDao, UserDetailsService {
@Autowired
private DataSource dataSource;
/**
* Constructor that sets the entity to User.class.
*/
public UserDaoJpa() {
super(User.class);
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public List getUsers() {
Query q = getEntityManager().createQuery("select u from User u order by upper(u.username)");
return q.getResultList();
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Transactional
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Query q = getEntityManager().createQuery("select u from User u where username=?");
q.setParameter(1, username);
List users = q.getResultList();
if (users == null || users.isEmpty()) {
throw new UsernameNotFoundException("user '" + username + "' not found...");
} else {
return users.get(0);
}
}
/**
* Save user and flush entityManager
* @param user the user to save
* @return the updated user
*/
public User saveUser(User user) {
User u = super.save(user);
getEntityManager().flush();
return u;
}
/**
* {@inheritDoc}
*/
public String getUserPassword(String username) {
SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource);
Table table = AnnotationUtils.findAnnotation(User.class, Table.class);
return jdbcTemplate.queryForObject(
"select password from " + table.name() + " where username=?", String.class, username);
}
}