Subversion Repositories SmartDukaan

Rev

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