@Override protected void onBindAndValidate( HttpServletRequest request, Object command, BindException errors) { StudentAccountForm accountForm = (StudentAccountForm) command; StudentUserDetails userDetails = (StudentUserDetails) accountForm.getUserDetails(); if (accountForm.isNewAccount()) { userDetails.setSignupdate(Calendar.getInstance().getTime()); Calendar birthday = Calendar.getInstance(); int birthmonth = Integer.parseInt(accountForm.getBirthmonth()); int birthdate = Integer.parseInt(accountForm.getBirthdate()); birthday.set(Calendar.MONTH, birthmonth - 1); // month is 0-based birthday.set(Calendar.DATE, birthdate); userDetails.setBirthday(birthday.getTime()); } getValidator().validate(accountForm, errors); }
/** * On submission of the signup form, a user is created and saved to the data store. * * @see * org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse, java.lang.Object, * org.springframework.validation.BindException) */ @Override @Transactional( rollbackFor = { DuplicateUsernameException.class, ObjectNotFoundException.class, PeriodNotFoundException.class }) protected synchronized ModelAndView onSubmit( HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { String domain = "http://" + request.getServerName(); String domainWithPort = domain + ":" + request.getLocalPort(); String referrer = request.getHeader("referer"); String registerUrl = "/webapp/student/registerstudent.html"; if (referrer != null && (referrer.contains(domain + registerUrl) || referrer.contains(domainWithPort + registerUrl))) { StudentAccountForm accountForm = (StudentAccountForm) command; StudentUserDetails userDetails = (StudentUserDetails) accountForm.getUserDetails(); if (accountForm.isNewAccount()) { try { // get the first name and last name String firstName = userDetails.getFirstname(); String lastName = userDetails.getLastname(); // check if first name and last name only contain letters Pattern pattern = Pattern.compile("[a-zA-Z]*"); Matcher firstNameMatcher = pattern.matcher(firstName); Matcher lastNameMatcher = pattern.matcher(lastName); if (!firstNameMatcher.matches()) { // first name contains non letter characters errors.rejectValue("userDetails.firstname", "error.firstname-illegal-characters"); return showForm(request, response, errors); } if (!lastNameMatcher.matches()) { // last name contains non letter characters errors.rejectValue("userDetails.lastname", "error.lastname-illegal-characters"); return showForm(request, response, errors); } User user = userService.createUser(userDetails); Projectcode projectcode = new Projectcode(accountForm.getProjectCode()); studentService.addStudentToRun(user, projectcode); } catch (DuplicateUsernameException e) { errors.rejectValue( "userDetails.username", "error.duplicate-username", new Object[] {userDetails.getUsername()}, "Duplicate Username."); return showForm(request, response, errors); } catch (ObjectNotFoundException e) { errors.rejectValue("projectCode", "error.illegal-projectcode"); return showForm(request, response, errors); } catch (PeriodNotFoundException e) { errors.rejectValue("projectCode", "error.illegal-projectcode"); return showForm(request, response, errors); } } else { // userService.updateUser(userDetails); // TODO HT: add updateUser() to UserService } ModelAndView modelAndView = new ModelAndView(getSuccessView()); modelAndView.addObject(USERNAME_KEY, userDetails.getUsername()); return modelAndView; } else { response.sendError(HttpServletResponse.SC_FORBIDDEN); return null; } }