| 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 {
|
| 20278 |
aman.kumar |
69 |
log.info("in register");
|
|
|
70 |
boolean result= registerUser();
|
|
|
71 |
log.info("result "+result);
|
| 20202 |
aman.kumar |
72 |
return "register-response";
|
|
|
73 |
//else
|
|
|
74 |
// return "failure";
|
| 507 |
rajveer |
75 |
}
|
|
|
76 |
|
| 5036 |
rajveer |
77 |
public boolean registerUser(){
|
| 4453 |
varun.gupt |
78 |
String email, password;
|
| 741 |
rajveer |
79 |
boolean isValid = true;
|
| 20202 |
aman.kumar |
80 |
JSONObject json = new JSONObject();
|
|
|
81 |
String errorMessage = "{\"status\":\"error\",\"message\":\"Some Problem occurred. Please try again.\"}";
|
| 507 |
rajveer |
82 |
email = this.request.getParameter("email");
|
| 20205 |
aman.kumar |
83 |
password = this.request.getParameter("password");
|
| 20202 |
aman.kumar |
84 |
try{
|
| 4911 |
varun.gupt |
85 |
if(!Utils.isValidEmail(email)) {
|
| 4891 |
varun.gupt |
86 |
addActionError("Please enter valid email address.");
|
| 20202 |
aman.kumar |
87 |
json.put("message", "Please enter valid email address.");
|
|
|
88 |
json.put("status", "error");
|
| 741 |
rajveer |
89 |
isValid = false;
|
|
|
90 |
}
|
| 4891 |
varun.gupt |
91 |
if(password == null ) {
|
| 741 |
rajveer |
92 |
addActionError("Please enter password.");
|
| 20202 |
aman.kumar |
93 |
json.put("message", "Please enter password.");
|
|
|
94 |
json.put("status", "error");
|
| 741 |
rajveer |
95 |
isValid = false;
|
|
|
96 |
}
|
| 507 |
rajveer |
97 |
|
| 741 |
rajveer |
98 |
if(!isValid){
|
| 20202 |
aman.kumar |
99 |
setRegisterJson(json.toString());
|
| 12616 |
anikendra |
100 |
// DataLogger.logData(EventType.REGISTER_DATA_INCOMPLETE, getSessionId(), userinfo.getUserId(), email, "", email);
|
| 741 |
rajveer |
101 |
return isValid;
|
|
|
102 |
}
|
|
|
103 |
|
| 555 |
chandransh |
104 |
User user = new User();
|
|
|
105 |
user.setEmail(email);
|
| 815 |
rajveer |
106 |
String encryptedPassword = desEncrypter.encrypt(password);
|
|
|
107 |
user.setPassword(encryptedPassword);
|
| 3830 |
chandransh |
108 |
user.setCommunicationEmail(email);
|
| 2021 |
vikas |
109 |
Cookie sourceCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_COOKIE);
|
|
|
110 |
if (sourceCookie != null) {
|
| 2817 |
vikas |
111 |
DesEncrypter des = new DesEncrypter(TrackingInterceptor.ENCRIPTION_STRING);
|
| 2021 |
vikas |
112 |
String sourceCookieVal = des.decrypt(sourceCookie.getValue());
|
|
|
113 |
user.setSource(sourceCookieVal);
|
|
|
114 |
}
|
| 2817 |
vikas |
115 |
|
|
|
116 |
Cookie sourceTimeCookie = (Cookie) cookiesMap.get(TrackingInterceptor.SRC_TIME_COOKIE);
|
|
|
117 |
long sourceTime = 0;
|
|
|
118 |
if (sourceTimeCookie != null) {
|
|
|
119 |
try {
|
|
|
120 |
sourceTime = Long.parseLong(sourceTimeCookie.getValue());
|
|
|
121 |
}
|
|
|
122 |
catch (Exception e) {
|
|
|
123 |
log.warn("Unable to parse session src time cookie.");
|
|
|
124 |
}
|
|
|
125 |
user.setSourceStartTime(sourceTime);
|
|
|
126 |
}
|
| 555 |
chandransh |
127 |
|
|
|
128 |
user.setSex(Sex.WONT_SAY);
|
| 3830 |
chandransh |
129 |
|
| 507 |
rajveer |
130 |
|
| 5036 |
rajveer |
131 |
UserClient userContextServiceClient;
|
|
|
132 |
try {
|
|
|
133 |
userContextServiceClient = new UserClient();
|
| 793 |
rajveer |
134 |
|
| 5036 |
rajveer |
135 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
136 |
try{
|
| 20278 |
aman.kumar |
137 |
log.info("trying to create user");
|
| 5036 |
rajveer |
138 |
user = userClient.createUser(user);
|
|
|
139 |
}catch (UserContextException ux){
|
| 20278 |
aman.kumar |
140 |
ux.printStackTrace();
|
|
|
141 |
log.info("exception in creating user");
|
| 5036 |
rajveer |
142 |
addActionError("User already exists with this email id.");
|
| 20202 |
aman.kumar |
143 |
json.put("message", "You are already a member. Please login.");
|
|
|
144 |
json.put("status", "error");
|
|
|
145 |
|
|
|
146 |
setRegisterJson(json.toString());
|
| 12616 |
anikendra |
147 |
// DataLogger.logData(EventType.REGISTER_FAILED_USER_EXISTS, getSessionId(), userinfo.getUserId(), email, "", email);
|
| 5036 |
rajveer |
148 |
return false;
|
| 1623 |
rajveer |
149 |
}
|
| 5036 |
rajveer |
150 |
|
|
|
151 |
long userId = user.getUserId();
|
|
|
152 |
userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
|
|
|
153 |
String pincode = userClient.getDefaultPincode(user.getUserId());
|
| 2982 |
rajveer |
154 |
|
| 5036 |
rajveer |
155 |
// TODO: setTotalItems shouldn't be a method on userinfo. This allows
|
|
|
156 |
// for potentially updating the item count wrongly. The method setCartId
|
|
|
157 |
// should update the item count as well. Also, there can be a method
|
|
|
158 |
// called refreshItemCount() that automatically updates the number of
|
|
|
159 |
// items currently in the cart.
|
|
|
160 |
if(userinfo.getUserId() != -1){
|
|
|
161 |
userClient.mergeCart(userinfo.getCartId(), user.getActiveCartId());
|
|
|
162 |
|
|
|
163 |
List<Long> items = userClient.getBrowseHistoryItems(userinfo.getUserId());
|
|
|
164 |
if(items != null){
|
|
|
165 |
for(Long itemId: items){
|
|
|
166 |
userClient.updateBrowseHistory(user.getUserId(), itemId);
|
|
|
167 |
}
|
| 1625 |
rajveer |
168 |
}
|
| 5036 |
rajveer |
169 |
|
|
|
170 |
items = userClient.getMyResearchItems(userinfo.getUserId());
|
|
|
171 |
if(items != null){
|
|
|
172 |
for(Long itemId: items){
|
|
|
173 |
userClient.updateMyResearch(user.getUserId(), itemId);
|
|
|
174 |
}
|
|
|
175 |
}
|
| 1625 |
rajveer |
176 |
}
|
| 5036 |
rajveer |
177 |
|
|
|
178 |
userinfo.setUserId(userId);
|
|
|
179 |
userinfo.setEmail(email);
|
|
|
180 |
userinfo.setLoggedIn(true);
|
|
|
181 |
userinfo.setPincode(pincode);
|
|
|
182 |
userinfo.setCartId(user.getActiveCartId());
|
| 11932 |
amit.gupta |
183 |
userinfo.setPrivateDealUser(isPrivateDealUser());
|
| 5036 |
rajveer |
184 |
Cart cart = userClient.getCart(userinfo.getCartId());
|
|
|
185 |
userinfo.setTotalItems(cart.getLinesSize());
|
|
|
186 |
userinfo.setTotalAmount(cart.getTotalPrice());
|
|
|
187 |
|
|
|
188 |
if (cookiesMap.containsKey(TrackingInterceptor.AFF_COOKIE)) {
|
|
|
189 |
long affId = Long.parseLong(cookiesMap.get(TrackingInterceptor.AFF_COOKIE).getValue());
|
|
|
190 |
userClient.addTrackLog(affId, userId, TrackLogType.NEW_REGISTRATION, "",email, (new Date()).getTime());
|
|
|
191 |
}
|
| 12616 |
anikendra |
192 |
// DataLogger.logData(EventType.REGISTER_SUCCESS, getSessionId(), userinfo.getUserId(), email, "", email);
|
| 20202 |
aman.kumar |
193 |
json.put("message", "Register successful");
|
|
|
194 |
json.put("status", "success");
|
|
|
195 |
|
|
|
196 |
setRegisterJson(json.toString());
|
| 5036 |
rajveer |
197 |
return true;
|
|
|
198 |
} catch (TTransportException e) {
|
| 5041 |
rajveer |
199 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
200 |
setRegisterJson(errorMessage);
|
| 5036 |
rajveer |
201 |
} catch (UserContextException e) {
|
| 5041 |
rajveer |
202 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
203 |
setRegisterJson(errorMessage);
|
| 5036 |
rajveer |
204 |
} catch (TException e) {
|
| 5041 |
rajveer |
205 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
206 |
setRegisterJson(errorMessage);
|
| 5036 |
rajveer |
207 |
} catch (UserAffiliateException e) {
|
| 5041 |
rajveer |
208 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
209 |
setRegisterJson(errorMessage);
|
| 5036 |
rajveer |
210 |
} catch (ShoppingCartException e) {
|
| 5041 |
rajveer |
211 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
212 |
setRegisterJson(errorMessage);
|
| 5036 |
rajveer |
213 |
} catch (WidgetException e) {
|
| 5041 |
rajveer |
214 |
log.error("Unable to register user." + e);
|
| 20202 |
aman.kumar |
215 |
setRegisterJson(errorMessage);
|
| 1623 |
rajveer |
216 |
}
|
| 20202 |
aman.kumar |
217 |
}catch(Exception e){
|
|
|
218 |
log.error("Unable to register user." + e);
|
|
|
219 |
setRegisterJson(errorMessage);
|
|
|
220 |
}
|
| 12616 |
anikendra |
221 |
// DataLogger.logData(EventType.REGISTER_FAILED_USER_EXISTS, getSessionId(), userinfo.getUserId(), email, "", email);
|
| 5036 |
rajveer |
222 |
return false;
|
| 1623 |
rajveer |
223 |
|
| 507 |
rajveer |
224 |
}
|
|
|
225 |
|
|
|
226 |
public String getRegistrationHeaderSnippet(){
|
|
|
227 |
return htmlSnippets.get("REGISTRATION_HEADER");
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
public String getRegistrationFormSnippet(){
|
|
|
231 |
return htmlSnippets.get("REGISTRATION_FORM");
|
|
|
232 |
}
|
| 1776 |
varun.gupt |
233 |
public String getRedirectUrl() {
|
|
|
234 |
return redirectUrl;
|
|
|
235 |
}
|
| 507 |
rajveer |
236 |
|
| 1776 |
varun.gupt |
237 |
public void setRedirectUrl(String redirectUrl) {
|
|
|
238 |
this.redirectUrl = redirectUrl;
|
|
|
239 |
}
|
| 11932 |
amit.gupta |
240 |
|
| 20202 |
aman.kumar |
241 |
|
|
|
242 |
|
|
|
243 |
public String getRegisterJson() {
|
|
|
244 |
return registerJson;
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
public void setRegisterJson(String registerJson) {
|
|
|
248 |
this.registerJson = registerJson;
|
|
|
249 |
}
|
|
|
250 |
|
| 11932 |
amit.gupta |
251 |
private boolean isPrivateDealUser() throws TTransportException, TException{
|
|
|
252 |
try {
|
|
|
253 |
in.shop2020.model.v1.user.UserContextService.Client uc = new UserClient().getClient();
|
|
|
254 |
return uc.isPrivateDealUser(userinfo.getUserId());
|
|
|
255 |
} catch (TTransportException e) {
|
|
|
256 |
log.error("Unable to get user service client.", e);
|
|
|
257 |
}
|
|
|
258 |
return false;
|
|
|
259 |
}
|
| 1776 |
varun.gupt |
260 |
}
|