Subversion Repositories SmartDukaan

Rev

Rev 28349 | Rev 32978 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 28349 Rev 30295
Line 1... Line 1...
1
package com.spice.profitmandi.web.processor;
1
package com.spice.profitmandi.web.processor;
2
 
2
 
3
import java.io.IOException;
-
 
4
import java.time.LocalDateTime;
-
 
5
import java.util.Arrays;
-
 
6
import java.util.HashMap;
-
 
7
import java.util.Iterator;
-
 
8
import java.util.List;
-
 
9
import java.util.Map;
-
 
10
import java.util.Set;
-
 
11
 
-
 
12
import org.apache.http.conn.HttpHostConnectException;
-
 
13
import org.apache.logging.log4j.LogManager;
-
 
14
import org.apache.logging.log4j.Logger;
-
 
15
import org.springframework.beans.factory.annotation.Autowired;
-
 
16
import org.springframework.http.HttpHeaders;
-
 
17
import org.springframework.http.MediaType;
-
 
18
import org.springframework.stereotype.Component;
-
 
19
 
-
 
20
import com.fasterxml.jackson.core.JsonProcessingException;
3
import com.fasterxml.jackson.core.JsonProcessingException;
21
import com.fasterxml.jackson.databind.JsonNode;
4
import com.fasterxml.jackson.databind.JsonNode;
22
import com.fasterxml.jackson.databind.ObjectMapper;
5
import com.fasterxml.jackson.databind.ObjectMapper;
23
import com.spice.profitmandi.common.enumuration.SchemeType;
6
import com.spice.profitmandi.common.enumuration.SchemeType;
24
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
7
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25
import com.spice.profitmandi.common.model.GoogleLoginRequest;
-
 
26
import com.spice.profitmandi.common.model.ProfitMandiConstants;
8
import com.spice.profitmandi.common.model.ProfitMandiConstants;
27
import com.spice.profitmandi.common.util.JWTUtil;
9
import com.spice.profitmandi.common.util.JWTUtil;
28
import com.spice.profitmandi.common.util.Utils;
-
 
29
import com.spice.profitmandi.common.web.client.RestClient;
10
import com.spice.profitmandi.common.web.client.RestClient;
30
import com.spice.profitmandi.dao.entity.auth.AuthUser;
-
 
31
import com.spice.profitmandi.dao.entity.dtr.SocialUser;
-
 
32
import com.spice.profitmandi.dao.entity.dtr.User;
-
 
33
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
-
 
34
import com.spice.profitmandi.dao.entity.user.Promoter;
-
 
35
import com.spice.profitmandi.dao.enumuration.dtr.Gender;
-
 
36
import com.spice.profitmandi.dao.enumuration.dtr.SocialType;
-
 
37
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
11
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
38
import com.spice.profitmandi.dao.repository.cs.CsService;
12
import com.spice.profitmandi.dao.repository.cs.CsService;
39
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
-
 
40
import com.spice.profitmandi.dao.repository.dtr.SocialUserRepository;
-
 
41
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
-
 
42
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
13
import com.spice.profitmandi.dao.repository.dtr.*;
43
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
-
 
44
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
14
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
45
import com.spice.profitmandi.service.AuthService;
15
import com.spice.profitmandi.service.AuthService;
46
import com.spice.profitmandi.service.user.RetailerService;
16
import com.spice.profitmandi.service.user.RetailerService;
-
 
17
import org.apache.http.conn.HttpHostConnectException;
-
 
18
import org.apache.logging.log4j.LogManager;
-
 
19
import org.apache.logging.log4j.Logger;
-
 
20
import org.springframework.beans.factory.annotation.Autowired;
-
 
21
import org.springframework.http.HttpHeaders;
-
 
22
import org.springframework.http.MediaType;
-
 
23
import org.springframework.stereotype.Component;
-
 
24
 
-
 
25
import java.io.IOException;
-
 
26
import java.util.HashMap;
-
 
27
import java.util.List;
-
 
28
import java.util.Map;
47
 
29
 
48
@Component
30
@Component
49
public class GoogleLoginProcessor {
31
public class GoogleLoginProcessor {
50
 
32
 
51
	private static final String V1_HOST_NAME = "content.googleapis.com";
33
	private static final String V1_HOST_NAME = "content.googleapis.com";
Line 90... Line 72...
90
	@Autowired
72
	@Autowired
91
	private RestClient restClient;
73
	private RestClient restClient;
92
	@Autowired
74
	@Autowired
93
	private RetailerService retailerService;
75
	private RetailerService retailerService;
94
 
76
 
95
	public Map<String, Object> process(String token) throws ProfitMandiBusinessException {
77
	public String process(String token) throws ProfitMandiBusinessException {
96
		Map<String, String> params = new HashMap<>();
78
		Map<String, String> params = new HashMap<>();
97
		params.put(ProfitMandiConstants.ID_TOKEN, token);
79
		params.put(ProfitMandiConstants.ID_TOKEN, token);
98
		Map<String, String> headers = new HashMap<>();
80
		Map<String, String> headers = new HashMap<>();
99
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
81
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
100
		String responseString = null;
82
		String responseString = null;
Line 105... Line 87...
105
			e.printStackTrace();
87
			e.printStackTrace();
106
		}
88
		}
107
		try {
89
		try {
108
			JsonNode rootNode = objectMapper.readTree(responseString);
90
			JsonNode rootNode = objectMapper.readTree(responseString);
109
			String email = rootNode.get("email").asText();
91
			String email = rootNode.get("email").asText();
110
			
-
 
111
			String name = authService.getNameByEmailId(email);
-
 
112
			
-
 
113
			Map<String, Object> responseMap = new HashMap<>(2);
-
 
114
			LOGGER.info("User Name from getNameByEmailId({}) is {}", email, name);
-
 
115
			if (name != null) {
-
 
116
				User registeredUser = null;
-
 
117
				AuthUser authUser = authRepository.selectByGmailId(email);
-
 
118
 
-
 
119
				if (authRepository.selectByGmailId(email) != null) {
-
 
120
					registeredUser = userRepository.selectByEmailId(authUser.getEmailId());
-
 
121
				} else if (promoterRepository.isExistByEmailId(email)) {
-
 
122
					Promoter promoter = promoterRepository.selectByEmailId(email);
-
 
123
					int userId = userAccountRepository.selectUserIdByRetailerId(promoter.getRetailerId());
-
 
124
					registeredUser = userRepository.selectById(userId);
-
 
125
				} else if (userRepository.isExistBySecondryEmailId(email)) {
-
 
126
					registeredUser = userRepository.selectBySecondryEmailId(email);
-
 
127
				}
-
 
128
				LOGGER.info("4");
-
 
129
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(registeredUser.getId());
-
 
130
				String[] roleTypes = new String[roleIds.size()];
-
 
131
				int index = 0;
-
 
132
				for (int roleId : roleIds) {
-
 
133
					roleTypes[index++] = String.valueOf(roleId);
-
 
134
				}
-
 
135
				int retailerId;
-
 
136
				try {
-
 
137
					retailerId = userAccountRepository.selectRetailerIdByUserId(registeredUser.getId());
-
 
138
				} catch (Exception e) {
-
 
139
					Set<Integer> authUserPartnerSet = csService.getAuthUserPartnerIdMapping().get(authUser.getEmailId());
-
 
140
					if(authUserPartnerSet != null && authUserPartnerSet.size() > 0) {
-
 
141
						retailerId = authUserPartnerSet.stream().findFirst().get();
-
 
142
						FofoStore fs = fofoStoreRepository.selectByRetailerId(retailerId);
-
 
143
						retailerId = ProfitMandiConstants.WAREHOUSE_NSSPL_PARTNER_MAP.get(fs.getWarehouseId());
-
 
144
					} else {
-
 
145
						com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectByEmailId(Utils.SYSTEM_PARTNER);
-
 
146
						retailerId = user.getId();
-
 
147
					}
-
 
148
				}
-
 
149
				responseMap.put(ProfitMandiConstants.TOKEN,
-
 
150
						JWTUtil.create(email, registeredUser.getId(), retailerId, roleTypes));
-
 
151
				LOGGER.info(
-
 
152
						"Param value for email, registeredUser.getId(), retailerId, roleTypes are {}, {}, {} and {}",
-
 
153
						email, registeredUser.getId(), retailerId, Arrays.asList(roleTypes));
-
 
154
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
-
 
155
				return responseMap;
-
 
156
			}
-
 
157
 
-
 
158
			User user = null;
-
 
159
			try {
-
 
160
				user = userRepository.selectByEmailId(email);
-
 
161
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
-
 
162
 
-
 
163
			}
-
 
164
			if (user == null) {
-
 
165
				try {
-
 
166
					user = userRepository.selectByEmailId(email);
-
 
167
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
-
 
168
					responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(email));
-
 
169
					responseMap.put(ProfitMandiConstants.REGISTERED, false);
-
 
170
				}
-
 
171
			} else {
-
 
172
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
-
 
173
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
-
 
174
				String[] roleTypes = new String[roleIds.size()];
-
 
175
				int index = 0;
-
 
176
				for (int roleId : roleIds) {
-
 
177
					roleTypes[index++] = String.valueOf(roleId);
-
 
178
				}
-
 
179
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
-
 
180
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
-
 
181
			}
-
 
182
 
-
 
183
			return responseMap;
-
 
184
 
92
 
-
 
93
			return email;
185
		} catch (JsonProcessingException jsonProcessingException) {
94
		} catch (JsonProcessingException jsonProcessingException) {
186
			throw new ProfitMandiBusinessException("", "", "VE_1001");
95
			throw new ProfitMandiBusinessException("", "", "VE_1001");
187
		} catch (IOException ioException) {
96
		} catch (IOException ioException) {
188
			throw new ProfitMandiBusinessException("", "", "VE_1001");
97
			throw new ProfitMandiBusinessException("", "", "VE_1001");
189
		}
98
		}