Subversion Repositories SmartDukaan

Rev

Rev 9103 | Rev 9577 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9103 Rev 9570
Line 1... Line 1...
1
package in.shop2020.serving.services;
1
package in.shop2020.mobileapi.serving.services;
2
 
2
 
3
import in.shop2020.model.v1.user.Cart;
-
 
4
import in.shop2020.model.v1.user.User;
3
import in.shop2020.model.v1.user.User;
5
import in.shop2020.model.v1.user.UserContextException;
4
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;
5
import in.shop2020.thrift.clients.UserClient;
9
 
6
 
10
import java.io.Serializable;
7
import java.io.Serializable;
11
 
8
 
12
import org.apache.log4j.Logger;
9
import org.apache.log4j.Logger;
13
import org.apache.thrift.TException;
10
import org.apache.thrift.TException;
14
 
11
 
15
import com.google.gson.Gson;
-
 
16
 
-
 
17
/**
12
/**
18
 * This class is used to cache data that is frequently accessed for displaying
13
 * This class is used to cache data about the user.
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
 * 
14
 * 
26
 */
15
 */
27
@SuppressWarnings("serial")
16
@SuppressWarnings("serial")
28
public class UserSessionInfo implements Serializable{
17
public class UserSessionInfo implements Serializable{
29
    
18
    
Line 33... Line 22...
33
	 * 
22
	 * 
34
	 */
23
	 */
35
	private boolean isLoggedIn;
24
	private boolean isLoggedIn;
36
	private long userId;
25
	private long userId;
37
	private String email;
26
	private String email;
38
	private int totalItems;
-
 
39
	private long cartId;
-
 
40
	private String pincode;
27
	private String pincode;
41
	private double totalAmount;
-
 
42
	
28
	
43
	public UserSessionInfo(){
29
	public UserSessionInfo(){
44
		this.isLoggedIn = false;
30
		this.isLoggedIn = false;
45
		this.userId = -1;
31
		this.userId = -1;
46
		this.email = "";
32
		this.email = "";
47
		this.totalItems = 0;
-
 
48
		this.cartId = -1;
-
 
49
		this.pincode = "110001";
33
		this.pincode = "110001";
50
		this.totalAmount = 0;
-
 
51
	}
34
	}
52
 
35
 
53
	public UserSessionInfo(long userId, String jsessionId){
36
	public UserSessionInfo(long userId){
54
		this();
37
		this();
55
		try {
38
		try {
56
		    UserClient ucsc = new UserClient();
39
		    UserClient ucsc = new UserClient();
57
		    in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
40
		    in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
58
		    User existingUser = userClient.getUserById(userId);
41
		    User existingUser = userClient.getUserById(userId);
59
			if(existingUser != null && existingUser.getUserId() != -1){
42
			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());
43
    			pincode = userClient.getDefaultPincode(existingUser.getUserId());
64
    			initialize(existingUser, totalItems, pincode, totalAmount);
44
    			initialize(existingUser, pincode);
65
			}
45
			}
66
		} catch (UserContextException e) {
46
		} catch (UserContextException e) {
67
		    logger.error("Unable to get user info from user service: ", e);
47
		    logger.error("Unable to get user info from user service: ", e);
68
		} catch (TException e) {
48
		} catch (TException e) {
69
		    logger.error("Unable to get user info from user service: ", e);
49
		    logger.error("Unable to get user info from user service: ", e);
Line 77... Line 57...
77
		UserClient ucsc = null;
57
		UserClient ucsc = null;
78
		try {
58
		try {
79
			ucsc = new UserClient();
59
			ucsc = new UserClient();
80
			in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
60
			in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
81
			User anonUser = userClient.createAnonymousUser(jsessionId);
61
			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());
62
			pincode = userClient.getDefaultPincode(anonUser.getUserId());
86
			initialize(anonUser, totalItems, pincode, totalAmount);
63
			initialize(anonUser, pincode);
87
		} catch (UserContextException e) {
64
		} catch (UserContextException e) {
88
		    logger.error("Unable to get user info from user service: ", e);
65
		    logger.error("Unable to get user info from user service: ", e);
89
		} catch (TException e) {
66
		} catch (TException e) {
90
		    logger.error("Unable to get user info from user service: ", e);
67
		    logger.error("Unable to get user info from user service: ", e);
91
		} catch (Exception e) {
68
		} catch (Exception e) {
92
		    logger.error("Unexpected exception: ", e);
69
		    logger.error("Unexpected exception: ", e);
93
		}
70
		}
94
	}
71
	}
95
 
72
 
96
	private void initialize(User user, int totalItems, String pincode, double totalAmount){
73
	private void initialize(User user, String pincode){
97
		if(user!=null){
74
		if(user!=null){
98
			this.isLoggedIn = !user.isIsAnonymous();
75
			this.isLoggedIn = !user.isIsAnonymous();
99
			this.userId = user.getUserId();
76
			this.userId = user.getUserId();
100
			this.email = user.getEmail();
77
			this.email = user.getEmail();
101
			this.cartId = user.getActiveCartId();
-
 
102
			this.totalItems = totalItems;
-
 
103
			this.pincode = pincode;
78
			this.pincode = pincode;
104
			this.totalAmount = totalAmount;
-
 
105
		}else{
79
		}else{
106
			this.isLoggedIn = false;
80
			this.isLoggedIn = false;
107
			this.userId = -1;
81
			this.userId = -1;
108
			this.email = "";
82
			this.email = "";
109
			this.totalItems = 0;
-
 
110
			this.cartId = -1;
-
 
111
			this.pincode = pincode;
83
			this.pincode = pincode;
112
			this.totalAmount = 0;
-
 
113
		}			
84
		}			
114
	}
85
	}
115
	
86
	
116
	public boolean isSessionId() {
87
	public boolean isSessionId() {
117
		return !isLoggedIn;
88
		return !isLoggedIn;
Line 139... Line 110...
139
 
110
 
140
	public void setEmail(String email) {
111
	public void setEmail(String email) {
141
		this.email = email;
112
		this.email = email;
142
	}
113
	}
143
	
114
	
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() {
115
	public String getPincode() {
160
		return pincode;
116
		return pincode;
161
	}
117
	}
162
 
118
 
163
	public void setPincode(String pincode) {
119
	public void setPincode(String pincode) {
164
		this.pincode = pincode;
120
		this.pincode = pincode;
165
	}
121
	}
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
	
122
	
208
	@Override
123
	@Override
209
	public String toString() {
124
	public String toString() {
210
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId=" + userId + ", email=" + email
125
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId=" + userId + ", email=" + email
211
				+ ", totalItems=" + totalItems + ", cartId=" + cartId
-
 
212
				+ ", pincode=" + pincode + ", totalAmount=" + totalAmount + "]";
126
				+ ", pincode=" + pincode + "]";
213
	}
127
	}
214
 
128
 
215
	public static void main(String[] args) {
129
	public static void main(String[] args) {
216
		System.out.println();
130
		System.out.println();
217
		String cookieValue = "fG0CKt4DUD_D9iP1Ero0v2Io1AgVLoGqXDp0NWAPkzZuw3zHKot5owJK6IodZQfE2aS-obOK3BwXUNRirVHDyd-ycsyG4GfBPd0Ypl1MkxuVBmY4csB0FEg_IgWUm9GaGEzvtmmiZ5bE24XlpUPqR4AoTUAp8d92DDTG61FOFktDIGg3L0Tyk4qpVlAU3xQ3";
131
		String cookieValue = "fG0CKt4DUD_D9iP1Ero0v2Io1AgVLoGqXDp0NWAPkzZuw3zHKot5owJK6IodZQfE2aS-obOK3BwXUNRirVHDyd-ycsyG4GfBPd0Ypl1MkxuVBmY4csB0FEg_IgWUm9GaGEzvtmmiZ5bE24XlpUPqR4AoTUAp8d92DDTG61FOFktDIGg3L0Tyk4qpVlAU3xQ3";