Subversion Repositories SmartDukaan

Rev

Rev 11808 | 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;
11808 amit.gupta 42
	private boolean isPrivateDealUser;
545 rajveer 43
 
44
	public UserSessionInfo(){
45
		this.isLoggedIn = false;
46
		this.userId = -1;
47
		this.email = "";
48
		this.totalItems = 0;
49
		this.cartId = -1;
793 rajveer 50
		this.pincode = "110001";
3830 chandransh 51
		this.totalAmount = 0;
11808 amit.gupta 52
		this.isPrivateDealUser = false;
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.cartId = user.getActiveCartId();
762 rajveer 104
			this.totalItems = totalItems;
793 rajveer 105
			this.pincode = pincode;
3830 chandransh 106
			this.totalAmount = totalAmount;
555 chandransh 107
		}else{
545 rajveer 108
			this.isLoggedIn = false;
109
			this.userId = -1;
110
			this.email = "";
111
			this.totalItems = 0;
112
			this.cartId = -1;
793 rajveer 113
			this.pincode = pincode;
3830 chandransh 114
			this.totalAmount = 0;
555 chandransh 115
		}			
545 rajveer 116
	}
555 chandransh 117
 
545 rajveer 118
	public boolean isSessionId() {
119
		return !isLoggedIn;
120
	}
121
 
122
	public boolean isLoggedIn() {
123
		return isLoggedIn;
124
	}
125
 
126
	public void setLoggedIn(boolean isLoggedIn) {
127
		this.isLoggedIn = isLoggedIn;
128
	}
129
 
130
	public long getUserId() {
131
		return userId;
132
	}
133
 
134
	public void setUserId(long userId) {
135
		this.userId = userId;
136
	}
137
 
138
	public String getEmail() {
139
		return email;
140
	}
141
 
142
	public void setEmail(String email) {
143
		this.email = email;
144
	}
145
 
146
	public int getTotalItems() {
147
		return totalItems;
148
	}
149
 
150
	public void setTotalItems(int totalItems) {
151
		this.totalItems = totalItems;
152
	}
153
	public long getCartId() {
154
		return cartId;
155
	}
156
 
157
	public void setCartId(long cartId) {
158
		this.cartId = cartId;
159
	}	
793 rajveer 160
 
161
	public String getPincode() {
162
		return pincode;
163
	}
164
 
165
	public void setPincode(String pincode) {
166
		this.pincode = pincode;
167
	}
168
 
3830 chandransh 169
	public void setTotalAmount(double totalAmount) {
170
		this.totalAmount = totalAmount;
171
	}
172
 
173
	public double getTotalAmount() {
174
		return totalAmount;
175
	}
176
 
2907 rajveer 177
	public static String getUserinfoCookieValueFromUserSessionInfo(UserSessionInfo userinfo){
2998 rajveer 178
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
2907 rajveer 179
		Gson gson = new Gson();
2998 rajveer 180
		return desEncrypter.encrypt(gson.toJson(userinfo));
2907 rajveer 181
	}
182
 
183
	public static UserSessionInfo getUserSessionInfoFromCookieValue(String jsonString){
2998 rajveer 184
		DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
2907 rajveer 185
		Gson gson = new Gson();
2973 chandransh 186
		UserSessionInfo userinfo = null;
2907 rajveer 187
		try{
2998 rajveer 188
			userinfo = gson.fromJson(desEncrypter.decrypt(jsonString), UserSessionInfo.class);
3830 chandransh 189
			//As we are adding a new field in the cookie. So to populate this field for existing users
190
			if(userinfo.getTotalAmount() == 0 && userinfo.getTotalItems() != 0 ){
191
				try {
192
					UserClient ucsc = new UserClient();
193
					in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
194
					Cart cart = userClient.getCart(userinfo.getCartId());
195
					userinfo.setTotalItems(cart.getLinesSize());
196
					userinfo.setTotalAmount(cart.getTotalPrice());
197
				}catch (Exception e) {
198
					logger.error("Unable to update total amount in userinfo object from database.", e);		
199
				}
200
			}
2907 rajveer 201
		}catch (Exception e) {
2955 chandransh 202
		    logger.error("Unable to get the userinfo object from the JSON string because of", e);
2907 rajveer 203
		}
2973 chandransh 204
		if(userinfo==null){
205
		    userinfo = new UserSessionInfo();
206
		}
2907 rajveer 207
		return userinfo;
208
	}
209
 
2867 rajveer 210
	@Override
211
	public String toString() {
11990 amit.gupta 212
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId="
213
				+ userId + ", email=" + email + ", totalItems=" + totalItems
214
				+ ", cartId=" + cartId + ", pincode=" + pincode
215
				+ ", totalAmount=" + totalAmount + ", isPrivateDealUser="
216
				+ isPrivateDealUser + "]";
2867 rajveer 217
	}
793 rajveer 218
 
3830 chandransh 219
	public static void main(String[] args) {
220
		System.out.println();
221
		String cookieValue = "fG0CKt4DUD_D9iP1Ero0v2Io1AgVLoGqXDp0NWAPkzZuw3zHKot5owJK6IodZQfE2aS-obOK3BwXUNRirVHDyd-ycsyG4GfBPd0Ypl1MkxuVBmY4csB0FEg_IgWUm9GaGEzvtmmiZ5bE24XlpUPqR4AoTUAp8d92DDTG61FOFktDIGg3L0Tyk4qpVlAU3xQ3";
222
		UserSessionInfo uinfo = UserSessionInfo.getUserSessionInfoFromCookieValue(cookieValue);
223
		System.out.println(uinfo);
224
	}
11808 amit.gupta 225
 
226
	public void setPrivateDealUser(boolean isPrivateDealUser) {
227
		this.isPrivateDealUser = isPrivateDealUser;
228
	}
229
 
230
	public boolean isPrivateDealUser() {
231
		return isPrivateDealUser;
232
	}
4453 varun.gupt 233
}