Rename and merge controllers
[proteocache.git] / server / compbio / controllers / UserController.java
1 package compbio.controllers;
2
3 import java.security.Principal;
4 import java.util.Date;
5 import java.util.regex.Pattern;
6
7 import org.springframework.dao.DataIntegrityViolationException;
8 import org.springframework.mail.SimpleMailMessage;
9 import org.springframework.mail.javamail.JavaMailSender;
10 import org.springframework.stereotype.Controller;
11 import org.springframework.ui.Model;
12 import org.springframework.ui.ModelMap;
13 import org.springframework.validation.BindingResult;
14 import org.springframework.validation.FieldError;
15 import org.springframework.web.bind.annotation.ModelAttribute;
16 import org.springframework.web.bind.annotation.RequestMapping;
17 import org.springframework.web.bind.annotation.RequestMethod;
18 import org.springframework.web.bind.annotation.RequestParam;
19
20 import compbio.proteocache.users.User;
21 import compbio.proteocache.users.UserManager;
22 import compbio.cassandra.CassandraUserManager;
23
24 @Controller
25 public class UserController {
26
27         // @Inject
28         // JavaMailSender mailSender;
29         private final Pattern EMAIL = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}");
30
31         @RequestMapping(value = "/register/query", method = RequestMethod.GET)
32         public String printPublicHome(ModelMap model) {
33                 User user = new User();
34                 model.addAttribute(user);
35                 return "Register";
36         }
37
38         @RequestMapping(value = "/register/do", method = RequestMethod.POST)
39         public String addUser(Model model, @ModelAttribute("user") User user, BindingResult bindingResult) {
40
41                 if (bindingResult.hasErrors()) {
42                         return "Register";
43                 }
44
45                 int fullName = user.getFullName().length();
46                 if (fullName < 6 || 50 < fullName) {
47                         bindingResult.addError(new FieldError("user", "fullName", "Your full name must be between 3 and 50 symbols long!"));
48                         model.addAttribute("error", "wrong password");
49                         return "Register";
50                 }
51
52                 if (!EMAIL.matcher(user.getEmail()).find()) {
53                         bindingResult.addError(new FieldError("user", "email", "Email is empty or in a wrong form!"));
54                         model.addAttribute("error", "wrong email");
55                         return "Register";
56                 }
57
58                 int password = user.getPassword().length();
59                 if (password < 6 || 20 < password) {
60                         bindingResult.addError(new FieldError("user", "password", "The password must be at least 6 symbols long!"));
61                         model.addAttribute("error", "wrong password");
62                         return "Register";
63                 }
64
65                 int organisation = 0;
66                 if (null != user.getOrganisation())
67                         organisation = user.getOrganisation().length();
68                 if (organisation < 3 || 250 < organisation) {
69                         bindingResult.addError(new FieldError("user", "organisation", "The organisation must be between 3 and 250 symbols long!"));
70                         model.addAttribute("error", "wrong organisation name");
71                         return "Register";
72                 }
73
74                 user.setRegistrationDate(new Date());
75                 UserManager cm = new CassandraUserManager();
76                 try {
77                         cm.addUser(user);
78                 } catch (DataIntegrityViolationException e) {
79                         bindingResult.addError(new FieldError("user", "email", "This email (username) is already in use!"));
80                         model.addAttribute("error", "used email");
81                         return "Register";
82                 }
83                 if (user.isUpdateByEmail()) {
84                         subscribeToList(user.getEmail());
85                 }
86                 /*
87                  * Account.autoLogin(user, request, authenticationManager);
88                  */
89                 return "redirect:/index";
90         }
91
92         @RequestMapping(value = "/register/edit/do", method = RequestMethod.POST)
93         public String editUser(Model model, @ModelAttribute("user") User user, BindingResult bindingResult) {
94
95                 if (bindingResult.hasErrors()) {
96                         return "Register";
97                 }
98
99                 user.setRegistrationDate(new Date());
100                 UserManager cm = new CassandraUserManager();
101                 try {
102                         cm.addUser(user);
103                 } catch (DataIntegrityViolationException e) {
104                         bindingResult.addError(new FieldError("user", "email", "This email (username) is already in use!"));
105                         return "Register";
106                 }
107                 if (user.isUpdateByEmail()) {
108                         subscribeToList(user.getEmail());
109                 }
110
111                 return "redirect:/index";
112         }
113
114         private void subscribeToList(String email) {
115                 SimpleMailMessage message = new SimpleMailMessage();
116                 message.setFrom(email);
117                 message.setTo("proteocache-discuss-subscribe@compbio.dundee.ac.uk");
118                 message.setSubject("ProteoCache mailing list subscription");
119                 message.setText("testing " + email);
120                 // mailSender.send(message);
121         }
122
123 }