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