Subversion Repositories SmartDukaan

Rev

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