private static final Logger log = Logger.getLogger(SignUpServlet.class.getName());
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
IOException {
User user = ORMUtil.parseUser(req, "");
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");
}
String confirm = req.getParameter("confirm");
if (log.isLoggable(Level.INFO)) {
StringBuilder buf = new StringBuilder();
buf.append("Sign Up Request: ").append(user.toString()).append(",");
buf.append("confirm=[").append(confirm).append("].");
log.info(buf.toString());
}
List<DbError> errors = new ArrayList<DbError>();
if (user.getEmail() == null || user.getEmail().isEmpty()
|| !Validator.isValidEmail(user.getEmail())) {
errors.add(DbError.INVALID_EMAIL);
} else if (UserDAO.isEmailRegistered(user.getEmail())) {
errors.add(DbError.EMAIL_REGISTERED);
}
if (user.getPassword() == null || user.getPassword().isEmpty()) {
errors.add(DbError.EMPTY_PWD);
} else if (!user.getPassword().equals(confirm)) {
errors.add(DbError.CONFIRM_MISMATCH);
}
if (user.getFirstName() == null || user.getFirstName().isEmpty()) {
errors.add(DbError.EMPTY_FIRSTNAME);
}
if (user.getLastName() == null || user.getLastName().isEmpty()) {
errors.add(DbError.EMPTY_LASTNAME);
}
if (!Validator.isValidMonth(user.getBirthdayMonth())) {
errors.add(DbError.INVALID_MONTH);
}
if (!Validator.isValidDay(user.getBirthdayDay())) {
errors.add(DbError.INVALID_DAY);
}
if (!Validator.isValidYear(user.getBirthdayYear())) {
errors.add(DbError.INVALID_YEAR);
}
if (errors.isEmpty()) {
User createdUser = UserDAO.create(user);
if (createdUser == null) {
errors.add(DbError.CREATE_USER_FAIL);
} else {
log.info("Successfully create user: " + createdUser.getEmail());
req.getSession(true).setAttribute(Constants.SESSION_KEY_LOGIN_USER, createdUser);
resp.sendRedirect(Constants.HOME_PAGE_URL);
return;
}
}