private UserJsonResponse handleError(
     UserJsonResponse userJsonResponse, String type, Locale locale) {
   Map<String, String> errors = new HashMap<String, String>();
   String message = messageSource.getMessage("message." + type + "Taken", null, locale);
   errors.put(type, message);
   userJsonResponse.setStatus("ERROR");
   userJsonResponse.setErrorsMap(errors);
   return userJsonResponse;
 }
  @RequestMapping(value = "/register", method = RequestMethod.POST)
  public @ResponseBody UserJsonResponse handleRegistration(
      @Valid @RequestBody User user,
      BindingResult result,
      Locale locale,
      Model model,
      HttpServletRequest request) {
    logger.info(user.toString());
    UserJsonResponse userJsonResponse = new UserJsonResponse();
    if (request.getSession().getAttribute("loggedIn") != null) {
      userJsonResponse.setStatus("SUCCESS");
      return userJsonResponse;
    }

    if (result.hasErrors()) {

      Map<String, String> errors = new HashMap<String, String>();
      List<FieldError> fieldErrors = result.getFieldErrors();
      for (FieldError fieldError : fieldErrors) {
        String[] resolveMessageCodes = result.resolveMessageCodes(fieldError.getCode());
        String string = resolveMessageCodes[0];
        // System.out.println("resolveMessageCodes : "+string);
        String message =
            messageSource.getMessage(
                string + "." + fieldError.getField(),
                new Object[] {fieldError.getRejectedValue()},
                locale);
        // System.out.println("Meassage : "+message);
        errors.put(fieldError.getField(), message);
      }
      userJsonResponse.setStatus("ERROR");
      userJsonResponse.setErrorsMap(errors);
      return userJsonResponse;
    }

    UserDAO userDao = (UserDAO) context.getBean("userDao");
    if (userDao.get(user.getUsername()) != null) {
      return handleError(userJsonResponse, "username", locale);
    } else if (userDao.getUserBySerialNumber(user.getSerialNumber()) != null) {
      return handleError(userJsonResponse, "serialNumber", locale);
    } else {
      String hash = DaoHelper.getInstance().getPasswordHashing().getHashAndSalt(user.getPassword());
      user.setHash(hash);
      user.setRole("Student");
      Student student = new Student(user);
      student.setSubscriptionDate(new Date());
      logger.info(student.toString());
      userDao.create(student);
      userJsonResponse.setStatus("SUCCESS");
      return userJsonResponse;
    }
  }