View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.dao.hibernate;
24  
25  import java.util.List;
26  
27  import org.springframework.security.userdetails.UserDetails;
28  import org.springframework.security.userdetails.UserDetailsService;
29  import org.springframework.security.userdetails.UsernameNotFoundException;
30  
31  import com.gisgraphy.dao.UserDao;
32  import com.gisgraphy.model.User;
33  
34  /**
35   * This class interacts with Spring's HibernateTemplate to save/delete and
36   * retrieve User objects.
37   * 
38   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a> Modified by
39   *         <a href="mailto:dan@getrolling.com">Dan Kibler</a> Extended to
40   *         implement Acegi UserDetailsService interface by David Carter
41   *         david@carter.net Modified by <a href="mailto:bwnoll@gmail.com">Bryan
42   *         Noll</a> to work with the new BaseDaoHibernate implementation that
43   *         uses generics.
44   */
45  public class UserDaoHibernate extends GenericDaoHibernate<User, Long> implements
46  	UserDao, UserDetailsService {
47  
48      /**
49       * Constructor that sets the entity to User.class.
50       */
51      public UserDaoHibernate() {
52  	super(User.class);
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @SuppressWarnings("unchecked")
59      public List<User> getUsers() {
60  	return getHibernateTemplate().find(
61  		"from User u order by upper(u.username)");
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      public User saveUser(User user) {
68  	log.debug("user's id: " + user.getId());
69  	getHibernateTemplate().saveOrUpdate(user);
70  	// necessary to throw a DataIntegrityViolation and catch it in
71  	// UserManager
72  	getHibernateTemplate().flush();
73  	return user;
74      }
75  
76      /**
77       * Overridden simply to call the saveUser method. This is happenening
78       * because saveUser flushes the session and saveObject of BaseDaoHibernate
79       * does not.
80       * 
81       * @param user
82       *                the user to save
83       * @return the modified user (with a primary key set if they're new)
84       */
85      @Override
86      public User save(User user) {
87  	return this.saveUser(user);
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      public UserDetails loadUserByUsername(String username)
94  	    throws UsernameNotFoundException {
95  	List<?> users = getHibernateTemplate().find(
96  		"from User where username=?", username);
97  	if (users == null || users.isEmpty()) {
98  	    throw new UsernameNotFoundException("user '" + username
99  		    + "' not found...");
100 	} else {
101 	    return (UserDetails) users.get(0);
102 	}
103     }
104 }