Subversion Repositories SmartDukaan

Rev

Rev 21561 | Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
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.ProfitMandiConstants;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
import com.spice.profitmandi.web.model.FofoDetails;

@Component
public class GoogleLoginUtil {
        
        @Autowired
        RetailerRepository retailerRepository;
        
        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();
        
        public FofoDetails getFofoDetail(String token) throws ProfitMandiBusinessException{
                RestClient restClient = new RestClient(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER);
                Map<String, String> params = new HashMap<>();
                params.put(ProfitMandiConstants.ID_TOKEN, token);
                String responseString = restClient.get(V3_URI, params);
                try {
                        JsonNode rootNode = objectMapper.readTree(responseString);
                        Object[] idAndEmailId = retailerRepository.selectFofoIdAndEmailIdByEmailId(rootNode.get("email").asText());
                        FofoDetails fofoDetails = new FofoDetails();
                        fofoDetails.setFofoId((Integer)idAndEmailId[0]);
                        fofoDetails.setEmailId((String)idAndEmailId[1]);
                        return fofoDetails;
                }catch (JsonProcessingException jsonProcessingException) {
                        throw new ProfitMandiBusinessException("", "", "VE_1001");
                }catch (IOException ioException) {
                        throw new ProfitMandiBusinessException("", "", "VE_1001");
                }
        }
}