Open Kilda Java Documentation
RoleValidator.java
Go to the documentation of this file.
1 package org.usermanagement.validator;
2 
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.stereotype.Component;
7 
8 import java.util.List;
9 
16 
17 @Component
18 public class RoleValidator {
19 
20  private static final Logger LOGGER = LoggerFactory.getLogger(RoleValidator.class);
21 
22  @Autowired
23  private MessageUtils messageUtil;
24 
25  @Autowired
26  private RoleRepository roleRepository;
27 
28  public void validateRole(final Role role) {
29 
30  if (ValidatorUtil.isNull(role.getName())) {
31  LOGGER.error("Validation fail for role(name: " + role.getName()
32  + "). Error: " + messageUtil.getAttributeNotNull("name"));
33  throw new RequestValidationException(messageUtil.getAttributeNotNull("name"));
34  }
35 
36  List<RoleEntity> roleEntityList = roleRepository.findAll();
37  if (roleEntityList.parallelStream()
38  .anyMatch((roleEntity) -> roleEntity.getName().equalsIgnoreCase(role.getName()))) {
39  LOGGER.error("Validation fail for role(name: " + role.getName()
40  + "). Error: " + messageUtil.getAttributeUnique("name"));
41  throw new RequestValidationException(messageUtil.getAttributeUnique("name"));
42  }
43  }
44 
45  public void validateUpdateRole(final Role role, Long roleId) {
46 
47  if (ValidatorUtil.isNull(roleId)) {
48  LOGGER.error("Validation fail for role(role_id: " + roleId + "). Error: "
49  + messageUtil.getAttributeNotNull("role_id"));
50  throw new RequestValidationException(messageUtil.getAttributeNotNull("role_id"));
51  } else if (ValidatorUtil.isNull(role.getName()) && ValidatorUtil.isNull(role.getStatus())
53  LOGGER.error("Validation fail for role(name, status, description and permissions: " + role.getName() + ","
54  + role.getStatus() + "," + role.getDescription() + "," + role.getPermissions() + "). Error: "
55  + messageUtil.getAttributeNotNull("name, status, description and permissions"));
57  messageUtil.getAttributeNotNull("name, status, description and permissions"));
58  }
59 
60  if (!ValidatorUtil.isNull(role.getName())) {
61  RoleEntity roleEntity = roleRepository.findByRoleId(roleId);
62  if (!roleEntity.getName().equalsIgnoreCase(role.getName())) {
63  validateRole(role);
64  }
65  }
66  }
67 }
static boolean isNull(final Object obj)
List< Long > getPermissionId()
Definition: Role.java:62
void validateUpdateRole(final Role role, Long roleId)
List< Permission > getPermissions()
Definition: Role.java:134