Subversion Repositories SmartDukaan

Rev

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