Subversion Repositories SmartDukaan

Rev

Rev 1957 | Rev 2031 | 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
    	}
650 rajveer 56
		return "index";
781 vikas 57
	}
637 rajveer 58
 
781 vikas 59
	public String create() throws SecurityException, Exception {
60
		if (loginUser()) {
831 vikas 61
			log.info(redirectUrl);
1957 vikas 62
            dataLog.info(StringUtils.join(
63
                    new String[] { Event.LOGIN_SUCCESS.name(),
64
                            this.request.getParameter("email") }, ", "));
781 vikas 65
			return "redirect";
66
		} else {
1175 varun.gupt 67
			addActionError(UserMessage.USER_AUTHENTICATION_FAILURE);
1957 vikas 68
            dataLog.info(StringUtils.join(
69
                    new String[] { Event.LOGIN_FAILED.name(),
70
                            this.request.getParameter("email") }, ", "));
830 vikas 71
			return "login";
781 vikas 72
		}
73
	}
74
 
75
	private boolean loginUser() {
76
		try {
77
			String email, password;
78
 
79
			email = this.request.getParameter("email");
80
			password = this.request.getParameter("password");
81
 
82
			if (email == null || password == null) {
83
				return false;
84
			}
815 rajveer 85
 
1776 varun.gupt 86
			String encryptedPassword = desEncrypter.encrypt(password);
815 rajveer 87
 
781 vikas 88
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
1747 varun.gupt 89
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
815 rajveer 90
			User user = userClient.authenticateUser(email, encryptedPassword);
793 rajveer 91
			userClient.setUserAsLoggedIn(user.getUserId(),(new Date()).getTime());
92
			String pincode = userClient.getDefaultPincode(user.getUserId());
781 vikas 93
 
1175 varun.gupt 94
			// TODO: setTotalItems shouldn't be a method on userinfo. This allows
95
			// for potentially updating the item count wrongly. The method setCartId
637 rajveer 96
			// should update the item count as well. Also, there can be a method
1175 varun.gupt 97
			// called refreshItemCount() that automatically updates the number of
637 rajveer 98
			// items currently in the cart.
1625 rajveer 99
			if(userinfo.getUserId() != -1){
1623 rajveer 100
				userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
1625 rajveer 101
 
102
				Widget browseHistory = userClient.getBrowseHistory(userinfo.getUserId());
1623 rajveer 103
				if(browseHistory != null){
104
					List<WidgetItem> items =  browseHistory.getItems();
105
					if(items != null){
106
						for(WidgetItem item: items){
107
							userClient.updateBrowseHistory(user.getUserId(), item.getItem_id());
108
						}
109
					}
110
				}
1625 rajveer 111
 
112
				Widget myResearch = userClient.getMyResearch(userinfo.getUserId());
113
				if(myResearch != null){
114
					List<WidgetItem> items =  myResearch.getItems();
115
					if(items != null){
116
						for(WidgetItem item: items){
117
							userClient.updateMyResearch(user.getUserId(), item.getItem_id());
118
						}
119
					}
120
				}
1623 rajveer 121
			}
122
 
123
 
1625 rajveer 124
			userinfo.setUserId(user.getUserId());
125
			userinfo.setNameOfUser(user.getName());
126
			userinfo.setEmail(email);
127
			userinfo.setLoggedIn(true);
128
			userinfo.setPincode(pincode);
637 rajveer 129
			userinfo.setCartId(user.getActiveCartId());
1776 varun.gupt 130
			int totalItems = userClient.getCart(user.getActiveCartId()).getLinesSize();
762 rajveer 131
			userinfo.setTotalItems(totalItems);
1623 rajveer 132
			System.out.println(userinfo);
637 rajveer 133
			return true;
781 vikas 134
		} catch (Exception e) {
1175 varun.gupt 135
			log.error(UserMessage.USER_AUTHENTICATION_FAILURE);
781 vikas 136
			return false;
137
		}
138
	}
637 rajveer 139
 
924 vikas 140
	public String getRedirectUrl() {
141
		return redirectUrl;
781 vikas 142
	}
143
 
924 vikas 144
	public void setRedirectUrl(String redirectUrl) {
145
		this.redirectUrl = redirectUrl;
146
	}
147
 
781 vikas 148
	public String getLoginHeaderSnippet() {
637 rajveer 149
		return htmlSnippets.get("LOGIN_HEADER");
150
	}
151
}