All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.iintelligas.service.impl.PermissionServiceImpl Maven / Gradle / Ivy

The newest version!
package com.github.iintelligas.service.impl;


import com.github.iintelligas.exception.DuplicateDataException;
import com.github.iintelligas.persist.dto.Permission;
import com.github.iintelligas.persist.entity.PermissionEntity;
import com.github.iintelligas.repo.PermissionRepository;
import com.github.iintelligas.service.PermissionService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Profile({"ma", "production"})
@Service
public class PermissionServiceImpl implements PermissionService {
    
    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(PermissionServiceImpl.class);


    private final PermissionRepository permissionRepository;

    @Autowired
    public PermissionServiceImpl(PermissionRepository permissionRepository) {
        this.permissionRepository = permissionRepository;
    }

    @Override
    public Permission addPermission(Permission permission) {
        logger.info("Adding permission = " + permission.getName());
        try {
            return new Permission(permissionRepository.save(new PermissionEntity(permission.getName(), permission.getDescription())));
        }catch (DataIntegrityViolationException e) {
            throw new DuplicateDataException("Permission '"+permission.getName()+"' already present.");
        }
    }

    @Override
    public Permission getPermission(Long id) {
        logger.info("Get permission by permission id = " + id);
        return new Permission(permissionRepository.findOne(id));

    }

    @Override
    public Permission getPermission(String name) {
        logger.info("Get permission by permission name = " + name);
        PermissionEntity permissionEntityEntity = permissionRepository.findByName(name);
        if(permissionEntityEntity == null) {
            return null;
        } else {
            return new Permission(permissionEntityEntity);
        }
    }

    @Override
    public Permission updatePermission(Permission permission) {
        logger.info("Update permission = " + permission.getName());
        try {
            PermissionEntity permEntity = new PermissionEntity(permission.getName(), permission.getDescription());
            permEntity.setId(permission.getId());
            permissionRepository.save(permEntity);
            return permission;
        }catch (DataIntegrityViolationException e) {
            throw new DuplicateDataException("Permission "+permission.getName()+" already present.");
        }
    }

    @Override
    public void deletePermission(Long id) {
            permissionRepository.delete(id);
    }

    @Override
    public List getPermissions() {
        logger.info("Finding permissionEntities");
        List permissionEntities = permissionRepository.findAll();
        List permissions = new ArrayList<>(permissionEntities.size());
        for (PermissionEntity permissionEntity : permissionEntities) {
            permissions.add(new Permission(permissionEntity));
        }
        return permissions;
    }

    @Override
    public void addPermissions(List permissions) {
        List permissionEntities = new ArrayList<>();
        for(Permission dto : permissions) {
            permissionEntities.add(new PermissionEntity(dto.getName(), dto.getDescription()));
        }
        permissionRepository.save(permissionEntities);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy