Subversion Repositories SmartDukaan

Rev

Rev 2263 | Rev 2511 | 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;
1957 vikas 10
import in.shop2020.serving.utils.DataLogger;
815 rajveer 11
import in.shop2020.serving.utils.DesEncrypter;
1175 varun.gupt 12
import in.shop2020.serving.utils.UserMessage;
637 rajveer 13
import in.shop2020.thrift.clients.UserContextServiceClient;
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
 
924 vikas 43
	private String redirectUrl = null;
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()) {
831 vikas 58
			log.info(redirectUrl);
2419 vikas 59
            DataLogger.logData(EventType.LOGIN_SUCCESS, session.getId(), userinfo.getUserId(),
2157 vikas 60
                            this.request.getParameter("email"));
781 vikas 61
			return "redirect";
62
		} else {
1175 varun.gupt 63
			addActionError(UserMessage.USER_AUTHENTICATION_FAILURE);
2419 vikas 64
            DataLogger.logData(EventType.LOGIN_FAILED, session.getId(), userinfo.getUserId(), this.request.getParameter("email"));
830 vikas 65
			return "login";
781 vikas 66
		}
67
	}
68
 
69
	private boolean loginUser() {
70
		try {
71
			String email, password;
72
 
73
			email = this.request.getParameter("email");
74
			password = this.request.getParameter("password");
75
 
76
			if (email == null || password == null) {
77
				return false;
78
			}
815 rajveer 79
 
1776 varun.gupt 80
			String encryptedPassword = desEncrypter.encrypt(password);
815 rajveer 81
 
781 vikas 82
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
1747 varun.gupt 83
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
815 rajveer 84
			User user = userClient.authenticateUser(email, encryptedPassword);
793 rajveer 85
			userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
86
			String pincode = userClient.getDefaultPincode(user.getUserId());
781 vikas 87
 
1175 varun.gupt 88
			// TODO: setTotalItems shouldn't be a method on userinfo. This allows
89
			// for potentially updating the item count wrongly. The method setCartId
637 rajveer 90
			// should update the item count as well. Also, there can be a method
1175 varun.gupt 91
			// called refreshItemCount() that automatically updates the number of
637 rajveer 92
			// items currently in the cart.
1625 rajveer 93
			if(userinfo.getUserId() != -1){
1623 rajveer 94
				userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
1625 rajveer 95
 
96
				Widget browseHistory = userClient.getBrowseHistory(userinfo.getUserId());
1623 rajveer 97
				if(browseHistory != null){
98
					List<WidgetItem> items =  browseHistory.getItems();
99
					if(items != null){
100
						for(WidgetItem item: items){
101
							userClient.updateBrowseHistory(user.getUserId(), item.getItem_id());
102
						}
103
					}
104
				}
1625 rajveer 105
 
106
				Widget myResearch = userClient.getMyResearch(userinfo.getUserId());
107
				if(myResearch != null){
108
					List<WidgetItem> items =  myResearch.getItems();
109
					if(items != null){
110
						for(WidgetItem item: items){
111
							userClient.updateMyResearch(user.getUserId(), item.getItem_id());
112
						}
113
					}
114
				}
1623 rajveer 115
			}
116
 
117
 
1625 rajveer 118
			userinfo.setUserId(user.getUserId());
119
			userinfo.setNameOfUser(user.getName());
120
			userinfo.setEmail(email);
121
			userinfo.setLoggedIn(true);
122
			userinfo.setPincode(pincode);
637 rajveer 123
			userinfo.setCartId(user.getActiveCartId());
1776 varun.gupt 124
			int totalItems = userClient.getCart(user.getActiveCartId()).getLinesSize();
762 rajveer 125
			userinfo.setTotalItems(totalItems);
1623 rajveer 126
			System.out.println(userinfo);
637 rajveer 127
			return true;
781 vikas 128
		} catch (Exception e) {
1175 varun.gupt 129
			log.error(UserMessage.USER_AUTHENTICATION_FAILURE);
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
}