com.github.iintelligas.controller.RoleRestController Maven / Gradle / Ivy
package com.github.iintelligas.controller;
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.bean.RoleWrapper;
import com.github.iintelligas.service.RoleService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/admin/rest/profile")
public class RoleRestController
{
private static final Logger log = LoggerFactory.getLogger(RoleRestController.class);
private final RoleService roleService;
@Autowired
public RoleRestController(RoleService roleService) {
this.roleService = roleService;
}
@GetMapping(value = "/findAllRoles")
public @ResponseBody
ResponseEntity findAllRoles()
{
try
{
if (log.isDebugEnabled())
{
log.debug("--------------------- #START RoleRestController.findAllRoles ----------------");
}
List roles = roleService.getRoles();
if (log.isDebugEnabled())
{
log.debug("Number of role found : " + roles.size());
}
if (log.isDebugEnabled())
{
log.debug("--------------------- #END RoleRestController.findAllRoles ----------------");
}
return new ResponseEntity<>(new RoleWrapper(roles), HttpStatus.OK);
}
catch (Exception ex)
{
log.error("Unwanted exception.", ex);
return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
}
}
@PostMapping(value = "/addRole")
public @ResponseBody
ResponseEntity addRole(final @RequestBody Role role)
{
if (log.isDebugEnabled())
{
log.debug("--------------------- #START RoleRestController.addRole ----------------");
}
RoleWrapper wrapper = new RoleWrapper();
try
{
if (!validateRoleRequest(role))
{
wrapper.setMessage("Either RoleEntity name or one of the Permission name under role is missing.");
return new ResponseEntity<>(wrapper, HttpStatus.BAD_REQUEST);
}
List roles = new ArrayList<>(1);
Role addedRole = roleService.addRole(role);
roles.add(addedRole);
wrapper.setRoles(roles);
wrapper.setMessage("Successfully Added RoleEntity : "+role.getName());
if (log.isDebugEnabled())
{
log.debug("--------------------- #END RoleRestController.addRole ----------------");
}
return new ResponseEntity<>(wrapper, HttpStatus.OK);
}
catch (InvalidDataException ie) {
wrapper.setMessage(ie.getMessage());
return new ResponseEntity<>(wrapper, HttpStatus.NOT_ACCEPTABLE);
}
catch (DuplicateDataException de) {
wrapper.setMessage("RoleEntity "+role.getName()+" already present. Please change it.");
return new ResponseEntity<>(wrapper, HttpStatus.NOT_ACCEPTABLE);
}
catch (Exception ex)
{
log.error("Unwanted exception.", ex);
return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
}
}
@PutMapping(value = "/updateRole")
public @ResponseBody
ResponseEntity updateRole(final @RequestBody Role role)
{
if (log.isDebugEnabled())
{
log.debug("--------------------- #START RoleRestController.updateRole ----------------");
}
RoleWrapper wrapper = new RoleWrapper();
try
{
if (!validateRoleRequest(role))
{
wrapper.setMessage("Either RoleEntity name or one of the Permission name under role is missing.");
return new ResponseEntity<>(wrapper, HttpStatus.BAD_REQUEST);
}
List roles = new ArrayList<>(1);
Role storedRole = roleService.getRole(role.getName());
if(storedRole == null) {
wrapper.setMessage("Could not found role "+role.getName());
return new ResponseEntity<>(wrapper, HttpStatus.NOT_ACCEPTABLE);
}
storedRole = roleService.updateRole(role);
roles.add(storedRole);
wrapper.setRoles(roles);
wrapper.setMessage("Successfully updated RoleEntity : "+role.getName());
if (log.isDebugEnabled())
{
log.debug("--------------------- #END RoleRestController.updateRole ----------------");
}
return new ResponseEntity<>(wrapper, HttpStatus.OK);
}
catch (InvalidDataException ie) {
wrapper.setMessage(ie.getMessage());
return new ResponseEntity<>(wrapper, HttpStatus.NOT_ACCEPTABLE);
}
catch (DuplicateDataException de) {
wrapper.setMessage("RoleEntity "+role.getName()+" already present. Please change it.");
return new ResponseEntity<>(wrapper, HttpStatus.NOT_ACCEPTABLE);
}
catch (Exception ex)
{
log.error("Unwanted exception.", ex);
return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
}
}
@DeleteMapping(value = "/deleteRole")
public @ResponseBody
ResponseEntity deleteRole(final @RequestBody Role role)
{
if (log.isDebugEnabled())
{
log.debug("--------------------- #START RoleRestController.deleteRole ----------------");
}
RoleWrapper wrapper = new RoleWrapper();
try
{
if (!validateRoleRequest(role))
{
wrapper.setMessage("RoleEntity name is required.");
return new ResponseEntity<>(wrapper, HttpStatus.BAD_REQUEST);
}
List roles = new ArrayList<>(1);
Role storedRole = roleService.getRole(role.getName());
if(storedRole == null) {
wrapper.setMessage("Could not found role "+role.getName());
return new ResponseEntity<>(wrapper, HttpStatus.NOT_ACCEPTABLE);
}
roleService.deleteRole(storedRole.getId()); //TODO RolePermission data also
wrapper.setMessage("Successfully deleted RoleEntity : "+role.getName());
if (log.isDebugEnabled())
{
log.debug("--------------------- #END RoleRestController.deleteRole ----------------");
}
return new ResponseEntity<>(wrapper, HttpStatus.OK);
}
catch (DuplicateDataException de) {
wrapper.setMessage("RoleEntity "+role.getName()+" already present. Please change it.");
return new ResponseEntity<>(wrapper, HttpStatus.NOT_ACCEPTABLE);
}
catch (Exception ex)
{
log.error("Unwanted exception.", ex);
return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
}
}
private boolean validateRoleWrapperRequest(RoleWrapper roles) {
if(roles == null || roles.getRoles() == null || roles.getRoles().size() <= 0) {
return false;
}
for(Role role : roles.getRoles()) {
if(!validateRoleRequest(role)) {
return false;
}
}
return true;
}
private boolean validateRoleRequest(Role role) {
if(role == null || StringUtils.isBlank(role.getName())) {
return false;
} else if(CollectionUtils.isNotEmpty(role.getPermissions())) {
for(Permission permission : role.getPermissions()) {
if(StringUtils.isBlank(permission.getName())) {
return false;
}
}
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy