1 package compbio.controllers;
3 import java.security.Principal;
5 import java.util.regex.Pattern;
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;
20 import compbio.proteocache.users.User;
21 import compbio.proteocache.users.UserManager;
22 import compbio.cassandra.CassandraUserManager;
25 public class UserController {
28 //JavaMailSender mailSender;
29 private final Pattern EMAIL = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}");
31 @RequestMapping(value = "/register/query", method = RequestMethod.GET)
32 public String printPublicHome(ModelMap model) {
33 User user = new User();
34 model.addAttribute(user);
38 @RequestMapping(value = "/register/do", method = RequestMethod.POST)
39 public String addUser(Model model, @ModelAttribute("user") User user, BindingResult bindingResult) {
41 if (bindingResult.hasErrors()) {
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");
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");
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");
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");
74 user.setRegistrationDate(new Date());
75 UserManager cm = new CassandraUserManager();
78 } catch (DataIntegrityViolationException e) {
79 bindingResult.addError(new FieldError("user", "email", "This email (username) is already in use!"));
80 model.addAttribute("error", "used email");
83 if (user.isUpdateByEmail()) {
84 subscribeToList(user.getEmail());
87 Account.autoLogin(user, request, authenticationManager);
89 return "redirect:/index";
92 @RequestMapping(value = "/register/edit/do", method = RequestMethod.POST)
93 public String editUser(Model model, @ModelAttribute("user") User user, BindingResult bindingResult) {
95 if (bindingResult.hasErrors()) {
99 user.setRegistrationDate(new Date());
100 UserManager cm = new CassandraUserManager();
103 } catch (DataIntegrityViolationException e) {
104 bindingResult.addError(new FieldError("user", "email", "This email (username) is already in use!"));
107 if (user.isUpdateByEmail()) {
108 subscribeToList(user.getEmail());
111 return "redirect:/index";
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);