1 package org.usermanagement.service;
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Service;
8 import org.springframework.transaction.annotation.Propagation;
9 import org.springframework.transaction.annotation.Transactional;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
34 @Transactional(propagation = Propagation.REQUIRED, readOnly =
true)
37 private static final Logger LOGGER = LoggerFactory.getLogger(
RoleService.class);
57 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
59 roleValidator.validateRole(role);
60 Set<PermissionEntity> permissionEntities =
new HashSet<>();
61 List<PermissionEntity> permissionEntityList = permissionRepository.findAll();
62 for (Long permissionId : role.getPermissionId()) {
64 .filter((entity) -> entity.getPermissionId().equals(permissionId)).findFirst()
68 permissionEntities.add(permissionEntity);
70 LOGGER.error(
"Permission with id '" + permissionId +
"' not found.");
72 messageUtil.getAttributeNotFound(
"permission"));
77 roleRepository.save(roleEntity);
79 LOGGER.info(
"Role with name '" + roleEntity.
getName() +
"' created successfully.");
83 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
84 public List<
Role> getAllRole() {
85 List<RoleEntity> roleEntityList = roleRepository.findAll();
90 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
91 public Set<
RoleEntity> getRolesById(final List<Long> roleIds) {
92 Set<RoleEntity> roleEntities =
new HashSet<>();
93 List<RoleEntity> roleEntityList = roleRepository.findAll();
94 for (Long roleId : roleIds) {
95 RoleEntity roleEntity = roleEntityList.parallelStream()
96 .filter((entity) -> entity.getRoleId().equals(roleId)).findFirst().orElse(null);
99 roleEntities.add(roleEntity);
101 LOGGER.error(
"Role with role id '" + roleId +
"' not found. Error: " 102 + messageUtil.getAttributeNotFound(
"roles"));
109 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
110 public
Role getRoleById(final Long roleId) {
111 RoleEntity roleEntity = roleRepository.findByRoleId(roleId);
113 LOGGER.error(
"Role with role id '" + roleId +
"' not found. Error: " 114 + messageUtil.getAttributeInvalid(
"role_id", roleId +
""));
116 messageUtil.getAttributeInvalid(
"role_id", roleId +
""));
122 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
123 public
void deleteRoleById(final Long roleId) {
125 RoleEntity roleEntity = roleRepository.findByRoleId(roleId);
127 LOGGER.error(
"Role with role id '" + roleId +
"' not found. Error: " 128 + messageUtil.getAttributeInvalid(
"role_id", roleId +
""));
130 messageUtil.getAttributeInvalid(
"role_id", roleId +
""));
133 Set<UserEntity> userEntityList = userRepository.findByRoles_roleId(roleId);
134 if (userEntityList.size() > 0) {
136 for (
UserEntity userEntity : userEntityList) {
137 users += !
"".equals(users) ?
"," + userEntity.getName() : userEntity.getName();
139 LOGGER.error(
"Role with role id '" + roleId +
"' not allowed to delete. Error: " 140 + messageUtil.getAttributeDeletionNotAllowed(roleEntity.
getName(), users));
142 messageUtil.getAttributeDeletionNotAllowed(roleEntity.
getName(), users));
144 roleRepository.delete(roleEntity);
146 LOGGER.info(
"Role(roleId: " + roleId +
") deleted successfully.");
149 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
150 public
Permission getRolesByPermissionId(final Long permissionId) {
152 PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
154 Set<RoleEntity> roleEntityList =
155 roleRepository.findByPermissions_permissionId(permissionId);
160 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
161 public
Role updateRole(final Long roleId, final
Role role) {
163 roleValidator.validateUpdateRole(role, roleId);
165 RoleEntity roleEntity = roleRepository.findByRoleId(roleId);
168 LOGGER.error(
"Role with role id '" + roleId +
"' not found. Error: " 169 + messageUtil.getAttributeInvalid(
"role_id", roleId +
""));
171 messageUtil.getAttributeInvalid(
"role_id", roleId +
""));
174 if (role.getPermissionId() != null) {
176 for (Long permissionId : role.getPermissionId()) {
178 permissionRepository.findByPermissionId(permissionId);
179 if (permissionEntity != null) {
185 roleRepository.save(roleEntity);
187 LOGGER.info(
"Role updated successfully (roleId: " + roleId +
")");
192 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
194 PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
197 LOGGER.error(
"Permission with permissionId '" + permissionId +
"' not found. Error: " 198 + messageUtil.getAttributeInvalid(
"permissionId", permissionId +
""));
200 messageUtil.getAttributeInvalid(
"permissionId", permissionId +
""));
202 permissionEntity.
getRoles().clear();
203 if (request.getRoles() != null) {
204 for (
Role role : request.getRoles()) {
205 RoleEntity roleEntity = roleRepository.findByRoleId(role.getRoleId());
206 permissionEntity.
getRoles().add(roleEntity);
209 permissionRepository.save(permissionEntity);
211 LOGGER.info(
"Roles assigned with permission successfully (permissionId: " + permissionId +
")");
215 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
216 public List<
Role> getRoleByName(final Set<String> role) {
217 List<Role> roles =
new ArrayList<Role>();
218 List<RoleEntity> roleEntities = roleRepository.findByNameIn(role);
220 LOGGER.error(
"Roles with name '" + role +
"' not found. Error: " 221 + messageUtil.getAttributeInvalid(
"role", role +
""));
223 messageUtil.getAttributeInvalid(
"role", role +
""));
227 .equalsIgnoreCase(roleEntity.getStatusEntity().getStatus())) {
240 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
241 public
Role getUserByRoleId(final Long roleId) {
242 RoleEntity roleEntity = roleRepository.findByRoleId(roleId);
244 LOGGER.error(
"Role with role id '" + roleId +
"' not found. Error: " 245 + messageUtil.getAttributeInvalid(
"role_id", roleId +
""));
247 messageUtil.getAttributeInvalid(
"role_id", roleId +
""));
Set< PermissionEntity > getPermissions()
static RoleEntity toRoleEntity(final Role role, final Set< PermissionEntity > permissionEntitySet)
static List< Role > toAllRoleResponse(final List< RoleEntity > roleEntityList)
ASSIGN_ROLES_TO_PERMISSION
static Permission toPermissionByRole(final Set< RoleEntity > roleEntityList, final PermissionEntity permissionEntity)
static boolean isNull(final Object obj)
Set< RoleEntity > getRoles()
static Role toRole(final RoleEntity roleEntity, final boolean withPermissions, final boolean withUsers)
static RoleEntity toUpateRoleEntity(final Role role, final RoleEntity roleEntity)