Subversion Repositories SmartDukaan

Rev

Rev 9570 | Rev 12143 | Go to most recent revision | 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;
28
 
29
	public UserSessionInfo(){
30
		this.isLoggedIn = false;
31
		this.userId = -1;
32
		this.email = "";
33
		this.pincode = "110001";
34
	}
35
 
9570 anupam.sin 36
	public UserSessionInfo(long userId){
9103 anupam.sin 37
		this();
38
		try {
39
		    UserClient ucsc = new UserClient();
40
		    in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
41
		    User existingUser = userClient.getUserById(userId);
42
			if(existingUser != null && existingUser.getUserId() != -1){
43
    			pincode = userClient.getDefaultPincode(existingUser.getUserId());
9570 anupam.sin 44
    			initialize(existingUser, pincode);
9103 anupam.sin 45
			}
46
		} catch (UserContextException e) {
47
		    logger.error("Unable to get user info from user service: ", e);
48
		} catch (TException e) {
49
		    logger.error("Unable to get user info from user service: ", e);
50
		} catch (Exception e) {
51
		    logger.error("Unexpected exception: ", e);
52
		}	
53
	}
54
 
55
	public UserSessionInfo(String jsessionId) {
56
		this();
57
		UserClient ucsc = null;
58
		try {
59
			ucsc = new UserClient();
60
			in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
61
			User anonUser = userClient.createAnonymousUser(jsessionId);
62
			pincode = userClient.getDefaultPincode(anonUser.getUserId());
9570 anupam.sin 63
			initialize(anonUser, pincode);
9103 anupam.sin 64
		} catch (UserContextException e) {
65
		    logger.error("Unable to get user info from user service: ", e);
66
		} catch (TException e) {
67
		    logger.error("Unable to get user info from user service: ", e);
68
		} catch (Exception e) {
69
		    logger.error("Unexpected exception: ", e);
70
		}
71
	}
72
 
9570 anupam.sin 73
	private void initialize(User user, String pincode){
9103 anupam.sin 74
		if(user!=null){
75
			this.isLoggedIn = !user.isIsAnonymous();
76
			this.userId = user.getUserId();
77
			this.email = user.getEmail();
78
			this.pincode = pincode;
79
		}else{
80
			this.isLoggedIn = false;
81
			this.userId = -1;
82
			this.email = "";
83
			this.pincode = pincode;
84
		}			
85
	}
86
 
87
	public boolean isSessionId() {
88
		return !isLoggedIn;
89
	}
90
 
91
	public boolean isLoggedIn() {
92
		return isLoggedIn;
93
	}
94
 
95
	public void setLoggedIn(boolean isLoggedIn) {
96
		this.isLoggedIn = isLoggedIn;
97
	}
98
 
99
	public long getUserId() {
100
		return userId;
101
	}
102
 
103
	public void setUserId(long userId) {
104
		this.userId = userId;
105
	}
106
 
107
	public String getEmail() {
108
		return email;
109
	}
110
 
111
	public void setEmail(String email) {
112
		this.email = email;
113
	}
114
 
115
	public String getPincode() {
116
		return pincode;
117
	}
118
 
119
	public void setPincode(String pincode) {
120
		this.pincode = pincode;
121
	}
122
 
123
	@Override
124
	public String toString() {
125
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId=" + userId + ", email=" + email
9570 anupam.sin 126
				+ ", pincode=" + pincode + "]";
9103 anupam.sin 127
	}
128
 
129
	public static void main(String[] args) {
130
		System.out.println();
131
		String cookieValue = "fG0CKt4DUD_D9iP1Ero0v2Io1AgVLoGqXDp0NWAPkzZuw3zHKot5owJK6IodZQfE2aS-obOK3BwXUNRirVHDyd-ycsyG4GfBPd0Ypl1MkxuVBmY4csB0FEg_IgWUm9GaGEzvtmmiZ5bE24XlpUPqR4AoTUAp8d92DDTG61FOFktDIGg3L0Tyk4qpVlAU3xQ3";
9577 anupam.sin 132
//		UserSessionInfo uinfo = UserSessionInfo.getUserSessionInfoFromCookieValue(cookieValue);
133
//		System.out.println(uinfo);
9103 anupam.sin 134
	}
135
}