PageRenderTime 51ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/data/jpa/src/main/java/org/appfuse/dao/jpa/UserDaoJpa.java

http://github.com/myabc/appfuse
Java | 86 lines | 51 code | 8 blank | 27 comment | 4 complexity | f1f96d906bf6f78d1fe4c533ff5bb08b MD5 | raw file
  1. package org.appfuse.dao.jpa;
  2. import org.appfuse.dao.UserDao;
  3. import org.appfuse.model.User;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.core.annotation.AnnotationUtils;
  6. import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
  7. import org.springframework.security.core.userdetails.UserDetails;
  8. import org.springframework.security.core.userdetails.UserDetailsService;
  9. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  10. import org.springframework.stereotype.Repository;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import javax.persistence.Query;
  13. import javax.persistence.Table;
  14. import javax.sql.DataSource;
  15. import java.util.List;
  16. /**
  17. * This class interacts with Spring's HibernateTemplate to save/delete and
  18. * retrieve User objects.
  19. *
  20. * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
  21. * Modified by <a href="mailto:dan@getrolling.com">Dan Kibler</a>
  22. * Extended to implement Acegi UserDetailsService interface by David Carter david@carter.net
  23. * Modified by <a href="mailto:bwnoll@gmail.com">Bryan Noll</a> to work with
  24. * the new BaseDaoHibernate implementation that uses generics.
  25. */
  26. @Repository("userDao")
  27. public class UserDaoJpa extends GenericDaoJpa<User, Long> implements UserDao, UserDetailsService {
  28. @Autowired
  29. private DataSource dataSource;
  30. /**
  31. * Constructor that sets the entity to User.class.
  32. */
  33. public UserDaoJpa() {
  34. super(User.class);
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. @SuppressWarnings("unchecked")
  40. public List<User> getUsers() {
  41. Query q = getEntityManager().createQuery("select u from User u order by upper(u.username)");
  42. return q.getResultList();
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. @SuppressWarnings("unchecked")
  48. @Transactional
  49. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  50. Query q = getEntityManager().createQuery("select u from User u where username=?");
  51. q.setParameter(1, username);
  52. List<User> users = q.getResultList();
  53. if (users == null || users.isEmpty()) {
  54. throw new UsernameNotFoundException("user '" + username + "' not found...");
  55. } else {
  56. return users.get(0);
  57. }
  58. }
  59. /**
  60. * Save user and flush entityManager
  61. * @param user the user to save
  62. * @return the updated user
  63. */
  64. public User saveUser(User user) {
  65. User u = super.save(user);
  66. getEntityManager().flush();
  67. return u;
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public String getUserPassword(String username) {
  73. SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource);
  74. Table table = AnnotationUtils.findAnnotation(User.class, Table.class);
  75. return jdbcTemplate.queryForObject(
  76. "select password from " + table.name() + " where username=?", String.class, username);
  77. }
  78. }