Subversion Repositories SmartDukaan

Rev

Rev 12143 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9570 anupam.sin 1
package in.shop2020.mobileapi.serving.services;
9103 anupam.sin 2
 
3
import in.shop2020.model.v1.user.User;
4
import in.shop2020.model.v1.user.UserContextException;
5
import in.shop2020.thrift.clients.UserClient;
6
 
7
import java.io.Serializable;
8
 
9
import org.apache.log4j.Logger;
10
import org.apache.thrift.TException;
11
 
12
/**
9570 anupam.sin 13
 * This class is used to cache data about the user.
9103 anupam.sin 14
 * 
15
 */
16
@SuppressWarnings("serial")
17
public class UserSessionInfo implements Serializable{
18
 
19
    private static Logger logger = Logger.getLogger(UserSessionInfo.class);
20
 
21
	/**
22
	 * 
23
	 */
24
	private boolean isLoggedIn;
25
	private long userId;
26
	private String email;
27
	private String pincode;
12143 amit.gupta 28
	private boolean isPrivateDealUser;
9103 anupam.sin 29
 
30
	public UserSessionInfo(){
31
		this.isLoggedIn = false;
12143 amit.gupta 32
		this.setPrivateDealUser(false);
9103 anupam.sin 33
		this.userId = -1;
34
		this.email = "";
35
		this.pincode = "110001";
36
	}
37
 
9570 anupam.sin 38
	public UserSessionInfo(long userId){
9103 anupam.sin 39
		this();
40
		try {
41
		    UserClient ucsc = new UserClient();
42
		    in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
43
		    User existingUser = userClient.getUserById(userId);
44
			if(existingUser != null && existingUser.getUserId() != -1){
45
    			pincode = userClient.getDefaultPincode(existingUser.getUserId());
9570 anupam.sin 46
    			initialize(existingUser, pincode);
9103 anupam.sin 47
			}
48
		} catch (UserContextException e) {
49
		    logger.error("Unable to get user info from user service: ", e);
50
		} catch (TException e) {
51
		    logger.error("Unable to get user info from user service: ", e);
52
		} catch (Exception e) {
53
		    logger.error("Unexpected exception: ", e);
54
		}	
55
	}
56
 
57
	public UserSessionInfo(String jsessionId) {
58
		this();
59
		UserClient ucsc = null;
60
		try {
61
			ucsc = new UserClient();
62
			in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
63
			User anonUser = userClient.createAnonymousUser(jsessionId);
64
			pincode = userClient.getDefaultPincode(anonUser.getUserId());
9570 anupam.sin 65
			initialize(anonUser, pincode);
9103 anupam.sin 66
		} catch (UserContextException e) {
67
		    logger.error("Unable to get user info from user service: ", e);
68
		} catch (TException e) {
69
		    logger.error("Unable to get user info from user service: ", e);
70
		} catch (Exception e) {
71
		    logger.error("Unexpected exception: ", e);
72
		}
73
	}
74
 
9570 anupam.sin 75
	private void initialize(User user, String pincode){
9103 anupam.sin 76
		if(user!=null){
77
			this.isLoggedIn = !user.isIsAnonymous();
78
			this.userId = user.getUserId();
79
			this.email = user.getEmail();
80
			this.pincode = pincode;
81
		}else{
82
			this.isLoggedIn = false;
83
			this.userId = -1;
84
			this.email = "";
85
			this.pincode = pincode;
86
		}			
87
	}
88
 
89
	public boolean isSessionId() {
90
		return !isLoggedIn;
91
	}
92
 
93
	public boolean isLoggedIn() {
94
		return isLoggedIn;
95
	}
96
 
97
	public void setLoggedIn(boolean isLoggedIn) {
98
		this.isLoggedIn = isLoggedIn;
99
	}
100
 
101
	public long getUserId() {
102
		return userId;
103
	}
104
 
105
	public void setUserId(long userId) {
106
		this.userId = userId;
107
	}
108
 
109
	public String getEmail() {
110
		return email;
111
	}
112
 
113
	public void setEmail(String email) {
114
		this.email = email;
115
	}
116
 
117
	public String getPincode() {
118
		return pincode;
119
	}
120
 
121
	public void setPincode(String pincode) {
122
		this.pincode = pincode;
123
	}
124
 
125
	@Override
126
	public String toString() {
13160 amit.gupta 127
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId="
128
				+ userId + ", email=" + email + ", pincode=" + pincode
129
				+ ", isPrivateDealUser=" + isPrivateDealUser + "]";
9103 anupam.sin 130
	}
131
 
132
	public static void main(String[] args) {
133
		System.out.println();
134
		String cookieValue = "fG0CKt4DUD_D9iP1Ero0v2Io1AgVLoGqXDp0NWAPkzZuw3zHKot5owJK6IodZQfE2aS-obOK3BwXUNRirVHDyd-ycsyG4GfBPd0Ypl1MkxuVBmY4csB0FEg_IgWUm9GaGEzvtmmiZ5bE24XlpUPqR4AoTUAp8d92DDTG61FOFktDIGg3L0Tyk4qpVlAU3xQ3";
9577 anupam.sin 135
//		UserSessionInfo uinfo = UserSessionInfo.getUserSessionInfoFromCookieValue(cookieValue);
136
//		System.out.println(uinfo);
9103 anupam.sin 137
	}
12143 amit.gupta 138
 
139
	public void setPrivateDealUser(boolean isPrivateDealUser) {
140
		this.isPrivateDealUser = isPrivateDealUser;
141
	}
142
 
143
	public boolean isPrivateDealUser() {
144
		return isPrivateDealUser;
145
	}
9103 anupam.sin 146
}