@RequestMapping( value = {"/add-document-{userId}"}, method = RequestMethod.POST) public String uploadDocument( @Valid FileBucket fileBucket, BindingResult result, ModelMap model, @PathVariable int userId) throws IOException { if (result.hasErrors()) { System.out.println("validation errors"); User user = userService.findById(userId); model.addAttribute("user", user); List<UserDocument> documents = userDocumentService.findAllByUserId(userId); model.addAttribute("documents", documents); return "managedocuments"; } else { System.out.println("Fetching file"); User user = userService.findById(userId); model.addAttribute("user", user); saveDocument(fileBucket, user); return "redirect:/add-document-" + userId; } }
/** This method will delete an user by it's SSOID value. */ @RequestMapping( value = {"/delete-user-{ssoId}"}, method = RequestMethod.GET) public String deleteUser(@PathVariable String ssoId) { userService.deleteUserBySSO(ssoId); return "redirect:/list"; }
/** This method will list all existing users. */ @RequestMapping( value = {"/", "/list"}, method = RequestMethod.GET) public String listUsers(ModelMap model) { List<User> users = userService.findAllUsers(); model.addAttribute("users", users); return "userslist"; }
/** This method will provide the medium to update an existing user. */ @RequestMapping( value = {"/edit-user-{ssoId}"}, method = RequestMethod.GET) public String editUser(@PathVariable String ssoId, ModelMap model) { User user = userService.findBySSO(ssoId); model.addAttribute("user", user); model.addAttribute("edit", true); return "registration"; }
/** * This method will be called on form submission, handling POST request for saving user in * database. It also validates the user input */ @RequestMapping( value = {"/newuser"}, method = RequestMethod.POST) public String saveUser(@Valid User user, BindingResult result, ModelMap model) { if (result.hasErrors()) { return "registration"; } /* * Preferred way to achieve uniqueness of field [sso] should be implementing custom @Unique annotation * and applying it on field [sso] of Model class [User]. * * Below mentioned peace of code [if block] is to demonstrate that you can fill custom errors outside the validation * framework as well while still using internationalized messages. * */ if (!userService.isUserSSOUnique(user.getId(), user.getSsoId())) { FieldError ssoError = new FieldError( "user", "ssoId", messageSource.getMessage( "non.unique.ssoId", new String[] {user.getSsoId()}, Locale.getDefault())); result.addError(ssoError); return "registration"; } userService.saveUser(user); model.addAttribute("user", user); model.addAttribute( "success", "User " + user.getFirstName() + " " + user.getLastName() + " registered successfully"); // return "success"; return "registrationsuccess"; }
@RequestMapping( value = {"/add-document-{userId}"}, method = RequestMethod.GET) public String addDocuments(@PathVariable int userId, ModelMap model) { User user = userService.findById(userId); model.addAttribute("user", user); FileBucket fileModel = new FileBucket(); model.addAttribute("fileBucket", fileModel); List<UserDocument> documents = userDocumentService.findAllByUserId(userId); model.addAttribute("documents", documents); return "managedocuments"; }
/** * This method will be called on form submission, handling POST request for updating user in * database. It also validates the user input */ @RequestMapping( value = {"/edit-user-{ssoId}"}, method = RequestMethod.POST) public String updateUser( @Valid User user, BindingResult result, ModelMap model, @PathVariable String ssoId) { if (result.hasErrors()) { return "registration"; } userService.updateUser(user); model.addAttribute( "success", "User " + user.getFirstName() + " " + user.getLastName() + " updated successfully"); return "registrationsuccess"; }