Subversion Repositories SmartDukaan

Rev

Rev 27043 | Rev 27256 | 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());
173
				} catch (Exception e) {
27043 amit.gupta 174
					Set<Integer> authUserPartnerSet = csService.getAuthUserPartnerIdMapping().get(authUser.getEmailId());
175
					if(authUserPartnerSet.size() > 0) {
176
						retailerId = authUserPartnerSet.stream().findFirst().get();
27255 amit.gupta 177
						LOGGER.info("Auth User Email {}, Retailer Id - {}", authUser.getName(), retailerId);
27043 amit.gupta 178
					} else {
179
						com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectByEmailId(Utils.SYSTEM_PARTNER);
180
						retailerId = user.getId();
181
					}
25388 tejbeer 182
				}
183
				responseMap.put(ProfitMandiConstants.TOKEN,
184
						JWTUtil.create(socialUser.getEmailId(), registeredUser.getId(), retailerId, roleTypes));
185
				LOGGER.info(
186
						"Param value for socialUser.getEmailId(), registeredUser.getId(), retailerId, roleTypes are {}, {}, {} and {}",
187
						socialUser.getEmailId(), registeredUser.getId(), retailerId, Arrays.asList(roleTypes));
24491 amit.gupta 188
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
189
				return responseMap;
190
			}
25388 tejbeer 191
 
23204 ashik.ali 192
			User user = null;
24491 amit.gupta 193
			try {
23204 ashik.ali 194
				user = userRepository.selectByEmailId(socialUser.getEmailId());
24491 amit.gupta 195
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
196
 
23204 ashik.ali 197
			}
24491 amit.gupta 198
			if (user == null) {
199
				try {
23204 ashik.ali 200
					user = userRepository.selectByEmailId(socialUser.getEmailId());
24491 amit.gupta 201
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
23204 ashik.ali 202
					responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));
203
					responseMap.put(ProfitMandiConstants.REGISTERED, false);
204
				}
24527 amit.gupta 205
			} else {
23860 ashik.ali 206
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
207
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
208
				String[] roleTypes = new String[roleIds.size()];
21282 ashik.ali 209
				int index = 0;
23860 ashik.ali 210
				for (int roleId : roleIds) {
211
					roleTypes[index++] = String.valueOf(roleId);
21282 ashik.ali 212
				}
23860 ashik.ali 213
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
21277 ashik.ali 214
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
215
			}
24491 amit.gupta 216
 
21277 ashik.ali 217
			return responseMap;
24491 amit.gupta 218
		} catch (
219
 
220
		JsonProcessingException jsonProcessingException) {
221
			// LOGGER.error("Json parse exception of "+json,jsonProcessingException);
21277 ashik.ali 222
			throw new ProfitMandiBusinessException("", "", "VE_1001");
24491 amit.gupta 223
		} catch (IOException ioException) {
224
			// LOGGER.error("IO Exception occurred while parsing json String");
21277 ashik.ali 225
			throw new ProfitMandiBusinessException("", "", "VE_1001");
226
		}
227
	}
24491 amit.gupta 228
 
26396 amit.gupta 229
	public Map<String, Object> process(String token) throws ProfitMandiBusinessException {
21556 ashik.ali 230
		Map<String, String> params = new HashMap<>();
231
		params.put(ProfitMandiConstants.ID_TOKEN, token);
26673 amit.gupta 232
		Map<String, String> headers = new HashMap<>();
22355 ashik.ali 233
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
23860 ashik.ali 234
		String responseString = null;
21556 ashik.ali 235
		try {
23532 amit.gupta 236
			responseString = restClient.get(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER, V3_URI, params, headers);
23860 ashik.ali 237
		} catch (HttpHostConnectException e) {
238
			// TODO Auto-generated catch block
239
			e.printStackTrace();
23532 amit.gupta 240
		}
241
		try {
21556 ashik.ali 242
			JsonNode rootNode = objectMapper.readTree(responseString);
26396 amit.gupta 243
			String email = rootNode.get("email").asText();
244
 
245
			String name = authService.getNameByEmailId(email);
246
 
247
			Map<String, Object> responseMap = new HashMap<>(2);
248
			LOGGER.info("User Name from getNameByEmailId({}) is {}", email, name);
249
			if (name != null) {
250
				User registeredUser = null;
251
				AuthUser authUser = authRepository.selectByGmailId(email);
252
 
253
				if (authRepository.selectByGmailId(email) != null) {
254
					registeredUser = userRepository.selectByEmailId(authUser.getEmailId());
255
				} else if (promoterRepository.isExistByEmailId(email)) {
256
					Promoter promoter = promoterRepository.selectByEmailId(email);
257
					int userId = userAccountRepository.selectUserIdByRetailerId(promoter.getRetailerId());
258
					registeredUser = userRepository.selectById(userId);
259
				} else if (userRepository.isExistBySecondryEmailId(email)) {
260
					registeredUser = userRepository.selectBySecondryEmailId(email);
261
				}
262
				LOGGER.info("4");
263
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(registeredUser.getId());
264
				String[] roleTypes = new String[roleIds.size()];
265
				int index = 0;
266
				for (int roleId : roleIds) {
267
					roleTypes[index++] = String.valueOf(roleId);
268
				}
269
				int retailerId;
270
				try {
271
					retailerId = userAccountRepository.selectRetailerIdByUserId(registeredUser.getId());
272
				} catch (Exception e) {
273
					com.spice.profitmandi.dao.entity.user.User user = userUserRepository.selectByEmailId(Utils.SYSTEM_PARTNER);
274
					retailerId = user.getId();
275
				}
276
				responseMap.put(ProfitMandiConstants.TOKEN,
277
						JWTUtil.create(email, registeredUser.getId(), retailerId, roleTypes));
278
				LOGGER.info(
279
						"Param value for email, registeredUser.getId(), retailerId, roleTypes are {}, {}, {} and {}",
280
						email, registeredUser.getId(), retailerId, Arrays.asList(roleTypes));
281
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
282
				return responseMap;
283
			}
284
 
23204 ashik.ali 285
			User user = null;
24491 amit.gupta 286
			try {
26396 amit.gupta 287
				user = userRepository.selectByEmailId(email);
24491 amit.gupta 288
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
289
 
290
			}
291
			if (user == null) {
26396 amit.gupta 292
				try {
293
					user = userRepository.selectByEmailId(email);
294
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
295
					responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(email));
296
					responseMap.put(ProfitMandiConstants.REGISTERED, false);
297
				}
298
			} else {
299
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
300
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
301
				String[] roleTypes = new String[roleIds.size()];
302
				int index = 0;
303
				for (int roleId : roleIds) {
304
					roleTypes[index++] = String.valueOf(roleId);
305
				}
306
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
307
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
23204 ashik.ali 308
			}
24491 amit.gupta 309
 
26396 amit.gupta 310
			return responseMap;
311
 
24491 amit.gupta 312
		} catch (JsonProcessingException jsonProcessingException) {
21556 ashik.ali 313
			throw new ProfitMandiBusinessException("", "", "VE_1001");
24491 amit.gupta 314
		} catch (IOException ioException) {
21556 ashik.ali 315
			throw new ProfitMandiBusinessException("", "", "VE_1001");
316
		}
317
	}
26590 amit.gupta 318
 
319
	public Map<String, Object>  processStore(String storeCode) throws ProfitMandiBusinessException {
320
		Map<String, Object> responseMap = new HashMap<>();
26599 amit.gupta 321
		storeCode = storeCode.toUpperCase();
26598 amit.gupta 322
		int retailerId = retailerService.getStoreCodeRetailerMap().get(storeCode);
26590 amit.gupta 323
		int userId = userAccountRepository.selectUserIdByRetailerId(retailerId);
324
		List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(userId);
325
 
326
		String[] roleTypes = new String[roleIds.size()];
327
		int index = 0;
328
		for (int roleId : roleIds) {
329
			roleTypes[index++] = String.valueOf(roleId);
330
		}
331
		responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(userId, retailerId, roleTypes));
332
		responseMap.put(ProfitMandiConstants.REGISTERED, true);
333
		return responseMap;
334
	}
21277 ashik.ali 335
}