Java tutorial
/** * * Copyright (C) 2010 SYSNET International, Inc. <support@sysnetint.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. * */ package org.openhie.openempi.dao.hibernate; import java.util.List; import javax.persistence.Table; import org.openhie.openempi.dao.UserDao; import org.openhie.openempi.model.User; import org.openhie.openempi.util.ValidationUtil; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.orm.hibernate3.SessionFactoryUtils; import org.springframework.security.GrantedAuthority; import org.springframework.security.userdetails.UserDetails; import org.springframework.security.userdetails.UserDetailsService; import org.springframework.security.userdetails.UsernameNotFoundException; /** * 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, Integer> implements UserDao, UserDetailsService { /** * Constructor that sets the entity to User.class. */ public UserDaoHibernate() { super(User.class); } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public List<User> getUsers() { return 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 happening * 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} */ @SuppressWarnings("unchecked") public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { ValidationUtil.sanityCheckFieldName(username); List<UserDetails> users = getHibernateTemplate().find("from User where username=?", username); if (users == null || users.isEmpty()) { throw new UsernameNotFoundException("user '" + username + "' not found..."); } else { UserDetails userDetails = (UserDetails) users.get(0); for (GrantedAuthority authority : userDetails.getAuthorities()) { log.trace("User " + username + " has authority: " + authority.getAuthority()); } return userDetails; } } /** * {@inheritDoc} */ public String getUserPassword(String username) { ValidationUtil.sanityCheckFieldName(username); SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate( SessionFactoryUtils.getDataSource(getSessionFactory())); Table table = AnnotationUtils.findAnnotation(User.class, Table.class); return jdbcTemplate.queryForObject("select password from " + table.name() + " where username=?", String.class, username); } }