diff --git a/src/main/java/fr/plil/sio/persistence/api/Group.java b/src/main/java/fr/plil/sio/persistence/api/Group.java index fd64cec..2b641e5 100644 --- a/src/main/java/fr/plil/sio/persistence/api/Group.java +++ b/src/main/java/fr/plil/sio/persistence/api/Group.java @@ -3,6 +3,7 @@ package fr.plil.sio.persistence.api; import java.io.Serializable; import java.util.LinkedList; import java.util.List; +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -36,7 +37,7 @@ public class Group implements Serializable { /** * List of rights. The list CANNOT contains duplicate rights. */ - @OneToMany(mappedBy = "group") + @OneToMany(mappedBy = "group", cascade = CascadeType.REMOVE) private List rights = new LinkedList<>(); public List getRights() { diff --git a/src/main/java/fr/plil/sio/persistence/api/Right.java b/src/main/java/fr/plil/sio/persistence/api/Right.java index b37eceb..60b0c28 100644 --- a/src/main/java/fr/plil/sio/persistence/api/Right.java +++ b/src/main/java/fr/plil/sio/persistence/api/Right.java @@ -3,6 +3,7 @@ package fr.plil.sio.persistence.api; import java.io.Serializable; import java.util.LinkedList; import java.util.List; +import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -32,6 +33,7 @@ public class Right implements Serializable{ /// the parent right @ManyToOne(optional = true) + @JoinColumn(name = "PARENT") private Right parent; @ManyToOne(optional = true) @@ -39,7 +41,7 @@ public class Right implements Serializable{ private Group group; /// the sibling right(s), eventually empty - @OneToMany(mappedBy = "parent") + @OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE) private List siblings = new LinkedList<>(); public List getSiblings() { diff --git a/src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java b/src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java index 99fe2d9..9642752 100644 --- a/src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java +++ b/src/main/java/fr/plil/sio/persistence/jpa/GroupRepository.java @@ -6,4 +6,5 @@ import org.springframework.data.jpa.repository.JpaRepository; public interface GroupRepository extends JpaRepository { Group findByName(String name); + } diff --git a/src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java b/src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java index 18463cb..e46b2a7 100644 --- a/src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java +++ b/src/main/java/fr/plil/sio/persistence/jpa/GroupServiceJpa.java @@ -3,6 +3,7 @@ package fr.plil.sio.persistence.jpa; import fr.plil.sio.persistence.api.Group; import fr.plil.sio.persistence.api.GroupService; import fr.plil.sio.persistence.api.Right; +import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -50,12 +51,54 @@ public class GroupServiceJpa implements GroupService { @Override public boolean addRight(String groupName, Right right) { - - return false; + + if(groupName == null) + throw new IllegalArgumentException("Group name cannot be null"); + + if(right == null) + throw new IllegalArgumentException("Right cannot be null"); + + Group group = groupRepository.findByName(groupName); + + if(group == null){ + throw new IllegalStateException("Group not found"); + } + + List list = group.getRights(); + + if(list.contains(right)) + return false; + + list.add(right); + group.setRights(list); + + return true; } @Override public boolean removeRight(String groupName, Right right) { + + if(groupName == null) + throw new IllegalArgumentException("Group name cannot be null"); + + if(right == null) + throw new IllegalArgumentException("Right cannot be null"); + + if(right.getId() == null) + throw new IllegalArgumentException("Not a right"); + + Group group = groupRepository.findByName(groupName); + + if(group == null) + throw new IllegalStateException("Group not found"); + + List list = group.getRights(); + + if(list.contains(right)){ + list.remove(right); + return true; + } + return false; } } diff --git a/src/main/java/fr/plil/sio/persistence/jpa/RightRepository.java b/src/main/java/fr/plil/sio/persistence/jpa/RightRepository.java index 6aa776b..cb8d515 100644 --- a/src/main/java/fr/plil/sio/persistence/jpa/RightRepository.java +++ b/src/main/java/fr/plil/sio/persistence/jpa/RightRepository.java @@ -1,10 +1,11 @@ package fr.plil.sio.persistence.jpa; import fr.plil.sio.persistence.api.Right; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; public interface RightRepository extends JpaRepository { - Right findByName(String name); + List findByName(String name); } diff --git a/src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java b/src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java index 4a3b794..c4e9e1b 100644 --- a/src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java +++ b/src/main/java/fr/plil/sio/persistence/jpa/RightServiceJpa.java @@ -1,6 +1,5 @@ package fr.plil.sio.persistence.jpa; -import fr.plil.sio.persistence.api.Group; import fr.plil.sio.persistence.api.Right; import fr.plil.sio.persistence.api.RightService; import java.util.ArrayList; @@ -37,15 +36,27 @@ public class RightServiceJpa implements RightService { if (name == null) { throw new IllegalArgumentException("name cannot be null"); } + if(parent == null){ + throw new IllegalArgumentException("parent cannot be null"); + } List 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; } @@ -54,6 +65,12 @@ public class RightServiceJpa implements RightService { 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; } @@ -63,11 +80,8 @@ public class RightServiceJpa implements RightService { if(name == null) throw new IllegalArgumentException("Name cannot be null"); - Right right = rightRepository.findByName(name); - List rights = new ArrayList<>(); - rights.add(right); + return rightRepository.findByName(name); - return rights; } @Override diff --git a/src/main/java/fr/plil/sio/persistence/jpa/UserRepository.java b/src/main/java/fr/plil/sio/persistence/jpa/UserRepository.java new file mode 100644 index 0000000..564ff78 --- /dev/null +++ b/src/main/java/fr/plil/sio/persistence/jpa/UserRepository.java @@ -0,0 +1,11 @@ +package fr.plil.sio.persistence.jpa; + +import fr.plil.sio.persistence.api.User; +import org.springframework.data.jpa.repository.JpaRepository; + + +public interface UserRepository extends JpaRepository{ + + User findByName(String userName); + +} diff --git a/src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java b/src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java index a9ef8ec..f8ba283 100644 --- a/src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java +++ b/src/main/java/fr/plil/sio/persistence/jpa/UserServiceJpa.java @@ -1,63 +1,59 @@ package fr.plil.sio.persistence.jpa; +import fr.plil.sio.persistence.api.Group; import fr.plil.sio.persistence.api.Right; import fr.plil.sio.persistence.api.User; import fr.plil.sio.persistence.api.UserService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceJpa implements UserService { - /*@Autowired + @Autowired UserRepository userRepository; - GroupRepository groupRepository; */ + + @Autowired + GroupRepository groupRepository; @Override public User create(String name, String groupName) { - /* if (name == null) { - throw new IllegalArgumentException("name cannot be null"); - } + if(name == null) + throw new IllegalArgumentException("Name cannot be null"); + if(groupName == null) + throw new IllegalArgumentException("Group name cannot be null"); + + Group group = groupRepository.findByName(groupName); + if(group == null) + throw new IllegalStateException("Group not found"); - User user = userRepository.findByName(name); - Group group = groupRepository.findByName(name); - if (user != null) { - throw new IllegalStateException("a user with the same name already exists"); - } - user = new User(); + User user = new User(); user.setName(name); user.setGroup(group); - userRepository.save(user);*/ - return null; + userRepository.save(user); + + return user; } @Override public boolean delete(String name) { - /* if(name == null){ - return false; - } - User user = userRepository.findByName(name); - - userRepository.delete(user);*/ + return false; } @Override public User findByName(String name) { - /*if (name == null) { - throw new IllegalArgumentException("name cannot be null"); - } - return userRepository.findByName(name);*/ - return null; + + if(name == null) + throw new IllegalArgumentException("Name cannot be null"); + + return userRepository.findByName(name); } @Override public boolean isUserHasRight(String userName, Right right) { - /*if(userName == null) - throw new IllegalArgumentException("name cannot be null"); - if(right == null) - throw new IllegalArgumentException("Right cannot be null"); - */ + return false; } } -- libgit2 0.21.2