View Javadoc

1   package net.ramapuram.thomas.dao.hibernate;
2   
3   import net.ramapuram.thomas.dao.UserDao;
4   import net.ramapuram.thomas.model.User;
5   import org.springframework.core.annotation.AnnotationUtils;
6   import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
7   import org.springframework.orm.hibernate3.SessionFactoryUtils;
8   import org.springframework.security.core.userdetails.UserDetails;
9   import org.springframework.security.core.userdetails.UserDetailsService;
10  import org.springframework.security.core.userdetails.UsernameNotFoundException;
11  import org.springframework.stereotype.Repository;
12  
13  import javax.persistence.Table;
14  import java.util.List;
15  
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 UserDaoHibernate extends GenericDaoHibernate<User, Long> implements UserDao, UserDetailsService {
28  
29      /**
30       * Constructor that sets the entity to User.class.
31       */
32      public UserDaoHibernate() {
33          super(User.class);
34      }
35  
36      /**
37       * {@inheritDoc}
38       */
39      @SuppressWarnings("unchecked")
40      public List<User> getUsers() {
41          return getHibernateTemplate().find("from User u order by upper(u.username)");
42      }
43  
44      /**
45       * {@inheritDoc}
46       */
47      public User saveUser(User user) {
48          if (log.isDebugEnabled()) {
49              log.debug("user's id: " + user.getId());
50          }
51          getHibernateTemplate().saveOrUpdate(user);
52          // necessary to throw a DataIntegrityViolation and catch it in UserManager
53          getHibernateTemplate().flush();
54          return user;
55      }
56  
57      /**
58       * Overridden simply to call the saveUser method. This is happenening 
59       * because saveUser flushes the session and saveObject of BaseDaoHibernate 
60       * does not.
61       *
62       * @param user the user to save
63       * @return the modified user (with a primary key set if they're new)
64       */
65      @Override
66      public User save(User user) {
67          return this.saveUser(user);
68      }
69  
70      /** 
71       * {@inheritDoc}
72      */
73      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
74          List users = getHibernateTemplate().find("from User where username=?", username);
75          if (users == null || users.isEmpty()) {
76              throw new UsernameNotFoundException("user '" + username + "' not found...");
77          } else {
78              return (UserDetails) users.get(0);
79          }
80      }
81  
82      /** 
83       * {@inheritDoc}
84      */
85      public String getUserPassword(String username) {
86          SimpleJdbcTemplate jdbcTemplate =
87                  new SimpleJdbcTemplate(SessionFactoryUtils.getDataSource(getSessionFactory()));
88          Table table = AnnotationUtils.findAnnotation(User.class, Table.class);
89          return jdbcTemplate.queryForObject(
90                  "select password from " + table.name() + " where username=?", String.class, username);
91  
92      }
93      
94  }