Subversion Repositories SmartDukaan

Rev

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