This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
/
RegistrationController.java
80 lines (70 loc) · 2.62 KB
/
RegistrationController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package it.redoddity.portfolios.controller;
import it.redoddity.controller.BaseController;
import it.redoddity.portfolios.dao.UserDAO;
import it.redoddity.portfolios.model.User;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
*
* @author madchicken
*/
@Controller(value = "registration")
@Scope("prototype")
public class RegistrationController extends BaseController {
@Autowired
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
@Override
public void index() throws ServletException, IOException {
request.setAttribute("user", new User());
super.index();
}
public void register()
throws ServletException, IOException {
User user = new User();
if (method.equals("post")) {
user.bind(request.getParameterMap(), validator);
if (user.isValid()) {
if (!user.getPassword().equals(
request.getParameter("passwordConfirm"))) {
user.addError("Passwords don't match");
} else {
try {
User u = userDAO.findByEmail(user.getEmail());
if (u != null && !user.equals(u)) {
user.addError("email", user.getEmail()
+ " is already present in repository");
} else {
try {
userDAO.create(user);
response.sendRedirect(request.getContextPath());
return;
} catch (SQLException sqle) {
user.addError("Errore creating user: " + sqle.getMessage());
}
}
} catch (SQLException ex) {
Logger.getLogger(RegistrationController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
request.setAttribute("user", user);
render("index");
}
}