Subversion Repositories SmartDukaan

Rev

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