Subversion Repositories SmartDukaan

Rev

Rev 23786 | Rev 24491 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.processor;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.conn.HttpHostConnectException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spice.profitmandi.common.enumuration.SchemeType;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.GoogleLoginRequest;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.util.JWTUtil;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.dao.entity.dtr.SocialUser;
import com.spice.profitmandi.dao.entity.dtr.User;
import com.spice.profitmandi.dao.enumuration.dtr.Gender;
import com.spice.profitmandi.dao.enumuration.dtr.SocialType;
import com.spice.profitmandi.dao.repository.dtr.SocialUserRepository;
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;


@Component
public class GoogleLoginProcessor {
        
        private static final String V1_HOST_NAME = "content.googleapis.com";
        private static final String V1_URI = "/plus/v1/people/me";
        private static final String V3_HOST_NAME = "www.googleapis.com";
        private static final String V3_URI = "/oauth2/v3/tokeninfo";
        private static final int PORT_NUMBER = 443;
        private final ObjectMapper objectMapper = new ObjectMapper();
        
        @Autowired
        private SocialUserRepository socialUserRepository;
        
        @Autowired
        private UserRepository userRepository;
        
        @Autowired
        private UserRoleRepository userRoleRepository;
        
        @Autowired
        private UserAccountRepository userAccountRepository;
        
        @Autowired
        private RestClient restClient;
        
        public Map<String, Object> process(GoogleLoginRequest googleLoginRequest) throws ProfitMandiBusinessException{
                Map<String, String> params = new HashMap<>();
                params.put(ProfitMandiConstants.ACCESS_TOKEN, googleLoginRequest.getToken());
                Map<String, String> headers = new HashMap<>(1);
                headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
                String responseString = null;
                try {
                        responseString = restClient.get(SchemeType.HTTPS, V1_HOST_NAME, PORT_NUMBER, V1_URI, params, headers);
                } catch (HttpHostConnectException e) {
                        throw new ProfitMandiBusinessException("", "", "Could not connect to remote host");
                }
                try {
                        JsonNode rootNode = objectMapper.readTree(responseString);
                        SocialUser socialUser = new SocialUser();
                        if(rootNode.has("emails")){
                                JsonNode emails = rootNode.get("emails");
                                if(emails.isArray()){
                                        Iterator<JsonNode> emailsIterator = emails.elements();
                                        if(emailsIterator.hasNext()){
                                                JsonNode email = emailsIterator.next();
                                                if(email.has("value")){
                                                        socialUser.setEmailId(email.get("value").asText());
                                                }
                                        }
                                }
                                //socialUser.setEmailId(rootNode.get("email").asText());
                        }
                        if(!socialUserRepository.isExistByEmailId(socialUser.getEmailId())){
                                if(rootNode.has("displayName")){
                                        socialUser.setName(rootNode.get("displayName").asText());
                                }
                                if(rootNode.has("gender")){
                                        String genderName = rootNode.get("gender").asText();
                                        switch(genderName){
                                                case "male":{
                                                        socialUser.setGender(Gender.MALE);
                                                        break;
                                                }case "female":{
                                                        socialUser.setGender(Gender.FEMALE);
                                                        break;
                                                }
                                        }
                                }
                                socialUser.setCreateTimestamp(LocalDateTime.now());
                                socialUser.setType(SocialType.GOOGLE);
                                socialUser.setUpdateTimestamp(LocalDateTime.now());
                                socialUserRepository.persist(socialUser);
                        }
                        
                        Map<String, Object> responseMap = new HashMap<>(2);
                        User user = null;
                        try{
                                user = userRepository.selectByEmailId(socialUser.getEmailId());
                        }catch(ProfitMandiBusinessException profitMandiBusinessException){
                                
                        }
                        if(user == null){
                                try{
                                        user = userRepository.selectByEmailId(socialUser.getEmailId());
                                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                                        responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));
                                        responseMap.put(ProfitMandiConstants.REGISTERED, false);
                                }
                        }
                        if(user != null){
                                //Set<Role> roles = user.getRoles();
                                List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
                                int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
                                //Set<Role> roles = new HashSet<>();
                                String[] roleTypes = new String[roleIds.size()];
                                int index = 0;
                                for (int roleId : roleIds) {
                                        roleTypes[index++] = String.valueOf(roleId);
                                }
                                responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
                                responseMap.put(ProfitMandiConstants.REGISTERED, true);
                        }
                        
                        return responseMap;
                }catch (JsonProcessingException jsonProcessingException) {
                        //LOGGER.error("Json parse exception of "+json,jsonProcessingException);
                        throw new ProfitMandiBusinessException("", "", "VE_1001");
                }catch (IOException ioException) {
                        //LOGGER.error("IO Exception occurred while parsing json String");
                        throw new ProfitMandiBusinessException("", "", "VE_1001");
                }
        }
        
        public Map<String, Object> getFofoDetail(String token) throws ProfitMandiBusinessException{
                Map<String, String> params = new HashMap<>();
                params.put(ProfitMandiConstants.ID_TOKEN, token);
                Map<String, String> headers = new HashMap<>(1);
                headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
                String responseString = null;
                try {
                        responseString = restClient.get(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER, V3_URI, params, headers);
                } catch (HttpHostConnectException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                try {
                        JsonNode rootNode = objectMapper.readTree(responseString);
                        User user = null;
                        try{
                                user = userRepository.selectByEmailId(rootNode.get("email").asText());
                        }catch(ProfitMandiBusinessException profitMandiBusinessException){
                                
                        }       
                        if(user == null){
                                user = userRepository.selectBySecondryEmailId(rootNode.get("email").asText());
                        }
                        if(user != null){
                                Map<String, Object> responseMap = new HashMap<>(2);
                                responseMap.put(ProfitMandiConstants.FOFO_ID, user.getId());
                                responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
                                return responseMap;
                        }
                                
                }catch (JsonProcessingException jsonProcessingException) {
                        throw new ProfitMandiBusinessException("", "", "VE_1001");
                }catch (IOException ioException) {
                        throw new ProfitMandiBusinessException("", "", "VE_1001");
                }
                return null;
        }
}