GroupServiceJdbc.java
2.42 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
package fr.plil.sio.persistence.jdbc;
import fr.plil.sio.persistence.api.Group;
import fr.plil.sio.persistence.api.GroupService;
import fr.plil.sio.persistence.api.Right;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.LinkedList;
@Service
public class GroupServiceJdbc implements GroupService {
@Autowired
private GroupRepository groupRepository;
@Override
public Group create(String name) {
if (name == null) {
throw new IllegalArgumentException("name cannot be null");
}
Group group = groupRepository.findByName(name);
if (group != null) {
throw new IllegalStateException("a group with the same name already exists");
}
group = new Group();
group.setName(name);
groupRepository.save(group);
return group;
}
@Override
public boolean delete(String name) {
if (name == null) {
throw new IllegalArgumentException("name cannot be null");
}
Group group = findByName(name);
if(group==null){
return false;
}
groupRepository.delete(group.getId());
return true;
}
@Override
public Group findByName(String name) {
if (name == null) {
throw new IllegalArgumentException("name cannot be null");
}
return groupRepository.findByName(name);
}
@Override
public boolean addRight(String groupName, Right right) {
if(groupName==null || right ==null){
throw new IllegalArgumentException("name cannot be null");
}
Group group = groupRepository.findByName(groupName);
if (group == null) {
throw new IllegalArgumentException("group cannot be null");
}
group.setName(groupName);
List<Right> list = group.getRights();
list.add(right);
group.setRights(list);
return true;
}
@Override
public boolean removeRight(String groupName, Right right) {
if(right==null){
throw new IllegalArgumentException("name cannot be null");
}
Group group = groupRepository.findByName(groupName);
if (group != null) {
throw new IllegalStateException("a group with the same name already exists");
}
return true;
}
}