View Javadoc

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