Subversion Repositories SmartDukaan

Rev

Rev 2867 | Rev 2935 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
545 rajveer 1
package in.shop2020.serving.services;
2
 
555 chandransh 3
import in.shop2020.model.v1.user.User;
4
import in.shop2020.model.v1.user.UserContextException;
2907 rajveer 5
import in.shop2020.serving.interceptors.UserInterceptor;
555 chandransh 6
import in.shop2020.thrift.clients.UserContextServiceClient;
545 rajveer 7
 
555 chandransh 8
import java.io.Serializable;
545 rajveer 9
 
555 chandransh 10
import org.apache.thrift.TException;
11
 
2907 rajveer 12
import com.google.gson.Gson;
13
 
555 chandransh 14
/**
15
 * This class is used to cache data that is frequently accessed for displaying
16
 * on the pages of the site. It implements Serializable since
17
 * <a href="http://tomcat.apache.org/tomcat-5.5-doc/config/manager.html#Restart%20Persistence">Tomcat's restart persistence</a>
18
 * ensures that all session attributes will be preserved across application
19
 * restarts.
20
 * 
21
 * @author Chandranshu
22
 * 
23
 */
24
public class UserSessionInfo implements Serializable{
25
	/**
26
	 * 
27
	 */
28
	private static final long serialVersionUID = 1L;
545 rajveer 29
	private boolean isLoggedIn;
30
	private long userId;
31
	private String email;
32
	private String nameOfUser;
33
	private int totalItems;
34
	private long cartId;
793 rajveer 35
	private String pincode;
545 rajveer 36
 
37
	public UserSessionInfo(){
38
		this.isLoggedIn = false;
39
		this.userId = -1;
40
		this.email = "";
41
		this.nameOfUser = "";
42
		this.totalItems = 0;
43
		this.cartId = -1;
793 rajveer 44
		this.pincode = "110001";
545 rajveer 45
	}
46
 
555 chandransh 47
	public UserSessionInfo(long userId, String jsessionId){
48
		this();
49
		UserContextServiceClient ucsc = null;
50
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
51
		User existingUser = null;
52
		try {
53
			ucsc = new UserContextServiceClient();
54
			userClient = ucsc.getClient();
55
			existingUser = userClient.getUserById(userId);
762 rajveer 56
			if(existingUser == null || existingUser.getUserId() == -1){
555 chandransh 57
				existingUser = userClient.createAnonymousUser(jsessionId);
762 rajveer 58
			}
59
			totalItems = userClient.getCart(existingUser.getActiveCartId()).getLinesSize();
801 rajveer 60
			pincode = userClient.getDefaultPincode(existingUser.getUserId());
555 chandransh 61
		} catch (UserContextException e) {
62
			e.printStackTrace();
63
		} catch (TException e) {
64
			e.printStackTrace();
65
		} catch (Exception e) {
66
			e.printStackTrace();
67
		}
762 rajveer 68
		/*
69
		finally {
70
			ucsc.closeConnection();
71
		}
72
		*/
793 rajveer 73
		initialize(existingUser, totalItems, pincode);
555 chandransh 74
	}
75
 
76
	public UserSessionInfo(String jsessionId) {
77
		this();
78
		UserContextServiceClient ucsc = null;
79
		try {
80
			ucsc = new UserContextServiceClient();
81
			in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
82
			User anonUser = userClient.createAnonymousUser(jsessionId);
762 rajveer 83
			int totalItems= userClient.getCart(anonUser.getActiveCartId()).getLinesSize();
801 rajveer 84
			pincode = userClient.getDefaultPincode(anonUser.getUserId());
793 rajveer 85
			initialize(anonUser, totalItems, pincode);
555 chandransh 86
		} catch (UserContextException e) {
87
			e.printStackTrace();
88
		} catch (TException e) {
89
			e.printStackTrace();
90
		} catch (Exception e) {
91
			e.printStackTrace();
92
		}
762 rajveer 93
		/*
94
		finally{
95
			ucsc.closeConnection();
96
		}
97
		*/
555 chandransh 98
	}
99
 
793 rajveer 100
	private void initialize(User user, int totalItems, String pincode){
555 chandransh 101
		if(user!=null){
102
			this.isLoggedIn = !user.isIsAnonymous();
103
			this.userId = user.getUserId();
104
			this.email = user.getEmail();
105
			this.nameOfUser = user.getName();
106
			this.cartId = user.getActiveCartId();
762 rajveer 107
			this.totalItems = totalItems;
793 rajveer 108
			this.pincode = pincode;
555 chandransh 109
		}else{
545 rajveer 110
			this.isLoggedIn = false;
111
			this.userId = -1;
112
			this.email = "";
113
			this.nameOfUser = "";
114
			this.totalItems = 0;
115
			this.cartId = -1;
793 rajveer 116
			this.pincode = pincode;
555 chandransh 117
		}			
545 rajveer 118
	}
555 chandransh 119
 
545 rajveer 120
	public boolean isSessionId() {
121
		return !isLoggedIn;
122
	}
123
 
124
	public boolean isLoggedIn() {
125
		return isLoggedIn;
126
	}
127
 
128
	public void setLoggedIn(boolean isLoggedIn) {
129
		this.isLoggedIn = isLoggedIn;
130
	}
131
 
132
	public long getUserId() {
133
		return userId;
134
	}
135
 
136
	public void setUserId(long userId) {
137
		this.userId = userId;
138
	}
139
 
140
	public String getEmail() {
141
		return email;
142
	}
143
 
144
	public void setEmail(String email) {
145
		this.email = email;
146
	}
147
 
148
	public String getNameOfUser() {
149
		return nameOfUser;
150
	}
151
 
152
	public void setNameOfUser(String nameOfUser) {
153
		this.nameOfUser = nameOfUser;
154
	}
155
 
156
	public int getTotalItems() {
157
		return totalItems;
158
	}
159
 
160
	public void setTotalItems(int totalItems) {
161
		this.totalItems = totalItems;
162
	}
163
	public long getCartId() {
164
		return cartId;
165
	}
166
 
167
	public void setCartId(long cartId) {
168
		this.cartId = cartId;
169
	}	
793 rajveer 170
 
171
	public String getPincode() {
172
		return pincode;
173
	}
174
 
175
	public void setPincode(String pincode) {
176
		this.pincode = pincode;
177
	}
178
 
2907 rajveer 179
	public static String getUserinfoCookieValueFromUserSessionInfo(UserSessionInfo userinfo){
180
		Gson gson = new Gson();
181
		return  UserInterceptor.desEncrypter.encrypt(gson.toJson(userinfo));
182
	}
183
 
184
	public static UserSessionInfo getUserSessionInfoFromCookieValue(String jsonString){
185
		Gson gson = new Gson();
186
		UserSessionInfo userinfo;
187
		try{
188
			userinfo = gson.fromJson(UserInterceptor.desEncrypter.decrypt(jsonString), UserSessionInfo.class);
189
		}catch (Exception e) {
190
			userinfo = new UserSessionInfo();
191
			//FIXME Remove the old cookies
192
		}
193
		return userinfo;
194
	}
195
 
2867 rajveer 196
	@Override
197
	public String toString() {
198
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId="
199
				+ userId + ", email=" + email + ", nameOfUser=" + nameOfUser
200
				+ ", totalItems=" + totalItems + ", cartId=" + cartId
201
				+ ", pincode=" + pincode + "]";
202
	}
793 rajveer 203
 
545 rajveer 204
}