Subversion Repositories SmartDukaan

Rev

Rev 27258 | Rev 28094 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21277 ashik.ali 1
package com.spice.profitmandi.web.processor;
2
 
3
import java.io.IOException;
4
import java.time.LocalDateTime;
24491 amit.gupta 5
import java.util.Arrays;
21277 ashik.ali 6
import java.util.HashMap;
21278 ashik.ali 7
import java.util.Iterator;
22011 ashik.ali 8
import java.util.List;
21277 ashik.ali 9
import java.util.Map;
27043 amit.gupta 10
import java.util.Set;
21277 ashik.ali 11
 
23532 amit.gupta 12
import org.apache.http.conn.HttpHostConnectException;
24491 amit.gupta 13
import org.apache.logging.log4j.LogManager;
14
import org.apache.logging.log4j.Logger;
21278 ashik.ali 15
import org.springframework.beans.factory.annotation.Autowired;
22355 ashik.ali 16
import org.springframework.http.HttpHeaders;
17
import org.springframework.http.MediaType;
21282 ashik.ali 18
import org.springframework.stereotype.Component;
21278 ashik.ali 19
 
21277 ashik.ali 20
import com.fasterxml.jackson.core.JsonProcessingException;
21
import com.fasterxml.jackson.databind.JsonNode;
22
import com.fasterxml.jackson.databind.ObjectMapper;
21556 ashik.ali 23
import com.spice.profitmandi.common.enumuration.SchemeType;
21277 ashik.ali 24
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22931 ashik.ali 25
import com.spice.profitmandi.common.model.GoogleLoginRequest;
21277 ashik.ali 26
import com.spice.profitmandi.common.model.ProfitMandiConstants;
27
import com.spice.profitmandi.common.util.JWTUtil;
24491 amit.gupta 28
import com.spice.profitmandi.common.util.Utils;
21556 ashik.ali 29
import com.spice.profitmandi.common.web.client.RestClient;
25384 tejbeer 30
import com.spice.profitmandi.dao.entity.auth.AuthUser;
21735 ashik.ali 31
import com.spice.profitmandi.dao.entity.dtr.SocialUser;
32
import com.spice.profitmandi.dao.entity.dtr.User;
24491 amit.gupta 33
import com.spice.profitmandi.dao.entity.user.Promoter;
21735 ashik.ali 34
import com.spice.profitmandi.dao.enumuration.dtr.Gender;
35
import com.spice.profitmandi.dao.enumuration.dtr.SocialType;
24491 amit.gupta 36
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
27043 amit.gupta 37
import com.spice.profitmandi.dao.repository.cs.CsService;
21735 ashik.ali 38
import com.spice.profitmandi.dao.repository.dtr.SocialUserRepository;
23860 ashik.ali 39
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
21735 ashik.ali 40
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
22011 ashik.ali 41
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
24491 amit.gupta 42
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
43
import com.spice.profitmandi.service.AuthService;
26590 amit.gupta 44
import com.spice.profitmandi.service.user.RetailerService;
21277 ashik.ali 45
 
21282 ashik.ali 46
@Component
21277 ashik.ali 47
public class GoogleLoginProcessor {
24491 amit.gupta 48
 
21556 ashik.ali 49
	private static final String V1_HOST_NAME = "content.googleapis.com";
50
	private static final String V1_URI = "/plus/v1/people/me";
51
	private static final String V3_HOST_NAME = "www.googleapis.com";
52
	private static final String V3_URI = "/oauth2/v3/tokeninfo";
24491 amit.gupta 53
	private static final Logger LOGGER = LogManager.getLogger(GoogleLoginProcessor.class);
21277 ashik.ali 54
	private static final int PORT_NUMBER = 443;
55
	private final ObjectMapper objectMapper = new ObjectMapper();
24491 amit.gupta 56
 
21278 ashik.ali 57
	@Autowired
22931 ashik.ali 58
	private SocialUserRepository socialUserRepository;
24491 amit.gupta 59
 
21278 ashik.ali 60
	@Autowired
22931 ashik.ali 61
	private UserRepository userRepository;
24491 amit.gupta 62
 
63
	@Autowired
64
	private AuthService authService;
25388 tejbeer 65
 
22011 ashik.ali 66
	@Autowired
25388 tejbeer 67
	com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
68
 
69
	@Autowired
24491 amit.gupta 70
	private AuthRepository authRepository;
25388 tejbeer 71
 
26590 amit.gupta 72
 
23860 ashik.ali 73
	@Autowired
24491 amit.gupta 74
	private UserRoleRepository userRoleRepository;
75
 
76
	@Autowired
77
	private PromoterRepository promoterRepository;
78
 
79
	@Autowired
23860 ashik.ali 80
	private UserAccountRepository userAccountRepository;
27043 amit.gupta 81
 
82
	@Autowired
83
	private CsService csService;
24491 amit.gupta 84
 
23860 ashik.ali 85
	@Autowired
86
	private RestClient restClient;
26590 amit.gupta 87
	@Autowired
88
	private RetailerService retailerService;
25388 tejbeer 89
 
24491 amit.gupta 90
	public Map<String, Object> process(GoogleLoginRequest googleLoginRequest) throws ProfitMandiBusinessException {
24495 amit.gupta 91
		LOGGER.info("1");
21277 ashik.ali 92
		Map<String, String> params = new HashMap<>();
22931 ashik.ali 93
		params.put(ProfitMandiConstants.ACCESS_TOKEN, googleLoginRequest.getToken());
22355 ashik.ali 94
		Map<String, String> headers = new HashMap<>(1);
95
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
23860 ashik.ali 96
		String responseString = null;
21277 ashik.ali 97
		try {
23532 amit.gupta 98
			responseString = restClient.get(SchemeType.HTTPS, V1_HOST_NAME, PORT_NUMBER, V1_URI, params, headers);
99
		} catch (HttpHostConnectException e) {
100
			throw new ProfitMandiBusinessException("", "", "Could not connect to remote host");
101
		}
102
		try {
21278 ashik.ali 103
			JsonNode rootNode = objectMapper.readTree(responseString);
21277 ashik.ali 104
			SocialUser socialUser = new SocialUser();
24491 amit.gupta 105
			if (rootNode.has("emails")) {
21278 ashik.ali 106
				JsonNode emails = rootNode.get("emails");
24491 amit.gupta 107
				if (emails.isArray()) {
21286 ashik.ali 108
					Iterator<JsonNode> emailsIterator = emails.elements();
24491 amit.gupta 109
					if (emailsIterator.hasNext()) {
21278 ashik.ali 110
						JsonNode email = emailsIterator.next();
24491 amit.gupta 111
						if (email.has("value")) {
25954 amit.gupta 112
							/*if (ProfitMandiConstants.BLOCKED_EMAILS.contains(email.get("value").asText())) {
25388 tejbeer 113
								throw new ProfitMandiBusinessException("User Account", email.get("value").asText(),
114
										"User is temporarily suspended.");
25954 amit.gupta 115
							}*/
21278 ashik.ali 116
							socialUser.setEmailId(email.get("value").asText());
117
						}
118
					}
119
				}
24491 amit.gupta 120
				// socialUser.setEmailId(rootNode.get("email").asText());
21277 ashik.ali 121
			}
24495 amit.gupta 122
			LOGGER.info("2");
24491 amit.gupta 123
			if (!socialUserRepository.isExistByEmailId(socialUser.getEmailId())) {
124
				if (rootNode.has("displayName")) {
22024 ashik.ali 125
					socialUser.setName(rootNode.get("displayName").asText());
126
				}
24491 amit.gupta 127
				if (rootNode.has("gender")) {
22024 ashik.ali 128
					String genderName = rootNode.get("gender").asText();
24491 amit.gupta 129
					switch (genderName) {
130
					case "male": {
131
						socialUser.setGender(Gender.MALE);
132
						break;
21278 ashik.ali 133
					}
24491 amit.gupta 134
					case "female": {
135
						socialUser.setGender(Gender.FEMALE);
136
						break;
137
					}
138
					}
21278 ashik.ali 139
				}
22024 ashik.ali 140
				socialUser.setCreateTimestamp(LocalDateTime.now());
141
				socialUser.setType(SocialType.GOOGLE);
142
				socialUser.setUpdateTimestamp(LocalDateTime.now());
143
				socialUserRepository.persist(socialUser);
21278 ashik.ali 144
			}
24491 amit.gupta 145
			Map<String, Object> responseMap = new HashMap<>(2);
146
 
24495 amit.gupta 147
			LOGGER.info("3");
24491 amit.gupta 148
			String name = authService.getNameByEmailId(socialUser.getEmailId());
24493 amit.gupta 149
			LOGGER.info("User Name from getNameByEmailId({}) is {}", socialUser.getEmailId(), name);
25388 tejbeer 150
			if (name != null) {
24491 amit.gupta 151
				User registeredUser = null;
25384 tejbeer 152
				AuthUser authUser = authRepository.selectByGmailId(socialUser.getEmailId());
25388 tejbeer 153
 
154
				if (authRepository.selectByGmailId(socialUser.getEmailId()) != null) {
25384 tejbeer 155
					registeredUser = userRepository.selectByEmailId(authUser.getEmailId());
25388 tejbeer 156
				} else if (promoterRepository.isExistByEmailId(socialUser.getEmailId())) {
24491 amit.gupta 157
					Promoter promoter = promoterRepository.selectByEmailId(socialUser.getEmailId());
24493 amit.gupta 158
					int userId = userAccountRepository.selectUserIdByRetailerId(promoter.getRetailerId());
159
					registeredUser = userRepository.selectById(userId);
25388 tejbeer 160
				} else if (userRepository.isExistBySecondryEmailId(socialUser.getEmailId())) {
24491 amit.gupta 161
					registeredUser = userRepository.selectBySecondryEmailId(socialUser.getEmailId());
162
				}
24495 amit.gupta 163
				LOGGER.info("4");
24491 amit.gupta 164
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(registeredUser.getId());
165
				String[] roleTypes = new String[roleIds.size()];
166
				int index = 0;
167
				for (int roleId : roleIds) {
168
					roleTypes[index++] = String.valueOf(roleId);
169
				}
25388 tejbeer 170
				int retailerId;
171
				try {
172
					retailerId = userAccountRepository.selectRetailerIdByUserId(registeredUser.getId());
27257 amit.gupta 173
					LOGGER.info("173 - Auth User Email {}, Retailer Id - {}", authUser.getName(), retailerId);
25388 tejbeer 174
				} catch (Exception e) {
27043 amit.gupta 175
					Set<Integer> authUserPartnerSet = csService.getAuthUserPartnerIdMapping().get(authUser.getEmailId());
176
					if(authUserPartnerSet.size() > 0) {
177
						retailerId = authUserPartnerSet.stream().findFirst().get();
27256 amit.gupta 178
						LOGGER.info("178 - Auth User Email {}, Retailer Id - {}", socialUser.getEmailId(), retailerId);
27043 amit.gupta 179
					} else {
180
						com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectByEmailId(Utils.SYSTEM_PARTNER);
181
						retailerId = user.getId();
27256 amit.gupta 182
						LOGGER.info("Auth User Email {}, Retailer Id - {}", socialUser.getEmailId(), retailerId);
27043 amit.gupta 183
					}
25388 tejbeer 184
				}
185
				responseMap.put(ProfitMandiConstants.TOKEN,
186
						JWTUtil.create(socialUser.getEmailId(), registeredUser.getId(), retailerId, roleTypes));
187
				LOGGER.info(
188
						"Param value for socialUser.getEmailId(), registeredUser.getId(), retailerId, roleTypes are {}, {}, {} and {}",
189
						socialUser.getEmailId(), registeredUser.getId(), retailerId, Arrays.asList(roleTypes));
24491 amit.gupta 190
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
191
				return responseMap;
192
			}
25388 tejbeer 193
 
23204 ashik.ali 194
			User user = null;
24491 amit.gupta 195
			try {
23204 ashik.ali 196
				user = userRepository.selectByEmailId(socialUser.getEmailId());
24491 amit.gupta 197
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
198
 
23204 ashik.ali 199
			}
24491 amit.gupta 200
			if (user == null) {
201
				try {
23204 ashik.ali 202
					user = userRepository.selectByEmailId(socialUser.getEmailId());
24491 amit.gupta 203
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23204 ashik.ali 204
					responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));
205
					responseMap.put(ProfitMandiConstants.REGISTERED, false);
206
				}
24527 amit.gupta 207
			} else {
23860 ashik.ali 208
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
209
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
210
				String[] roleTypes = new String[roleIds.size()];
21282 ashik.ali 211
				int index = 0;
23860 ashik.ali 212
				for (int roleId : roleIds) {
213
					roleTypes[index++] = String.valueOf(roleId);
21282 ashik.ali 214
				}
23860 ashik.ali 215
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
21277 ashik.ali 216
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
217
			}
24491 amit.gupta 218
 
21277 ashik.ali 219
			return responseMap;
24491 amit.gupta 220
		} catch (
221
 
222
		JsonProcessingException jsonProcessingException) {
223
			// LOGGER.error("Json parse exception of "+json,jsonProcessingException);
21277 ashik.ali 224
			throw new ProfitMandiBusinessException("", "", "VE_1001");
24491 amit.gupta 225
		} catch (IOException ioException) {
226
			// LOGGER.error("IO Exception occurred while parsing json String");
21277 ashik.ali 227
			throw new ProfitMandiBusinessException("", "", "VE_1001");
228
		}
229
	}
24491 amit.gupta 230
 
26396 amit.gupta 231
	public Map<String, Object> process(String token) throws ProfitMandiBusinessException {
21556 ashik.ali 232
		Map<String, String> params = new HashMap<>();
233
		params.put(ProfitMandiConstants.ID_TOKEN, token);
26673 amit.gupta 234
		Map<String, String> headers = new HashMap<>();
22355 ashik.ali 235
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
23860 ashik.ali 236
		String responseString = null;
21556 ashik.ali 237
		try {
23532 amit.gupta 238
			responseString = restClient.get(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER, V3_URI, params, headers);
23860 ashik.ali 239
		} catch (HttpHostConnectException e) {
240
			// TODO Auto-generated catch block
241
			e.printStackTrace();
23532 amit.gupta 242
		}
243
		try {
21556 ashik.ali 244
			JsonNode rootNode = objectMapper.readTree(responseString);
26396 amit.gupta 245
			String email = rootNode.get("email").asText();
246
 
247
			String name = authService.getNameByEmailId(email);
248
 
249
			Map<String, Object> responseMap = new HashMap<>(2);
250
			LOGGER.info("User Name from getNameByEmailId({}) is {}", email, name);
251
			if (name != null) {
252
				User registeredUser = null;
253
				AuthUser authUser = authRepository.selectByGmailId(email);
254
 
255
				if (authRepository.selectByGmailId(email) != null) {
256
					registeredUser = userRepository.selectByEmailId(authUser.getEmailId());
257
				} else if (promoterRepository.isExistByEmailId(email)) {
258
					Promoter promoter = promoterRepository.selectByEmailId(email);
259
					int userId = userAccountRepository.selectUserIdByRetailerId(promoter.getRetailerId());
260
					registeredUser = userRepository.selectById(userId);
261
				} else if (userRepository.isExistBySecondryEmailId(email)) {
262
					registeredUser = userRepository.selectBySecondryEmailId(email);
263
				}
264
				LOGGER.info("4");
265
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(registeredUser.getId());
266
				String[] roleTypes = new String[roleIds.size()];
267
				int index = 0;
268
				for (int roleId : roleIds) {
269
					roleTypes[index++] = String.valueOf(roleId);
270
				}
271
				int retailerId;
272
				try {
27258 amit.gupta 273
					retailerId = userAccountRepository.selectRetailerIdByUserId(registeredUser.getId());
274
				} catch (Exception e) {
27257 amit.gupta 275
					Set<Integer> authUserPartnerSet = csService.getAuthUserPartnerIdMapping().get(authUser.getEmailId());
27268 amit.gupta 276
					if(authUserPartnerSet != null && authUserPartnerSet.size() > 0) {
27257 amit.gupta 277
						retailerId = authUserPartnerSet.stream().findFirst().get();
27258 amit.gupta 278
					} else {
279
						com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectByEmailId(Utils.SYSTEM_PARTNER);
280
						retailerId = user.getId();
281
					}
26396 amit.gupta 282
				}
283
				responseMap.put(ProfitMandiConstants.TOKEN,
284
						JWTUtil.create(email, registeredUser.getId(), retailerId, roleTypes));
285
				LOGGER.info(
286
						"Param value for email, registeredUser.getId(), retailerId, roleTypes are {}, {}, {} and {}",
287
						email, registeredUser.getId(), retailerId, Arrays.asList(roleTypes));
288
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
289
				return responseMap;
290
			}
291
 
23204 ashik.ali 292
			User user = null;
24491 amit.gupta 293
			try {
26396 amit.gupta 294
				user = userRepository.selectByEmailId(email);
24491 amit.gupta 295
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
296
 
297
			}
298
			if (user == null) {
26396 amit.gupta 299
				try {
300
					user = userRepository.selectByEmailId(email);
301
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
302
					responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(email));
303
					responseMap.put(ProfitMandiConstants.REGISTERED, false);
304
				}
305
			} else {
306
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
307
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
308
				String[] roleTypes = new String[roleIds.size()];
309
				int index = 0;
310
				for (int roleId : roleIds) {
311
					roleTypes[index++] = String.valueOf(roleId);
312
				}
313
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
314
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
23204 ashik.ali 315
			}
24491 amit.gupta 316
 
26396 amit.gupta 317
			return responseMap;
318
 
24491 amit.gupta 319
		} catch (JsonProcessingException jsonProcessingException) {
21556 ashik.ali 320
			throw new ProfitMandiBusinessException("", "", "VE_1001");
24491 amit.gupta 321
		} catch (IOException ioException) {
21556 ashik.ali 322
			throw new ProfitMandiBusinessException("", "", "VE_1001");
323
		}
324
	}
26590 amit.gupta 325
 
326
	public Map<String, Object>  processStore(String storeCode) throws ProfitMandiBusinessException {
327
		Map<String, Object> responseMap = new HashMap<>();
26599 amit.gupta 328
		storeCode = storeCode.toUpperCase();
26598 amit.gupta 329
		int retailerId = retailerService.getStoreCodeRetailerMap().get(storeCode);
26590 amit.gupta 330
		int userId = userAccountRepository.selectUserIdByRetailerId(retailerId);
331
		List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(userId);
332
 
333
		String[] roleTypes = new String[roleIds.size()];
334
		int index = 0;
335
		for (int roleId : roleIds) {
336
			roleTypes[index++] = String.valueOf(roleId);
337
		}
338
		responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(userId, retailerId, roleTypes));
339
		responseMap.put(ProfitMandiConstants.REGISTERED, true);
340
		return responseMap;
341
	}
21277 ashik.ali 342
}