Java tutorial
/* * The MIT License * * Copyright 2015 Tijme Gommers <t.gommers@jetcat.nl>. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.klm.workshop.controller.host.manage; import com.klm.workshop.dao.UserDAO; import com.klm.workshop.helper.PasswordHasher; import com.klm.workshop.model.User; import com.klm.workshop.validator.UserValidator; import java.util.Locale; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.support.PagedListHolder; import org.springframework.context.MessageSource; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.support.RedirectAttributes; /** * Host manage users controller * * @author Tijme Gommers <t.gommers@jetcat.nl> */ @Controller("hostManageUserController") @RequestMapping(value = "/host/manage") public class UserController { /** * User data access object */ @Autowired private UserDAO userDAO; /** * Translations */ @Autowired private MessageSource messageSource; /** * List of users * * @param model Objects and view * @param page Current pagination page * @param search Search value * @return The users list view */ @RequestMapping(value = "/users/index", method = RequestMethod.GET) public ModelAndView index(ModelAndView model, @RequestParam(name = "p", required = false, defaultValue = "0") int page, @RequestParam(name = "search", required = false, defaultValue = "") String search) { PagedListHolder pagedListHolder = new PagedListHolder(); pagedListHolder.setSource(userDAO.searchAll(search)); pagedListHolder.setPage(page); model.addObject("pagedListHolder", pagedListHolder); model.setViewName("host/manage/users/index"); return model; } /** * Show create user form * * @param model Objects and view * @return Form to create a user */ @RequestMapping(value = "/users/create", method = RequestMethod.GET) public ModelAndView getCreate(ModelAndView model) { model.addObject("user", new User()); model.addObject("roles", User.Role.values()); model.setViewName("host/manage/users/create"); return model; } /** * Create user, and show create user form. On error, show errors. On * success, redirect to index and show success message. * * @param model Objects and view * @param user The posted user * @param result Binded validation * @param redirect The redirect attributes * @param locale The current client locale * @return Form to create a user, or a redirect (if user was created successfully) */ @RequestMapping(value = "/users/create", method = RequestMethod.POST) public ModelAndView postCreate(ModelAndView model, @ModelAttribute("user") @Valid User user, BindingResult result, RedirectAttributes redirect, Locale locale) { if (result.hasErrors()) { model.addObject("roles", User.Role.values()); model.setViewName("host/manage/users/create"); } else { String hash = PasswordHasher.hash(user.getPassword()); user.setPassword(hash); userDAO.create(user); String success = messageSource.getMessage("general.create_success", new String[] { user.getFullName() }, locale); redirect.addFlashAttribute("alertSuccess", success); model.setViewName("redirect:/host/manage/users/index"); } return model; } /** * Show update user form * * @param id The ID of the user to update * @param model Objects and view * @return Form to create a user */ @RequestMapping(value = "/users/update/{id}", method = RequestMethod.GET) public ModelAndView getUpdate(ModelAndView model, @PathVariable int id) { model.addObject("user", new UserValidator((User) userDAO.findById(id))); model.addObject("roles", User.Role.values()); model.setViewName("host/manage/users/update"); return model; } /** * Update user, and show update user form. On error, show errors. On * success, redirect to index and show success message. * * @param model Objects and view * @param id The ID of the user to update * @param user The posted user * @param result Binded validation * @param redirect The redirect attributes * @param locale The current client locale * @return Form to update a user, or a redirect (if user was updated successfully) */ @RequestMapping(value = "/users/update/{id}", method = RequestMethod.POST) public ModelAndView postUpdate(ModelAndView model, @PathVariable int id, @ModelAttribute("user") @Valid UserValidator user, BindingResult result, RedirectAttributes redirect, Locale locale) { if (result.hasErrors()) { model.addObject("roles", User.Role.values()); model.setViewName("host/manage/users/update"); } else { User oldUser = (User) userDAO.findById(id); oldUser.setFirstName(user.getFirstName()); oldUser.setLastName(user.getLastName()); oldUser.setEmail(user.getEmail()); oldUser.setRole(user.getRole()); oldUser.setEnabled(user.isEnabled()); if (user.hasNewPassword()) { String hash = PasswordHasher.hash(user.getPassword()); oldUser.setPassword(hash); } userDAO.update(oldUser); String success = messageSource.getMessage("general.update_success", new String[] { user.getFullName() }, locale); redirect.addFlashAttribute("alertSuccess", success); model.setViewName("redirect:/host/manage/users/index"); } return model; } /** * Delete user, based on the given user id. Then redirects to the index, * with a alert message. * * @param id The ID of the user to delete * @param redirect The redirect attributes * @param locale The current client locale * @return Redirect to the index */ @RequestMapping(value = "/users/delete/{id}", method = RequestMethod.POST) public String getDelete(@PathVariable int id, RedirectAttributes redirect, Locale locale) { User user = (User) userDAO.findById(id); String success = messageSource.getMessage("general.delete_success", new String[] { user.getFullName() }, locale); userDAO.delete(user); redirect.addFlashAttribute("alertSuccess", success); return "redirect:/host/manage/users/index"; } }