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