Subversion Repositories SmartDukaan

Rev

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

Rev 3126 Rev 3830
Line 1... Line 1...
1
package in.shop2020.serving.services;
1
package in.shop2020.serving.services;
2
 
2
 
-
 
3
import in.shop2020.model.v1.user.Cart;
3
import in.shop2020.model.v1.user.User;
4
import in.shop2020.model.v1.user.User;
4
import in.shop2020.model.v1.user.UserContextException;
5
import in.shop2020.model.v1.user.UserContextException;
5
import in.shop2020.serving.interceptors.UserInterceptor;
6
import in.shop2020.serving.interceptors.UserInterceptor;
6
import in.shop2020.serving.utils.DesEncrypter;
7
import in.shop2020.serving.utils.DesEncrypter;
7
import in.shop2020.thrift.clients.UserClient;
8
import in.shop2020.thrift.clients.UserClient;
Line 36... Line 37...
36
	private String email;
37
	private String email;
37
	private String nameOfUser;
38
	private String nameOfUser;
38
	private int totalItems;
39
	private int totalItems;
39
	private long cartId;
40
	private long cartId;
40
	private String pincode;
41
	private String pincode;
-
 
42
	private double totalAmount;
41
	
43
	
42
	public UserSessionInfo(){
44
	public UserSessionInfo(){
43
		this.isLoggedIn = false;
45
		this.isLoggedIn = false;
44
		this.userId = -1;
46
		this.userId = -1;
45
		this.email = "";
47
		this.email = "";
46
		this.nameOfUser = "";
48
		this.nameOfUser = "";
47
		this.totalItems = 0;
49
		this.totalItems = 0;
48
		this.cartId = -1;
50
		this.cartId = -1;
49
		this.pincode = "110001";
51
		this.pincode = "110001";
-
 
52
		this.totalAmount = 0;
50
	}
53
	}
51
 
54
 
52
	public UserSessionInfo(long userId, String jsessionId){
55
	public UserSessionInfo(long userId, String jsessionId){
53
		this();
56
		this();
54
		try {
57
		try {
55
		    UserClient ucsc = new UserClient();
58
		    UserClient ucsc = new UserClient();
56
		    in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
59
		    in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
57
		    User existingUser = userClient.getUserById(userId);
60
		    User existingUser = userClient.getUserById(userId);
58
			if(existingUser != null && existingUser.getUserId() != -1){
61
			if(existingUser != null && existingUser.getUserId() != -1){
59
    			totalItems = userClient.getCart(existingUser.getActiveCartId()).getLinesSize();
62
				Cart cart = userClient.getCart(existingUser.getActiveCartId());
-
 
63
    			totalItems = cart.getLinesSize();
-
 
64
    			totalAmount = cart.getTotalPrice();
60
    			pincode = userClient.getDefaultPincode(existingUser.getUserId());
65
    			pincode = userClient.getDefaultPincode(existingUser.getUserId());
61
    			initialize(existingUser, totalItems, pincode);
66
    			initialize(existingUser, totalItems, pincode, totalAmount);
62
			}
67
			}
63
		} catch (UserContextException e) {
68
		} catch (UserContextException e) {
64
		    logger.error("Unable to get user info from user service: ", e);
69
		    logger.error("Unable to get user info from user service: ", e);
65
		} catch (TException e) {
70
		} catch (TException e) {
66
		    logger.error("Unable to get user info from user service: ", e);
71
		    logger.error("Unable to get user info from user service: ", e);
Line 74... Line 79...
74
		UserClient ucsc = null;
79
		UserClient ucsc = null;
75
		try {
80
		try {
76
			ucsc = new UserClient();
81
			ucsc = new UserClient();
77
			in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
82
			in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
78
			User anonUser = userClient.createAnonymousUser(jsessionId);
83
			User anonUser = userClient.createAnonymousUser(jsessionId);
79
			int totalItems= userClient.getCart(anonUser.getActiveCartId()).getLinesSize();
84
			Cart cart = userClient.getCart(anonUser.getActiveCartId());
-
 
85
			int totalItems = cart.getLinesSize();
-
 
86
			double totalAmount = cart.getTotalPrice();
80
			pincode = userClient.getDefaultPincode(anonUser.getUserId());
87
			pincode = userClient.getDefaultPincode(anonUser.getUserId());
81
			initialize(anonUser, totalItems, pincode);
88
			initialize(anonUser, totalItems, pincode, totalAmount);
82
		} catch (UserContextException e) {
89
		} catch (UserContextException e) {
83
		    logger.error("Unable to get user info from user service: ", e);
90
		    logger.error("Unable to get user info from user service: ", e);
84
		} catch (TException e) {
91
		} catch (TException e) {
85
		    logger.error("Unable to get user info from user service: ", e);
92
		    logger.error("Unable to get user info from user service: ", e);
86
		} catch (Exception e) {
93
		} catch (Exception e) {
87
		    logger.error("Unexpected exception: ", e);
94
		    logger.error("Unexpected exception: ", e);
88
		}
95
		}
89
	}
96
	}
90
 
97
 
91
	private void initialize(User user, int totalItems, String pincode){
98
	private void initialize(User user, int totalItems, String pincode, double totalAmount){
92
		if(user!=null){
99
		if(user!=null){
93
			this.isLoggedIn = !user.isIsAnonymous();
100
			this.isLoggedIn = !user.isIsAnonymous();
94
			this.userId = user.getUserId();
101
			this.userId = user.getUserId();
95
			this.email = user.getEmail();
102
			this.email = user.getEmail();
96
			this.nameOfUser = user.getName();
103
			this.nameOfUser = user.getName();
97
			this.cartId = user.getActiveCartId();
104
			this.cartId = user.getActiveCartId();
98
			this.totalItems = totalItems;
105
			this.totalItems = totalItems;
99
			this.pincode = pincode;
106
			this.pincode = pincode;
-
 
107
			this.totalAmount = totalAmount;
100
		}else{
108
		}else{
101
			this.isLoggedIn = false;
109
			this.isLoggedIn = false;
102
			this.userId = -1;
110
			this.userId = -1;
103
			this.email = "";
111
			this.email = "";
104
			this.nameOfUser = "";
112
			this.nameOfUser = "";
105
			this.totalItems = 0;
113
			this.totalItems = 0;
106
			this.cartId = -1;
114
			this.cartId = -1;
107
			this.pincode = pincode;
115
			this.pincode = pincode;
-
 
116
			this.totalAmount = 0;
108
		}			
117
		}			
109
	}
118
	}
110
	
119
	
111
	public boolean isSessionId() {
120
	public boolean isSessionId() {
112
		return !isLoggedIn;
121
		return !isLoggedIn;
Line 165... Line 174...
165
 
174
 
166
	public void setPincode(String pincode) {
175
	public void setPincode(String pincode) {
167
		this.pincode = pincode;
176
		this.pincode = pincode;
168
	}
177
	}
169
 
178
 
-
 
179
	public void setTotalAmount(double totalAmount) {
-
 
180
		this.totalAmount = totalAmount;
-
 
181
	}
-
 
182
 
-
 
183
	public double getTotalAmount() {
-
 
184
		return totalAmount;
-
 
185
	}
-
 
186
	
170
	public static String getUserinfoCookieValueFromUserSessionInfo(UserSessionInfo userinfo){
187
	public static String getUserinfoCookieValueFromUserSessionInfo(UserSessionInfo userinfo){
171
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
188
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
172
		Gson gson = new Gson();
189
		Gson gson = new Gson();
173
		return desEncrypter.encrypt(gson.toJson(userinfo));
190
		return desEncrypter.encrypt(gson.toJson(userinfo));
174
	}
191
	}
Line 177... Line 194...
177
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
194
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
178
		Gson gson = new Gson();
195
		Gson gson = new Gson();
179
		UserSessionInfo userinfo = null;
196
		UserSessionInfo userinfo = null;
180
		try{
197
		try{
181
			userinfo = gson.fromJson(desEncrypter.decrypt(jsonString), UserSessionInfo.class);
198
			userinfo = gson.fromJson(desEncrypter.decrypt(jsonString), UserSessionInfo.class);
-
 
199
			//As we are adding a new field in the cookie. So to populate this field for existing users
-
 
200
			if(userinfo.getTotalAmount() == 0 && userinfo.getTotalItems() != 0 ){
-
 
201
				try {
-
 
202
					UserClient ucsc = new UserClient();
-
 
203
					in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
-
 
204
					Cart cart = userClient.getCart(userinfo.getCartId());
-
 
205
					userinfo.setTotalItems(cart.getLinesSize());
-
 
206
					userinfo.setTotalAmount(cart.getTotalPrice());
-
 
207
				}catch (Exception e) {
-
 
208
					logger.error("Unable to update total amount in userinfo object from database.", e);		
-
 
209
				}
-
 
210
			}
182
		}catch (Exception e) {
211
		}catch (Exception e) {
183
		    logger.error("Unable to get the userinfo object from the JSON string because of", e);
212
		    logger.error("Unable to get the userinfo object from the JSON string because of", e);
184
		}
213
		}
185
		if(userinfo==null){
214
		if(userinfo==null){
186
		    userinfo = new UserSessionInfo();
215
		    userinfo = new UserSessionInfo();
Line 191... Line 220...
191
	@Override
220
	@Override
192
	public String toString() {
221
	public String toString() {
193
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId="
222
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId="
194
				+ userId + ", email=" + email + ", nameOfUser=" + nameOfUser
223
				+ userId + ", email=" + email + ", nameOfUser=" + nameOfUser
195
				+ ", totalItems=" + totalItems + ", cartId=" + cartId
224
				+ ", totalItems=" + totalItems + ", cartId=" + cartId
196
				+ ", pincode=" + pincode + "]";
225
				+ ", pincode=" + pincode + ", totalAmount=" + totalAmount + "]";
-
 
226
	}
-
 
227
 
-
 
228
	public static void main(String[] args) {
-
 
229
		System.out.println();
-
 
230
		String cookieValue = "fG0CKt4DUD_D9iP1Ero0v2Io1AgVLoGqXDp0NWAPkzZuw3zHKot5owJK6IodZQfE2aS-obOK3BwXUNRirVHDyd-ycsyG4GfBPd0Ypl1MkxuVBmY4csB0FEg_IgWUm9GaGEzvtmmiZ5bE24XlpUPqR4AoTUAp8d92DDTG61FOFktDIGg3L0Tyk4qpVlAU3xQ3";
-
 
231
		UserSessionInfo uinfo = UserSessionInfo.getUserSessionInfoFromCookieValue(cookieValue);
-
 
232
		System.out.println(uinfo);
197
	}
233
	}
-
 
234
	
198
 
235
 
199
}
236
}