Rev 22088 | Rev 22139 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.web.controller;import java.util.HashSet;import java.util.List;import java.util.Set;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.transaction.annotation.Transactional;import org.springframework.ui.Model;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.dao.entity.dtr.Retailer;import com.spice.profitmandi.dao.entity.dtr.User;import com.spice.profitmandi.dao.entity.dtr.UserAccounts;import com.spice.profitmandi.dao.entity.dtr.UserRole;import com.spice.profitmandi.dao.enumuration.dtr.AccountType;import com.spice.profitmandi.dao.enumuration.dtr.RoleType;import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;import com.spice.profitmandi.dao.repository.dtr.UserRepository;import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;import com.spice.profitmandi.web.model.FofoDetails;import com.spice.profitmandi.web.util.CookiesProcessor;import com.spice.profitmandi.web.util.GoogleTokenUtil;import com.spice.profitmandi.web.util.MVCResponseSender;@Controller@Transactional(rollbackFor=Throwable.class)public class LoginController {private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);@AutowiredGoogleTokenUtil googleTokenUtil;@AutowiredRetailerRepository retailerRepository;@AutowiredUserRepository userRepository;@AutowiredUserAccountRepository userAccountRepository;@AutowiredUserRoleRepository userRoleRepository;@AutowiredMVCResponseSender mvcResponseSender;@AutowiredCookiesProcessor cookiesProcessor;@Value("${google.api.key}")private String googleApiKey;@RequestMapping(value = "/login", method = RequestMethod.GET)public String loginPage(HttpServletRequest request, Model model) throws Exception{LOGGER.info("Context Path is {}", request.getContextPath());try{cookiesProcessor.getCookiesObject(request);LOGGER.info("Request session is already exist, should be redirect to /dashboard");return "redirect:/dashboard";}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){model.addAttribute("googleApiKey", googleApiKey);model.addAttribute("appContextPath", request.getContextPath());return "login";}}@RequestMapping(value = "/login", method = RequestMethod.POST)public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.TOKEN) String token, Model model) throws Exception{try{String emailId = googleTokenUtil.getEmailId(token);User user = null;try{user = userRepository.selectByEmailId(emailId);}catch(ProfitMandiBusinessException profitMandiBusinessException){LOGGER.error("User not found with given emailId", profitMandiBusinessException);model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + "/register"));return "response";}UserAccounts userAccounts = userAccountRepository.getUserAccountByType(user.getId(), AccountType.saholic);Retailer retailer = retailerRepository.selectById(Integer.parseInt(userAccounts.getAccount_key()));if(!retailer.isFofo()){LOGGER.error("Retailer is not fofo, should be registered");model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + "/register"));return "response";}List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());Set<RoleType> roleTypes = new HashSet<>();StringBuilder roleNames = new StringBuilder();for(int index = 0; index < userRoles.size(); index++){roleTypes.add(userRoles.get(index).getRoleType());roleNames.append(userRoles.get(index).getRoleType().toString());if(index + 1 != userRoles.size()){roleNames.append(",");}}FofoDetails fofoDetails = new FofoDetails();fofoDetails.setFofoId(retailer.getId());fofoDetails.setEmailId(emailId);fofoDetails.setRoleTypes(roleTypes);//FofoDetails fofoDetails = googleTokenUtil.getFofoDetail(token);Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));cookieFofoId.setDomain(request.getServerName());cookieFofoId.setPath(request.getContextPath());Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());cookieEmailId.setDomain(request.getServerName());cookieEmailId.setPath(request.getContextPath());Cookie cookieRoleNames = new Cookie(ProfitMandiConstants.ROLE_NAMES, roleNames.toString());response.addCookie(cookieFofoId);response.addCookie(cookieEmailId);response.addCookie(cookieRoleNames);LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to /dashboard");model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + "/dashboard"));return "response";// return mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard");}catch(ProfitMandiBusinessException profitMandiBusinessException){LOGGER.error("Error : ", profitMandiBusinessException);model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));return "response";}}@RequestMapping(value = "/logout", method = RequestMethod.GET)public String logout(HttpServletRequest request, @ModelAttribute("model") ModelMap model, HttpServletResponse response) throws Exception{try{cookiesProcessor.removeCookies(request, response);LOGGER.info("Logout is successfull, should be redirect to /login");return "redirect:/login";}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){LOGGER.info("Error occured while removing requested cookies, should be redirect to /login");return "redirect:/login";}}}