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