| 405 |
rajveer |
1 |
/**
|
|
|
2 |
*
|
|
|
3 |
*/
|
|
|
4 |
package in.shop2020.serving.controllers;
|
|
|
5 |
|
| 413 |
rajveer |
6 |
import in.shop2020.model.v1.user.Address;
|
|
|
7 |
import in.shop2020.model.v1.user.UserContext;
|
|
|
8 |
import in.shop2020.model.v1.user.UserPrimaryInfo;
|
|
|
9 |
import in.shop2020.model.v1.user.UserState;
|
| 405 |
rajveer |
10 |
import in.shop2020.serving.pages.PageContentKeys;
|
|
|
11 |
import in.shop2020.serving.pages.PageEnum;
|
|
|
12 |
import in.shop2020.serving.pages.PageManager;
|
| 413 |
rajveer |
13 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
| 405 |
rajveer |
14 |
|
|
|
15 |
import java.io.UnsupportedEncodingException;
|
| 413 |
rajveer |
16 |
import java.util.Date;
|
| 405 |
rajveer |
17 |
import java.util.HashMap;
|
|
|
18 |
import java.util.Map;
|
|
|
19 |
|
|
|
20 |
import org.apache.juli.logging.Log;
|
|
|
21 |
import org.apache.juli.logging.LogFactory;
|
| 413 |
rajveer |
22 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
23 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
24 |
import org.apache.struts2.interceptor.ParameterAware;
|
| 405 |
rajveer |
25 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
26 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
27 |
|
|
|
28 |
/**
|
| 413 |
rajveer |
29 |
* @author rajveer
|
| 405 |
rajveer |
30 |
*
|
|
|
31 |
*/
|
| 413 |
rajveer |
32 |
|
| 405 |
rajveer |
33 |
public class MyaccountController extends BaseController
|
| 419 |
rajveer |
34 |
implements ParameterAware {
|
| 405 |
rajveer |
35 |
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
*
|
|
|
39 |
*/
|
|
|
40 |
private static Log log = LogFactory.getLog(MyaccountController.class);
|
|
|
41 |
|
|
|
42 |
private Map<String,String> htmlSnippets;
|
|
|
43 |
/**
|
|
|
44 |
*
|
|
|
45 |
*/
|
|
|
46 |
|
| 419 |
rajveer |
47 |
|
| 413 |
rajveer |
48 |
private Map<String, String[]> reqparams;
|
|
|
49 |
|
| 405 |
rajveer |
50 |
private String id;
|
|
|
51 |
|
| 413 |
rajveer |
52 |
private int action;
|
|
|
53 |
|
|
|
54 |
|
| 405 |
rajveer |
55 |
public MyaccountController() {
|
|
|
56 |
super();
|
|
|
57 |
}
|
|
|
58 |
|
| 424 |
rajveer |
59 |
// POST /myaccount
|
| 413 |
rajveer |
60 |
public String create() {
|
|
|
61 |
log.info("MyAccountController.create");
|
|
|
62 |
log.info("action type is " + this.reqparams.get("action"));
|
|
|
63 |
|
|
|
64 |
action = Integer.parseInt(this.reqparams.get("action")[0]);
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
try {
|
| 419 |
rajveer |
68 |
if(addUpdateUserDetails(action)){
|
| 413 |
rajveer |
69 |
return "success";
|
|
|
70 |
}else{
|
|
|
71 |
return "failure";
|
|
|
72 |
}
|
|
|
73 |
} catch (Exception e) {
|
|
|
74 |
// TODO Auto-generated catch block
|
|
|
75 |
e.printStackTrace();
|
|
|
76 |
}
|
|
|
77 |
return "failure";
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
|
| 405 |
rajveer |
81 |
// GET /test
|
|
|
82 |
public HttpHeaders index() throws UnsupportedEncodingException {
|
| 507 |
rajveer |
83 |
log.info("this.request=" + this.request);
|
| 419 |
rajveer |
84 |
|
|
|
85 |
String userId = "";
|
|
|
86 |
|
|
|
87 |
if(this.userinfo.isLoggedIn()){
|
|
|
88 |
userId = (new Long(this.userinfo.getUserId())).toString();
|
|
|
89 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
|
|
90 |
params.put(PageContentKeys.CUSTOMER_ID, userId);
|
| 507 |
rajveer |
91 |
params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
|
| 419 |
rajveer |
92 |
htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
|
|
|
93 |
|
|
|
94 |
return new DefaultHttpHeaders("index").disableCaching();
|
|
|
95 |
|
|
|
96 |
}else{
|
|
|
97 |
return new DefaultHttpHeaders("login").disableCaching();
|
|
|
98 |
}
|
|
|
99 |
|
| 405 |
rajveer |
100 |
}
|
|
|
101 |
|
|
|
102 |
public HttpHeaders show(){
|
|
|
103 |
log.info("this.id=" + this.id);
|
| 507 |
rajveer |
104 |
log.info("this.id=" + userinfo.getTotalItems());
|
| 405 |
rajveer |
105 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
|
|
106 |
params.put(PageContentKeys.CUSTOMER_ID, id);
|
| 507 |
rajveer |
107 |
params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
|
| 405 |
rajveer |
108 |
htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.MY_ACCOUNT_PAGE, params);
|
|
|
109 |
|
| 419 |
rajveer |
110 |
return new DefaultHttpHeaders("index").disableCaching();
|
| 405 |
rajveer |
111 |
}
|
|
|
112 |
|
| 507 |
rajveer |
113 |
public String getHeaderSnippet(){
|
|
|
114 |
return htmlSnippets.get("HEADER");
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
public String getMainMenuSnippet(){
|
|
|
118 |
return htmlSnippets.get("MAIN_MENU");
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
public String getSearchBarSnippet(){
|
|
|
122 |
return htmlSnippets.get("SEARCH_BAR");
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
public String getCustomerServiceSnippet(){
|
|
|
127 |
return htmlSnippets.get("CUSTOMER_SERVICE");
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public String getMyaccountHeaderSnippet(){
|
|
|
131 |
return htmlSnippets.get("MYACCOUNT_HEADER");
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
public String getMyaccountDetailsSnippet(){
|
|
|
135 |
return htmlSnippets.get("MYACCOUNT_DETAILS");
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
public String getMyResearchSnippet(){
|
|
|
139 |
return htmlSnippets.get("MY_RESEARCH");
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public String getFooterSnippet(){
|
|
|
143 |
return htmlSnippets.get("FOOTER");
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
public String getJsFileSnippet(){
|
|
|
147 |
return htmlSnippets.get("JS_FILES");
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
public String getCssFileSnippet(){
|
|
|
151 |
return htmlSnippets.get("CSS_FILES");
|
|
|
152 |
}
|
|
|
153 |
|
| 405 |
rajveer |
154 |
/**
|
|
|
155 |
*
|
|
|
156 |
* @param id
|
|
|
157 |
*/
|
|
|
158 |
public void setId(String id) {
|
|
|
159 |
this.id = id;
|
|
|
160 |
}
|
|
|
161 |
|
| 419 |
rajveer |
162 |
public boolean logoutUser(int userId) throws Exception{
|
|
|
163 |
UserContextServiceClient userContextServiceClient = null;
|
|
|
164 |
in.shop2020.model.v1.user.UserContextService.Client userClient = null;
|
|
|
165 |
|
|
|
166 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
167 |
userClient = userContextServiceClient.getClient();
|
|
|
168 |
|
|
|
169 |
userClient.setUserAsLoggedOut(userId, (new Date()).getTime());
|
|
|
170 |
|
|
|
171 |
return true;
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
public boolean addUpdateUserDetails(int action) throws Exception{
|
| 413 |
rajveer |
175 |
|
|
|
176 |
UserContextServiceClient userContextServiceClient = null;
|
|
|
177 |
in.shop2020.model.v1.user.UserContextService.Client userClient = null;
|
|
|
178 |
|
|
|
179 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
180 |
userClient = userContextServiceClient.getClient();
|
|
|
181 |
|
|
|
182 |
switch (action) {
|
|
|
183 |
case 1:
|
|
|
184 |
String email = this.reqparams.get("email")[0];
|
|
|
185 |
String password = this.reqparams.get("password")[0];
|
|
|
186 |
|
|
|
187 |
if(userClient.userExists(email)){
|
|
|
188 |
return false;
|
|
|
189 |
}else{
|
|
|
190 |
UserContext context = new UserContext();
|
|
|
191 |
UserPrimaryInfo primaryInfo = new UserPrimaryInfo();
|
|
|
192 |
UserState userState = new UserState();
|
|
|
193 |
|
|
|
194 |
primaryInfo.setPassword(password);
|
|
|
195 |
primaryInfo.setEmail(email);
|
|
|
196 |
userState.setIsLoggedIn(true);
|
|
|
197 |
|
|
|
198 |
context.setPrimaryInfo(primaryInfo);
|
|
|
199 |
context.setUserState(userState);
|
|
|
200 |
|
|
|
201 |
context = userClient.createContext(context, false);
|
| 419 |
rajveer |
202 |
|
|
|
203 |
long userId = context.getId();
|
|
|
204 |
|
|
|
205 |
userClient.setUserAsLoggedIn(userId, (new Date()).getTime());
|
|
|
206 |
|
| 424 |
rajveer |
207 |
this.userinfo.setUserId(userId);
|
|
|
208 |
this.userinfo.setEmail(email);
|
|
|
209 |
this.userinfo.setLoggedIn(true);
|
| 419 |
rajveer |
210 |
|
| 413 |
rajveer |
211 |
return true;
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
//add address
|
|
|
215 |
case 2:
|
|
|
216 |
long userId = Long.parseLong(this.reqparams.get("user_id")[0]);
|
| 424 |
rajveer |
217 |
String name = this.reqparams.get("name")[0];
|
| 413 |
rajveer |
218 |
String line1 = this.reqparams.get("line1")[0];
|
|
|
219 |
String line2 = this.reqparams.get("line2")[0];
|
|
|
220 |
String landmark = this.reqparams.get("landmark")[0];
|
|
|
221 |
String city = this.reqparams.get("city")[0];
|
|
|
222 |
String state = this.reqparams.get("state")[0];
|
|
|
223 |
String pin = this.reqparams.get("pin")[0];
|
|
|
224 |
String country = this.reqparams.get("country")[0];
|
| 424 |
rajveer |
225 |
String phone = this.reqparams.get("phone")[0];
|
| 413 |
rajveer |
226 |
|
|
|
227 |
Address address = new Address();
|
| 424 |
rajveer |
228 |
address.setName(name);
|
| 413 |
rajveer |
229 |
address.setLine1(line1);
|
| 424 |
rajveer |
230 |
address.setLine2(line2);
|
| 413 |
rajveer |
231 |
address.setLandmark(landmark);
|
|
|
232 |
address.setCity(city);
|
|
|
233 |
address.setState(state);
|
|
|
234 |
address.setPin(pin);
|
|
|
235 |
address.setCountry(country);
|
| 424 |
rajveer |
236 |
address.setPhone(phone);
|
| 413 |
rajveer |
237 |
long timestamp = (new Date()).getTime();
|
|
|
238 |
|
| 517 |
rajveer |
239 |
if(userClient.addAddressForUser(address, userId, timestamp, false)){
|
| 413 |
rajveer |
240 |
return true;
|
|
|
241 |
} else{
|
|
|
242 |
return false;
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
|
|
|
246 |
case 3:
|
|
|
247 |
|
|
|
248 |
break;
|
|
|
249 |
|
|
|
250 |
default:
|
|
|
251 |
break;
|
|
|
252 |
}
|
|
|
253 |
return false;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
|
| 405 |
rajveer |
257 |
|
|
|
258 |
/**
|
|
|
259 |
*
|
|
|
260 |
*/
|
| 419 |
rajveer |
261 |
// @Override
|
|
|
262 |
// public void setServletRequest(HttpServletRequest request) {
|
|
|
263 |
// this.request = request;
|
|
|
264 |
// }
|
| 405 |
rajveer |
265 |
|
|
|
266 |
|
| 413 |
rajveer |
267 |
@Override
|
|
|
268 |
public void setParameters(Map<String, String[]> reqmap) {
|
|
|
269 |
log.info("setParameters:" + reqmap);
|
|
|
270 |
|
|
|
271 |
this.reqparams = reqmap;
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
|
| 405 |
rajveer |
275 |
public String getMyAccountSnippets(){
|
|
|
276 |
return htmlSnippets.get("My_ACCOUNT");
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
public String getMyOrdersSnippets(){
|
|
|
280 |
return htmlSnippets.get("My_ORDERS");
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
}
|