View Javadoc

1   package org.appfuse.dao.hibernate;
2   
3   import org.appfuse.dao.UserDao;
4   import org.appfuse.model.User;
5   import org.hibernate.Query;
6   import org.hibernate.criterion.Restrictions;
7   import org.springframework.core.annotation.AnnotationUtils;
8   import org.springframework.jdbc.core.JdbcTemplate;
9   import org.springframework.orm.hibernate4.SessionFactoryUtils;
10  import org.springframework.security.core.userdetails.UserDetails;
11  import org.springframework.security.core.userdetails.UserDetailsService;
12  import org.springframework.security.core.userdetails.UsernameNotFoundException;
13  import org.springframework.stereotype.Repository;
14  
15  import javax.persistence.Table;
16  import java.util.List;
17  
18  /**
19   * This class interacts with Hibernate session to save/delete and
20   * retrieve User objects.
21   *
22   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
23   *         Modified by <a href="mailto:dan@getrolling.com">Dan Kibler</a>
24   *         Extended to implement Acegi UserDetailsService interface by David Carter david@carter.net
25   *         Modified by <a href="mailto:bwnoll@gmail.com">Bryan Noll</a> to work with
26   *         the new BaseDaoHibernate implementation that uses generics.
27   *         Modified by jgarcia (updated to hibernate 4)
28   */
29  @Repository("userDao")
30  public class UserDaoHibernate extends GenericDaoHibernate<User, Long> implements UserDao, UserDetailsService {
31  
32      /**
33       * Constructor that sets the entity to User.class.
34       */
35      public UserDaoHibernate() {
36          super(User.class);
37      }
38  
39      /**
40       * {@inheritDoc}
41       */
42      @SuppressWarnings("unchecked")
43      public List<User> getUsers() {
44          Query qry = getSession().createQuery("from User u order by upper(u.username)");
45          return qry.list();
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      public User saveUser(User user) {
52          if (log.isDebugEnabled()) {
53              log.debug("user's id: " + user.getId());
54          }
55          getSession().saveOrUpdate(user);
56          // necessary to throw a DataIntegrityViolation and catch it in UserManager
57          getSession().flush();
58          return user;
59      }
60  
61      /**
62       * Overridden simply to call the saveUser method. This is happening
63       * because saveUser flushes the session and saveObject of BaseDaoHibernate
64       * does not.
65       *
66       * @param user the user to save
67       * @return the modified user (with a primary key set if they're new)
68       */
69      @Override
70      public User save(User user) {
71          return this.saveUser(user);
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
78          List users = getSession().createCriteria(User.class).add(Restrictions.eq("username", username)).list();
79          if (users == null || users.isEmpty()) {
80              throw new UsernameNotFoundException("user '" + username + "' not found...");
81          } else {
82              return (UserDetails) users.get(0);
83          }
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      public String getUserPassword(Long userId) {
90          JdbcTemplate jdbcTemplate =
91                  new JdbcTemplate(SessionFactoryUtils.getDataSource(getSessionFactory()));
92          Table table = AnnotationUtils.findAnnotation(User.class, Table.class);
93          return jdbcTemplate.queryForObject(
94                  "select password from " + table.name() + " where id=?", String.class, userId);
95      }
96  }