/**
   * 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);
    }
  }
  /**
   * 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);
    }
  }