Subversion Repositories SmartDukaan

Rev

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