Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
507 rajveer 1
/**
2
 * 
3
 */
4
package in.shop2020.serving.controllers;
5
 
6
 
7
import in.shop2020.model.v1.user.UserContext;
8
import in.shop2020.model.v1.user.UserPrimaryInfo;
9
import in.shop2020.model.v1.user.UserState;
10
import in.shop2020.serving.pages.PageContentKeys;
11
import in.shop2020.serving.pages.PageEnum;
12
import in.shop2020.serving.pages.PageManager;
13
import in.shop2020.thrift.clients.UserContextServiceClient;
14
 
15
import java.io.IOException;
16
import java.util.Date;
17
import java.util.HashMap;
18
import java.util.Map;
19
import java.util.StringTokenizer;
20
 
21
import javax.servlet.ServletContext;
22
 
23
import org.apache.juli.logging.Log;
24
import org.apache.juli.logging.LogFactory;
25
import org.apache.struts2.convention.annotation.Action;
26
import org.apache.struts2.convention.annotation.Actions;
27
import org.apache.struts2.convention.annotation.Result;
28
import org.apache.struts2.convention.annotation.Results;
29
import org.apache.struts2.interceptor.ParameterAware;
30
import org.apache.struts2.rest.DefaultHttpHeaders;
31
import org.apache.struts2.rest.HttpHeaders;
32
import org.apache.struts2.util.ServletContextAware;
33
 
34
import com.opensymphony.xwork2.ModelDriven;
35
 
36
/**
37
 * 
38
 * @author rajveer
39
 *
40
 */
41
 
550 rajveer 42
@Results({
43
    @Result(name="success", type="redirectAction", 
44
    		params = {"actionName" , "home"})
45
})
507 rajveer 46
public class RegisterController extends BaseController{
47
 
48
	private PageManager pageManager = null;
49
 
50
	/**
51
	 * 
52
	 */
53
	private static Log log = LogFactory.getLog(RegisterController.class);
54
 
55
	private Map<String,String> htmlSnippets;
56
 
57
 
58
	public RegisterController(){
59
		super();
60
		pageManager = PageManager.getPageManager();
61
	}
62
 
63
 
64
    public HttpHeaders index() throws SecurityException, IOException {
65
    	Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
66
 
67
    	params.put(PageContentKeys.USER_ID, new Long(userinfo.getUserId()).toString());
68
    	params.put(PageContentKeys.CART_ID, new Long(userinfo.getCartId()).toString());
69
    	params.put(PageContentKeys.SESSION_ID, new Long(userinfo.getSessionId()).toString());
70
    	params.put(PageContentKeys.ITEM_COUNT, new Long(userinfo.getTotalItems()).toString());
71
    	htmlSnippets = pageManager.getPageContents(PageEnum.REGISTRATION_PAGE, params);
72
 
73
        return new DefaultHttpHeaders("index");
74
    }
75
 
76
    public String create() throws SecurityException, Exception {
77
 
78
    	if(registerUser())
79
    		return "success";
80
    	else
81
    		return "failure";
82
    }
83
 
84
    public boolean registerUser() throws Exception{
550 rajveer 85
    	String email, password, nameOfUser, mobileNumber, communicationEmail, sex, dobDay, dobYear, dobMonth;
507 rajveer 86
 
550 rajveer 87
    	nameOfUser =  this.request.getParameter("nameOfUser");
507 rajveer 88
    	email = this.request.getParameter("email"); 
89
    	password = this.request.getParameter("password");
550 rajveer 90
 
91
    	mobileNumber = this.request.getParameter("mobileNumber");
92
    	communicationEmail = this.request.getParameter("communicationEmail");
93
 
94
    	dobDay = this.request.getParameter("dobDay");
95
    	dobMonth = this.request.getParameter("dobMonth");
96
    	dobYear = this.request.getParameter("dobYear");
507 rajveer 97
    	sex =  this.request.getParameter("sex");
98
 
99
 
100
		UserContextServiceClient userContextServiceClient = null;
101
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
102
 
103
		userContextServiceClient = new UserContextServiceClient();
104
		userClient = userContextServiceClient.getClient();
105
 
106
		if(userClient.userExists(email)){
107
			return false;
108
		}else{
109
			UserContext context = new UserContext();
110
			UserPrimaryInfo primaryInfo = new UserPrimaryInfo();
111
			UserState userState = new UserState();
112
			// set the data
113
			primaryInfo.setPassword(password);
114
			primaryInfo.setEmail(email);
115
 
116
			if(dobDay != null && dobYear != null && dobMonth != null){
117
				primaryInfo.setDateOfBirth((new Date(Integer.parseInt(dobYear), Integer.parseInt(dobMonth), Integer.parseInt(dobDay))).getTime());
118
			}
119
 
120
			context.setPrimaryInfo(primaryInfo);
121
			context.setUserState(userState);
122
 
123
			context = userClient.createContext(context, false);
124
 
125
 
126
			//TODO to verify weather user should be logged in with registration
127
 
128
			long userId = context.getId();
129
 
130
			userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
131
 
132
			this.userinfo.setUserId(userId);
133
			this.userinfo.setEmail(email);
134
			this.userinfo.setLoggedIn(true);
135
 
136
			return true;
137
		}
138
    }
139
 
140
	public Map<String,String> getHtmlSnippets(){
141
		System.out.println(" getHtmlSnippets  is called");
142
		return htmlSnippets;
143
	}
144
 
145
	public String getHeaderSnippet(){
146
		return htmlSnippets.get("HEADER");
147
	}
148
 
149
	public String getMainMenuSnippet(){
150
		return htmlSnippets.get("MAIN_MENU");
151
	}
152
 
153
	public String getSearchBarSnippet(){
154
		return htmlSnippets.get("SEARCH_BAR");
155
	}
156
 
157
 
158
	public String getCustomerServiceSnippet(){
159
		return htmlSnippets.get("CUSTOMER_SERVICE");
160
	}
161
 
162
	public String getRegistrationHeaderSnippet(){
163
		return htmlSnippets.get("REGISTRATION_HEADER");
164
	}
165
 
166
	public String getRegistrationFormSnippet(){
167
		return htmlSnippets.get("REGISTRATION_FORM");
168
	}
169
 
170
	public String getRecommendationsSnippet(){
171
		return htmlSnippets.get("RECOMMENDATIONS");
172
	}
173
 
174
	public String getBrowseHistorySnippet(){
175
		return htmlSnippets.get("BROWSE_HISTORY");
176
	}
177
 
178
	public String getFooterSnippet(){
179
		return htmlSnippets.get("FOOTER");
180
	}
181
 
182
	public String getJsFileSnippet(){
183
		return htmlSnippets.get("JS_FILES");
184
	}
185
 
186
	public String getCssFileSnippet(){
187
		return htmlSnippets.get("CSS_FILES");
188
	}
189
 
190
}