Subversion Repositories SmartDukaan

Rev

Rev 545 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 545 Rev 555
Line 1... Line 1...
1
package in.shop2020.serving.services;
1
package in.shop2020.serving.services;
2
 
2
 
-
 
3
import in.shop2020.model.v1.user.User;
-
 
4
import in.shop2020.model.v1.user.UserContextException;
3
import in.shop2020.serving.utils.Utils;
5
import in.shop2020.serving.utils.Utils;
-
 
6
import in.shop2020.thrift.clients.UserContextServiceClient;
4
 
7
 
5
import java.util.Date;
8
import java.io.Serializable;
6
 
9
 
-
 
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
 */
7
public class UserSessionInfo {
22
public class UserSessionInfo implements Serializable{
-
 
23
	/**
-
 
24
	 * 
-
 
25
	 */
-
 
26
	private static final long serialVersionUID = 1L;
8
	private boolean isLoggedIn;
27
	private boolean isLoggedIn;
9
	private long userId;
28
	private long userId;
10
	private long sessionId;
-
 
11
	private String email;
29
	private String email;
12
	private String nameOfUser;
30
	private String nameOfUser;
13
	private int totalItems;
31
	private int totalItems;
14
	private long cartId;
32
	private long cartId;
15
	
33
	
16
	public UserSessionInfo(){
34
	public UserSessionInfo(){
17
		this.isLoggedIn = false;
35
		this.isLoggedIn = false;
18
		this.userId = -1;
36
		this.userId = -1;
19
		this.sessionId = (new Date()).getTime();
-
 
20
		this.email = "";
37
		this.email = "";
21
		this.nameOfUser = "";
38
		this.nameOfUser = "";
22
		this.totalItems = 0;
39
		this.totalItems = 0;
23
		this.cartId = -1;
40
		this.cartId = -1;
24
	}
41
	}
25
 
42
 
26
	public UserSessionInfo(long userId, boolean isSessionId){
43
	public UserSessionInfo(long userId, String jsessionId){
27
		if(!isSessionId){
44
		this();
-
 
45
		UserContextServiceClient ucsc = null;
-
 
46
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
28
			this.isLoggedIn = true;
47
		User existingUser = null;
-
 
48
		try {
29
			this.userId = userId;
49
			ucsc = new UserContextServiceClient();
30
			this.sessionId = (new Date()).getTime();
50
			userClient = ucsc.getClient();
31
			this.email = Utils.getEmailId(userId);
51
			existingUser = userClient.getUserById(userId);
-
 
52
			if(existingUser == null)
32
			this.nameOfUser = Utils.getNameOfUser(userId);
53
				existingUser = userClient.createAnonymousUser(jsessionId);
-
 
54
		} catch (UserContextException e) {
33
			//this.totalItems = 0;
55
			e.printStackTrace();
-
 
56
		} catch (TException e) {
34
			//this.cartId = -1;
57
			e.printStackTrace();
35
			this.cartId = Utils.getCartId(userId);
58
		} catch (Exception e) {
36
			this.totalItems = Utils.getNumberOfItemsInCart(this.cartId);
59
			e.printStackTrace();
37
			
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();
38
		}
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();
-
 
89
			this.totalItems = Utils.getNumberOfItemsInCart(this.cartId);
39
		else{
90
		}else{
40
			this.isLoggedIn = false;
91
			this.isLoggedIn = false;
41
			this.userId = -1;
92
			this.userId = -1;
42
			this.sessionId = userId;
-
 
43
			this.email = "";
93
			this.email = "";
44
			this.nameOfUser = "";
94
			this.nameOfUser = "";
45
			this.totalItems = 0;
95
			this.totalItems = 0;
46
			this.cartId = -1;
96
			this.cartId = -1;
47
		}
97
		}			
48
	}
98
	}
49
 
99
	
50
	public boolean isSessionId() {
100
	public boolean isSessionId() {
51
		return !isLoggedIn;
101
		return !isLoggedIn;
52
	}
102
	}
53
 
103
 
54
	public boolean isLoggedIn() {
104
	public boolean isLoggedIn() {
Line 65... Line 115...
65
 
115
 
66
	public void setUserId(long userId) {
116
	public void setUserId(long userId) {
67
		this.userId = userId;
117
		this.userId = userId;
68
	}
118
	}
69
 
119
 
70
	public long getSessionId() {
-
 
71
		return sessionId;
-
 
72
	}
-
 
73
 
-
 
74
	public void setSessionId(long sessionId) {
-
 
75
		this.sessionId = sessionId;
-
 
76
	}
-
 
77
 
-
 
78
	public String getEmail() {
120
	public String getEmail() {
79
		return email;
121
		return email;
80
	}
122
	}
81
 
123
 
82
	public void setEmail(String email) {
124
	public void setEmail(String email) {
Line 104... Line 146...
104
 
146
 
105
	public void setCartId(long cartId) {
147
	public void setCartId(long cartId) {
106
		this.cartId = cartId;
148
		this.cartId = cartId;
107
	}	
149
	}	
108
	
150
	
-
 
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
	}
109
}
158
}