Subversion Repositories SmartDukaan

Rev

Rev 23860 | Rev 24493 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 23860 Rev 24491
Line 1... Line 1...
1
package com.spice.profitmandi.web.processor;
1
package com.spice.profitmandi.web.processor;
2
 
2
 
3
import java.io.IOException;
3
import java.io.IOException;
4
import java.time.LocalDateTime;
4
import java.time.LocalDateTime;
-
 
5
import java.util.Arrays;
5
import java.util.HashMap;
6
import java.util.HashMap;
6
import java.util.Iterator;
7
import java.util.Iterator;
7
import java.util.List;
8
import java.util.List;
8
import java.util.Map;
9
import java.util.Map;
9
 
10
 
10
import org.apache.http.conn.HttpHostConnectException;
11
import org.apache.http.conn.HttpHostConnectException;
-
 
12
import org.apache.logging.log4j.LogManager;
-
 
13
import org.apache.logging.log4j.Logger;
11
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.http.HttpHeaders;
15
import org.springframework.http.HttpHeaders;
13
import org.springframework.http.MediaType;
16
import org.springframework.http.MediaType;
14
import org.springframework.stereotype.Component;
17
import org.springframework.stereotype.Component;
15
 
18
 
Line 19... Line 22...
19
import com.spice.profitmandi.common.enumuration.SchemeType;
22
import com.spice.profitmandi.common.enumuration.SchemeType;
20
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21
import com.spice.profitmandi.common.model.GoogleLoginRequest;
24
import com.spice.profitmandi.common.model.GoogleLoginRequest;
22
import com.spice.profitmandi.common.model.ProfitMandiConstants;
25
import com.spice.profitmandi.common.model.ProfitMandiConstants;
23
import com.spice.profitmandi.common.util.JWTUtil;
26
import com.spice.profitmandi.common.util.JWTUtil;
-
 
27
import com.spice.profitmandi.common.util.Utils;
24
import com.spice.profitmandi.common.web.client.RestClient;
28
import com.spice.profitmandi.common.web.client.RestClient;
25
import com.spice.profitmandi.dao.entity.dtr.SocialUser;
29
import com.spice.profitmandi.dao.entity.dtr.SocialUser;
26
import com.spice.profitmandi.dao.entity.dtr.User;
30
import com.spice.profitmandi.dao.entity.dtr.User;
-
 
31
import com.spice.profitmandi.dao.entity.user.Promoter;
27
import com.spice.profitmandi.dao.enumuration.dtr.Gender;
32
import com.spice.profitmandi.dao.enumuration.dtr.Gender;
28
import com.spice.profitmandi.dao.enumuration.dtr.SocialType;
33
import com.spice.profitmandi.dao.enumuration.dtr.SocialType;
-
 
34
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
29
import com.spice.profitmandi.dao.repository.dtr.SocialUserRepository;
35
import com.spice.profitmandi.dao.repository.dtr.SocialUserRepository;
30
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
36
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
31
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
37
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
32
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
38
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
-
 
39
import com.spice.profitmandi.dao.repository.user.PromoterRepository;
-
 
40
import com.spice.profitmandi.service.AuthService;
33
 
41
 
34
 
42
 
35
@Component
43
@Component
36
public class GoogleLoginProcessor {
44
public class GoogleLoginProcessor {
37
	
45
 
38
	private static final String V1_HOST_NAME = "content.googleapis.com";
46
	private static final String V1_HOST_NAME = "content.googleapis.com";
39
	private static final String V1_URI = "/plus/v1/people/me";
47
	private static final String V1_URI = "/plus/v1/people/me";
40
	private static final String V3_HOST_NAME = "www.googleapis.com";
48
	private static final String V3_HOST_NAME = "www.googleapis.com";
41
	private static final String V3_URI = "/oauth2/v3/tokeninfo";
49
	private static final String V3_URI = "/oauth2/v3/tokeninfo";
-
 
50
	private static final Logger LOGGER = LogManager.getLogger(GoogleLoginProcessor.class);
42
	private static final int PORT_NUMBER = 443;
51
	private static final int PORT_NUMBER = 443;
43
	private final ObjectMapper objectMapper = new ObjectMapper();
52
	private final ObjectMapper objectMapper = new ObjectMapper();
44
	
53
 
45
	@Autowired
54
	@Autowired
46
	private SocialUserRepository socialUserRepository;
55
	private SocialUserRepository socialUserRepository;
47
	
56
 
48
	@Autowired
57
	@Autowired
49
	private UserRepository userRepository;
58
	private UserRepository userRepository;
-
 
59
 
-
 
60
	@Autowired
-
 
61
	private AuthService authService;
50
	
62
	
51
	@Autowired
63
	@Autowired
52
	private UserRoleRepository userRoleRepository;
64
	private AuthRepository authRepository;
53
	
65
	
54
	@Autowired
66
	@Autowired
-
 
67
	private UserRoleRepository userRoleRepository;
-
 
68
 
-
 
69
	@Autowired
-
 
70
	private PromoterRepository promoterRepository;
-
 
71
 
-
 
72
	@Autowired
55
	private UserAccountRepository userAccountRepository;
73
	private UserAccountRepository userAccountRepository;
56
	
74
 
57
	@Autowired
75
	@Autowired
58
	private RestClient restClient;
76
	private RestClient restClient;
59
	
77
 
60
	public Map<String, Object> process(GoogleLoginRequest googleLoginRequest) throws ProfitMandiBusinessException{
78
	public Map<String, Object> process(GoogleLoginRequest googleLoginRequest) throws ProfitMandiBusinessException {
61
		Map<String, String> params = new HashMap<>();
79
		Map<String, String> params = new HashMap<>();
62
		params.put(ProfitMandiConstants.ACCESS_TOKEN, googleLoginRequest.getToken());
80
		params.put(ProfitMandiConstants.ACCESS_TOKEN, googleLoginRequest.getToken());
63
		Map<String, String> headers = new HashMap<>(1);
81
		Map<String, String> headers = new HashMap<>(1);
64
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
82
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
65
		String responseString = null;
83
		String responseString = null;
Line 69... Line 87...
69
			throw new ProfitMandiBusinessException("", "", "Could not connect to remote host");
87
			throw new ProfitMandiBusinessException("", "", "Could not connect to remote host");
70
		}
88
		}
71
		try {
89
		try {
72
			JsonNode rootNode = objectMapper.readTree(responseString);
90
			JsonNode rootNode = objectMapper.readTree(responseString);
73
			SocialUser socialUser = new SocialUser();
91
			SocialUser socialUser = new SocialUser();
74
			if(rootNode.has("emails")){
92
			if (rootNode.has("emails")) {
75
				JsonNode emails = rootNode.get("emails");
93
				JsonNode emails = rootNode.get("emails");
76
				if(emails.isArray()){
94
				if (emails.isArray()) {
77
					Iterator<JsonNode> emailsIterator = emails.elements();
95
					Iterator<JsonNode> emailsIterator = emails.elements();
78
					if(emailsIterator.hasNext()){
96
					if (emailsIterator.hasNext()) {
79
						JsonNode email = emailsIterator.next();
97
						JsonNode email = emailsIterator.next();
80
						if(email.has("value")){
98
						if (email.has("value")) {
81
							socialUser.setEmailId(email.get("value").asText());
99
							socialUser.setEmailId(email.get("value").asText());
82
						}
100
						}
83
					}
101
					}
84
				}
102
				}
85
				//socialUser.setEmailId(rootNode.get("email").asText());
103
				// socialUser.setEmailId(rootNode.get("email").asText());
86
			}
104
			}
87
			if(!socialUserRepository.isExistByEmailId(socialUser.getEmailId())){
105
			if (!socialUserRepository.isExistByEmailId(socialUser.getEmailId())) {
88
				if(rootNode.has("displayName")){
106
				if (rootNode.has("displayName")) {
89
					socialUser.setName(rootNode.get("displayName").asText());
107
					socialUser.setName(rootNode.get("displayName").asText());
90
				}
108
				}
91
				if(rootNode.has("gender")){
109
				if (rootNode.has("gender")) {
92
					String genderName = rootNode.get("gender").asText();
110
					String genderName = rootNode.get("gender").asText();
93
					switch(genderName){
111
					switch (genderName) {
94
						case "male":{
112
					case "male": {
95
							socialUser.setGender(Gender.MALE);
113
						socialUser.setGender(Gender.MALE);
96
							break;
114
						break;
-
 
115
					}
97
						}case "female":{
116
					case "female": {
98
							socialUser.setGender(Gender.FEMALE);
117
						socialUser.setGender(Gender.FEMALE);
99
							break;
118
						break;
100
						}
119
					}
101
					}
120
					}
102
				}
121
				}
103
				socialUser.setCreateTimestamp(LocalDateTime.now());
122
				socialUser.setCreateTimestamp(LocalDateTime.now());
104
				socialUser.setType(SocialType.GOOGLE);
123
				socialUser.setType(SocialType.GOOGLE);
105
				socialUser.setUpdateTimestamp(LocalDateTime.now());
124
				socialUser.setUpdateTimestamp(LocalDateTime.now());
106
				socialUserRepository.persist(socialUser);
125
				socialUserRepository.persist(socialUser);
107
			}
126
			}
108
			
-
 
109
			Map<String, Object> responseMap = new HashMap<>(2);
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
			}
-
 
152
			
110
			User user = null;
153
			User user = null;
111
			try{
154
			try {
112
				user = userRepository.selectByEmailId(socialUser.getEmailId());
155
				user = userRepository.selectByEmailId(socialUser.getEmailId());
113
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
156
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
114
				
157
 
115
			}
158
			}
116
			if(user == null){
159
			if (user == null) {
117
				try{
160
				try {
118
					user = userRepository.selectByEmailId(socialUser.getEmailId());
161
					user = userRepository.selectByEmailId(socialUser.getEmailId());
119
				}catch (ProfitMandiBusinessException profitMandiBusinessException) {
162
				} catch (ProfitMandiBusinessException profitMandiBusinessException) {
120
					responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));
163
					responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));
121
					responseMap.put(ProfitMandiConstants.REGISTERED, false);
164
					responseMap.put(ProfitMandiConstants.REGISTERED, false);
122
				}
165
				}
123
			}
166
			}
124
			if(user != null){
167
			if (user != null) {
125
				//Set<Role> roles = user.getRoles();
168
				// Set<Role> roles = user.getRoles();
126
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
169
				List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
127
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
170
				int retailerId = userAccountRepository.selectRetailerIdByUserId(user.getId());
128
				//Set<Role> roles = new HashSet<>();
171
				// Set<Role> roles = new HashSet<>();
129
				String[] roleTypes = new String[roleIds.size()];
172
				String[] roleTypes = new String[roleIds.size()];
130
				int index = 0;
173
				int index = 0;
131
				for (int roleId : roleIds) {
174
				for (int roleId : roleIds) {
132
					roleTypes[index++] = String.valueOf(roleId);
175
					roleTypes[index++] = String.valueOf(roleId);
133
				}
176
				}
134
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
177
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), retailerId, roleTypes));
135
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
178
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
136
			}
179
			}
137
			
180
 
138
			return responseMap;
181
			return responseMap;
-
 
182
		} catch (
-
 
183
 
139
		}catch (JsonProcessingException jsonProcessingException) {
184
		JsonProcessingException jsonProcessingException) {
140
			//LOGGER.error("Json parse exception of "+json,jsonProcessingException);
185
			// LOGGER.error("Json parse exception of "+json,jsonProcessingException);
141
			throw new ProfitMandiBusinessException("", "", "VE_1001");
186
			throw new ProfitMandiBusinessException("", "", "VE_1001");
142
		}catch (IOException ioException) {
187
		} catch (IOException ioException) {
143
			//LOGGER.error("IO Exception occurred while parsing json String");
188
			// LOGGER.error("IO Exception occurred while parsing json String");
144
			throw new ProfitMandiBusinessException("", "", "VE_1001");
189
			throw new ProfitMandiBusinessException("", "", "VE_1001");
145
		}
190
		}
146
	}
191
	}
147
	
192
 
148
	public Map<String, Object> getFofoDetail(String token) throws ProfitMandiBusinessException{
193
	public Map<String, Object> getFofoDetail(String token) throws ProfitMandiBusinessException {
149
		Map<String, String> params = new HashMap<>();
194
		Map<String, String> params = new HashMap<>();
150
		params.put(ProfitMandiConstants.ID_TOKEN, token);
195
		params.put(ProfitMandiConstants.ID_TOKEN, token);
151
		Map<String, String> headers = new HashMap<>(1);
196
		Map<String, String> headers = new HashMap<>(1);
152
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
197
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
153
		String responseString = null;
198
		String responseString = null;
Line 158... Line 203...
158
			e.printStackTrace();
203
			e.printStackTrace();
159
		}
204
		}
160
		try {
205
		try {
161
			JsonNode rootNode = objectMapper.readTree(responseString);
206
			JsonNode rootNode = objectMapper.readTree(responseString);
162
			User user = null;
207
			User user = null;
163
			try{
208
			try {
164
				user = userRepository.selectByEmailId(rootNode.get("email").asText());
209
				user = userRepository.selectByEmailId(rootNode.get("email").asText());
165
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
210
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
166
				
211
 
167
			}	
212
			}
168
			if(user == null){
213
			if (user == null) {
169
				user = userRepository.selectBySecondryEmailId(rootNode.get("email").asText());
214
				user = userRepository.selectBySecondryEmailId(rootNode.get("email").asText());
170
			}
215
			}
171
			if(user != null){
216
			if (user != null) {
172
				Map<String, Object> responseMap = new HashMap<>(2);
217
				Map<String, Object> responseMap = new HashMap<>(2);
173
				responseMap.put(ProfitMandiConstants.FOFO_ID, user.getId());
218
				responseMap.put(ProfitMandiConstants.FOFO_ID, user.getId());
174
				responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
219
				responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
175
				return responseMap;
220
				return responseMap;
176
			}
221
			}
177
				
222
 
178
		}catch (JsonProcessingException jsonProcessingException) {
223
		} catch (JsonProcessingException jsonProcessingException) {
179
			throw new ProfitMandiBusinessException("", "", "VE_1001");
224
			throw new ProfitMandiBusinessException("", "", "VE_1001");
180
		}catch (IOException ioException) {
225
		} catch (IOException ioException) {
181
			throw new ProfitMandiBusinessException("", "", "VE_1001");
226
			throw new ProfitMandiBusinessException("", "", "VE_1001");
182
		}
227
		}
183
		return null;
228
		return null;
184
	}
229
	}
185
}
230
}