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