Subversion Repositories SmartDukaan

Rev

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