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