Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8842 anupam.sin 1
package in.shop2020.serving.services;
2
 
3
import in.shop2020.model.v1.user.Cart;
4
import in.shop2020.model.v1.user.User;
5
import in.shop2020.model.v1.user.UserContextException;
6
import in.shop2020.serving.interceptors.UserInterceptor;
7
import in.shop2020.serving.utils.DesEncrypter;
8
import in.shop2020.thrift.clients.UserClient;
9
 
10
import java.io.Serializable;
11
 
12
import org.apache.log4j.Logger;
13
import org.apache.thrift.TException;
14
 
15
import com.google.gson.Gson;
16
 
17
/**
18
 * This class is used to cache data that is frequently accessed for displaying
19
 * on the pages of the site. It implements Serializable since
20
 * <a href="http://tomcat.apache.org/tomcat-5.5-doc/config/manager.html#Restart%20Persistence">Tomcat's restart persistence</a>
21
 * ensures that all session attributes will be preserved across application
22
 * restarts.
23
 * 
24
 * @author Chandranshu
25
 * 
26
 */
27
@SuppressWarnings("serial")
28
public class UserSessionInfo implements Serializable{
29
 
30
    private static Logger logger = Logger.getLogger(UserSessionInfo.class);
31
 
32
	/**
33
	 * 
34
	 */
35
	private boolean isLoggedIn;
36
	private long userId;
37
	private String email;
38
	private int totalItems;
39
	private long cartId;
40
	private String pincode;
41
	private double totalAmount;
42
 
43
	public UserSessionInfo(){
44
		this.isLoggedIn = false;
45
		this.userId = -1;
46
		this.email = "";
47
		this.totalItems = 0;
48
		this.cartId = -1;
49
		this.pincode = "110001";
50
		this.totalAmount = 0;
51
	}
52
 
53
	public UserSessionInfo(long userId, String jsessionId){
54
		this();
55
		try {
56
		    UserClient ucsc = new UserClient();
57
		    in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
58
		    User existingUser = userClient.getUserById(userId);
59
			if(existingUser != null && existingUser.getUserId() != -1){
60
				Cart cart = userClient.getCart(existingUser.getActiveCartId());
61
    			totalItems = cart.getLinesSize();
62
    			totalAmount = cart.getTotalPrice();
63
    			pincode = userClient.getDefaultPincode(existingUser.getUserId());
64
    			initialize(existingUser, totalItems, pincode, totalAmount);
65
			}
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
 
75
	public UserSessionInfo(String jsessionId) {
76
		this();
77
		UserClient ucsc = null;
78
		try {
79
			ucsc = new UserClient();
80
			in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
81
			User anonUser = userClient.createAnonymousUser(jsessionId);
82
			Cart cart = userClient.getCart(anonUser.getActiveCartId());
83
			int totalItems = cart.getLinesSize();
84
			double totalAmount = cart.getTotalPrice();
85
			pincode = userClient.getDefaultPincode(anonUser.getUserId());
86
			initialize(anonUser, totalItems, pincode, totalAmount);
87
		} catch (UserContextException e) {
88
		    logger.error("Unable to get user info from user service: ", e);
89
		} catch (TException e) {
90
		    logger.error("Unable to get user info from user service: ", e);
91
		} catch (Exception e) {
92
		    logger.error("Unexpected exception: ", e);
93
		}
94
	}
95
 
96
	private void initialize(User user, int totalItems, String pincode, double totalAmount){
97
		if(user!=null){
98
			this.isLoggedIn = !user.isIsAnonymous();
99
			this.userId = user.getUserId();
100
			this.email = user.getEmail();
101
			this.cartId = user.getActiveCartId();
102
			this.totalItems = totalItems;
103
			this.pincode = pincode;
104
			this.totalAmount = totalAmount;
105
		}else{
106
			this.isLoggedIn = false;
107
			this.userId = -1;
108
			this.email = "";
109
			this.totalItems = 0;
110
			this.cartId = -1;
111
			this.pincode = pincode;
112
			this.totalAmount = 0;
113
		}			
114
	}
115
 
116
	public boolean isSessionId() {
117
		return !isLoggedIn;
118
	}
119
 
120
	public boolean isLoggedIn() {
121
		return isLoggedIn;
122
	}
123
 
124
	public void setLoggedIn(boolean isLoggedIn) {
125
		this.isLoggedIn = isLoggedIn;
126
	}
127
 
128
	public long getUserId() {
129
		return userId;
130
	}
131
 
132
	public void setUserId(long userId) {
133
		this.userId = userId;
134
	}
135
 
136
	public String getEmail() {
137
		return email;
138
	}
139
 
140
	public void setEmail(String email) {
141
		this.email = email;
142
	}
143
 
144
	public int getTotalItems() {
145
		return totalItems;
146
	}
147
 
148
	public void setTotalItems(int totalItems) {
149
		this.totalItems = totalItems;
150
	}
151
	public long getCartId() {
152
		return cartId;
153
	}
154
 
155
	public void setCartId(long cartId) {
156
		this.cartId = cartId;
157
	}	
158
 
159
	public String getPincode() {
160
		return pincode;
161
	}
162
 
163
	public void setPincode(String pincode) {
164
		this.pincode = pincode;
165
	}
166
 
167
	public void setTotalAmount(double totalAmount) {
168
		this.totalAmount = totalAmount;
169
	}
170
 
171
	public double getTotalAmount() {
172
		return totalAmount;
173
	}
174
 
175
	public static String getUserinfoCookieValueFromUserSessionInfo(UserSessionInfo userinfo){
176
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
177
		Gson gson = new Gson();
178
		return desEncrypter.encrypt(gson.toJson(userinfo));
179
	}
180
 
181
	public static UserSessionInfo getUserSessionInfoFromCookieValue(String jsonString){
182
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
183
		Gson gson = new Gson();
184
		UserSessionInfo userinfo = null;
185
		try{
186
			userinfo = gson.fromJson(desEncrypter.decrypt(jsonString), UserSessionInfo.class);
187
			//As we are adding a new field in the cookie. So to populate this field for existing users
188
			if(userinfo.getTotalAmount() == 0 && userinfo.getTotalItems() != 0 ){
189
				try {
190
					UserClient ucsc = new UserClient();
191
					in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
192
					Cart cart = userClient.getCart(userinfo.getCartId());
193
					userinfo.setTotalItems(cart.getLinesSize());
194
					userinfo.setTotalAmount(cart.getTotalPrice());
195
				}catch (Exception e) {
196
					logger.error("Unable to update total amount in userinfo object from database.", e);		
197
				}
198
			}
199
		}catch (Exception e) {
200
		    logger.error("Unable to get the userinfo object from the JSON string because of", e);
201
		}
202
		if(userinfo==null){
203
		    userinfo = new UserSessionInfo();
204
		}
205
		return userinfo;
206
	}
207
 
208
	@Override
209
	public String toString() {
210
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId=" + userId + ", email=" + email
211
				+ ", totalItems=" + totalItems + ", cartId=" + cartId
212
				+ ", pincode=" + pincode + ", totalAmount=" + totalAmount + "]";
213
	}
214
 
215
	public static void main(String[] args) {
216
		System.out.println();
217
		String cookieValue = "fG0CKt4DUD_D9iP1Ero0v2Io1AgVLoGqXDp0NWAPkzZuw3zHKot5owJK6IodZQfE2aS-obOK3BwXUNRirVHDyd-ycsyG4GfBPd0Ypl1MkxuVBmY4csB0FEg_IgWUm9GaGEzvtmmiZ5bE24XlpUPqR4AoTUAp8d92DDTG61FOFktDIGg3L0Tyk4qpVlAU3xQ3";
218
		UserSessionInfo uinfo = UserSessionInfo.getUserSessionInfoFromCookieValue(cookieValue);
219
		System.out.println(uinfo);
220
	}
221
}