| 410 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.shoppingcart.ShoppingCartException;
|
|
|
4 |
import in.shop2020.serving.controllers.BaseController;
|
|
|
5 |
import in.shop2020.serving.pages.PageContentKeys;
|
|
|
6 |
import in.shop2020.serving.pages.PageEnum;
|
|
|
7 |
import in.shop2020.serving.pages.PageManager;
|
| 419 |
rajveer |
8 |
import in.shop2020.serving.utils.Utils;
|
| 410 |
rajveer |
9 |
import in.shop2020.thrift.clients.ShoppingCartClient;
|
|
|
10 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
11 |
|
|
|
12 |
import java.util.*;
|
|
|
13 |
|
|
|
14 |
import org.apache.juli.logging.Log;
|
|
|
15 |
import org.apache.juli.logging.LogFactory;
|
|
|
16 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
17 |
import org.apache.struts2.convention.annotation.Results;
|
|
|
18 |
import org.apache.struts2.interceptor.ParameterAware;
|
|
|
19 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
20 |
import org.apache.struts2.rest.HttpHeaders;
|
|
|
21 |
import org.apache.thrift.TException;
|
|
|
22 |
|
| 507 |
rajveer |
23 |
public class CartController extends BaseController implements ParameterAware{
|
| 410 |
rajveer |
24 |
|
|
|
25 |
private static final long serialVersionUID = 1L;
|
| 507 |
rajveer |
26 |
private static Log log = LogFactory.getLog(CartController.class);
|
|
|
27 |
Map<String, String[]> reqparams = null;
|
| 410 |
rajveer |
28 |
|
|
|
29 |
private Map<String,String> htmlSnippets;
|
| 507 |
rajveer |
30 |
private PageManager pageManager = null;
|
| 410 |
rajveer |
31 |
|
|
|
32 |
public CartController(){
|
| 507 |
rajveer |
33 |
super();
|
|
|
34 |
pageManager = PageManager.getPageManager();
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
// GET /cart
|
|
|
38 |
public HttpHeaders index() {
|
|
|
39 |
long userId = 0;
|
|
|
40 |
boolean isSessionId = true;
|
|
|
41 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
| 410 |
rajveer |
42 |
|
| 507 |
rajveer |
43 |
if(userinfo.isLoggedIn()){
|
|
|
44 |
userId = userinfo.getUserId();
|
|
|
45 |
isSessionId = false;
|
|
|
46 |
}
|
|
|
47 |
else{
|
|
|
48 |
userId = userinfo.getSessionId();
|
|
|
49 |
isSessionId = true;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
params.put(PageContentKeys.CUSTOMER_ID, userId+"");
|
|
|
53 |
params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
|
|
|
54 |
params.put(PageContentKeys.CART_ID, userinfo.getCartId()+"");
|
|
|
55 |
|
|
|
56 |
params.put(PageContentKeys.ITEM_COUNT, userinfo.getTotalItems()+"");
|
| 517 |
rajveer |
57 |
params.put(PageContentKeys.USER_NAME, userinfo.getNameOfUser());
|
| 507 |
rajveer |
58 |
htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
|
|
|
59 |
|
|
|
60 |
return new DefaultHttpHeaders("index").disableCaching();
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// POST /entity
|
|
|
64 |
public String create() {
|
|
|
65 |
log.info("CartController.create");
|
|
|
66 |
|
|
|
67 |
printParams();
|
|
|
68 |
|
|
|
69 |
long userId = 0;
|
|
|
70 |
boolean isSessionId = true;
|
|
|
71 |
|
|
|
72 |
log.info("item id is " + this.reqparams.get("productid"));
|
|
|
73 |
|
|
|
74 |
String itemIds = "1000008_1000005";
|
|
|
75 |
if(this.reqparams.get("productid") != null){
|
|
|
76 |
itemIds = this.reqparams.get("productid")[0];
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
// log.info("list of item ids is " + this.request.getParameter("productid"));
|
|
|
81 |
// String itemIds = this.request.getParameter("productid");
|
|
|
82 |
|
|
|
83 |
if(userinfo.isLoggedIn()){
|
|
|
84 |
userId = userinfo.getUserId();
|
|
|
85 |
isSessionId = false;
|
|
|
86 |
}
|
|
|
87 |
else{
|
|
|
88 |
userId = userinfo.getSessionId();
|
|
|
89 |
isSessionId = true;
|
|
|
90 |
}
|
|
|
91 |
long cartId = 0;
|
|
|
92 |
|
|
|
93 |
StringTokenizer tokenizer = new StringTokenizer(itemIds,"_");
|
|
|
94 |
int numberOfItems = tokenizer.countTokens();
|
|
|
95 |
while(tokenizer.hasMoreTokens()){
|
|
|
96 |
long itemId = Long.parseLong(tokenizer.nextToken());
|
| 536 |
rajveer |
97 |
//cartId = Utils.addItemToCart(itemId, userId, isSessionId);
|
|
|
98 |
|
|
|
99 |
try {
|
|
|
100 |
ShoppingCartClient shoppingCartClient = new ShoppingCartClient();
|
|
|
101 |
in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = shoppingCartClient.getClient();
|
|
|
102 |
cartId = cartClient.createCart(userId, isSessionId);
|
|
|
103 |
cartClient.addItemToCart(cartId, itemId, 1);
|
|
|
104 |
|
|
|
105 |
} catch (ShoppingCartException e) {
|
|
|
106 |
// TODO Auto-generated catch block
|
|
|
107 |
e.printStackTrace();
|
|
|
108 |
} catch (TException e) {
|
|
|
109 |
// TODO Auto-generated catch block
|
|
|
110 |
e.printStackTrace();
|
|
|
111 |
} catch (Exception e) {
|
|
|
112 |
// TODO Auto-generated catch block
|
|
|
113 |
e.printStackTrace();
|
|
|
114 |
}
|
|
|
115 |
|
| 507 |
rajveer |
116 |
}
|
|
|
117 |
|
|
|
118 |
//long cartId = Utils.addItemToCart(Long.parseLong(itemId), userId, isSessionId);
|
|
|
119 |
|
|
|
120 |
userinfo.setCartId(cartId);
|
|
|
121 |
userinfo.setTotalItems(Utils.getNumberOfItemsInCart(cartId) );
|
|
|
122 |
|
|
|
123 |
this.session.setAttribute("userinfo", userinfo);
|
|
|
124 |
|
|
|
125 |
return "success";
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
|
|
|
129 |
// DELETE /entity
|
|
|
130 |
public String destroy() {
|
|
|
131 |
log.info("CartController.destroy");
|
|
|
132 |
printParams();
|
|
|
133 |
log.info("item id is " + this.request.getParameter("productid"));
|
|
|
134 |
String itemIdString = this.request.getParameter("productid");
|
|
|
135 |
long itemId = Long.parseLong(itemIdString);
|
| 517 |
rajveer |
136 |
if(userinfo.getCartId() == -1){
|
|
|
137 |
log.info("Cart does not exist. Nothing to delete.");
|
|
|
138 |
}else{
|
|
|
139 |
if(Utils.deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId())){
|
|
|
140 |
userinfo.setTotalItems(userinfo.getTotalItems() - 1 );
|
|
|
141 |
setUserSessionInfo();
|
|
|
142 |
return "delsuccess";
|
|
|
143 |
}
|
| 507 |
rajveer |
144 |
}
|
|
|
145 |
return "delfailure";
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
// DELETE /entity
|
|
|
150 |
public String update() {
|
|
|
151 |
log.info("CartController.update");
|
|
|
152 |
printParams();
|
|
|
153 |
log.info("item id is " + this.request.getParameter("productid"));
|
|
|
154 |
log.info("item id is " + this.request.getParameter("quantity"));
|
|
|
155 |
String itemIdString = this.request.getParameter("productid");
|
|
|
156 |
String quantityString = this.request.getParameter("quantity");
|
|
|
157 |
long itemId = Long.parseLong(itemIdString);
|
|
|
158 |
long quantity = Long.parseLong(quantityString);
|
| 517 |
rajveer |
159 |
if(quantity <= 0){
|
|
|
160 |
log.info("Not valid item quantity. Unable to change item quantity.");
|
|
|
161 |
}else{
|
|
|
162 |
if(Utils.updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity)){
|
|
|
163 |
return "delsuccess";
|
|
|
164 |
}
|
| 507 |
rajveer |
165 |
}
|
|
|
166 |
return "delfailure";
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
public void printParams(){
|
|
|
171 |
for(String param : reqparams.keySet()) {
|
|
|
172 |
log.info("param name is " + param);
|
|
|
173 |
log.info("param first is " + reqparams.get(param)[0]);
|
|
|
174 |
}
|
|
|
175 |
log.info(this.reqparams);
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
@Override
|
|
|
179 |
public void setParameters(Map<String, String[]> reqmap) {
|
|
|
180 |
log.info("setParameters:" + reqmap);
|
|
|
181 |
|
|
|
182 |
this.reqparams = reqmap;
|
| 410 |
rajveer |
183 |
}
|
| 507 |
rajveer |
184 |
|
|
|
185 |
public String getHeaderSnippet(){
|
|
|
186 |
return htmlSnippets.get("HEADER");
|
|
|
187 |
}
|
| 410 |
rajveer |
188 |
|
| 507 |
rajveer |
189 |
public String getMainMenuSnippet(){
|
|
|
190 |
return htmlSnippets.get("MAIN_MENU");
|
|
|
191 |
}
|
| 410 |
rajveer |
192 |
|
| 507 |
rajveer |
193 |
public String getSearchBarSnippet(){
|
|
|
194 |
return htmlSnippets.get("SEARCH_BAR");
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
|
|
|
198 |
public String getCustomerServiceSnippet(){
|
|
|
199 |
return htmlSnippets.get("CUSTOMER_SERVICE");
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
public String getCartHeaderSnippet(){
|
|
|
203 |
return htmlSnippets.get("CART_HEADER");
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
public String getCartDetailsSnippet(){
|
|
|
207 |
return htmlSnippets.get("CART_DETAILS");
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
public String getMyResearchSnippet(){
|
|
|
211 |
return htmlSnippets.get("MY_RESEARCH");
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
public String getFooterSnippet(){
|
|
|
215 |
return htmlSnippets.get("FOOTER");
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
public String getJsFileSnippet(){
|
|
|
219 |
return htmlSnippets.get("JS_FILES");
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
public String getCssFileSnippet(){
|
|
|
223 |
return htmlSnippets.get("CSS_FILES");
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
public long getNumberOfItems(){
|
|
|
227 |
return userinfo.getTotalItems();
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
/*
|
| 419 |
rajveer |
231 |
// GET /cart
|
| 507 |
rajveer |
232 |
public HttpHeaders index() {
|
|
|
233 |
changed on 18 august
|
| 410 |
rajveer |
234 |
Cookie loginCookie = new Cookie("userId", "Rajveer");
|
|
|
235 |
Map cookiesMap = new HashMap();
|
|
|
236 |
cookiesMap.put("USER_ID", loginCookie);
|
|
|
237 |
setCookiesMap(cookiesMap);
|
|
|
238 |
|
|
|
239 |
log.info( "Cookies map is " + this.getCookiesMap());
|
|
|
240 |
log.info("CartController.index");
|
|
|
241 |
|
|
|
242 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
|
|
243 |
|
| 416 |
rajveer |
244 |
long userId = 0;
|
|
|
245 |
boolean isSessionId = true;
|
| 419 |
rajveer |
246 |
//if(this.session.getAttribute("loggedin")!=null){
|
|
|
247 |
if(this.session.getAttribute("loggedin").toString().equals("true")){
|
|
|
248 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
249 |
isSessionId = false;
|
|
|
250 |
}else{
|
|
|
251 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
252 |
isSessionId = true;
|
|
|
253 |
}
|
|
|
254 |
// }else{
|
|
|
255 |
// System.out.println("No User is logged in. Redirect to login page.");
|
|
|
256 |
// return new DefaultHttpHeaders("fatal");
|
|
|
257 |
//
|
|
|
258 |
// }
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
|
|
|
262 |
params.put(PageContentKeys.CUSTOMER_ID, userId+"");
|
|
|
263 |
params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
|
|
|
264 |
|
| 507 |
rajveer |
265 |
|
| 419 |
rajveer |
266 |
long userId = 0;
|
|
|
267 |
boolean isSessionId = true;
|
|
|
268 |
Map<PageContentKeys, String> params = new HashMap<PageContentKeys, String>();
|
| 416 |
rajveer |
269 |
|
| 419 |
rajveer |
270 |
if(userinfo.isLoggedIn()){
|
|
|
271 |
userId = userinfo.getUserId();
|
|
|
272 |
isSessionId = false;
|
|
|
273 |
}
|
|
|
274 |
else{
|
|
|
275 |
userId = userinfo.getSessionId();
|
|
|
276 |
isSessionId = true;
|
|
|
277 |
}
|
|
|
278 |
|
| 416 |
rajveer |
279 |
params.put(PageContentKeys.CUSTOMER_ID, userId+"");
|
|
|
280 |
params.put(PageContentKeys.IS_SESSION_ID, isSessionId+"");
|
|
|
281 |
|
| 410 |
rajveer |
282 |
htmlSnippets = PageManager.getPageManager().getPageContents(PageEnum.SHOPPING_CART_PAGE, params);
|
|
|
283 |
|
|
|
284 |
|
|
|
285 |
|
|
|
286 |
return new DefaultHttpHeaders("index").disableCaching();
|
|
|
287 |
}
|
| 507 |
rajveer |
288 |
*/
|
| 410 |
rajveer |
289 |
// POST /entity
|
| 507 |
rajveer |
290 |
/*
|
| 410 |
rajveer |
291 |
public String create() {
|
| 507 |
rajveer |
292 |
|
| 410 |
rajveer |
293 |
log.info("CartController.create");
|
|
|
294 |
printParams();
|
|
|
295 |
if(getCookiesMap() != null){
|
|
|
296 |
Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
|
|
|
297 |
log.info("login cookie name is " + loginCookie.getName() );
|
|
|
298 |
log.info("login cookie value is " + loginCookie.getValue());
|
|
|
299 |
}
|
|
|
300 |
log.info("item id is " + this.reqparams.get("item_id"));
|
|
|
301 |
itemId = this.reqparams.get("item_id")[0];
|
|
|
302 |
|
|
|
303 |
addItemToCart(itemId);
|
| 416 |
rajveer |
304 |
long userId = 0;
|
|
|
305 |
boolean isSessionId = true;
|
|
|
306 |
if(this.session.getAttribute("loggedin").toString().equals("true")){
|
|
|
307 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
308 |
isSessionId = false;
|
|
|
309 |
}else{
|
|
|
310 |
userId = Long.parseLong(this.session.getAttribute("userid").toString());
|
|
|
311 |
isSessionId = true;
|
|
|
312 |
}
|
|
|
313 |
addItemToCart(Long.parseLong(itemId), userId, isSessionId);
|
| 410 |
rajveer |
314 |
// Add data to the cart
|
| 507 |
rajveer |
315 |
|
| 419 |
rajveer |
316 |
long userId = 0;
|
|
|
317 |
boolean isSessionId = true;
|
| 410 |
rajveer |
318 |
|
| 449 |
rajveer |
319 |
log.info("item id is " + this.reqparams.get("productid"));
|
| 419 |
rajveer |
320 |
|
| 449 |
rajveer |
321 |
String itemIds = "1000008_1000005";
|
|
|
322 |
if(this.reqparams.get("productid") != null){
|
|
|
323 |
itemIds = this.reqparams.get("productid")[0];
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
|
|
|
327 |
// log.info("list of item ids is " + this.request.getParameter("productid"));
|
|
|
328 |
// String itemIds = this.request.getParameter("productid");
|
|
|
329 |
|
| 419 |
rajveer |
330 |
if(userinfo.isLoggedIn()){
|
|
|
331 |
userId = userinfo.getUserId();
|
|
|
332 |
isSessionId = false;
|
|
|
333 |
}
|
|
|
334 |
else{
|
|
|
335 |
userId = userinfo.getSessionId();
|
|
|
336 |
isSessionId = true;
|
|
|
337 |
}
|
| 449 |
rajveer |
338 |
long cartId = 0;
|
|
|
339 |
|
|
|
340 |
StringTokenizer tokenizer = new StringTokenizer(itemIds,"_");
|
|
|
341 |
int numberOfItems = tokenizer.countTokens();
|
|
|
342 |
while(tokenizer.hasMoreTokens()){
|
|
|
343 |
long itemId = Long.parseLong(tokenizer.nextToken());
|
|
|
344 |
cartId = Utils.addItemToCart(itemId, userId, isSessionId);
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
//long cartId = Utils.addItemToCart(Long.parseLong(itemId), userId, isSessionId);
|
| 419 |
rajveer |
348 |
|
|
|
349 |
userinfo.setCartId(cartId);
|
| 449 |
rajveer |
350 |
userinfo.setTotalItems(Utils.getNumberOfItemsInCart(cartId) );
|
| 419 |
rajveer |
351 |
|
|
|
352 |
this.session.setAttribute("userinfo", userinfo);
|
|
|
353 |
|
| 410 |
rajveer |
354 |
return "success";
|
|
|
355 |
}
|
|
|
356 |
|
| 507 |
rajveer |
357 |
|
| 410 |
rajveer |
358 |
public void printParams(){
|
|
|
359 |
for(String param : reqparams.keySet()) {
|
|
|
360 |
log.info("param name is " + param);
|
|
|
361 |
log.info("param first is " + reqparams.get(param)[0]);
|
|
|
362 |
}
|
|
|
363 |
log.info(this.reqparams);
|
|
|
364 |
}
|
|
|
365 |
|
|
|
366 |
private boolean addItemToCart(String itemId){
|
|
|
367 |
log.info("Session Id is " + request.getSession().getId());
|
|
|
368 |
log.info("Item Id is " + itemId);
|
|
|
369 |
return true;
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
|
| 416 |
rajveer |
373 |
private void addItemToCart(long catalogItemId, long userId, boolean isSessionId){
|
| 410 |
rajveer |
374 |
UserContextServiceClient userContextServiceClient = null;
|
|
|
375 |
in.shop2020.model.v1.user.UserContextService.Client userClient = null;
|
|
|
376 |
|
|
|
377 |
ShoppingCartClient shoppingCartClient = null;
|
|
|
378 |
in.shop2020.model.v1.shoppingcart.ShoppingCartService.Client cartClient = null;
|
|
|
379 |
long cartId = -1;
|
|
|
380 |
|
|
|
381 |
try {
|
|
|
382 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
383 |
userClient = userContextServiceClient.getClient();
|
|
|
384 |
|
|
|
385 |
shoppingCartClient = new ShoppingCartClient();
|
|
|
386 |
cartClient = shoppingCartClient.getClient();
|
| 416 |
rajveer |
387 |
|
|
|
388 |
cartId = cartClient.createCart(userId, isSessionId);
|
| 410 |
rajveer |
389 |
cartClient.addItemToCart(cartId, catalogItemId, 1);
|
| 419 |
rajveer |
390 |
|
|
|
391 |
int NumberOfLines = cartClient.getShadowCart(cartId).getLinesSize();
|
|
|
392 |
this.session.setAttribute("totalitems", NumberOfLines+"");
|
|
|
393 |
|
|
|
394 |
|
| 410 |
rajveer |
395 |
} catch (ShoppingCartException e) {
|
|
|
396 |
// TODO Auto-generated catch block
|
|
|
397 |
e.printStackTrace();
|
|
|
398 |
} catch (TException e) {
|
|
|
399 |
// TODO Auto-generated catch block
|
|
|
400 |
e.printStackTrace();
|
|
|
401 |
} catch (Exception e) {
|
|
|
402 |
// TODO Auto-generated catch block
|
|
|
403 |
e.printStackTrace();
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
//if user is logged in create new cart
|
|
|
407 |
//if( userClient.getState(userId, false).isIsLoggedIn()){
|
|
|
408 |
}
|
|
|
409 |
|
| 458 |
rajveer |
410 |
public long getNumberOfItems(){
|
| 449 |
rajveer |
411 |
return userinfo.getTotalItems();
|
|
|
412 |
}
|
|
|
413 |
|
| 410 |
rajveer |
414 |
public String getShoppingCartSnippets()
|
|
|
415 |
{
|
|
|
416 |
return htmlSnippets.get("SHOPPING_CART");
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
@Override
|
|
|
420 |
public void setParameters(Map<String, String[]> reqmap) {
|
|
|
421 |
log.info("setParameters:" + reqmap);
|
|
|
422 |
|
|
|
423 |
this.reqparams = reqmap;
|
|
|
424 |
}
|
| 507 |
rajveer |
425 |
*/
|
| 416 |
rajveer |
426 |
//
|
|
|
427 |
// @Override
|
|
|
428 |
// public void setServletRequest(HttpServletRequest request) {
|
|
|
429 |
// this.request = request;
|
|
|
430 |
// }
|
|
|
431 |
//
|
| 507 |
rajveer |
432 |
|