| 410 |
rajveer |
1 |
package in.shop2020.serving.controllers;
|
|
|
2 |
|
| 1981 |
varun.gupt |
3 |
import java.util.ArrayList;
|
|
|
4 |
import java.util.HashMap;
|
|
|
5 |
import java.util.List;
|
| 650 |
rajveer |
6 |
import java.util.Map;
|
|
|
7 |
import java.util.StringTokenizer;
|
|
|
8 |
|
| 1981 |
varun.gupt |
9 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
10 |
import in.shop2020.model.v1.user.Cart;
|
|
|
11 |
import in.shop2020.model.v1.user.Line;
|
| 555 |
chandransh |
12 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
| 572 |
chandransh |
13 |
import in.shop2020.model.v1.user.UserContextService;
|
| 410 |
rajveer |
14 |
import in.shop2020.serving.controllers.BaseController;
|
| 1981 |
varun.gupt |
15 |
|
|
|
16 |
import in.shop2020.thrift.clients.CatalogServiceClient;
|
| 1957 |
vikas |
17 |
import in.shop2020.serving.utils.DataLogger;
|
| 2137 |
chandransh |
18 |
import in.shop2020.serving.utils.FormattingUtils;
|
| 1957 |
vikas |
19 |
import in.shop2020.serving.utils.DataLogger.Event;
|
| 410 |
rajveer |
20 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
21 |
|
| 1957 |
vikas |
22 |
import org.apache.commons.lang.StringUtils;
|
| 832 |
rajveer |
23 |
import org.apache.log4j.Logger;
|
| 1614 |
rajveer |
24 |
import org.apache.struts2.convention.annotation.Action;
|
|
|
25 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
26 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
| 801 |
rajveer |
27 |
import org.apache.struts2.convention.annotation.Result;
|
| 1614 |
rajveer |
28 |
import org.apache.struts2.convention.annotation.Results;
|
| 410 |
rajveer |
29 |
import org.apache.struts2.interceptor.ParameterAware;
|
|
|
30 |
import org.apache.thrift.TException;
|
|
|
31 |
|
| 1614 |
rajveer |
32 |
|
|
|
33 |
@Results({
|
|
|
34 |
@Result(name="redirect", type="redirectAction",
|
|
|
35 |
params = {"actionName" , "cart"}),
|
|
|
36 |
@Result(name="failure", location="cart-failure.vm"),
|
|
|
37 |
@Result(name="success", location="cart-success.vm")
|
|
|
38 |
})
|
|
|
39 |
|
|
|
40 |
|
| 507 |
rajveer |
41 |
public class CartController extends BaseController implements ParameterAware{
|
| 410 |
rajveer |
42 |
|
|
|
43 |
private static final long serialVersionUID = 1L;
|
| 1957 |
vikas |
44 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
45 |
private static Logger dataLog = DataLogger.getLogger();
|
| 507 |
rajveer |
46 |
Map<String, String[]> reqparams = null;
|
| 1981 |
varun.gupt |
47 |
|
| 2137 |
chandransh |
48 |
private String totalamount;
|
| 410 |
rajveer |
49 |
|
| 572 |
chandransh |
50 |
private String errorMsg = "";
|
| 2099 |
rajveer |
51 |
private String cartMsg = "";
|
| 410 |
rajveer |
52 |
|
| 1981 |
varun.gupt |
53 |
private String pincode = "110001";
|
|
|
54 |
|
|
|
55 |
private String couponCode = null;
|
|
|
56 |
|
| 2137 |
chandransh |
57 |
private String discountedAmount;
|
| 1981 |
varun.gupt |
58 |
|
| 410 |
rajveer |
59 |
public CartController(){
|
| 507 |
rajveer |
60 |
super();
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
// GET /cart
|
| 1614 |
rajveer |
64 |
|
|
|
65 |
@Action(value="cart",interceptorRefs={@InterceptorRef("myDefault")})
|
| 650 |
rajveer |
66 |
public String index() {
|
| 572 |
chandransh |
67 |
try {
|
|
|
68 |
UserContextService.Client userClient = (new UserContextServiceClient()).getClient();
|
| 1466 |
ankur.sing |
69 |
errorMsg = userClient.validateCart(userinfo.getCartId());
|
| 572 |
chandransh |
70 |
} catch (Exception e) {
|
|
|
71 |
// This exception can be ignored for showing the cart. Not so
|
|
|
72 |
// innocent when this occurs at the time of checkout or when the
|
|
|
73 |
// user is proceeding to pay.
|
|
|
74 |
e.printStackTrace();
|
|
|
75 |
}
|
| 650 |
rajveer |
76 |
return "index";
|
| 507 |
rajveer |
77 |
}
|
| 572 |
chandransh |
78 |
// POST /entity
|
| 1614 |
rajveer |
79 |
|
|
|
80 |
@Action(value="addtocart",interceptorRefs={@InterceptorRef("createuser"),@InterceptorRef("myDefault")})
|
| 572 |
chandransh |
81 |
public String create() {
|
|
|
82 |
log.info("CartController.create");
|
| 555 |
chandransh |
83 |
|
| 572 |
chandransh |
84 |
printParams();
|
|
|
85 |
|
|
|
86 |
long userId = userinfo.getUserId();
|
|
|
87 |
long cartId = userinfo.getCartId();
|
|
|
88 |
|
| 1614 |
rajveer |
89 |
log.info("user id is " + userId);
|
|
|
90 |
log.info("cart id is " + cartId);
|
|
|
91 |
|
| 572 |
chandransh |
92 |
log.info("item id is " + this.reqparams.get("productid"));
|
| 1614 |
rajveer |
93 |
|
| 637 |
rajveer |
94 |
String itemIds = "";
|
| 572 |
chandransh |
95 |
if (this.reqparams.get("productid") != null) {
|
|
|
96 |
itemIds = this.reqparams.get("productid")[0];
|
| 637 |
rajveer |
97 |
}else{
|
|
|
98 |
return "failure";
|
| 572 |
chandransh |
99 |
}
|
| 637 |
rajveer |
100 |
|
| 572 |
chandransh |
101 |
StringTokenizer tokenizer = new StringTokenizer(itemIds, "_");
|
|
|
102 |
while (tokenizer.hasMoreTokens()) {
|
|
|
103 |
long itemId = Long.parseLong(tokenizer.nextToken());
|
|
|
104 |
|
|
|
105 |
try {
|
|
|
106 |
UserContextServiceClient userServiceClient = new UserContextServiceClient();
|
|
|
107 |
UserContextService.Client userClient = userServiceClient.getClient();
|
| 762 |
rajveer |
108 |
if (cartId == 0){
|
| 555 |
chandransh |
109 |
cartId = userClient.createCart(userId);
|
| 762 |
rajveer |
110 |
}
|
| 2099 |
rajveer |
111 |
if(cartMsg.equals("")){
|
| 2036 |
rajveer |
112 |
cartMsg = userClient.addItemToCart(cartId, itemId, 1);
|
|
|
113 |
}else{
|
|
|
114 |
userClient.addItemToCart(cartId, itemId, 1);
|
|
|
115 |
}
|
| 762 |
rajveer |
116 |
userinfo.setCartId(cartId);
|
|
|
117 |
int totalItems = userClient.getCart(cartId).getLinesSize();
|
|
|
118 |
userinfo.setTotalItems(totalItems);
|
| 572 |
chandransh |
119 |
} catch (TException e) {
|
|
|
120 |
e.printStackTrace();
|
|
|
121 |
} catch (Exception e) {
|
|
|
122 |
e.printStackTrace();
|
| 507 |
rajveer |
123 |
}
|
|
|
124 |
|
| 572 |
chandransh |
125 |
}
|
| 1957 |
vikas |
126 |
dataLog.info(StringUtils.join(new String[] { Event.ADD_TO_CART.name(),
|
|
|
127 |
userinfo.getEmail(), Long.toString(cartId), itemIds }, ", "));
|
| 572 |
chandransh |
128 |
return "success";
|
|
|
129 |
}
|
|
|
130 |
|
| 1614 |
rajveer |
131 |
|
| 507 |
rajveer |
132 |
// DELETE /entity
|
|
|
133 |
public String destroy() {
|
|
|
134 |
log.info("CartController.destroy");
|
|
|
135 |
printParams();
|
|
|
136 |
log.info("item id is " + this.request.getParameter("productid"));
|
|
|
137 |
String itemIdString = this.request.getParameter("productid");
|
|
|
138 |
long itemId = Long.parseLong(itemIdString);
|
| 517 |
rajveer |
139 |
if(userinfo.getCartId() == -1){
|
|
|
140 |
log.info("Cart does not exist. Nothing to delete.");
|
|
|
141 |
}else{
|
| 1957 |
vikas |
142 |
if(deleteItemFromCart(userinfo.getCartId(), itemId, userinfo.getUserId(), userinfo.isSessionId()))
|
|
|
143 |
{
|
| 762 |
rajveer |
144 |
userinfo.setTotalItems(getNumberOfItemsInCart(userinfo.getCartId()));
|
| 1957 |
vikas |
145 |
dataLog.info(StringUtils.join(new String[] {
|
|
|
146 |
Event.DELETE_FROM_CART.name(), userinfo.getEmail(),
|
|
|
147 |
Long.toString(userinfo.getCartId()), itemIdString },
|
|
|
148 |
", "));
|
| 801 |
rajveer |
149 |
return "redirect";
|
| 517 |
rajveer |
150 |
}
|
| 507 |
rajveer |
151 |
}
|
| 801 |
rajveer |
152 |
return "redirect";
|
| 507 |
rajveer |
153 |
}
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
// DELETE /entity
|
|
|
157 |
public String update() {
|
|
|
158 |
log.info("CartController.update");
|
|
|
159 |
printParams();
|
|
|
160 |
log.info("item id is " + this.request.getParameter("productid"));
|
|
|
161 |
log.info("item id is " + this.request.getParameter("quantity"));
|
|
|
162 |
String itemIdString = this.request.getParameter("productid");
|
|
|
163 |
String quantityString = this.request.getParameter("quantity");
|
|
|
164 |
long itemId = Long.parseLong(itemIdString);
|
|
|
165 |
long quantity = Long.parseLong(quantityString);
|
| 517 |
rajveer |
166 |
if(quantity <= 0){
|
|
|
167 |
log.info("Not valid item quantity. Unable to change item quantity.");
|
|
|
168 |
}else{
|
| 762 |
rajveer |
169 |
if(updateItemQuantityInCart(userinfo.getCartId(), itemId, quantity)){
|
| 1957 |
vikas |
170 |
dataLog.info(StringUtils.join(new String[] {
|
|
|
171 |
Event.UPDATE_CART_QUANTITY.name(), userinfo.getEmail(),
|
|
|
172 |
Long.toString(userinfo.getCartId()), Long.toString(itemId), Long.toString(quantity) },
|
|
|
173 |
", "));
|
| 801 |
rajveer |
174 |
return "redirect";
|
| 517 |
rajveer |
175 |
}
|
| 507 |
rajveer |
176 |
}
|
| 1957 |
vikas |
177 |
dataLog.info(StringUtils.join(new String[] {
|
|
|
178 |
Event.UPDATE_CART_QUANTITY_FAILED.name(), userinfo.getEmail(),
|
|
|
179 |
Long.toString(userinfo.getCartId()), Long.toString(itemId), Long.toString(quantity) },
|
|
|
180 |
", "));
|
| 801 |
rajveer |
181 |
addActionError("Unable to update the quantity");
|
|
|
182 |
return "redirect";
|
| 507 |
rajveer |
183 |
}
|
|
|
184 |
|
|
|
185 |
|
|
|
186 |
public void printParams(){
|
|
|
187 |
for(String param : reqparams.keySet()) {
|
|
|
188 |
log.info("param name is " + param);
|
|
|
189 |
log.info("param first is " + reqparams.get(param)[0]);
|
|
|
190 |
}
|
|
|
191 |
log.info(this.reqparams);
|
|
|
192 |
}
|
|
|
193 |
|
| 762 |
rajveer |
194 |
private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
|
|
|
195 |
try {
|
|
|
196 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
197 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
198 |
|
|
|
199 |
userClient.changeQuantity(cartId, itemId, quantity);
|
|
|
200 |
return true;
|
|
|
201 |
} catch (ShoppingCartException e) {
|
|
|
202 |
e.printStackTrace();
|
|
|
203 |
} catch (TException e) {
|
|
|
204 |
e.printStackTrace();
|
|
|
205 |
} catch (Exception e) {
|
|
|
206 |
e.printStackTrace();
|
|
|
207 |
}
|
|
|
208 |
return false;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
private boolean deleteItemFromCart(long cartId, long catalogItemId, long userId, boolean isSessionId){
|
|
|
212 |
try {
|
|
|
213 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
214 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
215 |
|
|
|
216 |
userClient.deleteItemFromCart(cartId, catalogItemId);
|
|
|
217 |
return true;
|
|
|
218 |
} catch (ShoppingCartException e) {
|
|
|
219 |
e.printStackTrace();
|
|
|
220 |
} catch (TException e) {
|
|
|
221 |
e.printStackTrace();
|
|
|
222 |
} catch (Exception e) {
|
|
|
223 |
e.printStackTrace();
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
return false;
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
private int getNumberOfItemsInCart(long cartId) {
|
|
|
230 |
int numberOfItems = 0;
|
|
|
231 |
UserContextServiceClient userContextServiceClient = null;
|
|
|
232 |
try {
|
|
|
233 |
userContextServiceClient = new UserContextServiceClient();
|
|
|
234 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
235 |
|
|
|
236 |
numberOfItems = userClient.getCart(cartId).getLinesSize();
|
|
|
237 |
} catch (ShoppingCartException e) {
|
|
|
238 |
e.printStackTrace();
|
|
|
239 |
} catch (TException e) {
|
|
|
240 |
e.printStackTrace();
|
|
|
241 |
} catch (Exception e) {
|
|
|
242 |
e.printStackTrace();
|
|
|
243 |
}
|
|
|
244 |
return numberOfItems;
|
|
|
245 |
}
|
| 1981 |
varun.gupt |
246 |
|
|
|
247 |
public List<Map<String,String>> getCartItems() {
|
|
|
248 |
List<Map<String,String>> items = null;
|
| 762 |
rajveer |
249 |
|
| 1981 |
varun.gupt |
250 |
UserContextServiceClient userServiceClient = null;
|
|
|
251 |
in.shop2020.model.v1.user.UserContextService.Client userClient = null;
|
|
|
252 |
CatalogServiceClient catalogServiceClient = null;
|
|
|
253 |
in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = null;
|
| 762 |
rajveer |
254 |
|
| 2137 |
chandransh |
255 |
FormattingUtils formattingUtils = new FormattingUtils();
|
|
|
256 |
|
| 1981 |
varun.gupt |
257 |
try {
|
|
|
258 |
catalogServiceClient = new CatalogServiceClient();
|
|
|
259 |
catalogClient = catalogServiceClient.getClient();
|
|
|
260 |
userServiceClient = new UserContextServiceClient();
|
|
|
261 |
userClient = userServiceClient.getClient();
|
|
|
262 |
|
|
|
263 |
pincode = userClient.getDefaultPincode(userinfo.getUserId());
|
|
|
264 |
Cart cart = userClient.getCart(userinfo.getCartId());
|
|
|
265 |
List<Line> lineItems = cart.getLines();
|
|
|
266 |
|
|
|
267 |
if(lineItems.size() != 0) {
|
|
|
268 |
items = new ArrayList<Map<String,String>>();
|
|
|
269 |
|
|
|
270 |
for (Line line : lineItems) {
|
|
|
271 |
Map<String, String> itemdetail = new HashMap<String, String>();
|
|
|
272 |
Item item = catalogClient.getItem(line.getItemId());
|
|
|
273 |
|
|
|
274 |
String itemName = ((item.getBrand() != null) ? item.getBrand() + " " : "")
|
|
|
275 |
+ ((item.getModelName() != null) ? item.getModelName() + " " : "")
|
|
|
276 |
+ (( item.getModelNumber() != null ) ? item.getModelNumber() + " " : "" )
|
|
|
277 |
+ (( (item.getColor() != null && !item.getColor().trim().equals("NA"))) ? "("+item.getColor()+")" : "" );
|
|
|
278 |
|
|
|
279 |
itemdetail.put("ITEM_NAME", itemName);
|
|
|
280 |
itemdetail.put("ITEM_ID", line.getItemId() + "");
|
|
|
281 |
itemdetail.put("CATALOG_ID", item.getCatalogItemId() + "");
|
|
|
282 |
itemdetail.put("ITEM_QUANTITY", ((int)line.getQuantity()) + "");
|
| 2137 |
chandransh |
283 |
itemdetail.put("MRP", formattingUtils.formatPrice(item.getMrp()));
|
|
|
284 |
itemdetail.put("SELLING_PRICE", formattingUtils.formatPrice(item.getSellingPrice()));
|
|
|
285 |
itemdetail.put("TOTAL_PRICE", formattingUtils.formatPrice(((item.getSellingPrice() * line.getQuantity()))));
|
| 1981 |
varun.gupt |
286 |
itemdetail.put("SHIPPING_TIME", line.getEstimate() + "");
|
|
|
287 |
|
|
|
288 |
items.add(itemdetail);
|
|
|
289 |
}
|
|
|
290 |
}
|
| 2137 |
chandransh |
291 |
|
|
|
292 |
totalamount = formattingUtils.formatPrice(cart.getTotalPrice());
|
| 1981 |
varun.gupt |
293 |
couponCode = cart.getCouponCode() == null ? "" : cart.getCouponCode();
|
| 2137 |
chandransh |
294 |
discountedAmount = formattingUtils.formatPrice(cart.getDiscountedPrice());
|
| 1981 |
varun.gupt |
295 |
} catch (Exception e) {
|
|
|
296 |
e.printStackTrace();
|
|
|
297 |
}
|
|
|
298 |
return items;
|
| 507 |
rajveer |
299 |
}
|
| 1981 |
varun.gupt |
300 |
|
| 2137 |
chandransh |
301 |
public String getTotalAmount() {
|
| 1981 |
varun.gupt |
302 |
return totalamount;
|
|
|
303 |
}
|
| 507 |
rajveer |
304 |
|
| 1981 |
varun.gupt |
305 |
public String getPinCode() {
|
|
|
306 |
return pincode;
|
| 507 |
rajveer |
307 |
}
|
|
|
308 |
|
| 1981 |
varun.gupt |
309 |
public String getCouponCode() {
|
|
|
310 |
return couponCode;
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
public String getDiscountedAmount() {
|
| 2137 |
chandransh |
314 |
return discountedAmount;
|
| 1981 |
varun.gupt |
315 |
}
|
|
|
316 |
|
|
|
317 |
public String getErrorMsg() {
|
|
|
318 |
return errorMsg;
|
|
|
319 |
}
|
|
|
320 |
|
| 507 |
rajveer |
321 |
public long getNumberOfItems(){
|
|
|
322 |
return userinfo.getTotalItems();
|
|
|
323 |
}
|
| 650 |
rajveer |
324 |
|
| 2036 |
rajveer |
325 |
public String getCartMsg(){
|
| 2099 |
rajveer |
326 |
if(cartMsg.equals("")){
|
|
|
327 |
return null;
|
|
|
328 |
}
|
|
|
329 |
return cartMsg;
|
| 2036 |
rajveer |
330 |
}
|
|
|
331 |
|
| 650 |
rajveer |
332 |
@Override
|
|
|
333 |
public void setParameters(Map<String, String[]> parameters) {
|
|
|
334 |
this.reqparams = parameters;
|
|
|
335 |
}
|
| 507 |
rajveer |
336 |
}
|