Subversion Repositories SmartDukaan

Rev

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