Subversion Repositories SmartDukaan

Rev

Rev 1747 | Rev 1957 | 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;
1747 varun.gupt 20
import org.apache.velocity.VelocityContext;
637 rajveer 21
 
22
/**
23
 * 
24
 * @author rajveer
781 vikas 25
 * 
637 rajveer 26
 */
925 rajveer 27
@Results({
28
	@Result(name="success", type="redirectAction", params = {"actionName" , "home"}),
29
	@Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
30
})
637 rajveer 31
 
781 vikas 32
public class LoginController extends BaseController {
650 rajveer 33
 
781 vikas 34
	/**
35
	 * 
36
	 */
37
	private static final long serialVersionUID = 5390035354379263121L;
650 rajveer 38
 
832 rajveer 39
	private static Logger log = Logger.getLogger(Class.class);
815 rajveer 40
	private DesEncrypter desEncrypter = new DesEncrypter("saholic");
41
 
924 vikas 42
	private String redirectUrl = null;
781 vikas 43
 
44
	public LoginController() {
637 rajveer 45
		super();
46
	}
47
 
781 vikas 48
	public String index() throws SecurityException, IOException {
925 rajveer 49
		if(userinfo.isLoggedIn()){
50
    		return "success";
51
    	}
781 vikas 52
		htmlSnippets.put("LOGIN_HEADER", pageLoader.getLoginHeaderHtml());
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);
781 vikas 59
			return "redirect";
60
		} else {
1175 varun.gupt 61
			addActionError(UserMessage.USER_AUTHENTICATION_FAILURE);
830 vikas 62
			return "login";
781 vikas 63
		}
64
	}
65
 
66
	private boolean loginUser() {
67
		try {
68
			String email, password;
69
 
70
			email = this.request.getParameter("email");
71
			password = this.request.getParameter("password");
72
 
73
			if (email == null || password == null) {
74
				return false;
75
			}
815 rajveer 76
 
1776 varun.gupt 77
			String encryptedPassword = desEncrypter.encrypt(password);
815 rajveer 78
 
781 vikas 79
			UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
1747 varun.gupt 80
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.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());
1776 varun.gupt 121
			int totalItems = userClient.getCart(user.getActiveCartId()).getLinesSize();
762 rajveer 122
			userinfo.setTotalItems(totalItems);
1623 rajveer 123
			System.out.println(userinfo);
637 rajveer 124
			return true;
781 vikas 125
		} catch (Exception e) {
1175 varun.gupt 126
			log.error(UserMessage.USER_AUTHENTICATION_FAILURE);
781 vikas 127
			return false;
128
		}
129
	}
637 rajveer 130
 
924 vikas 131
	public String getRedirectUrl() {
132
		return redirectUrl;
781 vikas 133
	}
134
 
924 vikas 135
	public void setRedirectUrl(String redirectUrl) {
136
		this.redirectUrl = redirectUrl;
137
	}
138
 
781 vikas 139
	public String getLoginHeaderSnippet() {
637 rajveer 140
		return htmlSnippets.get("LOGIN_HEADER");
141
	}
142
}