1 24 package org.ofbiz.order.shoppingcart; 25 26 import java.text.NumberFormat ; 27 import java.util.Iterator ; 28 import java.util.LinkedList ; 29 import java.util.List ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.http.HttpSession ; 35 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.UtilDateTime; 38 import org.ofbiz.base.util.UtilFormatOut; 39 import org.ofbiz.base.util.UtilHttp; 40 import org.ofbiz.base.util.UtilMisc; 41 import org.ofbiz.base.util.UtilProperties; 42 import org.ofbiz.base.util.UtilValidate; 43 import org.ofbiz.entity.GenericDelegator; 44 import org.ofbiz.entity.GenericEntityException; 45 import org.ofbiz.entity.GenericPK; 46 import org.ofbiz.entity.GenericValue; 47 import org.ofbiz.entity.util.EntityUtil; 48 import org.ofbiz.order.shoppingcart.product.ProductPromoWorker; 49 import org.ofbiz.product.catalog.CatalogWorker; 50 import org.ofbiz.product.config.ProductConfigWorker; 51 import org.ofbiz.product.config.ProductConfigWrapper; 52 import org.ofbiz.product.product.ProductWorker; 53 import org.ofbiz.product.store.ProductStoreSurveyWrapper; 54 import org.ofbiz.product.store.ProductStoreWorker; 55 import org.ofbiz.security.Security; 56 import org.ofbiz.service.GenericServiceException; 57 import org.ofbiz.service.LocalDispatcher; 58 import org.ofbiz.service.ModelService; 59 import org.ofbiz.service.ServiceUtil; 60 import org.ofbiz.webapp.control.RequestHandler; 61 62 70 public class ShoppingCartEvents { 71 72 public static String module = ShoppingCartEvents.class.getName(); 73 public static final String resource = "OrderUiLabels"; 74 public static final String resource_error = "OrderErrorUiLabels"; 75 76 private static final String NO_ERROR = "noerror"; 77 private static final String NON_CRITICAL_ERROR = "noncritical"; 78 private static final String ERROR = "error"; 79 80 public static String addProductPromoCode(HttpServletRequest request, HttpServletResponse response) { 81 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 82 ShoppingCart cart = getCartObject(request); 83 String productPromoCodeId = request.getParameter("productPromoCodeId"); 84 if (UtilValidate.isNotEmpty(productPromoCodeId)) { 85 String checkResult = cart.addProductPromoCode(productPromoCodeId, dispatcher); 86 if (UtilValidate.isNotEmpty(checkResult)) { 87 request.setAttribute("_ERROR_MESSAGE_", checkResult); 88 return "error"; 89 } 90 } 91 return "success"; 92 } 93 94 95 public static String addToCart(HttpServletRequest request, HttpServletResponse response) { 96 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 97 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 98 ShoppingCart cart = getCartObject(request); 99 ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart); 100 String controlDirective = null; 101 Map result = null; 102 String productId = null; 103 String itemType = null; 104 String itemDescription = null; 105 String productCategoryId = null; 106 String priceStr = null; 107 double price = 0.00; 108 String quantityStr = null; 109 double quantity = 0; 110 String reservStartStr = null; 111 String reservEndStr = null; 112 java.sql.Timestamp reservStart = null; 113 java.sql.Timestamp reservEnd = null; 114 String reservLengthStr = null; 115 double reservLength = 0; 116 String reservPersonsStr = null; 117 double reservPersons = 0; 118 String shipBeforeStr = null; 119 String shipBeforeDateStr = null; 120 String shipAfterDateStr = null; 121 java.sql.Timestamp shipBeforeDate = null; 122 java.sql.Timestamp shipAfterDate = null; 123 124 String catalogId = CatalogWorker.getCurrentCatalogId(request); 126 Locale locale = UtilHttp.getLocale(request); 127 NumberFormat nf = NumberFormat.getNumberInstance(locale); 128 129 Map paramMap = UtilHttp.getParameterMap(request); 131 132 String shoppingListId = request.getParameter("shoppingListId"); 134 String shoppingListItemSeqId = request.getParameter("shoppingListItemSeqId"); 135 if (paramMap.containsKey("ADD_PRODUCT_ID")) { 136 productId = (String ) paramMap.remove("ADD_PRODUCT_ID"); 137 } else if (paramMap.containsKey("add_product_id")) { 138 productId = (String ) paramMap.remove("add_product_id"); 139 } 140 141 if (paramMap.containsKey("ADD_CATEGORY_ID")) { 142 productCategoryId = (String ) paramMap.remove("ADD_CATEGORY_ID"); 143 } else if (paramMap.containsKey("add_category_id")) { 144 productCategoryId = (String ) paramMap.remove("add_category_id"); 145 } 146 if (productCategoryId != null && productCategoryId.length() == 0) { 147 productCategoryId = null; 148 } 149 150 if (productId == null) { 151 if (paramMap.containsKey("ADD_ITEM_TYPE")) { 153 itemType = (String ) paramMap.remove("ADD_ITEM_TYPE"); 154 } else if (paramMap.containsKey("add_item_type")) { 155 itemType = (String ) paramMap.remove("add_item_type"); 156 } else { 157 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource, "cart.addToCart.noProductInfoPassed", locale)); 158 return "success"; } 160 } else { 161 try { 162 String pId = ProductWorker.findProductId(delegator, productId); 163 if (pId != null) { 164 productId = pId; 165 } 166 } catch (Throwable e) { 167 Debug.logWarning(e, module); 168 } 169 } 170 171 if (paramMap.containsKey("ADD_ITEM_DESCRIPTION")) { 173 itemDescription = (String ) paramMap.remove("ADD_ITEM_DESCRIPTION"); 174 } else if (paramMap.containsKey("add_item_description")) { 175 itemDescription = (String ) paramMap.remove("add_item_description"); 176 } 177 if (itemDescription != null && itemDescription.length() == 0) { 178 itemDescription = null; 179 } 180 181 ProductConfigWrapper configWrapper = null; 183 configWrapper = ProductConfigWorker.getProductConfigWrapper(productId, cart.getCurrency(), request); 184 185 if (configWrapper != null) { 186 ProductConfigWorker.fillProductConfigWrapper(configWrapper, request); 188 if (!configWrapper.isCompleted()) { 189 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource, "cart.addToCart.productConfigurationIsNotValid", locale)); 191 return "error"; 192 } 193 } 194 195 if (paramMap.containsKey("PRICE")) { 197 priceStr = (String ) paramMap.remove("PRICE"); 198 } else if (paramMap.containsKey("price")) { 199 priceStr = (String ) paramMap.remove("price"); 200 } 201 if (priceStr == null) { 202 priceStr = "0"; } 204 205 if (paramMap.containsKey("reservStart")) { 207 reservStartStr = (String ) paramMap.remove("reservStart"); 208 if (reservStartStr.length() == 10) reservStartStr += " 00:00:00.000000000"; if (reservStartStr.length() >0) { 211 try { 212 reservStart = java.sql.Timestamp.valueOf((String ) reservStartStr); 213 } catch (Exception e) { 214 Debug.logWarning(e,"Problems parsing Reservation start string: " 215 + reservStartStr, module); 216 reservStart = null; 217 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource,"cart.addToCart.rental.startDate", locale)); 218 return "error"; 219 } 220 } 221 else reservStart = null; 222 223 if (paramMap.containsKey("reservEnd")) { 224 reservEndStr = (String ) paramMap.remove("reservEnd"); 225 if (reservEndStr.length() == 10) reservEndStr += " 00:00:00.000000000"; if (reservEndStr.length() > 0) { 228 try { 229 reservEnd = java.sql.Timestamp.valueOf((String ) reservEndStr); 230 } catch (Exception e) { 231 Debug.logWarning(e,"Problems parsing Reservation end string: " + reservEndStr, module); 232 reservEnd = null; 233 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource,"cart.addToCart.rental.endDate", locale)); 234 return "error"; 235 } 236 } 237 else reservEnd = null; 238 } 239 240 if (reservStart != null && reservEnd != null) { 241 reservLength = UtilDateTime.getInterval(reservStart,reservEnd)/86400000; 242 } 243 244 245 246 if (reservStart != null && paramMap.containsKey("reservLength")) { 247 reservLengthStr = (String ) paramMap.remove("reservLength"); 248 try { 250 reservLength = nf.parse(reservLengthStr).doubleValue(); 251 } catch (Exception e) { 252 Debug.logWarning(e,"Problems parsing reservation length string: " 253 + reservLengthStr, module); 254 reservLength = 1; 255 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderReservationLengthShouldBeAPositiveNumber", locale)); 256 return "error"; 257 } 258 } 259 260 if (reservStart != null && paramMap.containsKey("reservPersons")) { 261 reservPersonsStr = (String ) paramMap.remove("reservPersons"); 262 try { 264 reservPersons = nf.parse(reservPersonsStr).doubleValue(); 265 } catch (Exception e) { 266 Debug.logWarning(e,"Problems parsing reservation number of persons string: " + reservPersonsStr, module); 267 reservPersons = 1; 268 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderNumberOfPersonsShouldBeOneOrLarger", locale)); 269 return "error"; 270 } 271 } 272 } 273 274 if (paramMap.containsKey("QUANTITY")) { 276 quantityStr = (String ) paramMap.remove("QUANTITY"); 277 } else if (paramMap.containsKey("quantity")) { 278 quantityStr = (String ) paramMap.remove("quantity"); 279 } 280 if (quantityStr == null) { 281 quantityStr = "1"; } 283 284 try { 286 price = nf.parse(priceStr).doubleValue(); 287 } catch (Exception e) { 288 Debug.logWarning(e, "Problems parsing price string: " + priceStr, module); 289 price = 0.00; 290 } 291 292 try { 294 quantity = nf.parse(quantityStr).doubleValue(); 295 } catch (Exception e) { 296 Debug.logWarning(e, "Problems parsing quantity string: " + quantityStr, module); 297 quantity = 1; 298 } 299 300 String selectedAmountStr = "0.00"; 302 if (paramMap.containsKey("ADD_AMOUNT")) { 303 selectedAmountStr = (String ) paramMap.remove("ADD_AMOUNT"); 304 } else if (paramMap.containsKey("add_amount")) { 305 selectedAmountStr = (String ) paramMap.remove("add_amount"); 306 } 307 308 double amount = 0.00; 310 if (selectedAmountStr != null && selectedAmountStr.length() > 0) { 311 try { 312 amount = nf.parse(selectedAmountStr).doubleValue(); 313 } catch (Exception e) { 314 Debug.logWarning(e, "Problem parsing amount string: " + selectedAmountStr, module); 315 amount = 0.00; 316 } 317 } 318 319 shipBeforeDateStr = (String ) paramMap.remove("shipBeforeDate"); 321 if (shipBeforeDateStr != null && shipBeforeDateStr.length() > 0) { 322 if (shipBeforeDateStr.length() == 10) shipBeforeDateStr += " 00:00:00.000"; 323 try { 324 shipBeforeDate = java.sql.Timestamp.valueOf(shipBeforeDateStr); 325 } catch (IllegalArgumentException e) { 326 Debug.logWarning(e, "Bad shipBeforeDate input: " + e.getMessage(), module); 327 shipBeforeDate = null; 328 } 329 } 330 331 shipAfterDateStr = (String ) paramMap.remove("shipAfterDate"); 333 if (shipAfterDateStr != null && shipAfterDateStr.length() > 0) { 334 if (shipAfterDateStr.length() == 10) shipAfterDateStr += " 00:00:00.000"; 335 try { 336 shipAfterDate = java.sql.Timestamp.valueOf(shipAfterDateStr); 337 } catch (IllegalArgumentException e) { 338 Debug.logWarning(e, "Bad shipAfterDate input: " + e.getMessage(), module); 339 shipAfterDate = null; 340 } 341 } 342 343 List surveyResponses = null; 345 if (productId != null) { 346 String productStoreId = ProductStoreWorker.getProductStoreId(request); 347 List productSurvey = ProductStoreWorker.getProductSurveys(delegator, productStoreId, productId, "CART_ADD"); 348 if (productSurvey != null && productSurvey.size() > 0) { 349 GenericValue survey = EntityUtil.getFirst(productSurvey); 351 String surveyResponseId = (String ) request.getAttribute("surveyResponseId"); 352 if (surveyResponseId != null) { 353 surveyResponses = UtilMisc.toList(surveyResponseId); 354 } else { 355 Map surveyContext = UtilHttp.getParameterMap(request); 356 GenericValue userLogin = cart.getUserLogin(); 357 String partyId = null; 358 if (userLogin != null) { 359 partyId = userLogin.getString("partyId"); 360 } 361 String formAction = "/additemsurvey"; 362 String nextPage = RequestHandler.getNextPageUri(request.getPathInfo()); 363 if (nextPage != null) { 364 formAction = formAction + "/" + nextPage; 365 } 366 ProductStoreSurveyWrapper wrapper = new ProductStoreSurveyWrapper(survey, partyId, surveyContext); 367 request.setAttribute("surveyWrapper", wrapper); 368 request.setAttribute("surveyAction", formAction); return "survey"; 370 } 371 } 372 } 373 if (surveyResponses != null) { 374 paramMap.put("surveyResponses", surveyResponses); 375 } 376 377 result = cartHelper.addToCart(catalogId, shoppingListId, shoppingListItemSeqId, productId, productCategoryId, 379 itemType, itemDescription, price, amount, quantity, reservStart, reservLength, reservPersons, shipBeforeDate, shipAfterDate, configWrapper, paramMap); 380 controlDirective = processResult(result, request); 381 382 if (controlDirective.equals(ERROR)) { 384 return "error"; 385 } else { 386 if (cart.viewCartOnAdd()) { 387 return "viewcart"; 388 } else { 389 return "success"; 390 } 391 } 392 } 393 394 public static String addToCartFromOrder(HttpServletRequest request, HttpServletResponse response) { 395 String orderId = request.getParameter("orderId"); 396 String [] itemIds = request.getParameterValues("item_id"); 397 399 ShoppingCart cart = getCartObject(request); 400 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 401 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 402 ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart); 403 String catalogId = CatalogWorker.getCurrentCatalogId(request); 404 Map result; 405 String controlDirective; 406 407 boolean addAll = ("true".equals(request.getParameter("add_all"))); 408 result = cartHelper.addToCartFromOrder(catalogId, orderId, itemIds, addAll); 409 controlDirective = processResult(result, request); 410 411 if (controlDirective.equals(ERROR)) { 413 return "error"; 414 } else { 415 return "success"; 416 } 417 } 418 419 423 public static String addToCartBulk(HttpServletRequest request, HttpServletResponse response) { 424 String categoryId = request.getParameter("category_id"); 425 ShoppingCart cart = getCartObject(request); 426 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 427 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 428 ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart); 429 String controlDirective; 430 Map result; 431 433 Map paramMap = UtilHttp.getParameterMap(request); 435 String catalogId = CatalogWorker.getCurrentCatalogId(request); 436 result = cartHelper.addToCartBulk(catalogId, categoryId, paramMap); 437 controlDirective = processResult(result, request); 438 439 if (controlDirective.equals(ERROR)) { 441 return "error"; 442 } else { 443 return "success"; 444 } 445 } 446 447 449 public static String addToCartBulkRequirements(HttpServletRequest request, HttpServletResponse response) { 450 ShoppingCart cart = getCartObject(request); 451 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 452 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 453 ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart); 454 String controlDirective; 455 Map result; 456 458 Map paramMap = UtilHttp.getParameterMap(request); 460 String catalogId = CatalogWorker.getCurrentCatalogId(request); 461 result = cartHelper.addToCartBulkRequirements(catalogId, paramMap); 462 controlDirective = processResult(result, request); 463 464 if (controlDirective.equals(ERROR)) { 466 return "error"; 467 } else { 468 return "success"; 469 } 470 } 471 472 476 public static String addCategoryDefaults(HttpServletRequest request, HttpServletResponse response) { 477 String categoryId = request.getParameter("category_id"); 478 String catalogId = CatalogWorker.getCurrentCatalogId(request); 479 ShoppingCart cart = getCartObject(request); 480 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 481 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 482 ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart); 483 String controlDirective; 484 Map result; 485 Double totalQuantity; 486 Locale locale = UtilHttp.getLocale(request); 487 488 result = cartHelper.addCategoryDefaults(catalogId, categoryId); 489 controlDirective = processResult(result, request); 490 491 if (controlDirective.equals(ERROR)) { 493 return "error"; 494 } else { 495 totalQuantity = (Double )result.get("totalQuantity"); 496 Map messageMap = UtilMisc.toMap("totalQuantity", UtilFormatOut.formatQuantity(totalQuantity) ); 497 498 request.setAttribute("_EVENT_MESSAGE_", 499 UtilProperties.getMessage(resource, "cart.add_category_defaults", 500 messageMap, locale )); 501 502 return "success"; 503 } 504 } 505 506 507 public static String deleteFromCart(HttpServletRequest request, HttpServletResponse response) { 508 ShoppingCart cart = getCartObject(request); 509 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 510 ShoppingCartHelper cartHelper = new ShoppingCartHelper(null, dispatcher, cart); 511 String controlDirective; 512 Map result; 513 Map paramMap = UtilHttp.getParameterMap(request); 514 516 result = cartHelper.deleteFromCart(paramMap); 518 controlDirective = processResult(result, request); 519 520 if (controlDirective.equals(ERROR)) { 522 return "error"; 523 } else { 524 return "success"; 525 } 526 } 527 528 529 public static String modifyCart(HttpServletRequest request, HttpServletResponse response) { 530 HttpSession session = request.getSession(); 531 ShoppingCart cart = getCartObject(request); 532 Locale locale = UtilHttp.getLocale(request); 533 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 534 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 535 Security security = (Security) request.getAttribute("security"); 536 ShoppingCartHelper cartHelper = new ShoppingCartHelper(null, dispatcher, cart); 537 String controlDirective; 538 Map result; 539 541 Map paramMap = UtilHttp.getParameterMap(request); 542 543 String removeSelectedFlag = request.getParameter("removeSelected"); 544 String selectedItems[] = request.getParameterValues("selectedItem"); 545 boolean removeSelected = ("true".equals(removeSelectedFlag) && selectedItems != null && selectedItems.length > 0); 546 result = cartHelper.modifyCart(security, userLogin, paramMap, removeSelected, selectedItems, locale); 547 controlDirective = processResult(result, request); 548 549 if (controlDirective.equals(ERROR)) { 551 return "error"; 552 } else { 553 return "success"; 554 } 555 } 556 557 558 public static String clearCart(HttpServletRequest request, HttpServletResponse response) { 559 ShoppingCart cart = getCartObject(request); 560 cart.clear(); 561 562 HttpSession session = request.getSession(); 564 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 565 if ((userLogin != null) && ("anonymous".equals(userLogin.get("userLoginId")))) { 566 session.invalidate(); 568 session = request.getSession(true); 569 570 request.setAttribute("temporaryAnonymousUserLogin", userLogin); 572 573 Debug.logInfo("Doing clearCart for anonymous user, so logging out but put anonymous userLogin in userLogin request attribute", module); 574 } 575 576 return "success"; 577 } 578 579 580 public static String destroyCart(HttpServletRequest request, HttpServletResponse response) { 581 HttpSession session = request.getSession(); 582 clearCart(request, response); 583 session.removeAttribute("shoppingCart"); 584 session.removeAttribute("orderPartyId"); 585 session.removeAttribute("orderMode"); 586 session.removeAttribute("productStoreId"); 587 session.removeAttribute("CURRENT_CATALOG_ID"); 588 return "success"; 589 } 590 591 592 public static ShoppingCart getCartObject(HttpServletRequest request, Locale locale, String currencyUom) { 593 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 594 ShoppingCart cart = (ShoppingCart) request.getAttribute("shoppingCart"); 595 HttpSession session = request.getSession(true); 596 if (cart == null) { 597 cart = (ShoppingCart) session.getAttribute("shoppingCart"); 598 } else { 599 session.setAttribute("shoppingCart", cart); 600 } 601 602 if (cart == null) { 603 if (locale == null) { 604 locale = UtilHttp.getLocale(request); 605 } 606 if (currencyUom == null) { 607 currencyUom = UtilHttp.getCurrencyUom(request); 608 } 609 cart = new WebShoppingCart(request, locale, currencyUom); 610 session.setAttribute("shoppingCart", cart); 611 } else { 612 if (locale != null && !locale.equals(cart.getLocale())) { 613 cart.setLocale(locale); 614 } 615 if (currencyUom != null && !currencyUom.equals(cart.getCurrency())) { 616 try { 617 cart.setCurrency(dispatcher, currencyUom); 618 } catch (CartItemModifyException e) { 619 Debug.logError(e, "Unable to modify currency in cart", module); 620 } 621 } 622 } 623 return cart; 624 } 625 626 627 public static ShoppingCart getCartObject(HttpServletRequest request) { 628 return getCartObject(request, null, null); 629 } 630 631 632 public static String keepCartUpdated(HttpServletRequest request, HttpServletResponse response) { 633 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 634 HttpSession session = request.getSession(); 635 ShoppingCart cart = getCartObject(request); 636 637 if (cart.getUserLogin() == null) { 639 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 640 if (userLogin != null) { 641 try { 642 cart.setUserLogin(userLogin, dispatcher); 643 } catch (CartItemModifyException e) { 644 Debug.logWarning(e, module); 645 } 646 } 647 } 648 649 if (cart.getAutoUserLogin() == null) { 651 GenericValue autoUserLogin = (GenericValue) session.getAttribute("autoUserLogin"); 652 if (autoUserLogin != null) { 653 if (cart.getUserLogin() == null) { 654 try { 655 cart.setAutoUserLogin(autoUserLogin, dispatcher); 656 } catch (CartItemModifyException e) { 657 Debug.logWarning(e, module); 658 } 659 } else { 660 cart.setAutoUserLogin(autoUserLogin); 661 } 662 } 663 } 664 665 Locale locale = UtilHttp.getLocale(request); 667 if (cart.getLocale() == null || !locale.equals(cart.getLocale())) { 668 cart.setLocale(locale); 669 } 670 671 return "success"; 672 } 673 674 675 public static String setDesiredAlternateGwpProductId(HttpServletRequest request, HttpServletResponse response) { 676 ShoppingCart cart = getCartObject(request); 677 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 678 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 679 String alternateGwpProductId = request.getParameter("alternateGwpProductId"); 680 String alternateGwpLineStr = request.getParameter("alternateGwpLine"); 681 Locale locale = UtilHttp.getLocale(request); 682 683 if (UtilValidate.isEmpty(alternateGwpProductId)) { 684 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderCouldNotSelectAlternateGiftNoAlternateGwpProductIdPassed", locale)); 685 return "error"; 686 } 687 if (UtilValidate.isEmpty(alternateGwpLineStr)) { 688 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderCouldNotSelectAlternateGiftNoAlternateGwpLinePassed", locale)); 689 return "error"; 690 } 691 692 int alternateGwpLine = 0; 693 try { 694 alternateGwpLine = Integer.parseInt(alternateGwpLineStr); 695 } catch (Exception e) { 696 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderCouldNotSelectAlternateGiftAlternateGwpLineIsNotAValidNumber", locale)); 697 return "error"; 698 } 699 700 ShoppingCartItem cartLine = cart.findCartItem(alternateGwpLine); 701 if (cartLine == null) { 702 request.setAttribute("_ERROR_MESSAGE_", "Could not select alternate gift, no cart line item found for #" + alternateGwpLine + "."); 703 return "error"; 704 } 705 706 if (cartLine.getIsPromo()) { 707 Iterator checkOrderAdjustments = UtilMisc.toIterator(cartLine.getAdjustments()); 709 while (checkOrderAdjustments != null && checkOrderAdjustments.hasNext()) { 710 GenericValue checkOrderAdjustment = (GenericValue) checkOrderAdjustments.next(); 711 if (UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoId")) && 712 UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoRuleId")) && 713 UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoActionSeqId"))) { 714 GenericPK productPromoActionPk = delegator.makeValidValue("ProductPromoAction", checkOrderAdjustment).getPrimaryKey(); 715 cart.setDesiredAlternateGiftByAction(productPromoActionPk, alternateGwpProductId); 716 if (cart.getOrderType().equals("SALES_ORDER")) { 717 org.ofbiz.order.shoppingcart.product.ProductPromoWorker.doPromotions(cart, dispatcher); 718 } 719 return "success"; 720 } 721 } 722 } 723 724 request.setAttribute("_ERROR_MESSAGE_", "Could not select alternate gift, cart line item found for #" + alternateGwpLine + " does not appear to be a valid promotional gift."); 725 return "error"; 726 } 727 728 729 public static String addAdditionalParty(HttpServletRequest request, HttpServletResponse response) { 730 ShoppingCart cart = getCartObject(request); 731 String partyId = request.getParameter("additionalPartyId"); 732 String roleTypeId[] = request.getParameterValues("additionalRoleTypeId"); 733 List eventList = new LinkedList (); 734 Locale locale = UtilHttp.getLocale(request); 735 int i; 736 737 if (UtilValidate.isEmpty(partyId) || roleTypeId.length < 1) { 738 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderPartyIdAndOrRoleTypeIdNotDefined", locale)); 739 return "error"; 740 } 741 742 if (request.getAttribute("_EVENT_MESSAGE_LIST_") != null) { 743 eventList.addAll((List ) request.getAttribute("_EVENT_MESSAGE_LIST_")); 744 } 745 746 for (i = 0; i < roleTypeId.length; i++) { 747 try { 748 cart.addAdditionalPartyRole(partyId, roleTypeId[i]); 749 } catch (Exception e) { 750 eventList.add(e.getLocalizedMessage()); 751 } 752 } 753 754 request.removeAttribute("_EVENT_MESSAGE_LIST_"); 755 request.setAttribute("_EVENT_MESSAGE_LIST_", eventList); 756 return "success"; 757 } 758 759 760 public static String removeAdditionalParty(HttpServletRequest request, HttpServletResponse response) { 761 ShoppingCart cart = getCartObject(request); 762 String partyId = request.getParameter("additionalPartyId"); 763 String roleTypeId[] = request.getParameterValues("additionalRoleTypeId"); 764 List eventList = new LinkedList (); 765 Locale locale = UtilHttp.getLocale(request); 766 int i; 767 768 if (UtilValidate.isEmpty(partyId) || roleTypeId.length < 1) { 769 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderPartyIdAndOrRoleTypeIdNotDefined", locale)); 770 return "error"; 771 } 772 773 if (request.getAttribute("_EVENT_MESSAGE_LIST_") != null) { 774 eventList.addAll((List ) request.getAttribute("_EVENT_MESSAGE_LIST_")); 775 } 776 777 for (i = 0; i < roleTypeId.length; i++) { 778 try { 779 cart.removeAdditionalPartyRole(partyId, roleTypeId[i]); 780 } catch (Exception e) { 781 Debug.logInfo(e.getLocalizedMessage(), module); 782 eventList.add(e.getLocalizedMessage()); 783 } 784 } 785 786 request.removeAttribute("_EVENT_MESSAGE_LIST_"); 787 request.setAttribute("_EVENT_MESSAGE_LIST_", eventList); 788 return "success"; 789 } 790 791 804 private static String processResult(Map result, HttpServletRequest request) { 805 StringBuffer errMsg = new StringBuffer (); 807 if (result.containsKey(ModelService.ERROR_MESSAGE_LIST)) { 808 List errorMsgs = (List )result.get(ModelService.ERROR_MESSAGE_LIST); 809 Iterator iterator = errorMsgs.iterator(); 810 errMsg.append("<ul>"); 811 while (iterator.hasNext()) { 812 errMsg.append("<li>"); 813 errMsg.append(iterator.next()); 814 errMsg.append("</li>"); 815 } 816 errMsg.append("</ul>"); 817 } else if (result.containsKey(ModelService.ERROR_MESSAGE)) { 818 errMsg.append(result.get(ModelService.ERROR_MESSAGE)); 819 request.setAttribute("_ERROR_MESSAGE_", errMsg.toString()); 820 } 821 822 if (errMsg.length() > 0) { 824 request.setAttribute("_ERROR_MESSAGE_", errMsg.toString()); 825 if (result.get(ModelService.RESPONSE_MESSAGE).equals(ModelService.RESPOND_SUCCESS)) { 826 return NON_CRITICAL_ERROR; 827 } else { 828 return ERROR; 829 } 830 } else { 831 return NO_ERROR; 832 } 833 } 834 835 836 public static String selectAgreement(HttpServletRequest request, HttpServletResponse response) { 837 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 838 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 839 ShoppingCart cart = getCartObject(request); 840 ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart); 841 String agreementId = request.getParameter("agreementId"); 842 Map result = cartHelper.selectAgreement(agreementId); 843 if (ServiceUtil.isError(result)) { 844 request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(result)); 845 return "error"; 846 } 847 return "success"; 848 } 849 850 851 public static String setCurrency(HttpServletRequest request, HttpServletResponse response) { 852 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 853 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 854 ShoppingCart cart = getCartObject(request); 855 ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart); 856 String currencyUomId = request.getParameter("currencyUomId"); 857 Map result = cartHelper.setCurrency(currencyUomId); 858 if (ServiceUtil.isError(result)) { 859 request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(result)); 860 return "error"; 861 } 862 return "success"; 863 } 864 865 866 public static String addOrderTerm(HttpServletRequest request, HttpServletResponse response) { 867 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 868 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 869 ShoppingCart cart = getCartObject(request); 870 ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart); 871 String termTypeId = request.getParameter("termTypeId"); 872 String termValue = request.getParameter("termValue"); 873 String termDays = request.getParameter("termDays"); 874 String termIndex = request.getParameter("termIndex"); 875 Locale locale = UtilHttp.getLocale(request); 876 877 Double dTermValue = null; 878 Long lTermDays = null; 879 880 if (termValue.trim().equals("")) { 881 termValue = null; 882 } 883 if (termDays.trim().equals("")) { 884 termDays = null; 885 } 886 if (UtilValidate.isEmpty(termTypeId)) { 887 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderOrderTermTypeIsRequired", locale)); 888 return "error"; 889 } 890 if (!UtilValidate.isSignedDouble(termValue)) { 891 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderOrderTermValue", UtilMisc.toMap("orderTermValue",UtilValidate.isSignedFloatMsg), locale)); 892 return "error"; 893 } 894 if (termValue != null) { 895 dTermValue =new Double (termValue); 896 } 897 if (!UtilValidate.isInteger(termDays)) { 898 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderOrderTermDays", UtilMisc.toMap("orderTermDays",UtilValidate.isLongMsg), locale)); 899 return "error"; 900 } 901 if (termDays != null) { 902 lTermDays = new Long (termDays); 903 } 904 if ((termIndex != null) && (termIndex != "-1") && (UtilValidate.isInteger(termIndex))) { 905 cartHelper.removeOrderTerm(Integer.parseInt(termIndex)); 906 } 907 908 Map result = cartHelper.addOrderTerm(termTypeId, dTermValue, lTermDays); 909 if (ServiceUtil.isError(result)) { 910 request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(result)); 911 return "error"; 912 } 913 return "success"; 914 } 915 916 917 public static String removeOrderTerm(HttpServletRequest request, HttpServletResponse response) { 918 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 919 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 920 ShoppingCart cart = getCartObject(request); 921 ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart); 922 String index = request.getParameter("termIndex"); 923 Map result = cartHelper.removeOrderTerm(Integer.parseInt(index)); 924 if (ServiceUtil.isError(result)) { 925 request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(result)); 926 return "error"; 927 } 928 return "success"; 929 } 930 931 932 public static String loadCartFromShoppingList(HttpServletRequest request, HttpServletResponse response) { 933 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 934 HttpSession session = request.getSession(); 935 GenericValue userLogin = (GenericValue)session.getAttribute("userLogin"); 936 Locale locale = UtilHttp.getLocale(request); 937 938 String shoppingListId = request.getParameter("shoppingListId"); 939 940 ShoppingCart cart = null; 941 try { 942 Map outMap = dispatcher.runSync("loadCartFromShoppingList", 943 UtilMisc.toMap("shoppingListId", shoppingListId, 944 "userLogin", userLogin)); 945 cart = (ShoppingCart)outMap.get("shoppingCart"); 946 } catch(GenericServiceException exc) { 947 request.setAttribute("_ERROR_MESSAGE_", exc.getMessage()); 948 return "error"; 949 } 950 951 session.setAttribute("shoppingCart", cart); 952 session.setAttribute("productStoreId", cart.getProductStoreId()); 953 session.setAttribute("orderMode", cart.getOrderType()); 954 session.setAttribute("orderPartyId", cart.getOrderPartyId()); 955 956 return "success"; 957 } 958 959 960 public static String loadCartFromQuote(HttpServletRequest request, HttpServletResponse response) { 961 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 962 HttpSession session = request.getSession(); 963 GenericValue userLogin = (GenericValue)session.getAttribute("userLogin"); 964 Locale locale = UtilHttp.getLocale(request); 965 966 String quoteId = request.getParameter("quoteId"); 967 968 ShoppingCart cart = null; 969 try { 970 Map outMap = dispatcher.runSync("loadCartFromQuote", 971 UtilMisc.toMap("quoteId", quoteId, 972 "applyQuoteAdjustments", "true", 973 "userLogin", userLogin)); 974 cart = (ShoppingCart) outMap.get("shoppingCart"); 975 } catch (GenericServiceException exc) { 976 request.setAttribute("_ERROR_MESSAGE_", exc.getMessage()); 977 return "error"; 978 } 979 980 cart.setDefaultCheckoutOptions(dispatcher); 982 cart.setReadOnlyCart(true); 984 985 session.setAttribute("shoppingCart", cart); 986 session.setAttribute("productStoreId", cart.getProductStoreId()); 987 session.setAttribute("orderMode", cart.getOrderType()); 988 session.setAttribute("orderPartyId", cart.getOrderPartyId()); 989 990 return "success"; 991 } 992 993 public static String createQuoteFromCart(HttpServletRequest request, HttpServletResponse response) { 994 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 995 HttpSession session = request.getSession(); 996 GenericValue userLogin = (GenericValue)session.getAttribute("userLogin"); 997 String destroyCart = request.getParameter("destroyCart"); 998 999 ShoppingCart cart = getCartObject(request); 1000 Map result = null; 1001 String quoteId = null; 1002 try { 1003 result = dispatcher.runSync("createQuoteFromCart", 1004 UtilMisc.toMap("cart", cart, 1005 "userLogin", userLogin)); 1006 quoteId = (String ) result.get("quoteId"); 1007 } catch (GenericServiceException exc) { 1008 request.setAttribute("_ERROR_MESSAGE_", exc.getMessage()); 1009 return "error"; 1010 } 1011 if (ServiceUtil.isError(result)) { 1012 request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(result)); 1013 return "error"; 1014 } 1015 request.setAttribute("quoteId", quoteId); 1016 if (destroyCart != null && destroyCart.equals("Y")) { 1017 ShoppingCartEvents.destroyCart(request, response); 1018 } 1019 1020 return "success"; 1021 } 1022 1023 public static String createCustRequestFromCart(HttpServletRequest request, HttpServletResponse response) { 1024 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 1025 HttpSession session = request.getSession(); 1026 GenericValue userLogin = (GenericValue)session.getAttribute("userLogin"); 1027 String destroyCart = request.getParameter("destroyCart"); 1028 1029 ShoppingCart cart = getCartObject(request); 1030 Map result = null; 1031 String custRequestId = null; 1032 try { 1033 result = dispatcher.runSync("createCustRequestFromCart", 1034 UtilMisc.toMap("cart", cart, 1035 "userLogin", userLogin)); 1036 custRequestId = (String ) result.get("custRequestId"); 1037 } catch (GenericServiceException exc) { 1038 request.setAttribute("_ERROR_MESSAGE_", exc.getMessage()); 1039 return "error"; 1040 } 1041 if (ServiceUtil.isError(result)) { 1042 request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(result)); 1043 return "error"; 1044 } 1045 request.setAttribute("custRequestId", custRequestId); 1046 if (destroyCart != null && destroyCart.equals("Y")) { 1047 ShoppingCartEvents.destroyCart(request, response); 1048 } 1049 1050 return "success"; 1051 } 1052 1053 1054 public static String initializeOrderEntry(HttpServletRequest request, HttpServletResponse response) { 1055 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 1056 HttpSession session = request.getSession(); 1057 Security security = (Security) request.getAttribute("security"); 1058 GenericValue userLogin = (GenericValue)session.getAttribute("userLogin"); 1059 String finalizeMode = (String )session.getAttribute("finalizeMode"); 1060 Locale locale = UtilHttp.getLocale(request); 1061 1062 String productStoreId = request.getParameter("productStoreId"); 1063 1064 if (UtilValidate.isNotEmpty(productStoreId)) { 1065 session.setAttribute("productStoreId", productStoreId); 1066 } 1067 ShoppingCart cart = getCartObject(request); 1068 1069 String orderMode = request.getParameter("orderMode"); 1071 if (orderMode != null) { 1072 cart.setOrderType(orderMode); 1073 session.setAttribute("orderMode", orderMode); 1074 } else { 1075 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderPleaseSelectEitherSaleOrPurchaseOrder", locale)); 1076 return "error"; 1077 } 1078 1079 GenericValue productStore = null; 1081 if (UtilValidate.isNotEmpty(productStoreId)) { 1082 productStore = ProductStoreWorker.getProductStore(productStoreId, delegator); 1083 if (productStore != null) { 1084 1085 boolean hasPermission = false; 1087 if ((cart.getOrderType().equals("PURCHASE_ORDER")) && (security.hasEntityPermission("ORDERMGR", "_PURCHASE_CREATE", session))) { 1088 hasPermission = true; 1089 } else if (cart.getOrderType().equals("SALES_ORDER")) { 1090 if (security.hasEntityPermission("ORDERMGR", "_SALES_CREATE", session)) { 1091 hasPermission = true; 1092 } else { 1093 List storeReps = null; 1095 try { 1096 storeReps = delegator.findByAnd("ProductStoreRole", UtilMisc.toMap("productStoreId", productStore.getString("productStoreId"), 1097 "partyId", userLogin.getString("partyId"), "roleTypeId", "SALES_REP")); 1098 } catch(GenericEntityException gee) { 1099 } 1101 storeReps = EntityUtil.filterByDate(storeReps); 1102 if (storeReps != null && storeReps.size() > 0) { 1103 hasPermission = true; 1104 } 1105 } 1106 } 1107 1108 if (hasPermission) { 1109 cart = ShoppingCartEvents.getCartObject(request, null, productStore.getString("defaultCurrencyUomId")); 1110 } else { 1111 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderYouDoNotHavePermissionToTakeOrdersForThisStore", locale)); 1112 cart.clear(); 1113 session.removeAttribute("orderMode"); 1114 return "error"; 1115 } 1116 cart.setProductStoreId(productStoreId); 1117 } else { 1118 cart.setProductStoreId(null); 1119 } 1120 } 1121 1122 if ("SALES_ORDER".equals(cart.getOrderType()) && UtilValidate.isEmpty(cart.getProductStoreId())) { 1123 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderAProductStoreMustBeSelectedForASalesOrder", locale)); 1124 cart.clear(); 1125 session.removeAttribute("orderMode"); 1126 return "error"; 1127 } 1128 1129 String salesChannelEnumId = request.getParameter("salesChannelEnumId"); 1130 if (UtilValidate.isNotEmpty(salesChannelEnumId)) { 1131 cart.setChannelType(salesChannelEnumId); 1132 } 1133 1134 String partyId = request.getParameter("supplierPartyId"); 1136 if (!UtilValidate.isEmpty(request.getParameter("partyId"))) { 1137 partyId = request.getParameter("partyId"); 1138 } 1139 String userLoginId = request.getParameter("userLoginId"); 1140 if (partyId != null || userLoginId != null) { 1141 if ((partyId == null || partyId.length() == 0) && userLoginId != null && userLoginId.length() > 0) { 1142 GenericValue thisUserLogin = null; 1143 try { 1144 thisUserLogin = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", userLoginId)); 1145 } catch(GenericEntityException gee) { 1146 } 1148 if (thisUserLogin != null) { 1149 partyId = thisUserLogin.getString("partyId"); 1150 } else { 1151 partyId = userLoginId; 1152 } 1153 } 1154 if (partyId != null && partyId.length() > 0) { 1155 GenericValue thisParty = null; 1156 try{ 1157 thisParty = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId)); 1158 } catch(GenericEntityException gee) { 1159 } 1161 if (thisParty == null) { 1162 request.setAttribute("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error,"OrderCouldNotLocateTheSelectedParty", locale)); 1163 return "error"; 1164 } else { 1165 cart.setOrderPartyId(partyId); 1166 } 1167 } else if (partyId != null && partyId.length() == 0) { 1168 cart.setOrderPartyId("_NA_"); 1169 partyId = null; 1170 } 1171 } else { 1172 partyId = cart.getPartyId(); 1173 if (partyId != null && partyId.equals("_NA_")) partyId = null; 1174 } 1175 1176 return "success"; 1177 } 1178 1179 1180 public static String routeOrderEntry(HttpServletRequest request, HttpServletResponse response) { 1181 HttpSession session = request.getSession(); 1182 1183 if (session.getAttribute("orderMode") == null) { 1185 return "init"; 1186 } 1187 1188 if (request.getParameter("orderMode") != null) { 1190 return "agreements"; } 1192 1193 return "cart"; 1195 } 1196 1197 public static String doManualPromotions(HttpServletRequest request, HttpServletResponse response) { 1198 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 1199 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 1200 ShoppingCart cart = getCartObject(request); 1201 List manualPromotions = new LinkedList (); 1202 1203 Map context = UtilHttp.getParameterMap(request); 1205 String keyPrefix = "productPromoId_"; 1206 for (int i = 1; i <= 50; i++) { 1207 String productPromoId = (String )context.get(keyPrefix + i); 1208 if (UtilValidate.isNotEmpty(productPromoId)) { 1209 try { 1210 GenericValue promo = delegator.findByPrimaryKey("ProductPromo", UtilMisc.toMap("productPromoId", productPromoId)); 1211 if (promo != null) { 1212 manualPromotions.add(promo); 1213 } 1214 } catch(GenericEntityException gee) { 1215 request.setAttribute("_ERROR_MESSAGE_", gee.getMessage()); 1216 return "error"; 1217 } 1218 } else { 1219 break; 1220 } 1221 } 1222 ProductPromoWorker.doPromotions(cart, manualPromotions, dispatcher); 1223 return "success"; 1224 } 1225 1226 1227 public static String bulkAddProducts(HttpServletRequest request, HttpServletResponse response) { 1228 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 1229 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 1230 ShoppingCart cart = ShoppingCartEvents.getCartObject(request); 1231 ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart); 1232 String controlDirective = null; 1233 Map result = null; 1234 String productId = null; 1235 String productCategoryId = null; 1236 String quantityStr = null; 1237 String itemDesiredDeliveryDateStr = null; 1238 double quantity = 0; 1239 String catalogId = CatalogWorker.getCurrentCatalogId(request); 1240 1241 String rowCountField = null; 1242 int rowCount = 0; String DELIMITER = "_o_"; 1245 Map paramMap = UtilHttp.getParameterMap(request); 1247 1248 String shoppingListId = request.getParameter("shoppingListId"); 1250 String shoppingListItemSeqId = request.getParameter("shoppingListItemSeqId"); 1251 1252 if (paramMap.containsKey("_rowCount")) { 1254 rowCountField = (String ) paramMap.remove("_rowCount"); 1255 } else { 1256 Debug.logWarning("No _rowCount was passed in", ShoppingCartEvents.module); 1257 } 1258 try { 1259 rowCount = Integer.parseInt(rowCountField); 1260 } catch (NumberFormatException e) { 1261 Debug.logWarning("Invalid value for rowCount =" + rowCountField, module); 1262 } 1263 1264 if (rowCount < 1) { 1265 Debug.logWarning("No rows to process, as rowCount = " + rowCount, module); 1266 } else { 1267 for (int i = 0; i < rowCount; i++) { 1268 controlDirective = null; String thisSuffix = DELIMITER + i; 1271 if (paramMap.containsKey("productId" + thisSuffix)) { 1273 productId = (String ) paramMap.remove("productId" + thisSuffix); 1274 } 1275 1276 if (paramMap.containsKey("quantity" + thisSuffix)) { 1277 quantityStr = (String ) paramMap.remove("quantity" + thisSuffix); 1278 } 1279 if ((quantityStr == null) || (quantityStr.equals(""))){ quantityStr = "0"; } 1282 1283 try { 1285 quantity = NumberFormat.getNumberInstance().parse(quantityStr).doubleValue(); 1286 } catch (Exception e) { 1287 Debug.logWarning(e, "Problems parsing quantity string: " + quantityStr, module); 1288 quantity = 0; 1289 } 1290 1291 String selectedAmountStr = "0.00"; 1293 if (paramMap.containsKey("amount" + thisSuffix)) { 1294 selectedAmountStr = (String ) paramMap.remove("amount" + thisSuffix); 1295 } 1296 1297 double amount = 0.00; 1299 if (selectedAmountStr != null && selectedAmountStr.length() > 0) { 1300 try { 1301 amount = NumberFormat.getNumberInstance().parse(selectedAmountStr).doubleValue(); 1302 } catch (Exception e) { 1303 Debug.logWarning(e, "Problem parsing amount string: " + selectedAmountStr, module); 1304 amount = 0.00; 1305 } 1306 } 1307 1308 if (paramMap.containsKey("itemDesiredDeliveryDate" + thisSuffix)) { 1309 itemDesiredDeliveryDateStr = (String ) paramMap.remove("itemDesiredDeliveryDate" + thisSuffix); 1310 } 1311 1312 Map itemAttributes = UtilMisc.toMap("itemDesiredDeliveryDate", itemDesiredDeliveryDateStr); 1313 1314 if (quantity > 0) { 1315 Debug.logInfo("Attempting to add to cart with productId = " + productId + ", categoryId = " + productCategoryId + 1316 " and quantity = " + quantity, module); 1317 result = cartHelper.addToCart(catalogId, shoppingListId, shoppingListItemSeqId, productId, productCategoryId, 1318 "", "", 0.00, amount, quantity, null, 0.00, 0.00, null, null, null, itemAttributes); 1319 controlDirective = processResult(result, request); 1321 if (controlDirective.equals(ERROR)){ return "error"; 1323 } 1324 } 1325 } 1326 } 1327 1328 return cart.viewCartOnAdd() ? "viewcart" : "success"; 1330 } 1331 1332 public static String setOrderCurrencyAgreementShipDates(HttpServletRequest request, HttpServletResponse response) { 1334 LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); 1335 GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator"); 1336 ShoppingCart cart = getCartObject(request); 1337 ShoppingCartHelper cartHelper = new ShoppingCartHelper(delegator, dispatcher, cart); 1338 1339 String agreementId = (String ) request.getParameter("agreementId"); 1340 String currencyUomId = (String ) request.getParameter("currencyUomId"); 1341 String shipBeforeDateStr = (String ) request.getParameter("shipBeforeDate"); 1342 String shipAfterDateStr = (String ) request.getParameter("shipAfterDate"); 1343 Map result = null; 1344 1345 if (agreementId != null && agreementId.length() > 0) { 1347 result = cartHelper.selectAgreement(agreementId); 1348 } else { 1349 result = cartHelper.setCurrency(currencyUomId); 1350 } 1351 if (ServiceUtil.isError(result)) { 1352 request.setAttribute("_ERROR_MESSAGE_", ServiceUtil.getErrorMessage(result)); 1353 return "error"; 1354 } 1355 1356 try { 1358 if (UtilValidate.isNotEmpty(shipBeforeDateStr)) { 1359 if (shipBeforeDateStr.length() == 10) shipBeforeDateStr += " 00:00:00.000"; 1360 cart.setDefaultShipBeforeDate(java.sql.Timestamp.valueOf(shipBeforeDateStr)); 1361 } 1362 if (UtilValidate.isNotEmpty(shipAfterDateStr)) { 1363 if (shipAfterDateStr.length() == 10) shipAfterDateStr += " 00:00:00.000"; 1364 cart.setDefaultShipAfterDate(java.sql.Timestamp.valueOf(shipAfterDateStr)); 1365 } 1366 } catch (IllegalArgumentException e) { 1367 request.setAttribute("_ERROR_MESSAGE_", e.getMessage()); 1368 return "error"; 1369 } 1370 return "success"; 1371 } 1372} 1373 | Popular Tags |