GroupServiceJpa.java 2.91 KB
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;

@Service
public class GroupServiceJpa 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 = groupRepository.findByName(name);
        if(group == null)
            return false;
        
        groupRepository.delete(group);
        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)
            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<Right> 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<Right> list = group.getRights();
        
        if(list.contains(right)){
            list.remove(right);
            return true;
        }
                        
        return false;
    }
}