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