|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package controllers.admin; |
| 7 | + |
| 8 | +import exceptions.UserAlredyExistsException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Locale; |
| 12 | +import models.User; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.context.support.ReloadableResourceBundleMessageSource; |
| 17 | +import org.springframework.stereotype.Controller; |
| 18 | +import org.springframework.ui.Model; |
| 19 | +import org.springframework.validation.BindingResult; |
| 20 | +import org.springframework.validation.annotation.Validated; |
| 21 | +import org.springframework.web.bind.WebDataBinder; |
| 22 | +import org.springframework.web.bind.annotation.InitBinder; |
| 23 | +import org.springframework.web.bind.annotation.ModelAttribute; |
| 24 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 25 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 26 | +import org.springframework.web.bind.annotation.SessionAttributes; |
| 27 | +import org.springframework.web.bind.support.SessionStatus; |
| 28 | +import org.springframework.web.servlet.mvc.support.RedirectAttributes; |
| 29 | +import services.UserService; |
| 30 | +import services.security.CurrentUser; |
| 31 | + |
| 32 | +/** |
| 33 | + * |
| 34 | + * @author sergio |
| 35 | + */ |
| 36 | +@Controller |
| 37 | +@RequestMapping("/admin/users/self") |
| 38 | +@SessionAttributes({SelfUserController.ATTRIBUTE_NAME}) |
| 39 | +public class SelfUserController { |
| 40 | + |
| 41 | + private static Logger logger = LoggerFactory.getLogger(SelfUserController.class); |
| 42 | + |
| 43 | + public static final String ATTRIBUTE_NAME = "user"; |
| 44 | + public static final String BINDING_RESULT_NAME = "org.springframework.validation.BindingResult." + ATTRIBUTE_NAME; |
| 45 | + |
| 46 | + @Autowired |
| 47 | + private UserService userService; |
| 48 | + @Autowired |
| 49 | + private ReloadableResourceBundleMessageSource messageSource; |
| 50 | + |
| 51 | + |
| 52 | + @InitBinder |
| 53 | + void allowFields(WebDataBinder webDataBinder){ |
| 54 | + webDataBinder.setAllowedFields("username", "email", "fullName"); |
| 55 | + } |
| 56 | + |
| 57 | + @RequestMapping(method = RequestMethod.GET) |
| 58 | + public String self(@CurrentUser User user, Model model) { |
| 59 | + |
| 60 | + /* if "fresh" GET (ie, not redirect w validation errors): */ |
| 61 | + if(!model.containsAttribute(BINDING_RESULT_NAME)) { |
| 62 | + model.addAttribute(ATTRIBUTE_NAME, user); |
| 63 | + } |
| 64 | + return "admin/user/self"; |
| 65 | + } |
| 66 | + |
| 67 | + @RequestMapping(method = RequestMethod.POST) |
| 68 | + public String self( |
| 69 | + // @ModelAttribute will load User from session but also set values from the form post |
| 70 | + @Validated(User.UserUpdate.class) @ModelAttribute(ATTRIBUTE_NAME) User user, |
| 71 | + BindingResult bindingResult, |
| 72 | + RedirectAttributes redirectAttributes, |
| 73 | + // SessionStatus lets you clear your SessionAttributes |
| 74 | + SessionStatus sessionStatus |
| 75 | + ){ |
| 76 | + |
| 77 | + logger.info("Usuario a actualizar: " + user.toString()); |
| 78 | + if(!bindingResult.hasErrors()) { |
| 79 | + try { |
| 80 | + userService.update(user); |
| 81 | + } catch(UserAlredyExistsException e){ |
| 82 | + bindingResult.rejectValue("email", "user.exists"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if(bindingResult.hasErrors()) { |
| 87 | + //put the validation errors in Flash session and redirect to self |
| 88 | + redirectAttributes.addFlashAttribute(BINDING_RESULT_NAME, bindingResult); |
| 89 | + return "redirect:/admin/users/self"; |
| 90 | + } |
| 91 | + |
| 92 | + sessionStatus.setComplete(); //remove user from session |
| 93 | + |
| 94 | + List<String> successMessages = new ArrayList(); |
| 95 | + successMessages.add(messageSource.getMessage("message.profile.save.success", new Object[]{ }, Locale.getDefault())); |
| 96 | + redirectAttributes.addFlashAttribute("successFlashMessages", successMessages); |
| 97 | + return "redirect:/admin/users/self"; |
| 98 | + } |
| 99 | +} |
0 commit comments