Subversion Repositories SmartDukaan

Rev

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