| 507 |
rajveer |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
|
|
6 |
|
| 2263 |
vikas |
7 |
import in.shop2020.datalogger.EventType;
|
| 3830 |
chandransh |
8 |
import in.shop2020.model.v1.user.Cart;
|
| 555 |
chandransh |
9 |
import in.shop2020.model.v1.user.Sex;
|
| 5036 |
rajveer |
10 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
| 3378 |
vikas |
11 |
import in.shop2020.model.v1.user.TrackLogType;
|
| 555 |
chandransh |
12 |
import in.shop2020.model.v1.user.User;
|
| 5036 |
rajveer |
13 |
import in.shop2020.model.v1.user.UserAffiliateException;
|
|
|
14 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
15 |
import in.shop2020.model.v1.user.WidgetException;
|
| 1868 |
vikas |
16 |
import in.shop2020.serving.interceptors.TrackingInterceptor;
|
| 815 |
rajveer |
17 |
import in.shop2020.serving.utils.DesEncrypter;
|
| 4891 |
varun.gupt |
18 |
import in.shop2020.serving.utils.Utils;
|
| 3126 |
rajveer |
19 |
import in.shop2020.thrift.clients.UserClient;
|
| 2511 |
vikas |
20 |
import in.shop2020.utils.DataLogger;
|
| 507 |
rajveer |
21 |
|
|
|
22 |
import java.io.IOException;
|
|
|
23 |
import java.util.Date;
|
| 1623 |
rajveer |
24 |
import java.util.List;
|
| 507 |
rajveer |
25 |
|
| 2021 |
vikas |
26 |
import javax.servlet.http.Cookie;
|
|
|
27 |
|
| 832 |
rajveer |
28 |
import org.apache.log4j.Logger;
|
| 507 |
rajveer |
29 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
30 |
import org.apache.struts2.convention.annotation.Results;
|
| 5036 |
rajveer |
31 |
import org.apache.thrift.TException;
|
|
|
32 |
import org.apache.thrift.transport.TTransportException;
|
| 20202 |
aman.kumar |
33 |
import org.json.JSONObject;
|
| 507 |
rajveer |
34 |
|
|
|
35 |
/**
|
|
|
36 |
*
|
|
|
37 |
* @author rajveer
|
|
|
38 |
*
|
|
|
39 |
*/
|
|
|
40 |
|
| 550 |
rajveer |
41 |
@Results({
|
| 1776 |
varun.gupt |
42 |
@Result(name = "success", type = "redirectAction", params = {"actionName" , "home"}),
|
| 1904 |
varun.gupt |
43 |
@Result(name = "failure", type = "redirectAction", params = {"actionName" , "login"}),
|
|
|
44 |
@Result(name = "redirect-to-login", type = "redirectAction", params = {"actionName" , "login"}),
|
| 1776 |
varun.gupt |
45 |
@Result(name = "redirect", location = "${redirectUrl}", type = "redirect")
|
| 550 |
rajveer |
46 |
})
|
| 507 |
rajveer |
47 |
public class RegisterController extends BaseController{
|
| 1904 |
varun.gupt |
48 |
|
| 650 |
rajveer |
49 |
private static final long serialVersionUID = 1L;
|
| 832 |
rajveer |
50 |
private static Logger log = Logger.getLogger(Class.class);
|
| 815 |
rajveer |
51 |
private DesEncrypter desEncrypter = new DesEncrypter("saholic");
|
| 507 |
rajveer |
52 |
|
| 4891 |
varun.gupt |
53 |
private String redirectUrl = "/";
|
| 20202 |
aman.kumar |
54 |
private String registerJson = "{}";
|
| 507 |
rajveer |
55 |
public RegisterController(){
|
|
|
56 |
super();
|
|
|
57 |
}
|
|
|
58 |
|
| 650 |
rajveer |
59 |
public String index() throws SecurityException, IOException {
|
| 925 |
rajveer |
60 |
if(userinfo.isLoggedIn()){
|
|
|
61 |
return "success";
|
|
|
62 |
}
|
| 1904 |
varun.gupt |
63 |
// htmlSnippets.put("REGISTRATION_HEADER",pageLoader.getRegistrationHeaderHtml());
|
|
|
64 |
// htmlSnippets.put("REGISTRATION_FORM",pageLoader.getRegistrationFormHtml());
|
|
|
65 |
return "redirect-to-login";
|
| 507 |
rajveer |
66 |
}
|
|
|
67 |
|
|
|
68 |
public String create() throws SecurityException, Exception {
|
|
|
69 |
|
| 20202 |
aman.kumar |
70 |
registerUser();
|
|
|
71 |
return "register-response";
|
|
|
72 |
//else
|
|
|
73 |
// return "failure";
|
| 507 |
rajveer |
74 |
}
|
|
|
75 |
|
| 5036 |
rajveer |
76 |
public boolean registerUser(){
|
| 4453 |
varun.gupt |
77 |
String email, password;
|
| 741 |
rajveer |
78 |
boolean isValid = true;
|
| 20202 |
aman.kumar |
79 |
JSONObject json = new JSONObject();
|
|
|
80 |
String errorMessage = "{\"status\":\"error\",\"message\":\"Some Problem occurred. Please try again.\"}";
|
| 507 |
rajveer |
81 |
email = this.request.getParameter("email");
|
| 762 |
rajveer |
82 |
password = this.request.getParameter("txtPassword");
|
| 20202 |
aman.kumar |
83 |
try{
|
| 4911 |
varun.gupt |
84 |
if(!Utils.isValidEmail(email)) {
|
| 4891 |
varun.gupt |
85 |
addActionError("Please enter valid email address.");
|
| 20202 |
aman.kumar |
86 |
json.put("message", "Please enter valid email address.");
|
|
|
87 |
json.put("status", "error");
|
| 741 |
rajveer |
88 |
isValid = false;
|
|
|
89 |
}
|
| 4891 |
varun.gupt |
90 |
if(password == null ) {
|
| 741 |
rajveer |
91 |
addActionError("Please enter password.");
|
| 20202 |
aman.kumar |
92 |
json.put("message", "Please enter password.");
|
|
|
93 |
json.put("status", "error");
|
| 741 |
rajveer |
94 |
isValid = false;
|
|
|
95 |
}
|
| 507 |
rajveer |
96 |
|
| 741 |
rajveer |
97 |
if(!isValid){
|
| 20202 |
aman.kumar |
98 |
setRegisterJson(json.toString());
|
| 12616 |
anikendra |
99 |
// DataLogger.logData(EventType.REGISTER_DATA_INCOMPLETE, getSessionId(), userinfo.getUserId(), email, "", email);
|
| 741 |
rajveer |
100 |
return isValid;
|
|
|
101 |
}
|
|
|
102 |
|
| 555 |
chandransh |
103 |
User user = new User();
|
|
|
104 |
user.setEmail(email);
|
| 815 |
rajveer |
105 |
String encryptedPassword = desEncrypter.encrypt(password);
|
|
|
106 |
user.setPassword(encryptedPassword);
|
| 3830 |
chandransh |
107 |
user.setCommunicationEmail(email);
|
| 2021 |
vikas |
108 |
Cookie sourceCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_COOKIE);
|
|
|
109 |
if (sourceCookie != null) {
|
| 2817 |
vikas |
110 |
DesEncrypter des = new DesEncrypter(TrackingInterceptor.ENCRIPTION_STRING);
|
| 2021 |
vikas |
111 |
String sourceCookieVal = des.decrypt(sourceCookie.getValue());
|
|
|
112 |
user.setSource(sourceCookieVal);
|
|
|
113 |
}
|
| 2817 |
vikas |
114 |
|
|
|
115 |
Cookie sourceTimeCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_TIME_COOKIE);
|
|
|
116 |
long sourceTime = 0;
|
|
|
117 |
if (sourceTimeCookie != null) {
|
|
|
118 |
try {
|
|
|
119 |
sourceTime = Long.parseLong(sourceTimeCookie.getValue());
|
|
|
120 |
}
|
|
|
121 |
catch (Exception e) {
|
|
|
122 |
log.warn("Unable to parse session src time cookie.");
|
|
|
123 |
}
|
|
|
124 |
user.setSourceStartTime(sourceTime);
|
|
|
125 |
}
|
| 555 |
chandransh |
126 |
|
|
|
127 |
user.setSex(Sex.WONT_SAY);
|
| 3830 |
chandransh |
128 |
|
| 507 |
rajveer |
129 |
|
| 5036 |
rajveer |
130 |
UserClient userContextServiceClient;
|
|
|
131 |
try {
|
|
|
132 |
userContextServiceClient = new UserClient();
|
| 793 |
rajveer |
133 |
|
| 5036 |
rajveer |
134 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
135 |
try{
|
|
|
136 |
user = userClient.createUser(user);
|
|
|
137 |
}catch (UserContextException ux){
|
|
|
138 |
addActionError("User already exists with this email id.");
|
| 20202 |
aman.kumar |
139 |
json.put("message", "You are already a member. Please login.");
|
|
|
140 |
json.put("status", "error");
|
|
|
141 |
|
|
|
142 |
setRegisterJson(json.toString());
|
| 12616 |
anikendra |
143 |
// DataLogger.logData(EventType.REGISTER_FAILED_USER_EXISTS, getSessionId(), userinfo.getUserId(), email, "", email);
|
| 5036 |
rajveer |
144 |
return false;
|
| 1623 |
rajveer |
145 |
}
|
| 5036 |
rajveer |
146 |
|
|
|
147 |
long userId = user.getUserId();
|
|
|
148 |
userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
|
|
|
149 |
String pincode = userClient.getDefaultPincode(user.getUserId());
|
| 2982 |
rajveer |
150 |
|
| 5036 |
rajveer |
151 |
// TODO: setTotalItems shouldn't be a method on userinfo. This allows
|
|
|
152 |
// for potentially updating the item count wrongly. The method setCartId
|
|
|
153 |
// should update the item count as well. Also, there can be a method
|
|
|
154 |
// called refreshItemCount() that automatically updates the number of
|
|
|
155 |
// items currently in the cart.
|
|
|
156 |
if(userinfo.getUserId() != -1){
|
|
|
157 |
userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
|
|
|
158 |
|
|
|
159 |
List<Long> items = userClient.getBrowseHistoryItems(userinfo.getUserId());
|
|
|
160 |
if(items != null){
|
|
|
161 |
for(Long itemId: items){
|
|
|
162 |
userClient.updateBrowseHistory(user.getUserId(), itemId);
|
|
|
163 |
}
|
| 1625 |
rajveer |
164 |
}
|
| 5036 |
rajveer |
165 |
|
|
|
166 |
items = userClient.getMyResearchItems(userinfo.getUserId());
|
|
|
167 |
if(items != null){
|
|
|
168 |
for(Long itemId: items){
|
|
|
169 |
userClient.updateMyResearch(user.getUserId(), itemId);
|
|
|
170 |
}
|
|
|
171 |
}
|
| 1625 |
rajveer |
172 |
}
|
| 5036 |
rajveer |
173 |
|
|
|
174 |
userinfo.setUserId(userId);
|
|
|
175 |
userinfo.setEmail(email);
|
|
|
176 |
userinfo.setLoggedIn(true);
|
|
|
177 |
userinfo.setPincode(pincode);
|
|
|
178 |
userinfo.setCartId(user.getActiveCartId());
|
| 11932 |
amit.gupta |
179 |
userinfo.setPrivateDealUser(isPrivateDealUser());
|
| 5036 |
rajveer |
180 |
Cart cart = userClient.getCart(userinfo.getCartId());
|
|
|
181 |
userinfo.setTotalItems(cart.getLinesSize());
|
|
|
182 |
userinfo.setTotalAmount(cart.getTotalPrice());
|
|
|
183 |
|
|
|
184 |
if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
|
|
|
185 |
long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
|
|
|
186 |
userClient.addTrackLog(affId, userId, TrackLogType.NEW_REGISTRATION, "",email, (new Date()).getTime());
|
|
|
187 |
}
|
| 12616 |
anikendra |
188 |
// DataLogger.logData(EventType.REGISTER_SUCCESS, getSessionId(), userinfo.getUserId(), email, "", email);
|
| 20202 |
aman.kumar |
189 |
json.put("message", "Register successful");
|
|
|
190 |
json.put("status", "success");
|
|
|
191 |
|
|
|
192 |
setRegisterJson(json.toString());
|
| 5036 |
rajveer |
193 |
return true;
|
|
|
194 |
} catch (TTransportException e) {
|
| 5041 |
rajveer |
195 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
196 |
setRegisterJson(errorMessage);
|
| 5036 |
rajveer |
197 |
} catch (UserContextException e) {
|
| 5041 |
rajveer |
198 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
199 |
setRegisterJson(errorMessage);
|
| 5036 |
rajveer |
200 |
} catch (TException e) {
|
| 5041 |
rajveer |
201 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
202 |
setRegisterJson(errorMessage);
|
| 5036 |
rajveer |
203 |
} catch (UserAffiliateException e) {
|
| 5041 |
rajveer |
204 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
205 |
setRegisterJson(errorMessage);
|
| 5036 |
rajveer |
206 |
} catch (ShoppingCartException e) {
|
| 5041 |
rajveer |
207 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
208 |
setRegisterJson(errorMessage);
|
| 5036 |
rajveer |
209 |
} catch (WidgetException e) {
|
| 5041 |
rajveer |
210 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
211 |
setRegisterJson(errorMessage);
|
| 1623 |
rajveer |
212 |
}
|
| 20202 |
aman.kumar |
213 |
}catch(Exception e){
|
|
|
214 |
log.error("Unable to register user." + e);
|
|
|
215 |
setRegisterJson(errorMessage);
|
|
|
216 |
}
|
| 12616 |
anikendra |
217 |
// DataLogger.logData(EventType.REGISTER_FAILED_USER_EXISTS, getSessionId(), userinfo.getUserId(), email, "", email);
|
| 5036 |
rajveer |
218 |
return false;
|
| 1623 |
rajveer |
219 |
|
| 507 |
rajveer |
220 |
}
|
|
|
221 |
|
|
|
222 |
public String getRegistrationHeaderSnippet(){
|
|
|
223 |
return htmlSnippets.get("REGISTRATION_HEADER");
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
public String getRegistrationFormSnippet(){
|
|
|
227 |
return htmlSnippets.get("REGISTRATION_FORM");
|
|
|
228 |
}
|
| 1776 |
varun.gupt |
229 |
public String getRedirectUrl() {
|
|
|
230 |
return redirectUrl;
|
|
|
231 |
}
|
| 507 |
rajveer |
232 |
|
| 1776 |
varun.gupt |
233 |
public void setRedirectUrl(String redirectUrl) {
|
|
|
234 |
this.redirectUrl = redirectUrl;
|
|
|
235 |
}
|
| 11932 |
amit.gupta |
236 |
|
| 20202 |
aman.kumar |
237 |
|
|
|
238 |
|
|
|
239 |
public String getRegisterJson() {
|
|
|
240 |
return registerJson;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
public void setRegisterJson(String registerJson) {
|
|
|
244 |
this.registerJson = registerJson;
|
|
|
245 |
}
|
|
|
246 |
|
| 11932 |
amit.gupta |
247 |
private boolean isPrivateDealUser() throws TTransportException, TException{
|
|
|
248 |
try {
|
|
|
249 |
in.shop2020.model.v1.user.UserContextService.Client uc = new UserClient().getClient();
|
|
|
250 |
return uc.isPrivateDealUser(userinfo.getUserId());
|
|
|
251 |
} catch (TTransportException e) {
|
|
|
252 |
log.error("Unable to get user service client.", e);
|
|
|
253 |
}
|
|
|
254 |
return false;
|
|
|
255 |
}
|
| 1776 |
varun.gupt |
256 |
}
|