Subversion Repositories SmartDukaan

Rev

Rev 32978 | 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 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.util.JWTUtil;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.dao.entity.fofo.PincodePartner;
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
import com.spice.profitmandi.dao.repository.cs.CsService;
import com.spice.profitmandi.dao.repository.dtr.*;
import com.spice.profitmandi.dao.repository.fofo.PincodePartnerRepository;
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
import com.spice.profitmandi.service.AuthService;
import com.spice.profitmandi.service.user.RetailerService;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

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

@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 Logger LOGGER = LogManager.getLogger(GoogleLoginProcessor.class);
        private static final int PORT_NUMBER = 443;
        private final ObjectMapper objectMapper = new ObjectMapper();

        @Autowired
        private SocialUserRepository socialUserRepository;

        @Autowired
        private UserRepository userRepository;

        @Autowired
        private AuthService authService;

        @Autowired
        com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;

        @Autowired
        private AuthRepository authRepository;

        
        @Autowired
        private UserRoleRepository userRoleRepository;

        @Autowired
        private PromoterRepository promoterRepository;

        @Autowired
        private UserAccountRepository userAccountRepository;
        
        @Autowired
        private FofoStoreRepository fofoStoreRepository;
        
        @Autowired
        private CsService csService;

        @Autowired
        private RestClient restClient;
        @Autowired
        private RetailerService retailerService;

        @Autowired
        private PincodePartnerRepository pincodePartnerRepository;

        public String process(String token) throws ProfitMandiBusinessException {
                Map<String, String> params = new HashMap<>();
                params.put(ProfitMandiConstants.ID_TOKEN, token);
                Map<String, String> headers = new HashMap<>();
                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);
                        String email = rootNode.get("email").asText();

                        return email;
                } catch (JsonProcessingException jsonProcessingException) {
                        throw new ProfitMandiBusinessException("", "", "VE_1001");
                }
        }

        public Map<String, Object>  processStore(String storeCode) throws ProfitMandiBusinessException {
                Map<String, Object> responseMap = new HashMap<>();
                int retailerId = retailerService.getStoreCodeRetailerMap().get(storeCode);
                int userId = userAccountRepository.selectUserIdByRetailerId(retailerId);
                List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(userId);
                PincodePartner partnerPin = pincodePartnerRepository.selectPinCodeByPartnerId(retailerId);
                String[] roleTypes = new String[roleIds.size()];
                int index = 0;
                for (int roleId : roleIds) {
                        roleTypes[index++] = String.valueOf(roleId);
                }
                responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(userId, retailerId, roleTypes));
                responseMap.put(ProfitMandiConstants.REGISTERED, true);
                responseMap.put(ProfitMandiConstants.PIN_CODE, partnerPin != null ? partnerPin.getPincode() : partnerPin);
                return responseMap;
        }
}