Subversion Repositories SmartDukaan

Rev

Rev 2935 | Rev 2973 | 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
 
555 chandransh 3
import in.shop2020.model.v1.user.User;
4
import in.shop2020.model.v1.user.UserContextException;
2907 rajveer 5
import in.shop2020.serving.interceptors.UserInterceptor;
555 chandransh 6
import in.shop2020.thrift.clients.UserContextServiceClient;
545 rajveer 7
 
555 chandransh 8
import java.io.Serializable;
545 rajveer 9
 
2935 chandransh 10
import org.apache.log4j.Logger;
555 chandransh 11
import org.apache.thrift.TException;
12
 
2907 rajveer 13
import com.google.gson.Gson;
14
 
555 chandransh 15
/**
16
 * This class is used to cache data that is frequently accessed for displaying
17
 * on the pages of the site. It implements Serializable since
18
 * <a href="http://tomcat.apache.org/tomcat-5.5-doc/config/manager.html#Restart%20Persistence">Tomcat's restart persistence</a>
19
 * ensures that all session attributes will be preserved across application
20
 * restarts.
21
 * 
22
 * @author Chandranshu
23
 * 
24
 */
2935 chandransh 25
@SuppressWarnings("serial")
555 chandransh 26
public class UserSessionInfo implements Serializable{
2935 chandransh 27
 
28
    private static Logger logger = Logger.getLogger(UserSessionInfo.class);
29
 
555 chandransh 30
	/**
31
	 * 
32
	 */
545 rajveer 33
	private boolean isLoggedIn;
34
	private long userId;
35
	private String email;
36
	private String nameOfUser;
37
	private int totalItems;
38
	private long cartId;
793 rajveer 39
	private String pincode;
545 rajveer 40
 
41
	public UserSessionInfo(){
42
		this.isLoggedIn = false;
43
		this.userId = -1;
44
		this.email = "";
45
		this.nameOfUser = "";
46
		this.totalItems = 0;
47
		this.cartId = -1;
793 rajveer 48
		this.pincode = "110001";
545 rajveer 49
	}
50
 
555 chandransh 51
	public UserSessionInfo(long userId, String jsessionId){
52
		this();
53
		try {
2935 chandransh 54
		    UserContextServiceClient ucsc = new UserContextServiceClient();
55
		    in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
56
		    User existingUser = userClient.getUserById(userId);
57
			if(existingUser != null && existingUser.getUserId() != -1){
58
    			totalItems = userClient.getCart(existingUser.getActiveCartId()).getLinesSize();
59
    			pincode = userClient.getDefaultPincode(existingUser.getUserId());
60
    			initialize(existingUser, totalItems, pincode);
762 rajveer 61
			}
555 chandransh 62
		} catch (UserContextException e) {
2935 chandransh 63
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 64
		} catch (TException e) {
2935 chandransh 65
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 66
		} catch (Exception e) {
2935 chandransh 67
		    logger.error("Unexpected exception: ", e);
68
		}	
555 chandransh 69
	}
70
 
71
	public UserSessionInfo(String jsessionId) {
72
		this();
73
		UserContextServiceClient ucsc = null;
74
		try {
75
			ucsc = new UserContextServiceClient();
76
			in.shop2020.model.v1.user.UserContextService.Client userClient = ucsc.getClient();
77
			User anonUser = userClient.createAnonymousUser(jsessionId);
762 rajveer 78
			int totalItems= userClient.getCart(anonUser.getActiveCartId()).getLinesSize();
801 rajveer 79
			pincode = userClient.getDefaultPincode(anonUser.getUserId());
793 rajveer 80
			initialize(anonUser, totalItems, pincode);
555 chandransh 81
		} catch (UserContextException e) {
2935 chandransh 82
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 83
		} catch (TException e) {
2935 chandransh 84
		    logger.error("Unable to get user info from user service: ", e);
555 chandransh 85
		} catch (Exception e) {
2935 chandransh 86
		    logger.error("Unexpected exception: ", e);
555 chandransh 87
		}
88
	}
89
 
793 rajveer 90
	private void initialize(User user, int totalItems, String pincode){
555 chandransh 91
		if(user!=null){
92
			this.isLoggedIn = !user.isIsAnonymous();
93
			this.userId = user.getUserId();
94
			this.email = user.getEmail();
95
			this.nameOfUser = user.getName();
96
			this.cartId = user.getActiveCartId();
762 rajveer 97
			this.totalItems = totalItems;
793 rajveer 98
			this.pincode = pincode;
555 chandransh 99
		}else{
545 rajveer 100
			this.isLoggedIn = false;
101
			this.userId = -1;
102
			this.email = "";
103
			this.nameOfUser = "";
104
			this.totalItems = 0;
105
			this.cartId = -1;
793 rajveer 106
			this.pincode = pincode;
555 chandransh 107
		}			
545 rajveer 108
	}
555 chandransh 109
 
545 rajveer 110
	public boolean isSessionId() {
111
		return !isLoggedIn;
112
	}
113
 
114
	public boolean isLoggedIn() {
115
		return isLoggedIn;
116
	}
117
 
118
	public void setLoggedIn(boolean isLoggedIn) {
119
		this.isLoggedIn = isLoggedIn;
120
	}
121
 
122
	public long getUserId() {
123
		return userId;
124
	}
125
 
126
	public void setUserId(long userId) {
127
		this.userId = userId;
128
	}
129
 
130
	public String getEmail() {
131
		return email;
132
	}
133
 
134
	public void setEmail(String email) {
135
		this.email = email;
136
	}
137
 
138
	public String getNameOfUser() {
139
		return nameOfUser;
140
	}
141
 
142
	public void setNameOfUser(String nameOfUser) {
143
		this.nameOfUser = nameOfUser;
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
 
2907 rajveer 169
	public static String getUserinfoCookieValueFromUserSessionInfo(UserSessionInfo userinfo){
170
		Gson gson = new Gson();
171
		return  UserInterceptor.desEncrypter.encrypt(gson.toJson(userinfo));
172
	}
173
 
174
	public static UserSessionInfo getUserSessionInfoFromCookieValue(String jsonString){
175
		Gson gson = new Gson();
176
		UserSessionInfo userinfo;
177
		try{
178
			userinfo = gson.fromJson(UserInterceptor.desEncrypter.decrypt(jsonString), UserSessionInfo.class);
179
		}catch (Exception e) {
2955 chandransh 180
		    logger.error("Unable to get the userinfo object from the JSON string because of", e);
2907 rajveer 181
			userinfo = new UserSessionInfo();
182
			//FIXME Remove the old cookies
183
		}
184
		return userinfo;
185
	}
186
 
2867 rajveer 187
	@Override
188
	public String toString() {
189
		return "UserSessionInfo [isLoggedIn=" + isLoggedIn + ", userId="
190
				+ userId + ", email=" + email + ", nameOfUser=" + nameOfUser
191
				+ ", totalItems=" + totalItems + ", cartId=" + cartId
192
				+ ", pincode=" + pincode + "]";
193
	}
793 rajveer 194
 
545 rajveer 195
}