package org.appfuse.dao.hibernate;
import java.util.List;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.appfuse.dao.UserDao;
import org.appfuse.model.User;
import org.springframework.orm.ObjectRetrievalFailureException;
public class UserDaoHibernate extends BaseDaoHibernate implements UserDao, UserDetailsService {
public User getUser(Long userId) {
User user = (User) getHibernateTemplate().get(User.class, userId);
if (user == null) {
log.warn("uh oh, user '" + userId + "' not found...");
throw new ObjectRetrievalFailureException(User.class, userId);
}
return user;
}
public List getUsers(User user) {
return getHibernateTemplate().find("from User u order by upper(u.username)");
}
public void saveUser(final User user) {
if (log.isDebugEnabled()) {
log.debug("user's id: " + user.getId());
}
getHibernateTemplate().saveOrUpdate(user);
getHibernateTemplate().flush();
}
public void removeUser(Long userId) {
getHibernateTemplate().delete(getUser(userId));
}
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);
}
}
}