Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
637 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
 
7
import in.shop2020.model.v1.user.Phone;
8
import in.shop2020.model.v1.user.PhoneType;
9
import in.shop2020.model.v1.user.Sex;
10
import in.shop2020.model.v1.user.User;
11
import in.shop2020.model.v1.user.UserState;
12
import in.shop2020.serving.pages.PageContentKeys;
13
import in.shop2020.serving.pages.PageEnum;
14
import in.shop2020.serving.pages.PageManager;
15
import in.shop2020.serving.utils.Utils;
16
import in.shop2020.thrift.clients.UserContextServiceClient;
17
 
18
import java.io.IOException;
19
import java.util.Date;
20
import java.util.GregorianCalendar;
21
import java.util.HashMap;
22
import java.util.HashSet;
23
import java.util.Map;
24
import java.util.Set;
25
import java.util.StringTokenizer;
26
 
27
import javax.servlet.ServletContext;
28
import javax.servlet.http.Cookie;
29
 
30
import org.apache.juli.logging.Log;
31
import org.apache.juli.logging.LogFactory;
32
import org.apache.struts2.convention.annotation.Action;
33
import org.apache.struts2.convention.annotation.Actions;
34
import org.apache.struts2.convention.annotation.Result;
35
import org.apache.struts2.convention.annotation.Results;
36
import org.apache.struts2.interceptor.ParameterAware;
37
import org.apache.struts2.rest.DefaultHttpHeaders;
38
import org.apache.struts2.rest.HttpHeaders;
39
import org.apache.struts2.util.ServletContextAware;
40
 
41
import com.opensymphony.xwork2.ModelDriven;
42
 
43
/**
44
 * 
45
 * @author rajveer
46
 *
47
 */
48
 
49
@Results({
50
    @Result(name="success", type="redirectAction", 
51
    		params = {"actionName" , "home"}),
52
    @Result(name="failure", type="redirectAction", 
53
       		params = {"actionName" , "login"})
54
})
55
public class LoginController extends BaseController{
56
 
57
	/**
58
	 * 
59
	 */
60
	private static Log log = LogFactory.getLog(LoginController.class);
61
 
62
	private Map<String,String> htmlSnippets = new HashMap<String, String>();
63
 
64
	public LoginController(){
65
		super();
66
	}
67
 
68
 
69
    public HttpHeaders index() throws SecurityException, IOException {
70
    	htmlSnippets.put("LOGIN_HEADER", pageLoader.getLoginHeaderHtml());
71
		htmlSnippets.put("LOGIN_FORM", pageLoader.getLoginFormHtml());
72
		return new DefaultHttpHeaders("index");
73
    }
74
 
75
    public String create() throws SecurityException, Exception {
76
 
77
    	if(loginUser()){
78
    		this.request.getSession().getAttribute("REDIRECT_URL");	
79
    		return "success";
80
    	}
81
    	else
82
    		return "failure";
83
    }
84
 
85
    public boolean loginUser(){
86
    	try{
87
	    	String email, password;
88
 
89
	    	email = this.request.getParameter("email"); 
90
	    	password = this.request.getParameter("password");
91
 
92
	    	UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
93
			in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
94
			User user = userClient.authenticateUser(email, password);
95
			userClient.setUserAsLoggedIn(user.getUserId(), (new Date()).getTime());
96
 
97
			userinfo.setUserId(user.getUserId());
98
			userinfo.setNameOfUser(user.getName());
99
			userinfo.setEmail(email);
100
			userinfo.setLoggedIn(true);
101
 
102
			// TODO: setTotalItems shouldn't be a method on userinfo. This allows
103
			// for potentially updating the item count wrongly. The method setCartId
104
			// should update the item count as well. Also, there can be a method
105
			// called refreshItemCount() that automatically updates the number of
106
			// items currently in the cart.
107
			userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
108
			userinfo.setCartId(user.getActiveCartId());
109
			userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
110
			return true;
111
    	}catch(Exception e){
112
    		log.error("Wrong username or password.");
113
    		return false;
114
    	}
115
    }
116
 
117
	public Map<String,String> getHtmlSnippets(){
118
		System.out.println(" getHtmlSnippets  is called");
119
		return htmlSnippets;
120
	}
121
 
122
	/*
123
	public String getHeaderSnippet(){
124
		return htmlSnippets.get("HEADER");
125
	}
126
 
127
	public String getMainMenuSnippet(){
128
		return htmlSnippets.get("MAIN_MENU");
129
	}
130
 
131
	public String getSearchBarSnippet(){
132
		return htmlSnippets.get("SEARCH_BAR");
133
	}
134
 
135
 
136
	public String getCustomerServiceSnippet(){
137
		return htmlSnippets.get("CUSTOMER_SERVICE");
138
	}
139
	*/
140
 
141
	public String getLoginHeaderSnippet(){
142
		return htmlSnippets.get("LOGIN_HEADER");
143
	}
144
 
145
	public String getLoginFormSnippet(){
146
		return htmlSnippets.get("LOGIN_FORM");
147
	}
148
 
149
	/*
150
	public String getRecommendationsSnippet(){
151
		return htmlSnippets.get("RECOMMENDATIONS");
152
	}
153
 
154
	public String getBrowseHistorySnippet(){
155
		return htmlSnippets.get("BROWSE_HISTORY");
156
	}
157
 
158
	public String getFooterSnippet(){
159
		return htmlSnippets.get("FOOTER");
160
	}
161
	 */
162
	public String getJsFileSnippet(){
163
		return htmlSnippets.get("JS_FILES");
164
	}
165
 
166
	public String getCssFileSnippet(){
167
		return htmlSnippets.get("CSS_FILES");
168
	}
169
 
170
}