Subversion Repositories SmartDukaan

Rev

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