UserService.java
1.58 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
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.Session;
import net.ziemers.swxercise.lg.model.user.User;
import net.ziemers.swxercise.lg.user.dto.UserDto;
@Stateless
public class UserService {
@Inject
private UserDao dao;
@Inject
private Session session;
public boolean loginUser(UserDto dto) {
final User user = dao.findByUsername(dto.getUsername());
if (user != null && user.getProfile().isValidPassword(dto.getPassword())) {
session.setUser(user);
return true;
}
return false;
}
public boolean logoutUser() {
session.setUser(null);
return session.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 User getSessionUser() {
return session.getUser();
}
}