UserDaoHibernate.java
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.myproject.dao.hibernate;
import com.google.common.base.Strings;
import com.myproject.dao.UserDao;
import com.myproject.model.User;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;
import javax.persistence.Table;
import java.util.List;
/**
* This class interacts with Hibernate session 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.
* Modified by jgarcia (updated to hibernate 4)
*/
@Repository("userDao")
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() {
Query qry = getSession().createQuery("from User u order by upper(u.username)");
return qry.list();
}
/**
* {@inheritDoc}
*/
public User saveUser(User user) {
if (log.isDebugEnabled()) {
log.debug("user's id: " + user.getId());
}
getSession().saveOrUpdate(user);
// necessary to throw a DataIntegrityViolation and catch it in UserManager
getSession().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}
*/
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
List users = getSession().createCriteria(User.class).add(Restrictions.eq("username", username)).list();
if (users == null || users.isEmpty()) {
throw new UsernameNotFoundException("user '" + username + "' not found...");
} else {
return (UserDetails) users.get(0);
}
}
/**
* {@inheritDoc}
*/
public String getUserPassword(Long userId) {
JdbcTemplate jdbcTemplate =
new JdbcTemplate(SessionFactoryUtils.getDataSource(getSessionFactory()));
Table table = AnnotationUtils.findAnnotation(User.class, Table.class);
return jdbcTemplate.queryForObject(
"select password from " + table.name() + " where id=?", String.class, userId);
}
@Override
public User findByAuthCode(String authCode){
if(Strings.isNullOrEmpty(authCode)){
return null;
}
List users = getSession().createCriteria(User.class).add(Restrictions.eq("authCode", authCode)).list();
if (users.isEmpty()) {
return null;
} else {
return (User) users.get(0);
}
// JdbcTemplate jdbcTemplate =
// new JdbcTemplate(SessionFactoryUtils.getDataSource(getSessionFactory()));
// Table table = AnnotationUtils.findAnnotation(User.class, Table.class);
// return jdbcTemplate.queryForObject(
// "select * from " + table.name() + " where authCode=? limit 1", User.class, authCode);
}
}