/**
   * PUT /users : Updates an existing User.
   *
   * @param managedUserVM the user to update
   * @return the ResponseEntity with status 200 (OK) and with body the updated user, or with status
   *     400 (Bad Request) if the login or email is already in use, or with status 500 (Internal
   *     Server Error) if the user couldn't be updated
   */
  @PutMapping("/users")
  @Timed
  @Secured(AuthoritiesConstants.ADMIN)
  public ResponseEntity<ManagedUserVM> updateUser(@RequestBody ManagedUserVM managedUserVM) {
    log.debug("REST request to update User : {}", managedUserVM);
    Optional<User> existingUser = userRepository.findOneByEmail(managedUserVM.getEmail());
    if (existingUser.isPresent() && (!existingUser.get().getId().equals(managedUserVM.getId()))) {
      return ResponseEntity.badRequest()
          .headers(
              HeaderUtil.createFailureAlert(
                  "userManagement", "emailexists", "E-mail already in use"))
          .body(null);
    }
    existingUser = userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase());
    if (existingUser.isPresent() && (!existingUser.get().getId().equals(managedUserVM.getId()))) {
      return ResponseEntity.badRequest()
          .headers(
              HeaderUtil.createFailureAlert("userManagement", "userexists", "Login already in use"))
          .body(null);
    }
    userService.updateUser(
        managedUserVM.getId(),
        managedUserVM.getLogin(),
        managedUserVM.getFirstName(),
        managedUserVM.getLastName(),
        managedUserVM.getEmail(),
        managedUserVM.isActivated(),
        managedUserVM.getLangKey(),
        managedUserVM.getAuthorities());

    return ResponseEntity.ok()
        .headers(HeaderUtil.createAlert("userManagement.updated", managedUserVM.getLogin()))
        .body(new ManagedUserVM(userService.getUserWithAuthorities(managedUserVM.getId())));
  }
  /**
   * POST /users : Creates a new user.
   *
   * <p>Creates a new user if the login and email are not already used, and sends an mail with an
   * activation link. The user needs to be activated on creation.
   *
   * @param managedUserVM the user to create
   * @return the ResponseEntity with status 201 (Created) and with body the new user, or with status
   *     400 (Bad Request) if the login or email is already in use
   * @throws URISyntaxException if the Location URI syntax is incorrect
   */
  @PostMapping("/users")
  @Timed
  @Secured(AuthoritiesConstants.ADMIN)
  public ResponseEntity<?> createUser(@RequestBody ManagedUserVM managedUserVM)
      throws URISyntaxException {
    log.debug("REST request to save User : {}", managedUserVM);

    // Lowercase the user login before comparing with database
    if (userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()).isPresent()) {
      return ResponseEntity.badRequest()
          .headers(
              HeaderUtil.createFailureAlert("userManagement", "userexists", "Login already in use"))
          .body(null);
    } else if (userRepository.findOneByEmail(managedUserVM.getEmail()).isPresent()) {
      return ResponseEntity.badRequest()
          .headers(
              HeaderUtil.createFailureAlert(
                  "userManagement", "emailexists", "Email already in use"))
          .body(null);
    } else {
      User newUser = userService.createUser(managedUserVM);
      mailService.sendCreationEmail(newUser);
      return ResponseEntity.created(new URI("/api/users/" + newUser.getLogin()))
          .headers(HeaderUtil.createAlert("userManagement.created", newUser.getLogin()))
          .body(newUser);
    }
  }
 /**
  * DELETE /users/:login : delete the "login" User.
  *
  * @param login the login of the user to delete
  * @return the ResponseEntity with status 200 (OK)
  */
 @DeleteMapping("/users/{login:"******"}")
 @Timed
 @Secured(AuthoritiesConstants.ADMIN)
 public ResponseEntity<Void> deleteUser(@PathVariable String login) {
   log.debug("REST request to delete User: {}", login);
   userService.deleteUser(login);
   return ResponseEntity.ok()
       .headers(HeaderUtil.createAlert("userManagement.deleted", login))
       .build();
 }
 /**
  * GET /users/:login : get the "login" user.
  *
  * @param login the login of the user to find
  * @return the ResponseEntity with status 200 (OK) and with body the "login" user, or with status
  *     404 (Not Found)
  */
 @GetMapping("/users/{login:"******"}")
 @Timed
 public ResponseEntity<ManagedUserVM> getUser(@PathVariable String login) {
   log.debug("REST request to get User : {}", login);
   return userService
       .getUserWithAuthoritiesByLogin(login)
       .map(ManagedUserVM::new)
       .map(managedUserVM -> new ResponseEntity<>(managedUserVM, HttpStatus.OK))
       .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
 }
 /**
  * DELETE USER :login : delete the "login" User.
  *
  * @param login the login of the user to delete
  * @return the ResponseEntity with status 200 (OK)
  */
 @RequestMapping(
     value = "/users/{login:"******"}",
     method = RequestMethod.DELETE,
     produces = MediaType.APPLICATION_JSON_VALUE)
 @Timed
 @Secured(AuthoritiesConstants.ADMIN)
 public ResponseEntity<Void> deleteUser(@PathVariable String login) {
   log.debug("REST request to delete User: {}", login);
   userService.deleteUserInformation(login);
   return ResponseEntity.ok()
       .headers(HeaderUtil.createAlert("userManagement.deleted", login))
       .build();
 }
 /**
  * GET /users/:login : get the "login" user.
  *
  * @param login the login of the user to find
  * @return the ResponseEntity with status 200 (OK) and with body the "login" user, or with status
  *     404 (Not Found)
  */
 @RequestMapping(
     value = "/users/{login:"******"}",
     method = RequestMethod.GET,
     produces = MediaType.APPLICATION_JSON_VALUE)
 @Timed
 public ResponseEntity<ManagedUserDTO> getUser(@PathVariable String login) {
   log.debug("REST request to get User : {}", login);
   return userService
       .getUserWithAuthoritiesByLogin(login)
       .map(ManagedUserDTO::new)
       .map(managedUserDTO -> new ResponseEntity<>(managedUserDTO, HttpStatus.OK))
       .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
 }
  /**
   * POST /users : Creates a new user.
   *
   * <p>Creates a new user if the login and email are not already used, and sends an mail with an
   * activation link. The user needs to be activated on creation.
   *
   * @param managedUserDTO the user to create
   * @param request the HTTP request
   * @return the ResponseEntity with status 201 (Created) and with body the new user, or with status
   *     400 (Bad Request) if the login or email is already in use
   * @throws URISyntaxException if the Location URI syntaxt is incorrect
   */
  @RequestMapping(
      value = "/users",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  @Timed
  @Secured(AuthoritiesConstants.ADMIN)
  public ResponseEntity<?> createUser(
      @RequestBody ManagedUserDTO managedUserDTO, HttpServletRequest request)
      throws URISyntaxException {
    log.debug("REST request to save User : {}", managedUserDTO);

    // Lowercase the user login before comparing with database
    if (userRepository.findOneByLogin(managedUserDTO.getLogin().toLowerCase()).isPresent()) {
      return ResponseEntity.badRequest()
          .headers(
              HeaderUtil.createFailureAlert("userManagement", "userexists", "Login already in use"))
          .body(null);
    } else if (userRepository.findOneByEmail(managedUserDTO.getEmail()).isPresent()) {
      return ResponseEntity.badRequest()
          .headers(
              HeaderUtil.createFailureAlert(
                  "userManagement", "emailexists", "Email already in use"))
          .body(null);
    } else {
      User newUser = userService.createUser(managedUserDTO);
      String baseUrl =
          request.getScheme()
              + // "http"
              "://"
              + // "://"
              request.getServerName()
              + // "myhost"
              ":"
              + // ":"
              request.getServerPort()
              + // "80"
              request.getContextPath(); // "/myContextPath" or "" if deployed in root context
      mailService.sendCreationEmail(newUser, baseUrl);
      return ResponseEntity.created(new URI("/api/users/" + newUser.getLogin()))
          .headers(HeaderUtil.createAlert("userManagement.created", newUser.getLogin()))
          .body(newUser);
    }
  }