RightServiceJpa.java
2.49 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
package fr.plil.sio.persistence.jpa;
import fr.plil.sio.persistence.api.Right;
import fr.plil.sio.persistence.api.RightService;
import java.util.ArrayList;
import org.springframework.stereotype.Service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class RightServiceJpa implements RightService {
@Autowired
private RightRepository rightRepository;
@Override
public Right create(String name) {
if (name == null) {
throw new IllegalArgumentException("name cannot be null");
}
List<Right> rights = new ArrayList<>();
Right right = new Right();
right.setName(name);
rights.add(right);
rightRepository.save(rights);
return right;
}
@Override
public Right create(String name, Right parent) {
if (name == null) {
throw new IllegalArgumentException("name cannot be null");
}
if(parent == null){
throw new IllegalArgumentException("parent cannot be null");
}
List<Right> rights = new ArrayList<>();
Right right = new Right();
right.setName(name);
if(rightRepository.findByName(parent.getName()).isEmpty()){
throw new IllegalArgumentException("parent name cannot be null");
}
right.setParent(parent);
rights.add(right);
rightRepository.save(rights);
parent.setSiblings(rights);
return right;
}
@Override
public boolean delete(Right right) {
if(right == null)
throw new IllegalArgumentException("Right cannot be null");
if(right.getId() == null)
throw new IllegalArgumentException("Id cannot be null");
if(rightRepository.findByName(right.getName()).isEmpty())
return false;
rightRepository.delete(right);
return true;
}
@Override
public List<Right> findByName(String name) {
if(name == null)
throw new IllegalArgumentException("Name cannot be null");
return rightRepository.findByName(name);
}
@Override
public Right findOne(Long id) {
if(id == null)
throw new IllegalArgumentException("Id cannot be null");
return rightRepository.findOne(id);
}
}