UserService.java
1.89 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
package net.ziemers.swxercise.lg.user.service;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.inject.Inject;
import net.ziemers.swxercise.db.dao.user.UserDao;
import net.ziemers.swxercise.lg.model.user.Profile;
import net.ziemers.swxercise.lg.model.user.User;
import net.ziemers.swxercise.lg.user.dto.UserDto;
/**
* Diese Klasse stellt alle Dienste im Kontext einer Benutzerverwaltung zur Verfügung.
*/
@Stateless
public class UserService {
@Inject
private UserDao dao;
@Inject
private SessionContext sessionContext;
public boolean loginUser(UserDto dto) {
final User user = dao.findByUsername(dto.getUsername());
if (user != null && user.getProfile().isValidPassword(dto.getPassword())) {
sessionContext.setUser(user);
return true;
}
return false;
}
public boolean logoutUser() {
sessionContext.setUser(null);
return sessionContext.getUser() == null;
}
public User findUser(final Long id) {
return dao.findById(id);
}
public Collection<User> findAllUsers() {
return dao.findAll(User.class);
}
public Long createUser(final UserDto userDto) {
final Profile profile = new Profile(userDto.getUsername(), userDto.getPassword());
// wir füllen das User-Objekt mit Method Chaining
final User user = new User(userDto.getFirstname(), userDto.getLastname())
.withProfile(profile);
return dao.save(user);
}
public void deleteUser(final Long id) {
dao.remove(User.class, id);
}
public boolean deleteUser() {
final User user = sessionContext.getUser();
if (user != null) {
dao.remove(User.class, user.getId());
return true;
}
return false;
}
public User getSessionUser() {
return sessionContext.getUser();
}
}