Subversion Repositories SmartDukaan

Rev

Rev 3830 | Rev 11808 | 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
 
3830 chandransh 3
import in.shop2020.model.v1.user.Cart;
555 chandransh 4
import in.shop2020.model.v1.user.User;
5
import in.shop2020.model.v1.user.UserContextException;
2907 rajveer 6
import in.shop2020.serving.interceptors.UserInterceptor;
2998 rajveer 7
import in.shop2020.serving.utils.DesEncrypter;
3126 rajveer 8
import in.shop2020.thrift.clients.UserClient;
545 rajveer 9
 
555 chandransh 10
import java.io.Serializable;
545 rajveer 11
 
2935 chandransh 12
import org.apache.log4j.Logger;
555 chandransh 13
import org.apache.thrift.TException;
14
 
2907 rajveer 15
import com.google.gson.Gson;
16
 
555 chandransh 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
 */
2935 chandransh 27
@SuppressWarnings("serial")
555 chandransh 28
public class UserSessionInfo implements Serializable{
2935 chandransh 29
 
30
    private static Logger logger = Logger.getLogger(UserSessionInfo.class);
31
 
555 chandransh 32
	/**
33
	 * 
34
	 */
545 rajveer 35
	private boolean isLoggedIn;
36
	private long userId;
37
	private String email;
38
	private int totalItems;
39
	private long cartId;
793 rajveer 40
	private String pincode;
3830 chandransh 41
	private double totalAmount;
545 rajveer 42
 
43
	public UserSessionInfo(){
44
		this.isLoggedIn = false;
45
		this.userId = -1;
46
		this.email = "";
47
		this.totalItems = 0;
48
		this.cartId = -1;
793 rajveer 49
		this.pincode = "110001";
3830 chandransh 50
		this.totalAmount = 0;
545 rajveer 51
	}
52
 
555 chandransh 53
	public UserSessionInfo(long userId, String jsessionId){
54
		this();
55
		try {
3126 rajveer 56
		    UserClient ucsc = new UserClient();
2935 chandransh 57
		    in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
58
		    User existingUser = userClient.getUserById(userId);
59
			if(existingUser != null && existingUser.getUserId() != -1){
3830 chandransh 60
				Cart cart = userClient.getCart(existingUser.getActiveCartId());
61
    			totalItems = cart.getLinesSize();
62
    			totalAmount = cart.getTotalPrice();
2935 chandransh 63
    			pincode = userClient.getDefaultPincode(existingUser.getUserId());
3830 chandransh 64
    			initialize(existingUser, totalItems, pincode, totalAmount);
762 rajveer 65
			}
555 chandransh 66
		} catch (UserContextException e) {
2935 chandransh 67
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 68
		} catch (TException e) {
2935 chandransh 69
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 70
		} catch (Exception e) {
2935 chandransh 71
		    logger.error("Unexpected exception: ", e);
72
		}	
555 chandransh 73
	}
74
 
75
	public UserSessionInfo(String jsessionId) {
76
		this();
3126 rajveer 77
		UserClient ucsc = null;
555 chandransh 78
		try {
3126 rajveer 79
			ucsc = new UserClient();
555 chandransh 80
			in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
81
			User anonUser = userClient.createAnonymousUser(jsessionId);
3830 chandransh 82
			Cart cart = userClient.getCart(anonUser.getActiveCartId());
83
			int totalItems = cart.getLinesSize();
84
			double totalAmount = cart.getTotalPrice();
801 rajveer 85
			pincode = userClient.getDefaultPincode(anonUser.getUserId());
3830 chandransh 86
			initialize(anonUser, totalItems, pincode, totalAmount);
555 chandransh 87
		} catch (UserContextException e) {
2935 chandransh 88
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 89
		} catch (TException e) {
2935 chandransh 90
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 91
		} catch (Exception e) {
2935 chandransh 92
		    logger.error("Unexpected exception: ", e);
555 chandransh 93
		}
94
	}
95
 
3830 chandransh 96
	private void initialize(User user, int totalItems, String pincode, double totalAmount){
555 chandransh 97
		if(user!=null){
98
			this.isLoggedIn = !user.isIsAnonymous();
99
			this.userId = user.getUserId();
100
			this.email = user.getEmail();
101
			this.cartId = user.getActiveCartId();
762 rajveer 102
			this.totalItems = totalItems;
793 rajveer 103
			this.pincode = pincode;
3830 chandransh 104
			this.totalAmount = totalAmount;
555 chandransh 105
		}else{
545 rajveer 106
			this.isLoggedIn = false;
107
			this.userId = -1;
108
			this.email = "";
109
			this.totalItems = 0;
110
			this.cartId = -1;
793 rajveer 111
			this.pincode = pincode;
3830 chandransh 112
			this.totalAmount = 0;
555 chandransh 113
		}			
545 rajveer 114
	}
555 chandransh 115
 
545 rajveer 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
	}	
793 rajveer 158
 
159
	public String getPincode() {
160
		return pincode;
161
	}
162
 
163
	public void setPincode(String pincode) {
164
		this.pincode = pincode;
165
	}
166
 
3830 chandransh 167
	public void setTotalAmount(double totalAmount) {
168
		this.totalAmount = totalAmount;
169
	}
170
 
171
	public double getTotalAmount() {
172
		return totalAmount;
173
	}
174
 
2907 rajveer 175
	public static String getUserinfoCookieValueFromUserSessionInfo(UserSessionInfo userinfo){
2998 rajveer 176
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
2907 rajveer 177
		Gson gson = new Gson();
2998 rajveer 178
		return desEncrypter.encrypt(gson.toJson(userinfo));
2907 rajveer 179
	}
180
 
181
	public static UserSessionInfo getUserSessionInfoFromCookieValue(String jsonString){
2998 rajveer 182
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
2907 rajveer 183
		Gson gson = new Gson();
2973 chandransh 184
		UserSessionInfo userinfo = null;
2907 rajveer 185
		try{
2998 rajveer 186
			userinfo = gson.fromJson(desEncrypter.decrypt(jsonString), UserSessionInfo.class);
3830 chandransh 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
			}
2907 rajveer 199
		}catch (Exception e) {
2955 chandransh 200
		    logger.error("Unable to get the userinfo object from the JSON string because of", e);
2907 rajveer 201
		}
2973 chandransh 202
		if(userinfo==null){
203
		    userinfo = new UserSessionInfo();
204
		}
2907 rajveer 205
		return userinfo;
206
	}
207
 
2867 rajveer 208
	@Override
209
	public String toString() {
4453 varun.gupt 210
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId=" + userId + ", email=" + email
2867 rajveer 211
				+ ", totalItems=" + totalItems + ", cartId=" + cartId
3830 chandransh 212
				+ ", pincode=" + pincode + ", totalAmount=" + totalAmount + "]";
2867 rajveer 213
	}
793 rajveer 214
 
3830 chandransh 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
	}
4453 varun.gupt 221
}