Commit fb8c34ccfd65a4c766ddaf1485233c6932e024ef
1 parent
9ae0ef87
RightJPA à 100%, GroupJPA à 94%
Showing
8 changed files
with
108 additions
and
39 deletions
Show diff stats
src/main/java/fr/plil/sio/persistence/api/Group.java
... | ... | @@ -3,6 +3,7 @@ package fr.plil.sio.persistence.api; |
3 | 3 | import java.io.Serializable; |
4 | 4 | import java.util.LinkedList; |
5 | 5 | import java.util.List; |
6 | +import javax.persistence.CascadeType; | |
6 | 7 | import javax.persistence.Column; |
7 | 8 | import javax.persistence.Entity; |
8 | 9 | import javax.persistence.GeneratedValue; |
... | ... | @@ -36,7 +37,7 @@ public class Group implements Serializable { |
36 | 37 | /** |
37 | 38 | * List of rights. The list CANNOT contains duplicate rights. |
38 | 39 | */ |
39 | - @OneToMany(mappedBy = "group") | |
40 | + @OneToMany(mappedBy = "group", cascade = CascadeType.REMOVE) | |
40 | 41 | private List<Right> rights = new LinkedList<>(); |
41 | 42 | |
42 | 43 | public List<Right> getRights() { | ... | ... |
src/main/java/fr/plil/sio/persistence/api/Right.java
... | ... | @@ -3,6 +3,7 @@ package fr.plil.sio.persistence.api; |
3 | 3 | import java.io.Serializable; |
4 | 4 | import java.util.LinkedList; |
5 | 5 | import java.util.List; |
6 | +import javax.persistence.CascadeType; | |
6 | 7 | import javax.persistence.Column; |
7 | 8 | import javax.persistence.Entity; |
8 | 9 | import javax.persistence.GeneratedValue; |
... | ... | @@ -32,6 +33,7 @@ public class Right implements Serializable{ |
32 | 33 | |
33 | 34 | /// the parent right |
34 | 35 | @ManyToOne(optional = true) |
36 | + @JoinColumn(name = "PARENT") | |
35 | 37 | private Right parent; |
36 | 38 | |
37 | 39 | @ManyToOne(optional = true) |
... | ... | @@ -39,7 +41,7 @@ public class Right implements Serializable{ |
39 | 41 | private Group group; |
40 | 42 | |
41 | 43 | /// the sibling right(s), eventually empty |
42 | - @OneToMany(mappedBy = "parent") | |
44 | + @OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE) | |
43 | 45 | private List<Right> siblings = new LinkedList<>(); |
44 | 46 | |
45 | 47 | public List<Right> getSiblings() { | ... | ... |
src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java
src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java
... | ... | @@ -3,6 +3,7 @@ package fr.plil.sio.persistence.jpa; |
3 | 3 | import fr.plil.sio.persistence.api.Group; |
4 | 4 | import fr.plil.sio.persistence.api.GroupService; |
5 | 5 | import fr.plil.sio.persistence.api.Right; |
6 | +import java.util.List; | |
6 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
7 | 8 | import org.springframework.stereotype.Service; |
8 | 9 | |
... | ... | @@ -50,12 +51,54 @@ public class GroupServiceJpa implements GroupService { |
50 | 51 | |
51 | 52 | @Override |
52 | 53 | public boolean addRight(String groupName, Right right) { |
53 | - | |
54 | - return false; | |
54 | + | |
55 | + if(groupName == null) | |
56 | + throw new IllegalArgumentException("Group name cannot be null"); | |
57 | + | |
58 | + if(right == null) | |
59 | + throw new IllegalArgumentException("Right cannot be null"); | |
60 | + | |
61 | + Group group = groupRepository.findByName(groupName); | |
62 | + | |
63 | + if(group == null){ | |
64 | + throw new IllegalStateException("Group not found"); | |
65 | + } | |
66 | + | |
67 | + List<Right> list = group.getRights(); | |
68 | + | |
69 | + if(list.contains(right)) | |
70 | + return false; | |
71 | + | |
72 | + list.add(right); | |
73 | + group.setRights(list); | |
74 | + | |
75 | + return true; | |
55 | 76 | } |
56 | 77 | |
57 | 78 | @Override |
58 | 79 | public boolean removeRight(String groupName, Right right) { |
80 | + | |
81 | + if(groupName == null) | |
82 | + throw new IllegalArgumentException("Group name cannot be null"); | |
83 | + | |
84 | + if(right == null) | |
85 | + throw new IllegalArgumentException("Right cannot be null"); | |
86 | + | |
87 | + if(right.getId() == null) | |
88 | + throw new IllegalArgumentException("Not a right"); | |
89 | + | |
90 | + Group group = groupRepository.findByName(groupName); | |
91 | + | |
92 | + if(group == null) | |
93 | + throw new IllegalStateException("Group not found"); | |
94 | + | |
95 | + List<Right> list = group.getRights(); | |
96 | + | |
97 | + if(list.contains(right)){ | |
98 | + list.remove(right); | |
99 | + return true; | |
100 | + } | |
101 | + | |
59 | 102 | return false; |
60 | 103 | } |
61 | 104 | } | ... | ... |
src/main/java/fr/plil/sio/persistence/jpa/RightRepository.java
1 | 1 | package fr.plil.sio.persistence.jpa; |
2 | 2 | |
3 | 3 | import fr.plil.sio.persistence.api.Right; |
4 | +import java.util.List; | |
4 | 5 | import org.springframework.data.jpa.repository.JpaRepository; |
5 | 6 | |
6 | 7 | public interface RightRepository extends JpaRepository<Right, Long> { |
7 | 8 | |
8 | - Right findByName(String name); | |
9 | + List<Right> findByName(String name); | |
9 | 10 | |
10 | 11 | } | ... | ... |
src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java
1 | 1 | package fr.plil.sio.persistence.jpa; |
2 | 2 | |
3 | -import fr.plil.sio.persistence.api.Group; | |
4 | 3 | import fr.plil.sio.persistence.api.Right; |
5 | 4 | import fr.plil.sio.persistence.api.RightService; |
6 | 5 | import java.util.ArrayList; |
... | ... | @@ -37,15 +36,27 @@ public class RightServiceJpa implements RightService { |
37 | 36 | if (name == null) { |
38 | 37 | throw new IllegalArgumentException("name cannot be null"); |
39 | 38 | } |
39 | + if(parent == null){ | |
40 | + throw new IllegalArgumentException("parent cannot be null"); | |
41 | + } | |
40 | 42 | |
41 | 43 | List<Right> rights = new ArrayList<>(); |
42 | 44 | |
43 | 45 | Right right = new Right(); |
44 | 46 | right.setName(name); |
47 | + | |
48 | + if(rightRepository.findByName(parent.getName()).isEmpty()){ | |
49 | + throw new IllegalArgumentException("parent name cannot be null"); | |
50 | + } | |
51 | + | |
52 | + | |
53 | + | |
45 | 54 | right.setParent(parent); |
46 | 55 | rights.add(right); |
47 | 56 | rightRepository.save(rights); |
48 | 57 | |
58 | + parent.setSiblings(rights); | |
59 | + | |
49 | 60 | return right; |
50 | 61 | } |
51 | 62 | |
... | ... | @@ -54,6 +65,12 @@ public class RightServiceJpa implements RightService { |
54 | 65 | if(right == null) |
55 | 66 | throw new IllegalArgumentException("Right cannot be null"); |
56 | 67 | |
68 | + if(right.getId() == null) | |
69 | + throw new IllegalArgumentException("Id cannot be null"); | |
70 | + | |
71 | + if(rightRepository.findByName(right.getName()).isEmpty()) | |
72 | + return false; | |
73 | + | |
57 | 74 | rightRepository.delete(right); |
58 | 75 | return true; |
59 | 76 | } |
... | ... | @@ -63,11 +80,8 @@ public class RightServiceJpa implements RightService { |
63 | 80 | if(name == null) |
64 | 81 | throw new IllegalArgumentException("Name cannot be null"); |
65 | 82 | |
66 | - Right right = rightRepository.findByName(name); | |
67 | - List<Right> rights = new ArrayList<>(); | |
68 | - rights.add(right); | |
83 | + return rightRepository.findByName(name); | |
69 | 84 | |
70 | - return rights; | |
71 | 85 | } |
72 | 86 | |
73 | 87 | @Override | ... | ... |
src/main/java/fr/plil/sio/persistence/jpa/UserRepository.java
0 → 100644
... | ... | @@ -0,0 +1,11 @@ |
1 | +package fr.plil.sio.persistence.jpa; | |
2 | + | |
3 | +import fr.plil.sio.persistence.api.User; | |
4 | +import org.springframework.data.jpa.repository.JpaRepository; | |
5 | + | |
6 | + | |
7 | +public interface UserRepository extends JpaRepository<User, Long>{ | |
8 | + | |
9 | + User findByName(String userName); | |
10 | + | |
11 | +} | ... | ... |
src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java
1 | 1 | package fr.plil.sio.persistence.jpa; |
2 | 2 | |
3 | +import fr.plil.sio.persistence.api.Group; | |
3 | 4 | import fr.plil.sio.persistence.api.Right; |
4 | 5 | import fr.plil.sio.persistence.api.User; |
5 | 6 | import fr.plil.sio.persistence.api.UserService; |
7 | +import org.springframework.beans.factory.annotation.Autowired; | |
6 | 8 | import org.springframework.stereotype.Service; |
7 | 9 | |
8 | 10 | @Service |
9 | 11 | public class UserServiceJpa implements UserService { |
10 | 12 | |
11 | - /*@Autowired | |
13 | + @Autowired | |
12 | 14 | UserRepository userRepository; |
13 | - GroupRepository groupRepository; */ | |
15 | + | |
16 | + @Autowired | |
17 | + GroupRepository groupRepository; | |
14 | 18 | |
15 | 19 | @Override |
16 | 20 | public User create(String name, String groupName) { |
17 | - /* if (name == null) { | |
18 | - throw new IllegalArgumentException("name cannot be null"); | |
19 | - } | |
21 | + if(name == null) | |
22 | + throw new IllegalArgumentException("Name cannot be null"); | |
23 | + if(groupName == null) | |
24 | + throw new IllegalArgumentException("Group name cannot be null"); | |
25 | + | |
26 | + Group group = groupRepository.findByName(groupName); | |
27 | + if(group == null) | |
28 | + throw new IllegalStateException("Group not found"); | |
20 | 29 | |
21 | - User user = userRepository.findByName(name); | |
22 | - Group group = groupRepository.findByName(name); | |
23 | - if (user != null) { | |
24 | - throw new IllegalStateException("a user with the same name already exists"); | |
25 | - } | |
26 | - user = new User(); | |
30 | + User user = new User(); | |
27 | 31 | user.setName(name); |
28 | 32 | user.setGroup(group); |
29 | - userRepository.save(user);*/ | |
30 | - return null; | |
33 | + userRepository.save(user); | |
34 | + | |
35 | + return user; | |
31 | 36 | } |
32 | 37 | |
33 | 38 | @Override |
34 | 39 | public boolean delete(String name) { |
35 | - /* if(name == null){ | |
36 | - return false; | |
37 | - } | |
38 | - User user = userRepository.findByName(name); | |
39 | - | |
40 | - userRepository.delete(user);*/ | |
40 | + | |
41 | 41 | |
42 | 42 | return false; |
43 | 43 | } |
44 | 44 | |
45 | 45 | @Override |
46 | 46 | public User findByName(String name) { |
47 | - /*if (name == null) { | |
48 | - throw new IllegalArgumentException("name cannot be null"); | |
49 | - } | |
50 | - return userRepository.findByName(name);*/ | |
51 | - return null; | |
47 | + | |
48 | + if(name == null) | |
49 | + throw new IllegalArgumentException("Name cannot be null"); | |
50 | + | |
51 | + return userRepository.findByName(name); | |
52 | 52 | } |
53 | 53 | |
54 | 54 | @Override |
55 | 55 | public boolean isUserHasRight(String userName, Right right) { |
56 | - /*if(userName == null) | |
57 | - throw new IllegalArgumentException("name cannot be null"); | |
58 | - if(right == null) | |
59 | - throw new IllegalArgumentException("Right cannot be null"); | |
60 | - */ | |
56 | + | |
61 | 57 | return false; |
62 | 58 | } |
63 | 59 | } | ... | ... |