Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
637 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
import in.shop2020.model.v1.user.User;
7
import in.shop2020.thrift.clients.UserContextServiceClient;
8
 
9
import java.io.IOException;
10
import java.util.Date;
11
 
12
import org.apache.juli.logging.Log;
13
import org.apache.juli.logging.LogFactory;
650 rajveer 14
import org.apache.struts2.convention.annotation.InterceptorRef;
15
import org.apache.struts2.convention.annotation.InterceptorRefs;
637 rajveer 16
import org.apache.struts2.convention.annotation.Result;
17
import org.apache.struts2.convention.annotation.Results;
18
 
19
/**
20
 * 
21
 * @author rajveer
781 vikas 22
 * 
637 rajveer 23
 */
24
 
25
@Results({
781 vikas 26
		@Result(name = "failure", type = "redirectAction", params = {
27
				"actionName", "login" }),
28
		@Result(name = "redirect", location = "${url}", type = "redirect") })
29
public class LoginController extends BaseController {
650 rajveer 30
 
781 vikas 31
	/**
32
	 * 
33
	 */
34
	private static final long serialVersionUID = 5390035354379263121L;
650 rajveer 35
 
637 rajveer 36
	private static Log log = LogFactory.getLog(LoginController.class);
781 vikas 37
 
650 rajveer 38
	private String redirectUrl;
781 vikas 39
 
40
	public LoginController() {
637 rajveer 41
		super();
42
	}
43
 
781 vikas 44
	public String index() throws SecurityException, IOException {
45
		htmlSnippets.put("LOGIN_HEADER", pageLoader.getLoginHeaderHtml());
650 rajveer 46
		return "index";
781 vikas 47
	}
637 rajveer 48
 
781 vikas 49
	public String create() throws SecurityException, Exception {
50
		if (loginUser()) {
51
			redirectUrl = (String) this.session.getAttribute("REDIRECT_URL");
52
			if (redirectUrl == null) {
53
				redirectUrl = "";
54
			}
55
			log.debug(redirectUrl);
56
			resetRedirectUrl();
57
			return "redirect";
58
		} else {
59
			addActionError("Either email or password is wrong.");
60
			return "failure";
61
		}
62
	}
63
 
64
	private boolean loginUser() {
65
		try {
66
			String email, password;
67
 
68
			email = this.request.getParameter("email");
69
			password = this.request.getParameter("password");
70
 
71
			if (email == null || password == null) {
72
				return false;
73
			}
74
			log.debug(email);
75
			log.debug(password);
76
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
77
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient
78
					.getClient();
637 rajveer 79
			User user = userClient.authenticateUser(email, password);
781 vikas 80
			userClient.setUserAsLoggedIn(user.getUserId(),
81
					(new Date()).getTime());
82
 
637 rajveer 83
			userinfo.setUserId(user.getUserId());
84
			userinfo.setNameOfUser(user.getName());
85
			userinfo.setEmail(email);
86
			userinfo.setLoggedIn(true);
781 vikas 87
 
88
			// TODO: setTotalItems shouldn't be a method on userinfo. This
89
			// allows
90
			// for potentially updating the item count wrongly. The method
91
			// setCartId
637 rajveer 92
			// should update the item count as well. Also, there can be a method
781 vikas 93
			// called refreshItemCount() that automatically updates the number
94
			// of
637 rajveer 95
			// items currently in the cart.
96
			userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
97
			userinfo.setCartId(user.getActiveCartId());
781 vikas 98
			int totalItems = userClient.getCart(user.getActiveCartId())
99
					.getLinesSize();
762 rajveer 100
			userinfo.setTotalItems(totalItems);
101
 
637 rajveer 102
			return true;
781 vikas 103
		} catch (Exception e) {
104
			log.error("Wrong username or password.");
105
			return false;
106
		}
107
	}
637 rajveer 108
 
781 vikas 109
	String getUrl() {
110
		return this.redirectUrl;
111
	}
112
 
113
	public String getLoginHeaderSnippet() {
637 rajveer 114
		return htmlSnippets.get("LOGIN_HEADER");
115
	}
116
 
117
}