GroupServiceJdbc.java 3.24 KB
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 java.util.ArrayList;
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;
    
    @Autowired
    private RightRepository rightRepository;
    
    @Autowired
    private UserRepository userRepository;

    @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");
        }
        
        if(userRepository.isUserHasRight(groupRepository.findByName(groupName).getId(), right.getId())){
            return false;
        }
        Group g = groupRepository.findByName(groupName);
        
        groupRepository.addRight(right, g);
        
        if(!(g.getRights()).contains(right)){
            g.addRight(right);
        }
        
        return true;
    }

    @Override
    public boolean removeRight(String groupName, Right right) {
        if(groupName==null){
            throw new IllegalArgumentException("Groupname cannot be null");
        }
        if(right==null){
            throw new IllegalArgumentException("right cannot be null");
        }
        Group group = groupRepository.findByName(groupName);
        if (group != null) {
            throw new IllegalArgumentException("a group with the same name already exists");
        }
        
        groupRepository.removeRight(group.getId(), right.getId());
        
        group.getRights().remove(right);
        
        return true;
    }
    
    public List<Right> addListRight(String groupName, Right right){
        Group g = groupRepository.findByName(groupName);
        g.getRights().add(right);
        return g.getRights();
    }
    
    
}