Subversion Repositories SmartDukaan

Rev

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