Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
396 ashish 1
package in.shop2020.serving.controllers;
2
 
416 rajveer 3
import java.util.Date;
4
 
5
import javax.servlet.http.Cookie;
6
 
7
import org.apache.struts2.convention.annotation.Result;
8
import org.apache.struts2.convention.annotation.Results;
396 ashish 9
import org.apache.struts2.rest.DefaultHttpHeaders;
10
import org.apache.struts2.rest.HttpHeaders;
11
import org.apache.thrift.TException;
12
 
13
import in.shop2020.model.v1.user.AuthenticationException;
416 rajveer 14
import in.shop2020.model.v1.user.UserContextException;
396 ashish 15
import in.shop2020.model.v1.user.UserContextService.Client;
419 rajveer 16
import in.shop2020.serving.utils.Utils;
396 ashish 17
import in.shop2020.thrift.clients.UserContextServiceClient;
18
 
19
import com.opensymphony.xwork2.ModelDriven;
20
 
416 rajveer 21
@Results({
22
    @Result(name="success", type="redirectAction", params = {"actionName" , 
458 rajveer 23
    		"entity/1000111"})
416 rajveer 24
})
25
public class AuthController extends BaseController implements ModelDriven<Object>{
396 ashish 26
 
27
	private Auth auth = new Auth();
28
 
29
	private UserContextServiceClient client = null;
30
 
31
	private int errorCode = 0;
32
	private String errorMessage;
33
 
398 ashish 34
	private String id;
35
 
517 rajveer 36
	private String nameOfUser;
416 rajveer 37
 
517 rajveer 38
 
396 ashish 39
	public AuthController(){
40
		try {
416 rajveer 41
			client = new UserContextServiceClient();
396 ashish 42
		} catch (Exception e) {
43
			e.printStackTrace();
44
		}
45
	}
46
 
47
	@Override
48
	public Object getModel() {
399 rajveer 49
		return this.auth;
396 ashish 50
	}
51
 
419 rajveer 52
	public HttpHeaders show(){
53
		Client c = client.getClient();
54
 
55
		/*
56
		if(this.getId().equals("logout")){
57
			if(this.session.getAttribute("userid") != null 
58
					&& this.session.getAttribute("loggedin") != null
59
					&& this.session.getAttribute("loggedin").toString().equals("true")){
60
 
61
				long userId = Long.parseLong(this.session.getAttribute("userid").toString());
62
				try {
63
					c.setUserAsLoggedOut(userId, (new Date()).getTime());
64
				} catch (UserContextException e) {
65
					// TODO Auto-generated catch block
66
					e.printStackTrace();
67
				} catch (TException e) {
68
					// TODO Auto-generated catch block
69
					e.printStackTrace();
70
				}
71
 
72
				this.session.removeAttribute("userid");
73
				this.session.removeAttribute("loggedin");
74
				this.session.removeAttribute("issessionid");
75
				this.session.removeAttribute("email");
76
				return new DefaultHttpHeaders("lsuccess");
77
			}else{
78
				return new DefaultHttpHeaders("lfail");
79
			}
80
		}*/
81
 
82
		if(this.getId().equals("logout")){
83
			if(userinfo.isLoggedIn()){
84
				long userId = userinfo.getUserId();
85
 
86
				try {
87
					c.setUserAsLoggedOut(userId, (new Date()).getTime());
88
					this.session.removeAttribute("userinfo");
89
				} catch (UserContextException e) {
90
					// TODO Auto-generated catch block
91
					e.printStackTrace();
92
				} catch (TException e) {
93
					// TODO Auto-generated catch block
94
					e.printStackTrace();
95
				}
96
 
97
				return new DefaultHttpHeaders("lsuccess");
98
			}else{
99
				return new DefaultHttpHeaders("lfail");
100
			}
101
		}
102
 
103
 
104
		return new DefaultHttpHeaders("lfail");
105
 
106
	}
107
 
396 ashish 108
	public HttpHeaders create(){
398 ashish 109
		System.out.println(auth);
416 rajveer 110
		//return new DefaultHttpHeaders("lsuccess");
396 ashish 111
		//AUTH service to be invoked from here
416 rajveer 112
 
396 ashish 113
		Client c = client.getClient();
114
		try {
398 ashish 115
 
396 ashish 116
			if (c.authenticateUser(auth.getUserName(), auth.getPassword(), true)){
416 rajveer 117
				long userId = c.getContext(auth.getUserName(), auth.getPassword()).getId();
118
				c.setUserAsLoggedIn(userId, (new Date()).getTime());
119
 
419 rajveer 120
//				userinfo = (UserSessionInfo) this.session.getAttribute("userinfo");
416 rajveer 121
 
419 rajveer 122
				userinfo.setUserId(userId);
123
				userinfo.setLoggedIn(true);
424 rajveer 124
				userinfo.setEmail(auth.getUserName());
458 rajveer 125
				userinfo.setNameOfUser(Utils.getNameOfUser(userId));
517 rajveer 126
				this.nameOfUser = Utils.getNameOfUser(userId);
536 rajveer 127
				if(userinfo.getCartId() == -1){
128
					userinfo.setCartId(Utils.getCartId(userId));
129
					userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
130
				}else{
131
					Utils.mergeCarts(userinfo.getCartId(), Utils.getCartId(userId));
132
					userinfo.setCartId(Utils.getCartId(userId));
133
					userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
134
				}
419 rajveer 135
				this.session.setAttribute("userinfo", userinfo);
136
 
416 rajveer 137
				Cookie cookie1 = new Cookie("userid", userId+"");
138
		    	this.response.addCookie(cookie1);
396 ashish 139
				return new DefaultHttpHeaders("lsuccess");
140
			}
141
		} catch (AuthenticationException e) {
142
 
143
			errorCode = e.getErrorCode();
144
			errorMessage = e.getMessage();
145
 
146
		} catch (TException e) {
147
			// TODO Auto-generated catch block
148
			errorCode = -1;
149
			e.printStackTrace();
416 rajveer 150
		} catch (UserContextException e) {
151
			// TODO Auto-generated catch block
152
			errorCode = -1;
153
			e.printStackTrace();
396 ashish 154
		}
155
		return new DefaultHttpHeaders("lfail");
416 rajveer 156
 
396 ashish 157
	}
158
 
159
	public int getErrorCode() {
160
		return errorCode;
161
	}
162
 
163
	public String getErrorMessage() {
164
		return errorMessage;
165
	}
398 ashish 166
 
167
	public String getId(){
168
		return id;
169
	}
170
 
399 rajveer 171
	public void setId(String id){
398 ashish 172
		this.id = id;
173
	}
401 ashish 174
 
175
	public Auth getAuth() {
176
		return auth;
177
	}
178
 
179
	public void setAuth(Auth auth) {
180
		this.auth = auth;
181
	}
449 rajveer 182
 
183
	public String getUserName(){
517 rajveer 184
		return nameOfUser;
449 rajveer 185
	}
396 ashish 186
}