Subversion Repositories SmartDukaan

Rev

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