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;
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;
41
	}
42
 
555 chandransh 43
	public UserSessionInfo(long userId, String jsessionId){
44
		this();
45
		UserContextServiceClient ucsc = null;
46
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
47
		User existingUser = null;
48
		try {
49
			ucsc = new UserContextServiceClient();
50
			userClient = ucsc.getClient();
51
			existingUser = userClient.getUserById(userId);
762 rajveer 52
			if(existingUser == null || existingUser.getUserId() == -1){
555 chandransh 53
				existingUser = userClient.createAnonymousUser(jsessionId);
762 rajveer 54
			}
55
			totalItems = userClient.getCart(existingUser.getActiveCartId()).getLinesSize();
555 chandransh 56
		} catch (UserContextException e) {
57
			e.printStackTrace();
58
		} catch (TException e) {
59
			e.printStackTrace();
60
		} catch (Exception e) {
61
			e.printStackTrace();
62
		}
762 rajveer 63
		/*
64
		finally {
65
			ucsc.closeConnection();
66
		}
67
		*/
68
		initialize(existingUser, totalItems);
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();
79
			initialize(anonUser, totalItems);
555 chandransh 80
		} catch (UserContextException e) {
81
			e.printStackTrace();
82
		} catch (TException e) {
83
			e.printStackTrace();
84
		} catch (Exception e) {
85
			e.printStackTrace();
86
		}
762 rajveer 87
		/*
88
		finally{
89
			ucsc.closeConnection();
90
		}
91
		*/
555 chandransh 92
	}
93
 
762 rajveer 94
	private void initialize(User user, int totalItems){
555 chandransh 95
		if(user!=null){
96
			this.isLoggedIn = !user.isIsAnonymous();
97
			this.userId = user.getUserId();
98
			this.email = user.getEmail();
99
			this.nameOfUser = user.getName();
100
			this.cartId = user.getActiveCartId();
762 rajveer 101
			this.totalItems = totalItems;
555 chandransh 102
		}else{
545 rajveer 103
			this.isLoggedIn = false;
104
			this.userId = -1;
105
			this.email = "";
106
			this.nameOfUser = "";
107
			this.totalItems = 0;
108
			this.cartId = -1;
555 chandransh 109
		}			
545 rajveer 110
	}
555 chandransh 111
 
545 rajveer 112
	public boolean isSessionId() {
113
		return !isLoggedIn;
114
	}
115
 
116
	public boolean isLoggedIn() {
117
		return isLoggedIn;
118
	}
119
 
120
	public void setLoggedIn(boolean isLoggedIn) {
121
		this.isLoggedIn = isLoggedIn;
122
	}
123
 
124
	public long getUserId() {
125
		return userId;
126
	}
127
 
128
	public void setUserId(long userId) {
129
		this.userId = userId;
130
	}
131
 
132
	public String getEmail() {
133
		return email;
134
	}
135
 
136
	public void setEmail(String email) {
137
		this.email = email;
138
	}
139
 
140
	public String getNameOfUser() {
141
		return nameOfUser;
142
	}
143
 
144
	public void setNameOfUser(String nameOfUser) {
145
		this.nameOfUser = nameOfUser;
146
	}
147
 
148
	public int getTotalItems() {
149
		return totalItems;
150
	}
151
 
152
	public void setTotalItems(int totalItems) {
153
		this.totalItems = totalItems;
154
	}
155
	public long getCartId() {
156
		return cartId;
157
	}
158
 
159
	public void setCartId(long cartId) {
160
		this.cartId = cartId;
161
	}	
162
 
555 chandransh 163
	public String toString(){
164
		StringBuffer representation = new StringBuffer();
165
		representation.append("userId: " + userId + "\n");
166
		representation.append("isLoggedIn: " + isLoggedIn + "\n");
167
		representation.append("username: " + nameOfUser + "\n");
168
		return representation.toString();
169
	}
545 rajveer 170
}