Subversion Repositories SmartDukaan

Rev

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