Subversion Repositories SmartDukaan

Rev

Rev 3126 | Rev 4453 | 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 String nameOfUser;
39
	private int totalItems;
40
	private long cartId;
793 rajveer 41
	private String pincode;
3830 chandransh 42
	private double totalAmount;
545 rajveer 43
 
44
	public UserSessionInfo(){
45
		this.isLoggedIn = false;
46
		this.userId = -1;
47
		this.email = "";
48
		this.nameOfUser = "";
49
		this.totalItems = 0;
50
		this.cartId = -1;
793 rajveer 51
		this.pincode = "110001";
3830 chandransh 52
		this.totalAmount = 0;
545 rajveer 53
	}
54
 
555 chandransh 55
	public UserSessionInfo(long userId, String jsessionId){
56
		this();
57
		try {
3126 rajveer 58
		    UserClient ucsc = new UserClient();
2935 chandransh 59
		    in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
60
		    User existingUser = userClient.getUserById(userId);
61
			if(existingUser != null && existingUser.getUserId() != -1){
3830 chandransh 62
				Cart cart = userClient.getCart(existingUser.getActiveCartId());
63
    			totalItems = cart.getLinesSize();
64
    			totalAmount = cart.getTotalPrice();
2935 chandransh 65
    			pincode = userClient.getDefaultPincode(existingUser.getUserId());
3830 chandransh 66
    			initialize(existingUser, totalItems, pincode, totalAmount);
762 rajveer 67
			}
555 chandransh 68
		} catch (UserContextException e) {
2935 chandransh 69
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 70
		} catch (TException e) {
2935 chandransh 71
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 72
		} catch (Exception e) {
2935 chandransh 73
		    logger.error("Unexpected exception: ", e);
74
		}	
555 chandransh 75
	}
76
 
77
	public UserSessionInfo(String jsessionId) {
78
		this();
3126 rajveer 79
		UserClient ucsc = null;
555 chandransh 80
		try {
3126 rajveer 81
			ucsc = new UserClient();
555 chandransh 82
			in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
83
			User anonUser = userClient.createAnonymousUser(jsessionId);
3830 chandransh 84
			Cart cart = userClient.getCart(anonUser.getActiveCartId());
85
			int totalItems = cart.getLinesSize();
86
			double totalAmount = cart.getTotalPrice();
801 rajveer 87
			pincode = userClient.getDefaultPincode(anonUser.getUserId());
3830 chandransh 88
			initialize(anonUser, totalItems, pincode, totalAmount);
555 chandransh 89
		} catch (UserContextException e) {
2935 chandransh 90
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 91
		} catch (TException e) {
2935 chandransh 92
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 93
		} catch (Exception e) {
2935 chandransh 94
		    logger.error("Unexpected exception: ", e);
555 chandransh 95
		}
96
	}
97
 
3830 chandransh 98
	private void initialize(User user, int totalItems, String pincode, double totalAmount){
555 chandransh 99
		if(user!=null){
100
			this.isLoggedIn = !user.isIsAnonymous();
101
			this.userId = user.getUserId();
102
			this.email = user.getEmail();
103
			this.nameOfUser = user.getName();
104
			this.cartId = user.getActiveCartId();
762 rajveer 105
			this.totalItems = totalItems;
793 rajveer 106
			this.pincode = pincode;
3830 chandransh 107
			this.totalAmount = totalAmount;
555 chandransh 108
		}else{
545 rajveer 109
			this.isLoggedIn = false;
110
			this.userId = -1;
111
			this.email = "";
112
			this.nameOfUser = "";
113
			this.totalItems = 0;
114
			this.cartId = -1;
793 rajveer 115
			this.pincode = pincode;
3830 chandransh 116
			this.totalAmount = 0;
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
 
3830 chandransh 179
	public void setTotalAmount(double totalAmount) {
180
		this.totalAmount = totalAmount;
181
	}
182
 
183
	public double getTotalAmount() {
184
		return totalAmount;
185
	}
186
 
2907 rajveer 187
	public static String getUserinfoCookieValueFromUserSessionInfo(UserSessionInfo userinfo){
2998 rajveer 188
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
2907 rajveer 189
		Gson gson = new Gson();
2998 rajveer 190
		return desEncrypter.encrypt(gson.toJson(userinfo));
2907 rajveer 191
	}
192
 
193
	public static UserSessionInfo getUserSessionInfoFromCookieValue(String jsonString){
2998 rajveer 194
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
2907 rajveer 195
		Gson gson = new Gson();
2973 chandransh 196
		UserSessionInfo userinfo = null;
2907 rajveer 197
		try{
2998 rajveer 198
			userinfo = gson.fromJson(desEncrypter.decrypt(jsonString), UserSessionInfo.class);
3830 chandransh 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
			}
2907 rajveer 211
		}catch (Exception e) {
2955 chandransh 212
		    logger.error("Unable to get the userinfo object from the JSON string because of", e);
2907 rajveer 213
		}
2973 chandransh 214
		if(userinfo==null){
215
		    userinfo = new UserSessionInfo();
216
		}
2907 rajveer 217
		return userinfo;
218
	}
219
 
2867 rajveer 220
	@Override
221
	public String toString() {
222
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId="
223
				+ userId + ", email=" + email + ", nameOfUser=" + nameOfUser
224
				+ ", totalItems=" + totalItems + ", cartId=" + cartId
3830 chandransh 225
				+ ", pincode=" + pincode + ", totalAmount=" + totalAmount + "]";
2867 rajveer 226
	}
793 rajveer 227
 
3830 chandransh 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);
233
	}
234
 
235
 
545 rajveer 236
}