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