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

com.github.iintelligas.controller.UserRestController Maven / Gradle / Ivy

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

import com.github.iintelligas.bean.UserWrapper;
import com.github.iintelligas.exception.DuplicateDataException;
import com.github.iintelligas.exception.InvalidDataException;
import com.github.iintelligas.persist.dto.Role;
import com.github.iintelligas.persist.dto.User;
import com.github.iintelligas.service.UserService;
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.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/admin/rest/profile")
public class UserRestController
{
    private static final Logger log = LoggerFactory.getLogger(UserRestController.class);

    private final UserService userService;

    private final PasswordEncoder passwordEncoder;

    @Autowired
    public UserRestController(UserService userService, PasswordEncoder passwordEncoder) {
        this.userService = userService;
        this.passwordEncoder = passwordEncoder;
    }

    @GetMapping(value = "/findAllUsers")
    public @ResponseBody
    ResponseEntity findAllUsers()
    {
        try
        {
            if (log.isDebugEnabled())
            {
                log.debug("--------------------- #START UserRestController.findAllUsers ----------------");
            }
            List userDtos = userService.getUsers();

            if (log.isDebugEnabled())
            {
                log.debug("Number of user found : " + userDtos.size());
            }
            if (log.isDebugEnabled())
            {
                log.debug("--------------------- #END UserRestController.findAllUsers ----------------");
            }
            return new ResponseEntity<>(new UserWrapper(userDtos), HttpStatus.OK);
        }
        catch (Exception ex)
        {
            log.error("Unwanted exception.", ex);
            return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);
        }
    }


    @PostMapping(value = "/addUser")
    public @ResponseBody
    ResponseEntity addUser(final @RequestBody User user)
    {
        if (log.isDebugEnabled())
        {
            log.debug("--------------------- #START UserRestController.addUser ----------------");
        }

        UserWrapper wrapper = new UserWrapper();
        try
        {

            if (!validateUserRequest(user))
            {
                wrapper.setMessage("Required field is missing.");
                return new ResponseEntity<>(wrapper, HttpStatus.BAD_REQUEST);
            }
            List userDtos = new ArrayList<>(1);
            String plainPassword = user.getPassword();
            user.setPassword(passwordEncoder.encode(plainPassword));
            User addedUser = userService.addUser(user);
            userDtos.add(addedUser);
            wrapper.setUsers(userDtos);
            wrapper.setMessage("Successfully Added UserEntity : "+user.getUsername());
            if (log.isDebugEnabled())
            {
                log.debug("--------------------- #END UserRestController.addUser ----------------");
            }
            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("UserEntity "+user.getUsername()+" 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 = "/updateUser")
    public @ResponseBody ResponseEntity updateUser(final @RequestBody User user)
    {
        if (log.isDebugEnabled())
        {
            log.debug("--------------------- #START UserRestController.updateUser ----------------");
        }

        UserWrapper wrapper = new UserWrapper();
        try
        {
            if (!validateUserRequest(user))
            {
                wrapper.setMessage("Either UserEntity name or one of the RoleEntity name under user is missing.");
                return new ResponseEntity<>(wrapper, HttpStatus.BAD_REQUEST);
            }
            List userDtos = new ArrayList<>(1);
            User storedUser = userService.getUser(user.getName());
            if(storedUser == null) {
                wrapper.setMessage("Could not found user "+user.getName());
                return new ResponseEntity<>(wrapper, HttpStatus.NOT_ACCEPTABLE);
            }
            storedUser = userService.updateUser(user);
            userDtos.add(storedUser);
            wrapper.setUsers(userDtos);

            wrapper.setMessage("Successfully updated UserEntity : "+user.getName());
            if (log.isDebugEnabled())
            {
                log.debug("--------------------- #END UserRestController.updateUser ----------------");
            }
            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("UserEntity "+user.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 = "/deleteUser")
    public @ResponseBody ResponseEntity deleteUser(final @RequestBody User user)
    {
        if (log.isDebugEnabled())
        {
            log.debug("--------------------- #START UserRestController.deleteUser ----------------");
        }

        UserWrapper wrapper = new UserWrapper();
        try
        {

            if (!validateUserRequest(user))
            {
                wrapper.setMessage("UserEntity name is required.");
                return new ResponseEntity<>(wrapper, HttpStatus.BAD_REQUEST);
            }
            List userDtos = new ArrayList<>(1);
            User storedUser = userService.getUser(user.getName());
            if(storedUser == null) {
                wrapper.setMessage("Could not found user "+user.getName());
                return new ResponseEntity<>(wrapper, HttpStatus.NOT_ACCEPTABLE);
            }
            userService.deleteUser(storedUser.getId());   //TODO UserRole data also

            wrapper.setMessage("Successfully deleted UserEntity : "+user.getName());
            if (log.isDebugEnabled())
            {
                log.debug("--------------------- #END UserRestController.deleteUser ----------------");
            }
            return new ResponseEntity<>(wrapper, HttpStatus.OK);
        }
        catch (DuplicateDataException de) {
            wrapper.setMessage("UserEntity "+user.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 validateUserWrapperRequest(UserWrapper users) {
        if(users == null || users.getUsers() == null || users.getUsers().size() <= 0) {
            return false;
        }
        for(User userDto : users.getUsers()) {
            if(!validateUserRequest(userDto)) {
                return false;
            }
        }
        return true;
    }

    private boolean validateUserRequest(User user) {
        if(user == null || StringUtils.isBlank(user.getFirstName())
                || StringUtils.isBlank(user.getLastName())
                || StringUtils.isBlank(user.getEmail())
                || StringUtils.isBlank(user.getPassword())
                || StringUtils.isBlank(user.getUsername())
                ) {
            return false;
        } else if(CollectionUtils.isNotEmpty(user.getRoles())) {
            for(Role role : user.getRoles()) {
                if(StringUtils.isBlank(role.getName())) {
                    return false;
                }
            }
        }
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy