Subversion Repositories SmartDukaan

Rev

Rev 21735 | Rev 22024 | 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
			}
21278 ashik.ali 71
			if(rootNode.has("displayName")){
72
				socialUser.setName(rootNode.get("displayName").asText());
21277 ashik.ali 73
			}
21278 ashik.ali 74
			if(rootNode.has("gender")){
75
				String genderName = rootNode.get("gender").asText();
76
				switch(genderName){
77
					case "male":{
78
						socialUser.setGender(Gender.MALE);
79
						break;
80
					}case "female":{
81
						socialUser.setGender(Gender.FEMALE);
82
						break;
83
					}
84
				}
85
			}
21277 ashik.ali 86
			socialUser.setCreateTimestamp(LocalDateTime.now());
87
			socialUser.setType(SocialType.GOOGLE);
88
			socialUser.setUpdateTimestamp(LocalDateTime.now());
89
			socialUserRepository.persist(socialUser);
90
			Map<String, Object> responseMap = new HashMap<>(2);
91
			try{
92
				User user = userRepository.selectByEmailId(socialUser.getEmailId());
21426 ashik.ali 93
				//Set<Role> roles = user.getRoles();
22011 ashik.ali 94
				List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
95
				//Set<Role> roles = new HashSet<>();
96
				String[] roleTypes = new String[userRoles.size()];
21282 ashik.ali 97
				int index = 0;
22011 ashik.ali 98
				for (UserRole userRole : userRoles) {
99
					roleTypes[index++] = userRole.getRoleType().toString();
21282 ashik.ali 100
				}
22011 ashik.ali 101
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
21277 ashik.ali 102
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
21440 ashik.ali 103
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
21469 amit.gupta 104
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(socialUser.getEmailId()));
21277 ashik.ali 105
				responseMap.put(ProfitMandiConstants.REGISTERED, false);
106
			}
107
			return responseMap;
108
		}catch (JsonProcessingException jsonProcessingException) {
109
			//LOGGER.error("Json parse exception of "+json,jsonProcessingException);
110
			throw new ProfitMandiBusinessException("", "", "VE_1001");
111
		}catch (IOException ioException) {
112
			//LOGGER.error("IO Exception occurred while parsing json String");
113
			throw new ProfitMandiBusinessException("", "", "VE_1001");
114
		}
115
	}
21556 ashik.ali 116
 
117
	public Map<String, Object> getFofoDetail(String token) throws ProfitMandiBusinessException{
118
		RestClient restClient = new RestClient(SchemeType.HTTPS, V3_HOST_NAME, PORT_NUMBER);
119
		Map<String, String> params = new HashMap<>();
120
		params.put(ProfitMandiConstants.ID_TOKEN, token);
121
		String responseString = restClient.get(V3_URI, params);
122
		try {
123
			JsonNode rootNode = objectMapper.readTree(responseString);
124
			try{
125
				User user = userRepository.selectByEmailId(rootNode.get("email").asText());
126
				Map<String, Object> responseMap = new HashMap<>(2);
127
				responseMap.put(ProfitMandiConstants.FOFO_ID, user.getId());
128
				responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
129
				return responseMap;
130
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
131
				throw new ProfitMandiBusinessException(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(), "");
132
			}
133
 
134
		}catch (JsonProcessingException jsonProcessingException) {
135
			throw new ProfitMandiBusinessException("", "", "VE_1001");
136
		}catch (IOException ioException) {
137
			throw new ProfitMandiBusinessException("", "", "VE_1001");
138
		}
139
	}
21277 ashik.ali 140
}