Subversion Repositories SmartDukaan

Rev

Rev 3185 | Rev 4222 | 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
 
2263 vikas 6
import in.shop2020.datalogger.EventType;
3830 chandransh 7
import in.shop2020.model.v1.user.Cart;
637 rajveer 8
import in.shop2020.model.v1.user.User;
815 rajveer 9
import in.shop2020.serving.utils.DesEncrypter;
1175 varun.gupt 10
import in.shop2020.serving.utils.UserMessage;
3126 rajveer 11
import in.shop2020.thrift.clients.UserClient;
2511 vikas 12
import in.shop2020.utils.DataLogger;
637 rajveer 13
 
14
import java.io.IOException;
15
import java.util.Date;
1623 rajveer 16
import java.util.List;
637 rajveer 17
 
832 rajveer 18
import org.apache.log4j.Logger;
637 rajveer 19
import org.apache.struts2.convention.annotation.Result;
925 rajveer 20
import org.apache.struts2.convention.annotation.Results;
637 rajveer 21
 
22
/**
23
 * 
24
 * @author rajveer
781 vikas 25
 * 
637 rajveer 26
 */
925 rajveer 27
@Results({
28
	@Result(name="success", type="redirectAction", params = {"actionName" , "home"}),
29
	@Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
30
})
637 rajveer 31
 
781 vikas 32
public class LoginController extends BaseController {
650 rajveer 33
 
781 vikas 34
	/**
35
	 * 
36
	 */
37
	private static final long serialVersionUID = 5390035354379263121L;
650 rajveer 38
 
832 rajveer 39
	private static Logger log = Logger.getLogger(Class.class);
815 rajveer 40
	private DesEncrypter desEncrypter = new DesEncrypter("saholic");
41
 
2933 vikas 42
	private String redirectUrl = "/";
781 vikas 43
 
44
	public LoginController() {
637 rajveer 45
		super();
46
	}
47
 
781 vikas 48
	public String index() throws SecurityException, IOException {
925 rajveer 49
		if(userinfo.isLoggedIn()){
50
    		return "success";
51
    	}
650 rajveer 52
		return "index";
781 vikas 53
	}
637 rajveer 54
 
781 vikas 55
	public String create() throws SecurityException, Exception {
56
		if (loginUser()) {
2959 chandransh 57
			log.info("Will redirect the user to:" + redirectUrl);
2637 vikas 58
            return "redirect";
781 vikas 59
		} else {
1175 varun.gupt 60
			addActionError(UserMessage.USER_AUTHENTICATION_FAILURE);
3185 vikas 61
            DataLogger.logData(EventType.LOGIN_FAILED, getSessionId(), userinfo.getUserId(), this.request.getParameter("email"));
830 vikas 62
			return "login";
781 vikas 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
 
1776 varun.gupt 77
			String encryptedPassword = desEncrypter.encrypt(password);
815 rajveer 78
 
3126 rajveer 79
			UserClient userContextServiceClient = new UserClient();
1747 varun.gupt 80
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.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());
2637 vikas 84
 
1175 varun.gupt 85
			// TODO: setTotalItems shouldn't be a method on userinfo. This allows
86
			// for potentially updating the item count wrongly. The method setCartId
637 rajveer 87
			// should update the item count as well. Also, there can be a method
1175 varun.gupt 88
			// called refreshItemCount() that automatically updates the number of
637 rajveer 89
			// items currently in the cart.
1625 rajveer 90
			if(userinfo.getUserId() != -1){
1623 rajveer 91
				userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
1625 rajveer 92
 
2982 rajveer 93
				List<Long> items = userClient.getBrowseHistoryItems(userinfo.getUserId());
94
				if(items != null){
95
					for(Long itemId: items){
96
						userClient.updateBrowseHistory(user.getUserId(), itemId);
1623 rajveer 97
					}
98
				}
2982 rajveer 99
 
100
				items = userClient.getMyResearchItems(userinfo.getUserId());
101
				if(items != null){
102
					for(Long itemId: items){
103
						userClient.updateMyResearch(user.getUserId(), itemId);
1625 rajveer 104
					}
105
				}
1623 rajveer 106
			}
107
 
108
 
1625 rajveer 109
			userinfo.setUserId(user.getUserId());
110
			userinfo.setNameOfUser(user.getName());
111
			userinfo.setEmail(email);
112
			userinfo.setLoggedIn(true);
113
			userinfo.setPincode(pincode);
637 rajveer 114
			userinfo.setCartId(user.getActiveCartId());
3830 chandransh 115
			Cart cart = userClient.getCart(user.getActiveCartId());
116
			userinfo.setTotalItems(cart.getLinesSize());
117
			userinfo.setTotalAmount(cart.getTotalPrice());
2959 chandransh 118
			log.info(userinfo);
2996 vikas 119
			String src = user.getSource();
120
			if (src == null) {
121
			    src = "";
122
			}
3185 vikas 123
			DataLogger.logData(EventType.LOGIN_SUCCESS, getSessionId(), userinfo.getUserId(),
2996 vikas 124
                    email, src);
2637 vikas 125
    		return true;
781 vikas 126
		} catch (Exception e) {
2959 chandransh 127
			log.error(UserMessage.USER_AUTHENTICATION_FAILURE, e);
781 vikas 128
			return false;
129
		}
130
	}
637 rajveer 131
 
924 vikas 132
	public String getRedirectUrl() {
133
		return redirectUrl;
781 vikas 134
	}
135
 
924 vikas 136
	public void setRedirectUrl(String redirectUrl) {
137
		this.redirectUrl = redirectUrl;
138
	}
637 rajveer 139
}