Subversion Repositories SmartDukaan

Rev

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