Subversion Repositories SmartDukaan

Rev

Rev 22355 | Rev 23204 | 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;
5
import java.util.HashMap;
21278 ashik.ali 6
import java.util.Iterator;
22011 ashik.ali 7
import java.util.List;
21277 ashik.ali 8
import java.util.Map;
9
 
21278 ashik.ali 10
import org.springframework.beans.factory.annotation.Autowired;
22355 ashik.ali 11
import org.springframework.http.HttpHeaders;
12
import org.springframework.http.MediaType;
21282 ashik.ali 13
import org.springframework.stereotype.Component;
21278 ashik.ali 14
 
21277 ashik.ali 15
import com.fasterxml.jackson.core.JsonProcessingException;
16
import com.fasterxml.jackson.databind.JsonNode;
17
import com.fasterxml.jackson.databind.ObjectMapper;
21556 ashik.ali 18
import com.spice.profitmandi.common.enumuration.SchemeType;
21277 ashik.ali 19
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22931 ashik.ali 20
import com.spice.profitmandi.common.model.GoogleLoginRequest;
21277 ashik.ali 21
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22
import com.spice.profitmandi.common.util.JWTUtil;
21556 ashik.ali 23
import com.spice.profitmandi.common.web.client.RestClient;
21735 ashik.ali 24
import com.spice.profitmandi.dao.entity.dtr.SocialUser;
25
import com.spice.profitmandi.dao.entity.dtr.User;
22011 ashik.ali 26
import com.spice.profitmandi.dao.entity.dtr.UserRole;
21735 ashik.ali 27
import com.spice.profitmandi.dao.enumuration.dtr.Gender;
28
import com.spice.profitmandi.dao.enumuration.dtr.SocialType;
29
import com.spice.profitmandi.dao.repository.dtr.SocialUserRepository;
30
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
22011 ashik.ali 31
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
21277 ashik.ali 32
 
22011 ashik.ali 33
 
21282 ashik.ali 34
@Component
21277 ashik.ali 35
public class GoogleLoginProcessor {
21282 ashik.ali 36
 
21556 ashik.ali 37
	private static final String V1_HOST_NAME = "content.googleapis.com";
38
	private static final String V1_URI = "/plus/v1/people/me";
39
	private static final String V3_HOST_NAME = "www.googleapis.com";
40
	private static final String V3_URI = "/oauth2/v3/tokeninfo";
21277 ashik.ali 41
	private static final int PORT_NUMBER = 443;
42
	private final ObjectMapper objectMapper = new ObjectMapper();
43
 
21278 ashik.ali 44
	@Autowired
22931 ashik.ali 45
	private SocialUserRepository socialUserRepository;
21277 ashik.ali 46
 
21278 ashik.ali 47
	@Autowired
22931 ashik.ali 48
	private UserRepository userRepository;
21278 ashik.ali 49
 
22011 ashik.ali 50
	@Autowired
22931 ashik.ali 51
	private UserRoleRepository userRoleRepository;
22011 ashik.ali 52
 
22931 ashik.ali 53
	public Map<String, Object> process(GoogleLoginRequest googleLoginRequest) throws ProfitMandiBusinessException{
21556 ashik.ali 54
		RestClient restClient = new RestClient(SchemeType.HTTPS, V1_HOST_NAME, PORT_NUMBER);
21277 ashik.ali 55
		Map<String, String> params = new HashMap<>();
22931 ashik.ali 56
		params.put(ProfitMandiConstants.ACCESS_TOKEN, googleLoginRequest.getToken());
22355 ashik.ali 57
		Map<String, String> headers = new HashMap<>(1);
58
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
59
		String responseString = restClient.get(V1_URI, params, headers);
21277 ashik.ali 60
		try {
21278 ashik.ali 61
			JsonNode rootNode = objectMapper.readTree(responseString);
21277 ashik.ali 62
			SocialUser socialUser = new SocialUser();
21278 ashik.ali 63
			if(rootNode.has("emails")){
64
				JsonNode emails = rootNode.get("emails");
65
				if(emails.isArray()){
21286 ashik.ali 66
					Iterator<JsonNode> emailsIterator = emails.elements();
21278 ashik.ali 67
					if(emailsIterator.hasNext()){
68
						JsonNode email = emailsIterator.next();
69
						if(email.has("value")){
70
							socialUser.setEmailId(email.get("value").asText());
71
						}
72
					}
73
				}
21286 ashik.ali 74
				//socialUser.setEmailId(rootNode.get("email").asText());
21277 ashik.ali 75
			}
22024 ashik.ali 76
			if(!socialUserRepository.isExistByEmailId(socialUser.getEmailId())){
77
				if(rootNode.has("displayName")){
78
					socialUser.setName(rootNode.get("displayName").asText());
79
				}
80
				if(rootNode.has("gender")){
81
					String genderName = rootNode.get("gender").asText();
82
					switch(genderName){
83
						case "male":{
84
							socialUser.setGender(Gender.MALE);
85
							break;
86
						}case "female":{
87
							socialUser.setGender(Gender.FEMALE);
88
							break;
89
						}
21278 ashik.ali 90
					}
91
				}
22024 ashik.ali 92
				socialUser.setCreateTimestamp(LocalDateTime.now());
93
				socialUser.setType(SocialType.GOOGLE);
94
				socialUser.setUpdateTimestamp(LocalDateTime.now());
95
				socialUserRepository.persist(socialUser);
21278 ashik.ali 96
			}
22024 ashik.ali 97
 
21277 ashik.ali 98
			Map<String, Object> responseMap = new HashMap<>(2);
99
			try{
100
				User user = userRepository.selectByEmailId(socialUser.getEmailId());
21426 ashik.ali 101
				//Set<Role> roles = user.getRoles();
22011 ashik.ali 102
				List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
103
				//Set<Role> roles = new HashSet<>();
104
				String[] roleTypes = new String[userRoles.size()];
21282 ashik.ali 105
				int index = 0;
22011 ashik.ali 106
				for (UserRole userRole : userRoles) {
107
					roleTypes[index++] = userRole.getRoleType().toString();
21282 ashik.ali 108
				}
22011 ashik.ali 109
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
21277 ashik.ali 110
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
21440 ashik.ali 111
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
21469 amit.gupta 112
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));
21277 ashik.ali 113
				responseMap.put(ProfitMandiConstants.REGISTERED, false);
114
			}
115
			return responseMap;
116
		}catch (JsonProcessingException jsonProcessingException) {
117
			//LOGGER.error("Json parse exception of "+json,jsonProcessingException);
118
			throw new ProfitMandiBusinessException("", "", "VE_1001");
119
		}catch (IOException ioException) {
120
			//LOGGER.error("IO Exception occurred while parsing json String");
121
			throw new ProfitMandiBusinessException("", "", "VE_1001");
122
		}
123
	}
21556 ashik.ali 124
 
125
	public Map<String, Object> getFofoDetail(String token) throws ProfitMandiBusinessException{
126
		RestClient restClient = new RestClient(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER);
127
		Map<String, String> params = new HashMap<>();
128
		params.put(ProfitMandiConstants.ID_TOKEN, token);
22355 ashik.ali 129
		Map<String, String> headers = new HashMap<>(1);
130
		headers.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
131
		String responseString = restClient.get(V3_URI, params, headers);
21556 ashik.ali 132
		try {
133
			JsonNode rootNode = objectMapper.readTree(responseString);
134
			try{
135
				User user = userRepository.selectByEmailId(rootNode.get("email").asText());
136
				Map<String, Object> responseMap = new HashMap<>(2);
137
				responseMap.put(ProfitMandiConstants.FOFO_ID, user.getId());
138
				responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
139
				return responseMap;
140
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
141
				throw new ProfitMandiBusinessException(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(), "");
142
			}
143
 
144
		}catch (JsonProcessingException jsonProcessingException) {
145
			throw new ProfitMandiBusinessException("", "", "VE_1001");
146
		}catch (IOException ioException) {
147
			throw new ProfitMandiBusinessException("", "", "VE_1001");
148
		}
149
	}
21277 ashik.ali 150
}