| 11592 |
amit.gupta |
1 |
package in.shop2020.mobileapi.serving.controllers;
|
| 9570 |
anupam.sin |
2 |
|
| 17782 |
amit.gupta |
3 |
import in.shop2020.logistics.DeliveryType;
|
|
|
4 |
import in.shop2020.metamodel.util.ProductPojo;
|
| 11516 |
amit.gupta |
5 |
import in.shop2020.mobileapi.serving.pojos.CartPojo;
|
| 10724 |
amit.gupta |
6 |
import in.shop2020.mobileapi.serving.pojos.RedirectPojo;
|
| 9577 |
anupam.sin |
7 |
import in.shop2020.mobileapi.serving.services.ContentServingService;
|
| 17782 |
amit.gupta |
8 |
import in.shop2020.mobileapi.serving.services.LogisticsService;
|
| 9667 |
anupam.sin |
9 |
import in.shop2020.mobileapi.serving.utils.PojoPopulator;
|
| 9577 |
anupam.sin |
10 |
import in.shop2020.mobileapi.serving.utils.SnippetType;
|
| 9570 |
anupam.sin |
11 |
import in.shop2020.model.v1.catalog.CatalogService.Client;
|
|
|
12 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
13 |
import in.shop2020.model.v1.user.Cart;
|
| 11592 |
amit.gupta |
14 |
import in.shop2020.model.v1.user.CartPlus;
|
| 17782 |
amit.gupta |
15 |
import in.shop2020.model.v1.user.ItemQuantity;
|
| 9713 |
anupam.sin |
16 |
import in.shop2020.model.v1.user.PromotionException;
|
|
|
17 |
import in.shop2020.model.v1.user.PromotionService;
|
| 9570 |
anupam.sin |
18 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
|
|
19 |
import in.shop2020.model.v1.user.UserContextService;
|
|
|
20 |
import in.shop2020.thrift.clients.CatalogClient;
|
| 9713 |
anupam.sin |
21 |
import in.shop2020.thrift.clients.PromotionClient;
|
| 9570 |
anupam.sin |
22 |
import in.shop2020.thrift.clients.UserClient;
|
|
|
23 |
|
| 17782 |
amit.gupta |
24 |
import java.util.ArrayList;
|
| 17797 |
amit.gupta |
25 |
import java.util.Arrays;
|
| 9570 |
anupam.sin |
26 |
import java.util.List;
|
|
|
27 |
import java.util.Map;
|
|
|
28 |
|
| 17782 |
amit.gupta |
29 |
import org.apache.commons.lang.StringUtils;
|
| 9570 |
anupam.sin |
30 |
import org.apache.log4j.Logger;
|
|
|
31 |
import org.apache.struts2.interceptor.ParameterAware;
|
|
|
32 |
import org.apache.thrift.TException;
|
| 17782 |
amit.gupta |
33 |
import org.json.JSONArray;
|
|
|
34 |
import org.json.JSONObject;
|
| 9570 |
anupam.sin |
35 |
|
| 9667 |
anupam.sin |
36 |
import com.google.gson.Gson;
|
| 9570 |
anupam.sin |
37 |
|
|
|
38 |
public class CartController extends BaseController implements ParameterAware{
|
|
|
39 |
|
|
|
40 |
private static final long serialVersionUID = 1L;
|
|
|
41 |
private static Logger log = Logger.getLogger(Class.class);
|
|
|
42 |
Map<String, String[]> reqparams = null;
|
|
|
43 |
|
|
|
44 |
private int variationId = 0;
|
|
|
45 |
private String totalamount;
|
|
|
46 |
|
|
|
47 |
private String errorMsg = "";
|
|
|
48 |
private String cartMsg = "";
|
|
|
49 |
|
|
|
50 |
private String pincode = "110001";
|
|
|
51 |
|
|
|
52 |
private String couponCode = null;
|
|
|
53 |
|
|
|
54 |
private String discountedAmount;
|
|
|
55 |
|
|
|
56 |
private long itemId;
|
|
|
57 |
private String insuranceResult;
|
|
|
58 |
|
|
|
59 |
private boolean toInsure;
|
| 12021 |
amit.gupta |
60 |
private boolean privateDealUser = false;
|
| 12022 |
amit.gupta |
61 |
private boolean autoApplicationOff = true;
|
| 9570 |
anupam.sin |
62 |
|
| 11516 |
amit.gupta |
63 |
private long id =-1; //CartId
|
| 9667 |
anupam.sin |
64 |
private String cartPojoJson;
|
| 17784 |
amit.gupta |
65 |
private String cartMap = null;
|
| 9676 |
anupam.sin |
66 |
private long quantity;
|
| 9697 |
anupam.sin |
67 |
private int insuranceType;
|
| 11386 |
amit.gupta |
68 |
UserContextService.Client userClient;
|
| 9570 |
anupam.sin |
69 |
|
| 11386 |
amit.gupta |
70 |
public CartController() throws Exception{
|
| 9570 |
anupam.sin |
71 |
super();
|
|
|
72 |
}
|
|
|
73 |
|
| 11535 |
amit.gupta |
74 |
public UserContextService.Client getClient() throws Exception{
|
| 11602 |
amit.gupta |
75 |
userClient = new UserClient().getClient();
|
| 11535 |
amit.gupta |
76 |
return userClient;
|
| 11593 |
amit.gupta |
77 |
}
|
| 9808 |
anupam.sin |
78 |
public String index() {
|
| 9709 |
anupam.sin |
79 |
if(id != -1){
|
| 9570 |
anupam.sin |
80 |
try {
|
| 11983 |
amit.gupta |
81 |
String cc = null;
|
| 12022 |
amit.gupta |
82 |
log.info(privateDealUser + " " + autoApplicationOff);
|
|
|
83 |
if(privateDealUser && !autoApplicationOff) {
|
| 11983 |
amit.gupta |
84 |
cc = "saholicdeals";
|
|
|
85 |
}
|
| 13066 |
amit.gupta |
86 |
log.info("Before cart response--------");
|
| 11983 |
amit.gupta |
87 |
CartPlus cartResponse = getClient().validateCartPlus(id, -1, cc);
|
| 11511 |
amit.gupta |
88 |
log.info("After cart response--------");
|
| 11823 |
amit.gupta |
89 |
if (errorMsg == null || errorMsg.equals("")) {
|
| 11917 |
amit.gupta |
90 |
if(cartResponse.getValidateCartMessages().get(1)!=null && !cartResponse.getValidateCartMessages().get(1).equals("")){
|
|
|
91 |
errorMsg = cartResponse.getValidateCartMessages().get(1);
|
|
|
92 |
}else {
|
|
|
93 |
errorMsg = cartResponse.getValidateCartMessages().get(0);
|
|
|
94 |
}
|
| 11823 |
amit.gupta |
95 |
}
|
|
|
96 |
cartPojoJson = new Gson().toJson(PojoPopulator.getCartPojo(cartResponse.getCart(), errorMsg,
|
| 11592 |
amit.gupta |
97 |
cartResponse.getPinCode(), cartResponse.isNeedInsuranceInfo()));
|
| 10551 |
amit.gupta |
98 |
log.info(cartPojoJson);
|
| 9570 |
anupam.sin |
99 |
} catch (Exception e) {
|
| 11593 |
amit.gupta |
100 |
/*TTransportException te = (TTransportException)e;
|
|
|
101 |
log.warn(e.getLocalizedMessage() + " " + te.getType());*/
|
| 9570 |
anupam.sin |
102 |
log.warn("Unable to validate the cart: ", e);
|
|
|
103 |
}
|
| 11516 |
amit.gupta |
104 |
} else {
|
|
|
105 |
cartPojoJson = new Gson().toJson(new CartPojo());
|
| 9570 |
anupam.sin |
106 |
}
|
|
|
107 |
return "index";
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public String create() {
|
| 10724 |
amit.gupta |
111 |
RedirectPojo rp = new RedirectPojo();
|
|
|
112 |
rp.setRedirectUrl("");
|
| 9676 |
anupam.sin |
113 |
try {
|
| 11526 |
amit.gupta |
114 |
//Try adding to cart
|
| 11535 |
amit.gupta |
115 |
cartMsg = getClient().addItemToCart(id, itemId, 1, -1);
|
|
|
116 |
log.info("Cart message while adding item to cart - " + cartMsg);
|
| 11526 |
amit.gupta |
117 |
//If could not add to cart try to add notification reminder if email exists.
|
| 9680 |
amit.gupta |
118 |
if (!("".equals(cartMsg))) {
|
| 10767 |
amit.gupta |
119 |
String emailId = this.request.getParameter("email");
|
| 10766 |
amit.gupta |
120 |
log.info("QuesyString" + this.request.getParameterMap().toString());
|
| 10724 |
amit.gupta |
121 |
if(emailId==null || emailId.equals("")){
|
| 11526 |
amit.gupta |
122 |
//User tries to add to cart but product is actually out of stock
|
| 10724 |
amit.gupta |
123 |
rp.setMessage("Item is currently out of stock");
|
|
|
124 |
} else {
|
|
|
125 |
try {
|
|
|
126 |
CatalogClient catalogClientService;
|
|
|
127 |
catalogClientService = new CatalogClient();
|
|
|
128 |
in.shop2020.model.v1.catalog.CatalogService.Client client = catalogClientService.getClient();
|
|
|
129 |
client.addProductNotification(itemId, emailId);
|
|
|
130 |
log.debug("Got product notification for product: " + itemId + " from user: " + emailId );
|
|
|
131 |
rp.setMessage("You will be notified when product is avaliable");
|
|
|
132 |
} catch (Exception e) {
|
|
|
133 |
rp.setMessage("Error occurred while adding to notificiation");
|
|
|
134 |
log.error("Exception while adding product notification for item: " + itemId + " and email: " + emailId, e);
|
| 10261 |
amit.gupta |
135 |
}
|
| 10724 |
amit.gupta |
136 |
|
| 10261 |
amit.gupta |
137 |
}
|
| 10724 |
amit.gupta |
138 |
} else {
|
|
|
139 |
rp.setRedirectUrl("cart");
|
| 9676 |
anupam.sin |
140 |
}
|
|
|
141 |
} catch (TException e) {
|
|
|
142 |
log.error("Unable to create or add to cart because of: ", e);
|
| 9713 |
anupam.sin |
143 |
errorMsg = "Unable to add to cart. Please try again.";
|
| 9676 |
anupam.sin |
144 |
} catch (Exception e) {
|
|
|
145 |
log.error("Unable to create or add to cart because of: ", e);
|
| 9713 |
anupam.sin |
146 |
errorMsg = "Unable to add to cart. Please try again.";
|
| 9570 |
anupam.sin |
147 |
}
|
| 10724 |
amit.gupta |
148 |
cartPojoJson = new Gson().toJson(rp);
|
|
|
149 |
return "index";
|
| 9676 |
anupam.sin |
150 |
}
|
| 9570 |
anupam.sin |
151 |
|
| 9676 |
anupam.sin |
152 |
|
|
|
153 |
// DELETE /entity
|
|
|
154 |
public String destroy() {
|
| 9709 |
anupam.sin |
155 |
if(id > 0) {
|
| 9570 |
anupam.sin |
156 |
try {
|
| 11535 |
amit.gupta |
157 |
getClient().deleteItemFromCart(id, itemId);
|
| 9676 |
anupam.sin |
158 |
return index();
|
|
|
159 |
} catch (ShoppingCartException e) {
|
|
|
160 |
log.error("Unable to delete item from cart: ", e);
|
| 9713 |
anupam.sin |
161 |
errorMsg = "Unable to delete item. Please try again.";
|
| 9570 |
anupam.sin |
162 |
} catch (TException e) {
|
| 9676 |
anupam.sin |
163 |
log.error("Unable to delete item from cart: ", e);
|
| 9713 |
anupam.sin |
164 |
errorMsg = "Unable to delete item. Please try again.";
|
| 9570 |
anupam.sin |
165 |
} catch (Exception e) {
|
| 9676 |
anupam.sin |
166 |
log.error("Unable to delete item from cart: ", e);
|
| 9713 |
anupam.sin |
167 |
errorMsg = "Unable to delete item. Please try again.";
|
| 9570 |
anupam.sin |
168 |
}
|
|
|
169 |
}
|
| 9676 |
anupam.sin |
170 |
return index();
|
| 9570 |
anupam.sin |
171 |
}
|
| 9676 |
anupam.sin |
172 |
|
|
|
173 |
//PUT
|
| 9570 |
anupam.sin |
174 |
public String update() {
|
| 9676 |
anupam.sin |
175 |
if(quantity <= 0) {
|
|
|
176 |
log.info("Not valid item quantity. Unable to change item quantity.");
|
| 9713 |
anupam.sin |
177 |
errorMsg = "Invalid item quantity";
|
| 9676 |
anupam.sin |
178 |
} else {
|
| 9709 |
anupam.sin |
179 |
if(updateItemQuantityInCart(id, itemId, quantity)) {
|
| 9676 |
anupam.sin |
180 |
return index();
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
return index();
|
| 9570 |
anupam.sin |
184 |
}
|
|
|
185 |
|
|
|
186 |
private boolean updateItemQuantityInCart(long cartId, long itemId, long quantity){
|
|
|
187 |
try {
|
| 11535 |
amit.gupta |
188 |
getClient().addItemToCart(cartId, itemId, quantity, -1);
|
| 9570 |
anupam.sin |
189 |
return true;
|
|
|
190 |
} catch (ShoppingCartException e) {
|
|
|
191 |
log.error("Unable to update the item quantity in the cart: ", e);
|
| 9713 |
anupam.sin |
192 |
errorMsg = "Unable to change quantity. Please try again.";
|
| 9570 |
anupam.sin |
193 |
} catch (TException e) {
|
|
|
194 |
log.error("Unable to update the item quantity in the cart: ", e);
|
| 9713 |
anupam.sin |
195 |
errorMsg = "Unable to change quantity. Please try again.";
|
| 9570 |
anupam.sin |
196 |
} catch (Exception e) {
|
|
|
197 |
log.error("Unable to update the item quantity in the cart: ", e);
|
| 9713 |
anupam.sin |
198 |
errorMsg = "Unable to change quantity. Please try again.";
|
| 9570 |
anupam.sin |
199 |
}
|
|
|
200 |
return false;
|
|
|
201 |
}
|
| 9676 |
anupam.sin |
202 |
|
| 9570 |
anupam.sin |
203 |
public String insureItem() {
|
| 9697 |
anupam.sin |
204 |
insuranceResult = "";
|
|
|
205 |
try {
|
| 11535 |
amit.gupta |
206 |
if(getClient().insureItem(itemId, id, toInsure, insuranceType)) {
|
| 9697 |
anupam.sin |
207 |
setInsuranceResult("SUCCESS");
|
|
|
208 |
} else {
|
|
|
209 |
setInsuranceResult("FAILURE");
|
|
|
210 |
}
|
|
|
211 |
} catch (Exception e) {
|
| 9709 |
anupam.sin |
212 |
log.error("Unable to insure item : " + itemId + " for cart : " + id, e);
|
| 9697 |
anupam.sin |
213 |
setInsuranceResult("FAILURE");
|
|
|
214 |
}
|
| 9570 |
anupam.sin |
215 |
return "insurance-result";
|
|
|
216 |
}
|
| 9713 |
anupam.sin |
217 |
|
|
|
218 |
public String edit() {
|
|
|
219 |
String action = request.getParameter("action");
|
|
|
220 |
PromotionClient promotionServiceClient = null;
|
|
|
221 |
try {
|
|
|
222 |
if(action == null || action.isEmpty()) {
|
|
|
223 |
errorMsg = "Invalid Request Action";
|
|
|
224 |
return index();
|
|
|
225 |
}
|
|
|
226 |
String couponCode = request.getParameter("coupon_code");
|
|
|
227 |
|
|
|
228 |
if (action.equalsIgnoreCase("applycoupon")) {
|
|
|
229 |
if (couponCode == null || couponCode.isEmpty()) {
|
|
|
230 |
errorMsg = "Coupon Code field cannot be left empty";
|
|
|
231 |
return index();
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
promotionServiceClient = new PromotionClient();
|
|
|
235 |
PromotionService.Client promotionClient = promotionServiceClient.getClient();
|
|
|
236 |
|
| 11768 |
amit.gupta |
237 |
Cart cart = promotionClient.applyCoupon(couponCode, id);
|
| 11823 |
amit.gupta |
238 |
errorMsg = "Coupon successfully applied";
|
| 11861 |
amit.gupta |
239 |
if(cart.getMessage() != null && !cart.getMessage().equals("")){
|
| 11823 |
amit.gupta |
240 |
errorMsg = errorMsg + ", " + cart.getMessage();
|
| 11768 |
amit.gupta |
241 |
}
|
| 9713 |
anupam.sin |
242 |
}
|
|
|
243 |
else if (action.equals("removecoupon")) {
|
| 11535 |
amit.gupta |
244 |
getClient().removeCoupon(id);
|
| 9713 |
anupam.sin |
245 |
}
|
|
|
246 |
} catch (PromotionException e) {
|
|
|
247 |
log.info("Invalid coupon: " + e.getMessage());
|
| 11823 |
amit.gupta |
248 |
errorMsg = e.getMessage();
|
| 9713 |
anupam.sin |
249 |
} catch (Exception e) {
|
|
|
250 |
log.error("Unable to apply or remove coupon", e);
|
|
|
251 |
errorMsg = "Unable to apply or remove coupon";
|
|
|
252 |
}
|
|
|
253 |
return index();
|
|
|
254 |
}
|
| 9808 |
anupam.sin |
255 |
|
|
|
256 |
public String changeAddress() {
|
|
|
257 |
long addressId;
|
|
|
258 |
if(request.getParameter("addressId") == null || request.getParameter("addressId").isEmpty()) {
|
|
|
259 |
errorMsg = "Null or empty addressId";
|
|
|
260 |
return index();
|
|
|
261 |
} else {
|
| 9815 |
anupam.sin |
262 |
addressId = Long.parseLong(request.getParameter("addressId"));
|
| 9808 |
anupam.sin |
263 |
}
|
|
|
264 |
|
|
|
265 |
try {
|
| 9815 |
anupam.sin |
266 |
if(request.getParameter("addressType").equalsIgnoreCase("store")) {
|
| 11535 |
amit.gupta |
267 |
getClient().addStoreToCart(id, addressId);
|
| 9815 |
anupam.sin |
268 |
} else if (request.getParameter("addressType").equalsIgnoreCase("home")){
|
| 11535 |
amit.gupta |
269 |
getClient().addAddressToCart(id, addressId);
|
| 9808 |
anupam.sin |
270 |
} else {
|
|
|
271 |
errorMsg = "Invalid addressType. It can only be set to home or store.";
|
|
|
272 |
}
|
|
|
273 |
} catch(Exception e) {
|
|
|
274 |
log.error("Unable to change address", e);
|
|
|
275 |
}
|
|
|
276 |
return index();
|
|
|
277 |
}
|
| 9570 |
anupam.sin |
278 |
|
|
|
279 |
public long getItemId(){
|
|
|
280 |
return this.itemId;
|
|
|
281 |
}
|
|
|
282 |
|
| 9684 |
amit.gupta |
283 |
public void setItemId(long itemId){
|
|
|
284 |
this.itemId = itemId;
|
|
|
285 |
}
|
|
|
286 |
|
| 9570 |
anupam.sin |
287 |
public String getTotalAmount() {
|
|
|
288 |
return totalamount;
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
public String getPinCode() {
|
|
|
292 |
return pincode;
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
public String getCouponCode() {
|
|
|
296 |
return couponCode;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
public String getDiscountedAmount() {
|
|
|
300 |
return discountedAmount;
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
public String getErrorMsg() {
|
|
|
304 |
return errorMsg;
|
|
|
305 |
}
|
|
|
306 |
|
| 9576 |
anupam.sin |
307 |
// public long getNumberOfItems(){
|
|
|
308 |
// return userinfo.getTotalItems();
|
|
|
309 |
// }
|
| 9570 |
anupam.sin |
310 |
|
|
|
311 |
public String getCartMsg(){
|
|
|
312 |
if(cartMsg.equals("")){
|
|
|
313 |
return null;
|
|
|
314 |
}
|
|
|
315 |
return cartMsg;
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
public String getSnippets(){
|
|
|
319 |
String snippets = "";
|
|
|
320 |
CatalogClient csc;
|
|
|
321 |
try {
|
|
|
322 |
csc = new CatalogClient();
|
|
|
323 |
List<Long> similarItems = csc.getClient().getSimilarItemsCatalogIds(0, 4, itemId);
|
|
|
324 |
for(Long catalogId: similarItems){
|
| 9602 |
amit.gupta |
325 |
snippets = snippets + ContentServingService.getSnippet(SnippetType.WIDGET_SNIPPET, catalogId+"", -1);
|
| 9570 |
anupam.sin |
326 |
}
|
|
|
327 |
} catch (Exception e) {
|
|
|
328 |
log.error("Unable to initialise Catalogservice Client");
|
|
|
329 |
}
|
|
|
330 |
return snippets;
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
@Override
|
|
|
334 |
public void setParameters(Map<String, String[]> parameters) {
|
|
|
335 |
this.reqparams = parameters;
|
|
|
336 |
}
|
|
|
337 |
|
| 9576 |
anupam.sin |
338 |
// @Override
|
|
|
339 |
// public String getHeaderSnippet() {
|
|
|
340 |
// String url = request.getQueryString();
|
|
|
341 |
// if (url == null) {
|
|
|
342 |
// url = "";
|
|
|
343 |
// } else {
|
|
|
344 |
// url = "?" + url;
|
|
|
345 |
// }
|
|
|
346 |
// url = request.getRequestURI() + url;
|
|
|
347 |
// return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url , 0, false);
|
|
|
348 |
// }
|
| 9570 |
anupam.sin |
349 |
|
| 9576 |
anupam.sin |
350 |
// public boolean isUserLoggedIn() {
|
|
|
351 |
// return userinfo.isLoggedIn();
|
|
|
352 |
// }
|
| 9570 |
anupam.sin |
353 |
|
|
|
354 |
public void setVariationId(String uri) {
|
|
|
355 |
if (uri.equals("/cart1")) {
|
|
|
356 |
this.variationId = 1;
|
|
|
357 |
}
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
public int getVariationId() {
|
|
|
361 |
return this.variationId;
|
|
|
362 |
}
|
|
|
363 |
|
|
|
364 |
public String getActionMessage(){
|
|
|
365 |
if(cartMsg.contains("out of stock")){
|
|
|
366 |
return "Notify me when this product is in stock.";
|
|
|
367 |
}else {
|
|
|
368 |
return "Notify me when this product is available.";
|
|
|
369 |
}
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
public String getOfferNote(){
|
|
|
373 |
String note = null;
|
|
|
374 |
if(cartMsg.contains("out of stock")){
|
|
|
375 |
return note;
|
|
|
376 |
}
|
|
|
377 |
else {
|
|
|
378 |
try {
|
|
|
379 |
CatalogClient catalogServiceClient = new CatalogClient();
|
|
|
380 |
Client catalogClient = catalogServiceClient.getClient();
|
|
|
381 |
Item it = catalogClient.getItem(itemId);
|
|
|
382 |
note = it.getBestDealText();
|
|
|
383 |
} catch (Exception e) {
|
|
|
384 |
log.error("Unable to get the offertext because of: ", e);
|
|
|
385 |
}
|
|
|
386 |
}
|
|
|
387 |
return note;
|
|
|
388 |
}
|
|
|
389 |
|
|
|
390 |
public String getInsuranceResult() {
|
|
|
391 |
return insuranceResult;
|
|
|
392 |
}
|
|
|
393 |
|
|
|
394 |
public void setInsuranceResult(String insuranceResult) {
|
|
|
395 |
this.insuranceResult = insuranceResult;
|
|
|
396 |
}
|
|
|
397 |
|
|
|
398 |
public void setToInsure(boolean toInsure) {
|
|
|
399 |
this.toInsure = toInsure;
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
public boolean getToInsure() {
|
|
|
403 |
return toInsure;
|
|
|
404 |
}
|
|
|
405 |
|
| 9709 |
anupam.sin |
406 |
public long getId() {
|
|
|
407 |
return id;
|
| 9570 |
anupam.sin |
408 |
}
|
|
|
409 |
|
| 9709 |
anupam.sin |
410 |
public void setId(long id) {
|
|
|
411 |
this.id = id;
|
| 9570 |
anupam.sin |
412 |
}
|
| 9667 |
anupam.sin |
413 |
|
|
|
414 |
|
|
|
415 |
public void setCartPojoJson(String cartPojoJson) {
|
|
|
416 |
this.cartPojoJson = cartPojoJson;
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
|
|
|
420 |
public String getCartPojoJson() {
|
|
|
421 |
return cartPojoJson;
|
|
|
422 |
}
|
| 17782 |
amit.gupta |
423 |
|
| 17784 |
amit.gupta |
424 |
public void setCartMap(String cartMap) {
|
| 17785 |
amit.gupta |
425 |
log.info("I am in cartMap" + " " + cartMap);
|
|
|
426 |
log.info(this.request.getParameterMap());
|
| 17784 |
amit.gupta |
427 |
this.cartMap = cartMap;
|
|
|
428 |
}
|
| 17782 |
amit.gupta |
429 |
|
| 17784 |
amit.gupta |
430 |
|
| 17782 |
amit.gupta |
431 |
public String validateCart() {
|
| 17785 |
amit.gupta |
432 |
log.info(this.cartMap);
|
| 17858 |
amit.gupta |
433 |
JSONObject jsonObj = null;
|
| 17782 |
amit.gupta |
434 |
if(!userinfo.isPrivateDealUser()){
|
|
|
435 |
cartPojoJson = "{\"response\":\"error\", \"message\":\"Invalid request.\"}";
|
|
|
436 |
} else {
|
| 17858 |
amit.gupta |
437 |
if(StringUtils.isEmpty(cartMap)){
|
| 17860 |
amit.gupta |
438 |
jsonObj = new JSONObject("{\"cartItems\":[]}");
|
| 17858 |
amit.gupta |
439 |
}else {
|
|
|
440 |
jsonObj = new JSONObject(cartMap);
|
|
|
441 |
}
|
| 17869 |
amit.gupta |
442 |
try{
|
|
|
443 |
List<ItemQuantity> itemQuantities = new ArrayList<ItemQuantity>();
|
|
|
444 |
JSONArray cartItems = jsonObj.getJSONArray("cartItems");
|
|
|
445 |
List<Integer> itemsList = new ArrayList<Integer>();
|
|
|
446 |
for (int i=0; i< cartItems.length(); i++) {
|
|
|
447 |
JSONObject obj = cartItems.getJSONObject(i);
|
|
|
448 |
//Just to avoid garage values
|
|
|
449 |
int itemId = 0;
|
|
|
450 |
try{
|
|
|
451 |
itemId = obj.getInt("itemId");
|
|
|
452 |
} catch(Exception e){
|
|
|
453 |
continue;
|
| 17856 |
amit.gupta |
454 |
}
|
| 17869 |
amit.gupta |
455 |
Integer quantity = obj.getInt("quantity");
|
|
|
456 |
ItemQuantity iq = new ItemQuantity(itemId, quantity);
|
|
|
457 |
itemQuantities.add(iq);
|
|
|
458 |
itemsList.add(itemId);
|
|
|
459 |
}
|
|
|
460 |
userClient = getClient();
|
|
|
461 |
if (itemsList.size()==0 || userClient.addItemsToCart(id, itemQuantities, jsonObj.has("couponCode")? jsonObj.getString("couponCode"):null)){
|
|
|
462 |
log.info("Items added to cart Successfully");
|
|
|
463 |
//Now validate cart and provide appropriate response.
|
|
|
464 |
String cartString = userClient.validateCartNew(id, userinfo.getPincode(), -1);
|
|
|
465 |
JSONObject cartObj = new JSONObject(cartString);
|
|
|
466 |
JSONArray arr = cartObj.getJSONArray("cartItems");
|
|
|
467 |
int maxEstimate=-1;
|
|
|
468 |
boolean allSame=true;
|
|
|
469 |
int removedCount = 0;
|
|
|
470 |
cartObj.put("cartMessagesMerged", 0);
|
|
|
471 |
for (int j=0; j<arr.length(); j++){
|
|
|
472 |
JSONObject itemObj = arr.getJSONObject(j-removedCount);
|
|
|
473 |
if(!itemsList.contains(itemObj.getInt("itemId"))){
|
|
|
474 |
if(itemObj.getInt("quantity")==0){
|
|
|
475 |
arr.remove(j-removedCount);
|
|
|
476 |
removedCount++;
|
|
|
477 |
if (itemObj.getInt("estimate")==-1){
|
|
|
478 |
cartObj.put("cartMessageUndeliverable", cartObj.getInt("cartMessageUndeliverable") - 1);
|
| 17856 |
amit.gupta |
479 |
} else {
|
| 17869 |
amit.gupta |
480 |
cartObj.put("cartMessageOOS", cartObj.getInt("cartMessageOOS") - 1);
|
| 17856 |
amit.gupta |
481 |
}
|
| 17869 |
amit.gupta |
482 |
continue;
|
|
|
483 |
} else {
|
|
|
484 |
JSONArray messagesArray = new JSONArray();
|
|
|
485 |
itemObj.put("cartItemMessages", messagesArray);
|
| 17901 |
amit.gupta |
486 |
if(itemsList.size()==0){
|
|
|
487 |
JSONObject message=new JSONObject();
|
|
|
488 |
message.put("type","info");
|
|
|
489 |
message.put("messageText","Added from earlier cart");
|
|
|
490 |
messagesArray.put(message);
|
|
|
491 |
cartObj.put("cartMessagesMerged", cartObj.getInt("cartMessagesMerged") + 1);
|
|
|
492 |
}
|
| 17869 |
amit.gupta |
493 |
}
|
| 17856 |
amit.gupta |
494 |
}
|
| 17869 |
amit.gupta |
495 |
ProductPojo pp = PojoPopulator.getShortContent(itemObj.getLong("catalogItemId"));
|
|
|
496 |
if(allSame){
|
|
|
497 |
allSame = maxEstimate==-1 || maxEstimate==itemObj.getInt("estimate");
|
|
|
498 |
}
|
|
|
499 |
if(itemObj.getInt("estimate")>maxEstimate){
|
|
|
500 |
maxEstimate = itemObj.getInt("estimate");
|
|
|
501 |
}
|
|
|
502 |
itemObj.put("imageUrl", pp.getImageUrl());
|
|
|
503 |
itemObj.put("title", pp.getTitle());
|
|
|
504 |
}
|
|
|
505 |
|
| 17870 |
amit.gupta |
506 |
ArrayList<JSONObject> listdata = new ArrayList<JSONObject>();
|
| 17869 |
amit.gupta |
507 |
if (arr != null) {
|
|
|
508 |
for (int i=0;i<arr.length();i++){
|
|
|
509 |
JSONArray itemMessages = arr.getJSONObject(i).optJSONArray(("cartItemMessages"));
|
|
|
510 |
if(itemMessages!=null && itemMessages.length()>0){
|
| 17870 |
amit.gupta |
511 |
listdata.add(arr.getJSONObject(i));
|
| 17869 |
amit.gupta |
512 |
} else {
|
| 17870 |
amit.gupta |
513 |
listdata.add(0,arr.getJSONObject(i));
|
| 17869 |
amit.gupta |
514 |
}
|
|
|
515 |
}
|
|
|
516 |
}
|
|
|
517 |
arr = new JSONArray();
|
|
|
518 |
for(int i=0;i<listdata.size();i++){
|
|
|
519 |
arr.put(listdata.get(i));
|
|
|
520 |
}
|
|
|
521 |
cartObj.put("cartItems", arr);
|
|
|
522 |
JSONArray cartMessagesArray = new JSONArray();
|
|
|
523 |
for (String message :Arrays.asList("cartMessageOOS", "cartMessageUndeliverable", "cartMessageChanged","cartMessagesMerged")){
|
|
|
524 |
int count = cartObj.getInt(message);
|
|
|
525 |
if (count>0) {
|
|
|
526 |
String type="danger";
|
|
|
527 |
JSONObject cartMessage=new JSONObject();
|
|
|
528 |
if (message.equals("cartMessagesMerged")) {
|
|
|
529 |
type = "info";
|
|
|
530 |
if (count==1){
|
|
|
531 |
cartMessage.put("messageText","1 item is added from earlier cart");
|
|
|
532 |
}else {
|
|
|
533 |
cartMessage.put("messageText","Few items are added from earlier cart");
|
|
|
534 |
}
|
|
|
535 |
} else if (message.equals("cartMessageOOS")) {
|
|
|
536 |
if (count==1){
|
|
|
537 |
cartMessage.put("messageText","One item is currently Out of Stock");
|
|
|
538 |
}else {
|
|
|
539 |
cartMessage.put("messageText","Few items are currently Out of Stock");
|
|
|
540 |
}
|
|
|
541 |
} else if (message.equals("cartMessageUndeliverable")) {
|
|
|
542 |
if (count==1){
|
|
|
543 |
cartMessage.put("messageText","One item is undeliverable");
|
|
|
544 |
}else {
|
|
|
545 |
cartMessage.put("messageText","Few items are underiverable");
|
|
|
546 |
}
|
|
|
547 |
} else {
|
|
|
548 |
if (count==1){
|
|
|
549 |
cartMessage.put("messageText","One item qty has changed");
|
|
|
550 |
}else {
|
|
|
551 |
cartMessage.put("messageText","Few items qty have changed");
|
|
|
552 |
}
|
| 17857 |
amit.gupta |
553 |
}
|
| 17869 |
amit.gupta |
554 |
cartMessage.put("type",type);
|
|
|
555 |
cartMessagesArray.put(cartMessage);
|
| 17856 |
amit.gupta |
556 |
}
|
| 17869 |
amit.gupta |
557 |
}
|
|
|
558 |
cartObj.put("cartMessages", cartMessagesArray);
|
|
|
559 |
|
|
|
560 |
if (maxEstimate==-1){
|
|
|
561 |
cartObj.put("estimateString", "Can't ship here");
|
| 17856 |
amit.gupta |
562 |
} else {
|
| 17869 |
amit.gupta |
563 |
if(itemsList.size() == 0){
|
|
|
564 |
cartObj.put("cod", getClient().showCODOption(id, 0, userinfo.getPincode()));
|
|
|
565 |
}
|
|
|
566 |
cartObj.put("estimateString", LogisticsService.getDeliveryDateString(maxEstimate,DeliveryType.COD));
|
| 17856 |
amit.gupta |
567 |
}
|
| 17869 |
amit.gupta |
568 |
cartObj.put("maxEstimate", maxEstimate);
|
|
|
569 |
cartPojoJson = cartObj.toString();
|
|
|
570 |
} else {
|
|
|
571 |
cartPojoJson = "{\"response\":\"error\", \"message\":\"Problem occurred while updating cart\"}";
|
|
|
572 |
}
|
|
|
573 |
}catch(Exception e) {
|
|
|
574 |
cartPojoJson = "{\"response\":\"error\", \"message\":\"Something went wrong while validation\"}";
|
|
|
575 |
e.printStackTrace();
|
|
|
576 |
}
|
| 17782 |
amit.gupta |
577 |
}
|
|
|
578 |
setResultJson(cartPojoJson);
|
|
|
579 |
return "index";
|
|
|
580 |
}
|
| 9676 |
anupam.sin |
581 |
|
|
|
582 |
public void setQuantity(long quantity) {
|
|
|
583 |
this.quantity = quantity;
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
public long getQuantity() {
|
|
|
587 |
return quantity;
|
|
|
588 |
}
|
| 9697 |
anupam.sin |
589 |
|
|
|
590 |
public int getInsuranceType() {
|
|
|
591 |
return insuranceType;
|
|
|
592 |
}
|
|
|
593 |
|
|
|
594 |
public void setInsuranceType(int insuranceType) {
|
|
|
595 |
this.insuranceType = insuranceType;
|
|
|
596 |
}
|
| 11983 |
amit.gupta |
597 |
|
| 12021 |
amit.gupta |
598 |
public void setPrivateDealUser(boolean privateDealUser) {
|
|
|
599 |
this.privateDealUser = privateDealUser;
|
| 11983 |
amit.gupta |
600 |
}
|
|
|
601 |
|
|
|
602 |
public boolean isPrivateDealUser() {
|
| 12021 |
amit.gupta |
603 |
return privateDealUser;
|
| 11983 |
amit.gupta |
604 |
}
|
|
|
605 |
|
| 12022 |
amit.gupta |
606 |
public void setAutoApplicationOff(boolean autoApplicationOff) {
|
|
|
607 |
this.autoApplicationOff = autoApplicationOff;
|
| 11983 |
amit.gupta |
608 |
}
|
|
|
609 |
|
| 12022 |
amit.gupta |
610 |
public boolean isAutoApplicationOff() {
|
|
|
611 |
return autoApplicationOff;
|
| 11983 |
amit.gupta |
612 |
}
|
| 9570 |
anupam.sin |
613 |
}
|