Subversion Repositories SmartDukaan

Rev

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