resp.sendRedirect(Constants.DBMS_PAGE_URL + "?error=" + err.name());
}
}
private void newUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
DbError err = null;
User user = ORMUtil.parseUser(req, "");
if (log.isLoggable(Level.INFO)) {
StringBuilder buf = new StringBuilder();
buf.append("DBMS Create User: ").append(user.toString());
log.info(buf.toString());
}
if (user.getBirthdayDay() == null || user.getBirthdayDay().isEmpty()) {
user.setBirthdayDay("0");
}
if (user.getBirthdayMonth() == null || user.getBirthdayMonth().isEmpty()) {
user.setBirthdayMonth("0");
}
if (user.getBirthdayYear() == null || user.getBirthdayYear().isEmpty()) {
user.setBirthdayYear("0");
}
if (user.getEmail() == null || user.getEmail().isEmpty()
|| !Validator.isValidEmail(user.getEmail())) {
err = DbError.INVALID_EMAIL;
} else if (UserDAO.isEmailRegistered(user.getEmail())) {
err = DbError.EMAIL_REGISTERED;
} else if (user.getFirstName() == null || user.getFirstName().isEmpty()) {
err = DbError.EMPTY_FIRSTNAME;
} else if (user.getLastName() == null || user.getLastName().isEmpty()) {
err = DbError.EMPTY_LASTNAME;
} else if (user.getPassword() == null || user.getPassword().isEmpty()) {
err = DbError.EMPTY_PWD;
} else if (!Validator.isValidDay(user.getBirthdayDay())) {
err = DbError.INVALID_DAY;
} else if (!Validator.isValidMonth(user.getBirthdayMonth())) {
err = DbError.INVALID_MONTH;
} else if (!Validator.isValidYear(user.getBirthdayYear())) {
err = DbError.INVALID_YEAR;
}
if (err == null) {
User createdUser = UserDAO.create(user);
if (createdUser == null) {
err = DbError.CREATE_USER_FAIL;
}
}
if (err == null) {
UserDAO.update(user);
log.info("Successfully create user [" + user.getEmail() + "] by DBMS.");
resp.sendRedirect(Constants.DBMS_PAGE_URL);
} else {
StringBuilder buf = new StringBuilder();
buf.append(Constants.DBMS_PAGE_URL);
String parameters = getParameterUrl(req, req.getParameterNames());
if (parameters != null && !parameters.isEmpty()) {
buf.append("?").append(parameters);
}
buf.append("&tab=insert&error=").append(err.name());
resp.sendRedirect(buf.toString());
}
}