RightServiceJpa.java 2.08 KB
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;
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");
        }
       
        List<Right> rights = new ArrayList<>();
        
        Right right = new Right();
        right.setName(name);
        right.setParent(parent);
        rights.add(right);
        rightRepository.save(rights);
        
        return right;
    }

    @Override
    public boolean delete(Right right) {
        if(right == null)
            throw new IllegalArgumentException("Right cannot be null");
       
        rightRepository.delete(right);        
        return true;
    }

    @Override
    public List<Right> findByName(String name) {        
         if(name == null)
            throw new IllegalArgumentException("Name cannot be null");
                 
        Right right = rightRepository.findByName(name);
        List<Right> rights = new ArrayList<>();
        rights.add(right);
        
        return rights;
    }

    @Override
    public Right findOne(Long id) {
        
        if(id == null)
            throw new IllegalArgumentException("Id cannot be null");        
        
        return rightRepository.findOne(id);
    }
}