Subversion Repositories SmartDukaan

Rev

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