/******************************************************************************* * Gisgraphy Project * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA * * Copyright 2008 Gisgraphy project * David Masclet <davidmasclet@gisgraphy.com> * * *******************************************************************************/ package com.gisgraphy.dao.hibernate; import java.util.List; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import com.gisgraphy.dao.UserDao; import com.gisgraphy.model.User; /** * This class interacts with Spring's HibernateTemplate to save/delete and * retrieve User objects. * * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a> Modified by * <a href="mailto:dan@getrolling.com">Dan Kibler</a> Extended to * implement Acegi UserDetailsService interface by David Carter * david@carter.net Modified by <a href="mailto:bwnoll@gmail.com">Bryan * Noll</a> to work with the new BaseDaoHibernate implementation that * uses generics. */ public class UserDaoHibernate extends GenericDaoHibernate<User, Long> implements UserDao, UserDetailsService { /** * Constructor that sets the entity to User.class. */ public UserDaoHibernate() { super(User.class); } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public List<User> getUsers() { return (List<User>)getHibernateTemplate().find( "from User u order by upper(u.username)"); } /** * {@inheritDoc} */ public User saveUser(User user) { log.debug("user's id: " + user.getId()); getHibernateTemplate().saveOrUpdate(user); // necessary to throw a DataIntegrityViolation and catch it in // UserManager getHibernateTemplate().flush(); return user; } /** * Overridden simply to call the saveUser method. This is happenening * because saveUser flushes the session and saveObject of BaseDaoHibernate * does not. * * @param user * the user to save * @return the modified user (with a primary key set if they're new) */ @Override public User save(User user) { return this.saveUser(user); } /** * {@inheritDoc} */ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { List<?> users = getHibernateTemplate().find( "from User where username=?", username); if (users == null || users.isEmpty()) { throw new UsernameNotFoundException("user '" + username + "' not found..."); } else { return (UserDetails) users.get(0); } } }