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

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

There is a newer version: 1.0.3
Show newest version
package com.github.iintelligas.service.impl;


import com.github.iintelligas.exception.DuplicateDataException;
import com.github.iintelligas.exception.InvalidDataException;
import com.github.iintelligas.persist.dto.Permission;
import com.github.iintelligas.persist.dto.Role;
import com.github.iintelligas.persist.entity.PermissionEntity;
import com.github.iintelligas.persist.entity.RoleEntity;
import com.github.iintelligas.repo.PermissionRepository;
import com.github.iintelligas.repo.RoleRepository;
import com.github.iintelligas.service.RoleService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import javax.persistence.NoResultException;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    private static final Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);
    
    private final RoleRepository roleRepository;
    
    private final PermissionRepository permissionRepository;

    @Autowired
    public RoleServiceImpl(RoleRepository roleRepository, PermissionRepository permissionRepository) {
        this.roleRepository = roleRepository;
        this.permissionRepository = permissionRepository;
    }

    @Override
    public Role addRole(Role role) {
        logger.info("Adding role = " + role.getName());
        try {
            RoleEntity roleEntity = new RoleEntity(role.getName(), role.getDescription());
            if(CollectionUtils.isNotEmpty(role.getPermissions())) {
                for(Permission permission : role.getPermissions()) {
                    PermissionEntity permissionEntity = permissionRepository.findByName(permission.getName());
                    if(permissionEntity == null) {
                        throw new InvalidDataException("Could not found PermissionEntity "+ permission.getName()+" from DB");
                    } else {
                        roleEntity.addPermission(permissionEntity);
                    }
                }

            }
            roleRepository.save(roleEntity);
            role = new Role(roleEntity);
            if(CollectionUtils.isNotEmpty(roleEntity.getPermissions())) {
                for(PermissionEntity permissionEntity : roleEntity.getPermissions()) {
                    role.addPermission(new Permission(permissionEntity));
                }
            }
            return role;
        }catch (DataIntegrityViolationException e) {
            throw new DuplicateDataException("RoleEntity "+role.getName()+" already present.");
        }
    }

    @Override
    public Role getRole(Long roleId) {
        logger.info("Get role by role id = " + roleId);
        RoleEntity role = roleRepository.findOne(roleId);
        if(role != null) {
            Role roleDto = new Role(role);
            for (PermissionEntity permissionEntity : role.getPermissions()) {
                roleDto.addPermission(new Permission(permissionEntity));
            }
            return roleDto;
        } else {
            return null;
        }

    }

    @Override
    public Role getRole(String roleName) {
        logger.info("Get role by role name = " + roleName);
        RoleEntity role = roleRepository.findByName(roleName);
        if(role == null) {
            return null;
        } else {
            return new Role(roleRepository.findByName(roleName));
        }
    }

    @Override
    public Role updateRole(Role role) {
        logger.info("Update role = " + role.getName());
        try {
//            RoleEntity storedRole = roleRepository.findByName(role.getName());
        	 RoleEntity storedRole = roleRepository.findOne(role.getId());
            List storedPermDtos = new ArrayList<>();
            storedRole.setDescription(role.getDescription());
            storedRole.setName(role.getName());
            if(storedRole.getPermissions() != null) {
                storedRole.getPermissions().clear();
            }
            if(! CollectionUtils.isEmpty(role.getPermissions())) {
                for(Permission permission : role.getPermissions()) {
                    PermissionEntity storedPermissionEntity = permissionRepository.findByName(permission.getName());
                    if(storedPermissionEntity == null) {
                        throw new InvalidDataException("Could not found permission "+permission.getName() +" from DB");
                    }
                    storedRole.addPermission(storedPermissionEntity);
                    storedPermDtos.add(new Permission(storedPermissionEntity));
                }
                role.setPermissions(storedPermDtos);
            }
            roleRepository.save(storedRole);
            return role;
        }catch (DataIntegrityViolationException e) {
            throw new DuplicateDataException("RoleEntity "+role.getName()+" already present.");
        }


    }

    @Override
    public void deleteRole(Long roleId) {
        RoleEntity toBeDeletedRole = roleRepository.findOne(roleId);
        if(toBeDeletedRole != null) {
            logger.info("Deleting role = " + toBeDeletedRole.getName());
            roleRepository.delete(toBeDeletedRole);
        }

    }

    @Override
    public List getRoles() {
        logger.info("Finding roles");
        List roleEntities = roleRepository.findAll();
        List roles = new ArrayList<>(roleEntities.size());
        for(RoleEntity role : roleEntities) {
            Role roleDto = new Role(role.getId(), role.getName(), role.getDescription());
            if(CollectionUtils.isNotEmpty(role.getPermissions()))
            {
                for (PermissionEntity permissionEntity : role.getPermissions())
                {
                    roleDto.addPermission(new Permission(permissionEntity));
                }
            }
            roles.add(roleDto);

        }
        return roles;
    }

    @Override
    public void assignPermissionToRole(Permission permission, Role role) {
        logger.info("Assigning permission= [ " +permission.getName() + " ] to role ->  " + role.getName());
        RoleEntity assignedRole = roleRepository.findByName(role.getName());
        if(assignedRole  == null) {
            throw new NoResultException("RoleEntity does not exist");
        }
        PermissionEntity assignedPerm = permissionRepository.findByName(permission.getName());
        if(assignedPerm == null) {
            throw new NoResultException("PermissionEntity does not exist");
        }
        assignedRole.addPermission(assignedPerm);
        roleRepository.save(assignedRole);

    }

    @Override
    public void addRoles(List roles) {
        List roleEntities = new ArrayList<>();
        for(Role dto : roles) {
            roleEntities.add(new RoleEntity(dto.getName(), dto.getDescription()));
        }
        roleRepository.save(roleEntities);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy