package eu.smartlm.abs.portal.security.login.admin; import javax.portlet.ActionResponse; import javax.portlet.RenderResponse; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.bind.support.SessionStatus; import org.springframework.web.portlet.bind.annotation.ActionMapping; import org.springframework.web.portlet.bind.annotation.RenderMapping; import eu.smartlm.abs.portal.security.login.model.User; import eu.smartlm.abs.portal.security.login.service.LoginService; /** * Adds a User to the User Database to grant limited access to the acounting and billing data * @author David García Pérez - CESGA */ @Controller(value="addUserController") @RequestMapping(value = "VIEW") @SessionAttributes(types = User.class) public class AddUserController { private User user; @Autowired @Qualifier("myLoginService") private LoginService loginService; public void setLoginService(LoginService loginService) { this.loginService = loginService; } @RenderMapping(params = "myaction=addUserForm") public String showAddUserForm(RenderResponse response) { return "addUserForm"; } /** * User object that it is created to be passed to the jsp page * @return a User Object */ @ModelAttribute("user") public User getUser() { if (user == null) return new User(); return user; } /** * Initicialices the binder that associates fields of the jsp page to the user model * @param binder */ @InitBinder("user") public void initBinder(WebDataBinder binder) { } /** * Adds the user to the database * @param user * @param bindingResult * @param response * @param sessionStatus */ @ActionMapping(params = "myaction=addUser") public void addBook(@Valid @ModelAttribute User user, BindingResult bindingResult, ActionResponse response, SessionStatus sessionStatus) { //myValidator.validate(user, bindingResult); if (!bindingResult.hasErrors()) { int message = loginService.addUser(user); if(message == LoginService.USER_SUCCESSFULLY_ADDED) response.setRenderParameter("myaction", "users"); else if (message == LoginService.USER_ALREADY_EXISTS) response.setRenderParameter("myaction", "addUserForm"); else if (message == LoginService.WRONG_PASSWORD) response.setRenderParameter("myaction", "addUserForm"); else if (message == LoginService.WRONG_USERNAME) response.setRenderParameter("myaction", "addUserForm"); //--set the session status as complete to cleanup the model attributes //--stored using @SessionAttributes, otherwise when you click //--'Add Book' button you'll see the book information pre-populated //-- because the getCommandObject method of the controller is not //--invoked sessionStatus.setComplete(); } else { response.setRenderParameter("myaction", "addUserForm"); } } }