| 410 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
| 650 |
rajveer |
3 |
import java.util.Map;
|
|
|
4 |
import java.util.StringTokenizer;
|
|
|
5 |
|
| 555 |
chandransh |
6 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
| 572 |
chandransh |
7 |
import in.shop2020.model.v1.user.UserContextService;
|
| 410 |
rajveer |
8 |
import in.shop2020.serving.controllers.BaseController;
|
| 419 |
rajveer |
9 |
import in.shop2020.serving.utils.Utils;
|
| 410 |
rajveer |
10 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
11 |
|
|
|
12 |
import org.apache.juli.logging.Log;
|
|
|
13 |
import org.apache.juli.logging.LogFactory;
|
|
|
14 |
import org.apache.struts2.interceptor.ParameterAware;
|
|
|
15 |
import org.apache.thrift.TException;
|
|
|
16 |
|
| 507 |
rajveer |
17 |
public class CartController extends BaseController implements ParameterAware{
|
| 410 |
rajveer |
18 |
|
|
|
19 |
private static final long serialVersionUID = 1L;
|
| 507 |
rajveer |
20 |
private static Log log = LogFactory.getLog(CartController.class);
|
|
|
21 |
Map<String, String[]> reqparams = null;
|
| 410 |
rajveer |
22 |
|
| 650 |
rajveer |
23 |
|
| 572 |
chandransh |
24 |
private String errorMsg = "";
|
| 410 |
rajveer |
25 |
|
|
|
26 |
public CartController(){
|
| 507 |
rajveer |
27 |
super();
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
// GET /cart
|
| 650 |
rajveer |
31 |
public String index() {
|
| 572 |
chandransh |
32 |
try {
|
|
|
33 |
UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
|
|
|
34 |
if(!userClient.validateCart(userinfo.getCartId()))
|
|
|
35 |
errorMsg = "Your cart has been updated.";
|
|
|
36 |
} catch (Exception e) {
|
|
|
37 |
// This exception can be ignored for showing the cart. Not so
|
|
|
38 |
// innocent when this occurs at the time of checkout or when the
|
|
|
39 |
// user is proceeding to pay.
|
|
|
40 |
e.printStackTrace();
|
|
|
41 |
}
|
| 410 |
rajveer |
42 |
|
| 650 |
rajveer |
43 |
htmlSnippets.put("CART_HEADER", pageLoader.getCartHeaderHtml());
|
|
|
44 |
htmlSnippets.put("CART_DETAILS", pageLoader.getCartDetailsHtml(userinfo.getCartId(), errorMsg));
|
|
|
45 |
return "index";
|
| 507 |
rajveer |
46 |
}
|
|
|
47 |
|
| 572 |
chandransh |
48 |
// POST /entity
|
|
|
49 |
public String create() {
|
|
|
50 |
log.info("CartController.create");
|
| 555 |
chandransh |
51 |
|
| 572 |
chandransh |
52 |
printParams();
|
|
|
53 |
|
|
|
54 |
long userId = userinfo.getUserId();
|
|
|
55 |
long cartId = userinfo.getCartId();
|
|
|
56 |
|
|
|
57 |
log.info("item id is " + this.reqparams.get("productid"));
|
|
|
58 |
|
| 637 |
rajveer |
59 |
String itemIds = "";
|
| 572 |
chandransh |
60 |
if (this.reqparams.get("productid") != null) {
|
|
|
61 |
itemIds = this.reqparams.get("productid")[0];
|
| 637 |
rajveer |
62 |
}else{
|
|
|
63 |
return "failure";
|
| 572 |
chandransh |
64 |
}
|
| 637 |
rajveer |
65 |
|
| 572 |
chandransh |
66 |
|
|
|
67 |
StringTokenizer tokenizer = new StringTokenizer(itemIds, "_");
|
|
|
68 |
while (tokenizer.hasMoreTokens()) {
|
|
|
69 |
long itemId = Long.parseLong(tokenizer.nextToken());
|
|
|
70 |
|
|
|
71 |
try {
|
|
|
72 |
UserContextServiceClient userServiceClient = new UserContextServiceClient();
|
|
|
73 |
UserContextService.Client userClient = userServiceClient.getClient();
|
|
|
74 |
if (cartId == 0)
|
| 555 |
chandransh |
75 |
cartId = userClient.createCart(userId);
|
| 572 |
chandransh |
76 |
userClient.addItemToCart(cartId, itemId, 1);
|
| 536 |
rajveer |
77 |
|
| 572 |
chandransh |
78 |
} catch (ShoppingCartException e) {
|
|
|
79 |
e.printStackTrace();
|
|
|
80 |
} catch (TException e) {
|
|
|
81 |
e.printStackTrace();
|
|
|
82 |
} catch (Exception e) {
|
|
|
83 |
e.printStackTrace();
|
| 507 |
rajveer |
84 |
}
|
|
|
85 |
|
| 572 |
chandransh |
86 |
}
|
| 507 |
rajveer |
87 |
|
| 572 |
chandransh |
88 |
userinfo.setCartId(cartId);
|
|
|
89 |
userinfo.setTotalItems(Utils.getNumberOfItemsInCart(cartId));
|
|
|
90 |
|
|
|
91 |
return "success";
|
|
|
92 |
}
|
|
|
93 |
|
| 507 |
rajveer |
94 |
// DELETE /entity
|
|
|
95 |
public String destroy() {
|
|
|
96 |
log.info("CartController.destroy");
|
|
|
97 |
printParams();
|
|
|
98 |
log.info("item id is " + this.request.getParameter("productid"));
|
|
|
99 |
String itemIdString = this.request.getParameter("productid");
|
|
|
100 |
long itemId = Long.parseLong(itemIdString);
|
| 517 |
rajveer |
101 |
if(userinfo.getCartId() == -1){
|
|
|
102 |
log.info("Cart does not exist. Nothing to delete.");
|
|
|
103 |
}else{
|
|
|
104 |
if(Utils.deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId())){
|
| 649 |
chandransh |
105 |
userinfo.setTotalItems(Utils.getNumberOfItemsInCart(userinfo.getCartId()));
|
| 517 |
rajveer |
106 |
return "delsuccess";
|
|
|
107 |
}
|
| 507 |
rajveer |
108 |
}
|
|
|
109 |
return "delfailure";
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
// DELETE /entity
|
|
|
114 |
public String update() {
|
|
|
115 |
log.info("CartController.update");
|
|
|
116 |
printParams();
|
|
|
117 |
log.info("item id is " + this.request.getParameter("productid"));
|
|
|
118 |
log.info("item id is " + this.request.getParameter("quantity"));
|
|
|
119 |
String itemIdString = this.request.getParameter("productid");
|
|
|
120 |
String quantityString = this.request.getParameter("quantity");
|
|
|
121 |
long itemId = Long.parseLong(itemIdString);
|
|
|
122 |
long quantity = Long.parseLong(quantityString);
|
| 517 |
rajveer |
123 |
if(quantity <= 0){
|
|
|
124 |
log.info("Not valid item quantity. Unable to change item quantity.");
|
|
|
125 |
}else{
|
|
|
126 |
if(Utils.updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity)){
|
|
|
127 |
return "delsuccess";
|
|
|
128 |
}
|
| 507 |
rajveer |
129 |
}
|
|
|
130 |
return "delfailure";
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
|
|
|
134 |
public void printParams(){
|
|
|
135 |
for(String param : reqparams.keySet()) {
|
|
|
136 |
log.info("param name is " + param);
|
|
|
137 |
log.info("param first is " + reqparams.get(param)[0]);
|
|
|
138 |
}
|
|
|
139 |
log.info(this.reqparams);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public String getCartHeaderSnippet(){
|
|
|
143 |
return htmlSnippets.get("CART_HEADER");
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
public String getCartDetailsSnippet(){
|
|
|
147 |
return htmlSnippets.get("CART_DETAILS");
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
public long getNumberOfItems(){
|
|
|
151 |
return userinfo.getTotalItems();
|
|
|
152 |
}
|
| 650 |
rajveer |
153 |
|
|
|
154 |
@Override
|
|
|
155 |
public void setParameters(Map<String, String[]> parameters) {
|
|
|
156 |
this.reqparams = parameters;
|
|
|
157 |
}
|
| 507 |
rajveer |
158 |
}
|