| 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;
|
| 801 |
rajveer |
14 |
import org.apache.struts2.convention.annotation.Result;
|
| 410 |
rajveer |
15 |
import org.apache.struts2.interceptor.ParameterAware;
|
|
|
16 |
import org.apache.thrift.TException;
|
|
|
17 |
|
| 801 |
rajveer |
18 |
@Result(name="redirect", type="redirectAction",
|
|
|
19 |
params = {"actionName" , "cart"})
|
| 507 |
rajveer |
20 |
public class CartController extends BaseController implements ParameterAware{
|
| 410 |
rajveer |
21 |
|
|
|
22 |
private static final long serialVersionUID = 1L;
|
| 507 |
rajveer |
23 |
private static Log log = LogFactory.getLog(CartController.class);
|
|
|
24 |
Map<String, String[]> reqparams = null;
|
| 410 |
rajveer |
25 |
|
| 650 |
rajveer |
26 |
|
| 572 |
chandransh |
27 |
private String errorMsg = "";
|
| 410 |
rajveer |
28 |
|
|
|
29 |
public CartController(){
|
| 507 |
rajveer |
30 |
super();
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
// GET /cart
|
| 650 |
rajveer |
34 |
public String index() {
|
| 572 |
chandransh |
35 |
try {
|
|
|
36 |
UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
|
|
|
37 |
if(!userClient.validateCart(userinfo.getCartId()))
|
|
|
38 |
errorMsg = "Your cart has been updated.";
|
|
|
39 |
} catch (Exception e) {
|
|
|
40 |
// This exception can be ignored for showing the cart. Not so
|
|
|
41 |
// innocent when this occurs at the time of checkout or when the
|
|
|
42 |
// user is proceeding to pay.
|
|
|
43 |
e.printStackTrace();
|
|
|
44 |
}
|
| 410 |
rajveer |
45 |
|
| 650 |
rajveer |
46 |
htmlSnippets.put("CART_HEADER", pageLoader.getCartHeaderHtml());
|
| 786 |
rajveer |
47 |
htmlSnippets.put("CART_DETAILS", pageLoader.getCartDetailsHtml(userinfo.getUserId(), userinfo.getCartId(), errorMsg));
|
| 650 |
rajveer |
48 |
return "index";
|
| 507 |
rajveer |
49 |
}
|
|
|
50 |
|
| 572 |
chandransh |
51 |
// POST /entity
|
|
|
52 |
public String create() {
|
|
|
53 |
log.info("CartController.create");
|
| 555 |
chandransh |
54 |
|
| 572 |
chandransh |
55 |
printParams();
|
|
|
56 |
|
|
|
57 |
long userId = userinfo.getUserId();
|
|
|
58 |
long cartId = userinfo.getCartId();
|
|
|
59 |
|
|
|
60 |
log.info("item id is " + this.reqparams.get("productid"));
|
|
|
61 |
|
| 637 |
rajveer |
62 |
String itemIds = "";
|
| 572 |
chandransh |
63 |
if (this.reqparams.get("productid") != null) {
|
|
|
64 |
itemIds = this.reqparams.get("productid")[0];
|
| 637 |
rajveer |
65 |
}else{
|
|
|
66 |
return "failure";
|
| 572 |
chandransh |
67 |
}
|
| 637 |
rajveer |
68 |
|
| 572 |
chandransh |
69 |
|
|
|
70 |
StringTokenizer tokenizer = new StringTokenizer(itemIds, "_");
|
|
|
71 |
while (tokenizer.hasMoreTokens()) {
|
|
|
72 |
long itemId = Long.parseLong(tokenizer.nextToken());
|
|
|
73 |
|
|
|
74 |
try {
|
|
|
75 |
UserContextServiceClient userServiceClient = new UserContextServiceClient();
|
|
|
76 |
UserContextService.Client userClient = userServiceClient.getClient();
|
| 762 |
rajveer |
77 |
if (cartId == 0){
|
| 555 |
chandransh |
78 |
cartId = userClient.createCart(userId);
|
| 762 |
rajveer |
79 |
}
|
| 572 |
chandransh |
80 |
userClient.addItemToCart(cartId, itemId, 1);
|
| 762 |
rajveer |
81 |
userinfo.setCartId(cartId);
|
|
|
82 |
int totalItems = userClient.getCart(cartId).getLinesSize();
|
|
|
83 |
userinfo.setTotalItems(totalItems);
|
| 572 |
chandransh |
84 |
} catch (TException e) {
|
|
|
85 |
e.printStackTrace();
|
|
|
86 |
} catch (Exception e) {
|
|
|
87 |
e.printStackTrace();
|
| 507 |
rajveer |
88 |
}
|
|
|
89 |
|
| 572 |
chandransh |
90 |
}
|
| 507 |
rajveer |
91 |
|
| 572 |
chandransh |
92 |
return "success";
|
|
|
93 |
}
|
|
|
94 |
|
| 507 |
rajveer |
95 |
// DELETE /entity
|
|
|
96 |
public String destroy() {
|
|
|
97 |
log.info("CartController.destroy");
|
|
|
98 |
printParams();
|
|
|
99 |
log.info("item id is " + this.request.getParameter("productid"));
|
|
|
100 |
String itemIdString = this.request.getParameter("productid");
|
|
|
101 |
long itemId = Long.parseLong(itemIdString);
|
| 517 |
rajveer |
102 |
if(userinfo.getCartId() == -1){
|
|
|
103 |
log.info("Cart does not exist. Nothing to delete.");
|
|
|
104 |
}else{
|
| 762 |
rajveer |
105 |
if(deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId())){
|
|
|
106 |
userinfo.setTotalItems(getNumberOfItemsInCart(userinfo.getCartId()));
|
| 801 |
rajveer |
107 |
return "redirect";
|
| 517 |
rajveer |
108 |
}
|
| 507 |
rajveer |
109 |
}
|
| 801 |
rajveer |
110 |
return "redirect";
|
| 507 |
rajveer |
111 |
}
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
// DELETE /entity
|
|
|
115 |
public String update() {
|
|
|
116 |
log.info("CartController.update");
|
|
|
117 |
printParams();
|
|
|
118 |
log.info("item id is " + this.request.getParameter("productid"));
|
|
|
119 |
log.info("item id is " + this.request.getParameter("quantity"));
|
|
|
120 |
String itemIdString = this.request.getParameter("productid");
|
|
|
121 |
String quantityString = this.request.getParameter("quantity");
|
|
|
122 |
long itemId = Long.parseLong(itemIdString);
|
|
|
123 |
long quantity = Long.parseLong(quantityString);
|
| 517 |
rajveer |
124 |
if(quantity <= 0){
|
|
|
125 |
log.info("Not valid item quantity. Unable to change item quantity.");
|
|
|
126 |
}else{
|
| 762 |
rajveer |
127 |
if(updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity)){
|
| 801 |
rajveer |
128 |
return "redirect";
|
| 517 |
rajveer |
129 |
}
|
| 507 |
rajveer |
130 |
}
|
| 801 |
rajveer |
131 |
addActionError("Unable to update the quantity");
|
|
|
132 |
return "redirect";
|
| 507 |
rajveer |
133 |
}
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
public void printParams(){
|
|
|
137 |
for(String param : reqparams.keySet()) {
|
|
|
138 |
log.info("param name is " + param);
|
|
|
139 |
log.info("param first is " + reqparams.get(param)[0]);
|
|
|
140 |
}
|
|
|
141 |
log.info(this.reqparams);
|
|
|
142 |
}
|
|
|
143 |
|
| 762 |
rajveer |
144 |
private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
|
|
|
145 |
try {
|
|
|
146 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
147 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
148 |
|
|
|
149 |
userClient.changeQuantity(cartId, itemId, quantity);
|
|
|
150 |
return true;
|
|
|
151 |
} catch (ShoppingCartException e) {
|
|
|
152 |
e.printStackTrace();
|
|
|
153 |
} catch (TException e) {
|
|
|
154 |
e.printStackTrace();
|
|
|
155 |
} catch (Exception e) {
|
|
|
156 |
e.printStackTrace();
|
|
|
157 |
}
|
|
|
158 |
return false;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
private boolean deleteItemFromCart(long cartId, long catalogItemId, long userId, boolean isSessionId){
|
|
|
162 |
try {
|
|
|
163 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
164 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
165 |
|
|
|
166 |
userClient.deleteItemFromCart(cartId, catalogItemId);
|
|
|
167 |
return true;
|
|
|
168 |
} catch (ShoppingCartException e) {
|
|
|
169 |
e.printStackTrace();
|
|
|
170 |
} catch (TException e) {
|
|
|
171 |
e.printStackTrace();
|
|
|
172 |
} catch (Exception e) {
|
|
|
173 |
e.printStackTrace();
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
return false;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
private int getNumberOfItemsInCart(long cartId) {
|
|
|
182 |
int numberOfItems = 0;
|
|
|
183 |
UserContextServiceClient userContextServiceClient = null;
|
|
|
184 |
try {
|
|
|
185 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
186 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
187 |
|
|
|
188 |
numberOfItems = userClient.getCart(cartId).getLinesSize();
|
|
|
189 |
} catch (ShoppingCartException e) {
|
|
|
190 |
e.printStackTrace();
|
|
|
191 |
} catch (TException e) {
|
|
|
192 |
e.printStackTrace();
|
|
|
193 |
} catch (Exception e) {
|
|
|
194 |
e.printStackTrace();
|
|
|
195 |
}
|
|
|
196 |
return numberOfItems;
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
|
| 507 |
rajveer |
200 |
public String getCartHeaderSnippet(){
|
|
|
201 |
return htmlSnippets.get("CART_HEADER");
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
public String getCartDetailsSnippet(){
|
|
|
205 |
return htmlSnippets.get("CART_DETAILS");
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
public long getNumberOfItems(){
|
|
|
209 |
return userinfo.getTotalItems();
|
|
|
210 |
}
|
| 650 |
rajveer |
211 |
|
|
|
212 |
@Override
|
|
|
213 |
public void setParameters(Map<String, String[]> parameters) {
|
|
|
214 |
this.reqparams = parameters;
|
|
|
215 |
}
|
| 507 |
rajveer |
216 |
}
|