Subversion Repositories SmartDukaan

Rev

Rev 21277 | Rev 21282 | 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;
21277 ashik.ali 7
import java.util.Map;
8
 
21278 ashik.ali 9
import org.springframework.beans.factory.annotation.Autowired;
10
 
21277 ashik.ali 11
import com.fasterxml.jackson.core.JsonProcessingException;
12
import com.fasterxml.jackson.databind.JsonNode;
13
import com.fasterxml.jackson.databind.ObjectMapper;
14
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
15
import com.spice.profitmandi.common.model.ProfitMandiConstants;
16
import com.spice.profitmandi.common.util.JWTUtil;
17
import com.spice.profitmandi.dao.entity.SocialUser;
18
import com.spice.profitmandi.dao.entity.User;
21278 ashik.ali 19
import com.spice.profitmandi.dao.enumuration.Gender;
21277 ashik.ali 20
import com.spice.profitmandi.dao.enumuration.SocialType;
21
import com.spice.profitmandi.dao.repository.SocialUserRepository;
22
import com.spice.profitmandi.dao.repository.UserRepository;
23
import com.spice.profitmandi.web.client.RestClient;
24
import com.spice.profitmandi.web.enumuration.SchemeType;
25
 
26
public class GoogleLoginProcessor {
21278 ashik.ali 27
	//https://content.googleapis.com/plus/v1/people/me?access_token
28
	private static final String HOST_NAME = "content.googleapis.com";
29
	private static final String URI = "/plus/v1/people/me";
21277 ashik.ali 30
	private static final int PORT_NUMBER = 443;
31
	private final ObjectMapper objectMapper = new ObjectMapper();
32
 
21278 ashik.ali 33
	@Autowired
34
	SocialUserRepository socialUserRepository;
21277 ashik.ali 35
 
21278 ashik.ali 36
	@Autowired
37
	UserRepository userRepository;
38
 
21277 ashik.ali 39
	public Map<String, Object> process(Map<String, Object> map) throws ProfitMandiBusinessException{
40
		RestClient restClient = new RestClient(SchemeType.HTTPS, HOST_NAME, PORT_NUMBER);
41
		Map<String, String> params = new HashMap<>();
21278 ashik.ali 42
		params.put(ProfitMandiConstants.ACCESS_TOKEN, map.get(ProfitMandiConstants.TOKEN).toString());
21277 ashik.ali 43
		String responseString = restClient.get(URI, params);
44
		try {
21278 ashik.ali 45
			JsonNode rootNode = objectMapper.readTree(responseString);
21277 ashik.ali 46
			SocialUser socialUser = new SocialUser();
21278 ashik.ali 47
			if(rootNode.has("emails")){
48
				JsonNode emails = rootNode.get("emails");
49
				if(emails.isArray()){
50
					Iterator<JsonNode> emailsIterator = rootNode.elements();
51
					if(emailsIterator.hasNext()){
52
						JsonNode email = emailsIterator.next();
53
						if(email.has("value")){
54
							socialUser.setEmailId(email.get("value").asText());
55
						}
56
					}
57
				}
58
				socialUser.setEmailId(rootNode.get("email").asText());
21277 ashik.ali 59
			}
21278 ashik.ali 60
			if(rootNode.has("displayName")){
61
				socialUser.setName(rootNode.get("displayName").asText());
21277 ashik.ali 62
			}
21278 ashik.ali 63
			if(rootNode.has("gender")){
64
				String genderName = rootNode.get("gender").asText();
65
				switch(genderName){
66
					case "male":{
67
						socialUser.setGender(Gender.MALE);
68
						break;
69
					}case "female":{
70
						socialUser.setGender(Gender.FEMALE);
71
						break;
72
					}
73
				}
74
			}
21277 ashik.ali 75
			socialUser.setCreateTimestamp(LocalDateTime.now());
76
			socialUser.setType(SocialType.GOOGLE);
77
			socialUser.setUpdateTimestamp(LocalDateTime.now());
78
			socialUserRepository.persist(socialUser);
79
			Map<String, Object> responseMap = new HashMap<>(2);
80
			try{
81
				User user = userRepository.selectByEmailId(socialUser.getEmailId());
82
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId()));
83
				responseMap.put(ProfitMandiConstants.REGISTERED, true);
84
			}catch (ProfitMandiBusinessException pmbe) {
85
				responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(-1));
86
				responseMap.put(ProfitMandiConstants.REGISTERED, false);
87
			}
88
			return responseMap;
89
		}catch (JsonProcessingException jsonProcessingException) {
90
			//LOGGER.error("Json parse exception of "+json,jsonProcessingException);
91
			throw new ProfitMandiBusinessException("", "", "VE_1001");
92
		}catch (IOException ioException) {
93
			//LOGGER.error("IO Exception occurred while parsing json String");
94
			throw new ProfitMandiBusinessException("", "", "VE_1001");
95
		}
96
	}
97
}