Subversion Repositories SmartDukaan

Rev

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
 
555 chandransh 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;
507 rajveer 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;
555 chandransh 15
import in.shop2020.serving.utils.Utils;
507 rajveer 16
import in.shop2020.thrift.clients.UserContextServiceClient;
17
 
18
import java.io.IOException;
19
import java.util.Date;
555 chandransh 20
import java.util.GregorianCalendar;
507 rajveer 21
import java.util.HashMap;
555 chandransh 22
import java.util.HashSet;
507 rajveer 23
import java.util.Map;
555 chandransh 24
import java.util.Set;
507 rajveer 25
import java.util.StringTokenizer;
26
 
27
import javax.servlet.ServletContext;
555 chandransh 28
import javax.servlet.http.Cookie;
507 rajveer 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
 
550 rajveer 49
@Results({
50
    @Result(name="success", type="redirectAction", 
51
    		params = {"actionName" , "home"})
52
})
507 rajveer 53
public class RegisterController extends BaseController{
54
 
55
	private PageManager pageManager = null;
56
 
57
	/**
58
	 * 
59
	 */
60
	private static Log log = LogFactory.getLog(RegisterController.class);
61
 
62
	private Map<String,String> htmlSnippets;
63
 
64
 
65
	public RegisterController(){
66
		super();
67
		pageManager = PageManager.getPageManager();
68
	}
69
 
70
 
71
    public HttpHeaders index() throws SecurityException, IOException {
72
    	Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
73
 
74
    	params.put(PageContentKeys.USER_ID, new Long(userinfo.getUserId()).toString());
75
    	params.put(PageContentKeys.CART_ID, new Long(userinfo.getCartId()).toString());
76
    	params.put(PageContentKeys.ITEM_COUNT, new Long(userinfo.getTotalItems()).toString());
77
    	htmlSnippets = pageManager.getPageContents(PageEnum.REGISTRATION_PAGE, params);
78
 
79
        return new DefaultHttpHeaders("index");
80
    }
81
 
82
    public String create() throws SecurityException, Exception {
83
 
84
    	if(registerUser())
85
    		return "success";
86
    	else
87
    		return "failure";
88
    }
89
 
90
    public boolean registerUser() throws Exception{
569 rajveer 91
    	String email, password, userName, mobileNumber, communicationEmail, sex, dateOfBirth;
507 rajveer 92
 
555 chandransh 93
    	userName =  this.request.getParameter("nameOfUser");
507 rajveer 94
    	email = this.request.getParameter("email"); 
95
    	password = this.request.getParameter("password");
550 rajveer 96
 
97
    	mobileNumber = this.request.getParameter("mobileNumber");
98
    	communicationEmail = this.request.getParameter("communicationEmail");
99
 
569 rajveer 100
    	dateOfBirth = this.request.getParameter("dateOfBirth");
507 rajveer 101
    	sex =  this.request.getParameter("sex");
102
 
555 chandransh 103
		UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
104
		in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
507 rajveer 105
 
106
		if(userClient.userExists(email)){
555 chandransh 107
			log.error("User Exists!!!!");
507 rajveer 108
			return false;
555 chandransh 109
		}
110
 
111
		User user = new User();
112
		user.setName(userName);
113
		user.setEmail(email);
114
		user.setPassword(password);
115
		user.setCommunicationEmail(communicationEmail);
569 rajveer 116
		user.setMobileNumber(mobileNumber);
117
		user.setDateOfBirth(dateOfBirth);
555 chandransh 118
 
119
 
120
		user.setSex(Sex.WONT_SAY);
121
		if (sex != null) {
122
			try {
123
				user.setSex(Sex.findByValue(Integer.parseInt(sex)));
124
			} catch (NumberFormatException nfe) {
125
				log.error("Unusual value for sex. Leaving it marked as won't say.");
507 rajveer 126
			}
555 chandransh 127
		}
507 rajveer 128
 
129
 
555 chandransh 130
		user = userClient.createUser(user);
131
		long userId = user.getUserId();
132
		userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
133
 
134
		userinfo.setUserId(userId);
135
		userinfo.setNameOfUser(userName);
136
		userinfo.setEmail(email);
137
		userinfo.setLoggedIn(true);
138
 
139
		// TODO: setTotalItems shouldn't be a method on userinfo. This allows
140
		// for potentially updating the item count wrongly. The method setCartId
141
		// should update the item count as well. Also, there can be a method
142
		// called refreshItemCount() that automatically updates the number of
143
		// items currently in the cart.
144
		userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
145
		userinfo.setCartId(user.getActiveCartId());
146
		userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
147
 
148
		return true;
507 rajveer 149
    }
150
 
151
	public Map<String,String> getHtmlSnippets(){
152
		System.out.println(" getHtmlSnippets  is called");
153
		return htmlSnippets;
154
	}
155
 
156
	public String getHeaderSnippet(){
157
		return htmlSnippets.get("HEADER");
158
	}
159
 
160
	public String getMainMenuSnippet(){
161
		return htmlSnippets.get("MAIN_MENU");
162
	}
163
 
164
	public String getSearchBarSnippet(){
165
		return htmlSnippets.get("SEARCH_BAR");
166
	}
167
 
168
 
169
	public String getCustomerServiceSnippet(){
170
		return htmlSnippets.get("CUSTOMER_SERVICE");
171
	}
172
 
173
	public String getRegistrationHeaderSnippet(){
174
		return htmlSnippets.get("REGISTRATION_HEADER");
175
	}
176
 
177
	public String getRegistrationFormSnippet(){
178
		return htmlSnippets.get("REGISTRATION_FORM");
179
	}
180
 
181
	public String getRecommendationsSnippet(){
182
		return htmlSnippets.get("RECOMMENDATIONS");
183
	}
184
 
185
	public String getBrowseHistorySnippet(){
186
		return htmlSnippets.get("BROWSE_HISTORY");
187
	}
188
 
189
	public String getFooterSnippet(){
190
		return htmlSnippets.get("FOOTER");
191
	}
192
 
193
	public String getJsFileSnippet(){
194
		return htmlSnippets.get("JS_FILES");
195
	}
196
 
197
	public String getCssFileSnippet(){
198
		return htmlSnippets.get("CSS_FILES");
199
	}
200
 
201
}