Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.neeti.neg.controller; import com.neeti.neg.common.Constant; import com.neeti.neg.modal.RegisterUser; import com.neeti.neg.modal.UserRole; import com.neeti.neg.service.RegisterUserService; import com.neeti.neg.service.UtilityService; import java.util.ArrayList; import java.util.List; import javax.validation.groups.Default; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * * @author manishas */ @Controller @RequestMapping("/") public class MainController { @Autowired private MessageSource messageSource; @Autowired private RegisterUserService registerUserService; Logger logger = LoggerFactory.getLogger(MainController.class); @RequestMapping(value = "/", method = RequestMethod.GET) public String getDefaultPage(ModelMap model) { return "index"; } @RequestMapping(value = "/register", method = RequestMethod.GET) public String viewRegisterUser(Model m) { m.addAttribute("registerUser", new RegisterUser()); return "register"; } @RequestMapping(value = "/register", method = RequestMethod.POST) public String saveRegisterUser(@Validated({ Default.class }) RegisterUser registerUser, BindingResult result, Model m) { if (!result.hasErrors()) { //check for valid email Id (username) from database boolean validUserName, validGameId; validUserName = registerUserService.checkUserByUserName(registerUser.getGroupName()); validGameId = registerUserService.checkGameRefId(registerUser.getGameRefId(), registerUser.getGamePassword()); if (validUserName) { m.addAttribute("regmsg", messageSource.getMessage("UserNameExists", null, null)); } else if (!validGameId) { m.addAttribute("regmsg", messageSource.getMessage("GameRefIdDNE", null, null)); } else { //save user String hashedPassword = UtilityService.encodePassword(registerUser.getGroupPassword()); registerUser.setGroupPassword(hashedPassword); registerUser.setEnabled(Constant.USER_ENABLED); List<UserRole> listUserRole = new ArrayList<>(); listUserRole.add(new UserRole(Constant.UserRoleEnum.USERROLE_PLAYER.getValue())); registerUser.setUserRoleList(listUserRole); logger.debug("Before registering user for username [" + registerUser.getGroupName() + "]"); registerUserService.createRegisterUser(registerUser); logger.info("Register username [" + registerUser.getGroupName() + "] Successful"); m.addAttribute("regmsg", registerUser.getGroupName() + ": Registered Successfully"); //clear form values registerUser.emptyFieldValues(); } } return "register"; } }