/**
   * This method used for getting the information of user
   *
   * @param userId
   * @param model
   * @param response
   */
  @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
  public void getUserByUserId(
      int userId, ModelMap model, HttpServletResponse response, HttpSession session) {
    UserSessionInfo userSession =
        (UserSessionInfo) session.getAttribute(ApplicationConstants.USER_SESSION_KEY);
    try {
      SimpleDateFormat dobFormat = new SimpleDateFormat("MM/dd/yyyy");
      Users user = new Users();
      if (user.getUserId() != userId) {
        user = userService.getUserByUserId(userId);

        if (user != null && user.getUserId() > 0) {

          JSONObject jsonObject = new JSONObject();
          jsonObject.put("confirmPassword", user.getPassword());
          jsonObject.put("email", user.getEmailId().toString());
          jsonObject.put("extn1", "" + user.getExtn1());
          jsonObject.put("extn2", "" + user.getExtn2());
          jsonObject.put("firstName", user.getFirstName().toString());
          jsonObject.put("lastName", user.getLastName().toString());
          jsonObject.put("role", user.getRoles().getRoleName());
          jsonObject.put("mobileNumber1", user.getMobileNumber1().toString());
          jsonObject.put("mobileNumber2", "" + user.getMobileNumber2());
          jsonObject.put("password", user.getPassword().toString());
          jsonObject.put("active", user.getStatus().toString());
          jsonObject.put("userName", user.getUserName().toString());
          jsonObject.put("workingPhone1", user.getWorkingPhone1().toString());
          jsonObject.put("workingPhone2", "" + user.getWorkingPhone2());
          jsonObject.put("department", "" + user.getDepartment());
          jsonObject.put("employeeId", "" + user.getEmployeeId());

          response.getWriter().write(jsonObject.toString());
        } else {
          log.error("UserId not found::::" + userId);
        }
      }
    } catch (Exception e) {
      log.error("Exception in getUserByUserId::" + ExceptionUtils.getStackTrace(e));
    }
  }
  /**
   * This Method used to save the newly created user
   *
   * @param user
   * @param result
   * @param model
   * @param session
   * @param locale
   * @return
   */
  @RequestMapping(value = "/saveNewUser", method = RequestMethod.POST)
  public String saveUser(
      @ModelAttribute("user") Users user,
      BindingResult result,
      ModelMap model,
      HttpSession session,
      Locale locale) {

    UserSessionInfo userSessionInfo =
        (UserSessionInfo) session.getAttribute(ApplicationConstants.USER_SESSION_KEY);
    List<Roles> roleInfoList = null;
    try {
      createUserValidator.userValidate(user, result, userSessionInfo);

      if (!result.hasErrors()) {
        // Save
        int UserId = userService.saveUser(user);

        if (UserId == 0) {
          log.info("User " + user.getUserName() + " save failed.");
          model.addAttribute(
              "Error",
              messageSource.getMessage(
                  "user.save.failure", new String[] {user.getUserName()}, locale));
          model.addAttribute("user", user);
          model.addAttribute("roleInfoList", roleInfoList);
          model.addAttribute("headers", "Create User");
          if (user.getUserId() != 0) {
            model.addAttribute("Save", "Modify");
          } else {
            model.addAttribute("Save", "Save");
          }

          return "createUserPage";

        } else {
          log.info("User " + user.getUserName() + " saved successfully.");
          model.addAttribute("response", "User \"" + user.getUserName() + "\" saved successfully");
          model.addAttribute("errorresponse", "");
          model.addAttribute("searchUserName", "");
          model.addAttribute("headers", "Users");
          return "usersList";
        }

      } else {

        roleInfoList = userService.getRoleInfo();
        model.addAttribute("roleInfoList", roleInfoList);
        model.addAttribute("headers", "Create User");
        if (user.getUserId() != 0) {
          model.addAttribute("Save", "Modify");
        } else {
          model.addAttribute("Save", "Save");
        }
        return "createUserPage";
      }

    } catch (Exception e) {
      model.addAttribute("user", user);
      model.addAttribute("response", "");
      model.addAttribute("errorresponse", "Failed to save user!");
      model.addAttribute("roleInfoList", roleInfoList);
      model.addAttribute("headers", "Create User");
      model.addAttribute("Save", "Save");
      log.error("Exception in saveUser::" + ExceptionUtils.getStackTrace(e));
    }
    model.addAttribute(
        "Error",
        messageSource.getMessage("user.save.failure", new String[] {user.getUserName()}, locale));
    return "createUserPage";
  }