View Javadoc

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