public static String addBulkFromCart( Delegator delegator, LocalDispatcher dispatcher, ShoppingCart cart, GenericValue userLogin, String shoppingListId, String shoppingListTypeId, String[] items, boolean allowPromo, boolean append) throws IllegalArgumentException { String errMsg = null; if (items == null || items.length == 0) { errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.select_items_to_add_to_list", cart.getLocale()); throw new IllegalArgumentException(errMsg); } if (UtilValidate.isEmpty(shoppingListId)) { // create a new shopping list Map<String, Object> newListResult = null; try { newListResult = dispatcher.runSync( "createShoppingList", UtilMisc.<String, Object>toMap( "userLogin", userLogin, "productStoreId", cart.getProductStoreId(), "partyId", cart.getOrderPartyId(), "shoppingListTypeId", shoppingListTypeId, "currencyUom", cart.getCurrency())); } catch (GenericServiceException e) { Debug.logError(e, "Problems creating new ShoppingList", module); errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.cannot_create_new_shopping_list", cart.getLocale()); throw new IllegalArgumentException(errMsg); } // check for errors if (ServiceUtil.isError(newListResult)) { throw new IllegalArgumentException(ServiceUtil.getErrorMessage(newListResult)); } // get the new list id if (newListResult != null) { shoppingListId = (String) newListResult.get("shoppingListId"); } // if no list was created throw an error if (shoppingListId == null || shoppingListId.equals("")) { errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.shoppingListId_is_required_parameter", cart.getLocale()); throw new IllegalArgumentException(errMsg); } } else if (!append) { try { clearListInfo(delegator, shoppingListId); } catch (GenericEntityException e) { Debug.logError(e, module); throw new IllegalArgumentException( "Could not clear current shopping list: " + e.toString()); } } for (int i = 0; i < items.length; i++) { Integer cartIdInt = null; try { cartIdInt = Integer.valueOf(items[i]); } catch (Exception e) { Debug.logWarning( e, UtilProperties.getMessage( resource_error, "OrderIllegalCharacterInSelectedItemField", cart.getLocale()), module); } if (cartIdInt != null) { ShoppingCartItem item = cart.findCartItem(cartIdInt.intValue()); if (allowPromo || !item.getIsPromo()) { Debug.logInfo( "Adding cart item to shopping list [" + shoppingListId + "], allowPromo=" + allowPromo + ", item.getIsPromo()=" + item.getIsPromo() + ", item.getProductId()=" + item.getProductId() + ", item.getQuantity()=" + item.getQuantity(), module); Map<String, Object> serviceResult = null; try { Map<String, Object> ctx = UtilMisc.<String, Object>toMap( "userLogin", userLogin, "shoppingListId", shoppingListId, "productId", item.getProductId(), "quantity", item.getQuantity()); ctx.put("reservStart", item.getReservStart()); ctx.put("reservLength", item.getReservLength()); ctx.put("reservPersons", item.getReservPersons()); // ctx.put("accommodationMapId", item.getAccommodationMapId()); // ctx.put("accommodationSpotId", item.getAccommodationSpotId()); if (item.getConfigWrapper() != null) { ctx.put("configId", item.getConfigWrapper().getConfigId()); } serviceResult = dispatcher.runSync("createShoppingListItem", ctx); } catch (GenericServiceException e) { Debug.logError(e, "Problems creating ShoppingList item entity", module); errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.error_adding_item_to_shopping_list", cart.getLocale()); throw new IllegalArgumentException(errMsg); } // check for errors if (ServiceUtil.isError(serviceResult)) { throw new IllegalArgumentException(ServiceUtil.getErrorMessage(serviceResult)); } } } } // return the shoppinglist id return shoppingListId; }
public static String addListToCart( Delegator delegator, LocalDispatcher dispatcher, ShoppingCart cart, String prodCatalogId, String shoppingListId, boolean includeChild, boolean setAsListItem, boolean append) throws java.lang.IllegalArgumentException { String errMsg = null; // no list; no add if (shoppingListId == null) { errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.choose_shopping_list", cart.getLocale()); throw new IllegalArgumentException(errMsg); } // get the shopping list GenericValue shoppingList = null; List<GenericValue> shoppingListItems = null; try { shoppingList = EntityQuery.use(delegator) .from("ShoppingList") .where("shoppingListId", shoppingListId) .queryOne(); if (shoppingList == null) { errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.error_getting_shopping_list_and_items", cart.getLocale()); throw new IllegalArgumentException(errMsg); } shoppingListItems = shoppingList.getRelated("ShoppingListItem", null, null, false); if (shoppingListItems == null) { shoppingListItems = FastList.newInstance(); } // include all items of child lists if flagged to do so if (includeChild) { List<GenericValue> childShoppingLists = shoppingList.getRelated("ChildShoppingList", null, null, false); for (GenericValue v : childShoppingLists) { List<GenericValue> items = v.getRelated("ShoppingListItem", null, null, false); shoppingListItems.addAll(items); } } } catch (GenericEntityException e) { Debug.logError(e, "Problems getting ShoppingList and ShoppingListItem records", module); errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.error_getting_shopping_list_and_items", cart.getLocale()); throw new IllegalArgumentException(errMsg); } // no items; not an error; just mention that nothing was added if (UtilValidate.isEmpty(shoppingListItems)) { errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.no_items_added", cart.getLocale()); return errMsg; } // check if we are to clear the cart first if (!append) { cart.clear(); // Prevent the system from creating a new shopping list every time the cart is restored for // anonymous user. cart.setAutoSaveListId(shoppingListId); } // get the survey info for all the items Map<String, List<String>> shoppingListSurveyInfo = getItemSurveyInfos(shoppingListItems); // add the items StringBuilder eventMessage = new StringBuilder(); for (GenericValue shoppingListItem : shoppingListItems) { String productId = shoppingListItem.getString("productId"); BigDecimal quantity = shoppingListItem.getBigDecimal("quantity"); Timestamp reservStart = shoppingListItem.getTimestamp("reservStart"); BigDecimal reservLength = shoppingListItem.getBigDecimal("reservLength"); BigDecimal reservPersons = shoppingListItem.getBigDecimal("reservPersons"); // String accommodationMapId = shoppingListItem.getString("accommodationMapId"); // String accommodationSpotId = shoppingListItem.getString("accommodationSpotId"); String configId = shoppingListItem.getString("configId"); try { String listId = shoppingListItem.getString("shoppingListId"); String itemId = shoppingListItem.getString("shoppingListItemSeqId"); Map<String, Object> attributes = FastMap.newInstance(); // list items are noted in the shopping cart if (setAsListItem) { attributes.put("shoppingListId", listId); attributes.put("shoppingListItemSeqId", itemId); } // check if we have existing survey responses to append if (shoppingListSurveyInfo.containsKey(listId + "." + itemId) && UtilValidate.isNotEmpty(shoppingListSurveyInfo.get(listId + "." + itemId))) { attributes.put("surveyResponses", shoppingListSurveyInfo.get(listId + "." + itemId)); } ProductConfigWrapper configWrapper = null; if (UtilValidate.isNotEmpty(configId)) { configWrapper = ProductConfigWorker.loadProductConfigWrapper( delegator, dispatcher, configId, productId, cart.getProductStoreId(), prodCatalogId, cart.getWebSiteId(), cart.getCurrency(), cart.getLocale(), cart.getAutoUserLogin()); } // TODO: add code to check for survey response requirement // i cannot get the addOrDecrease function to accept a null reservStart field: i get a null // pointer exception a null constant works.... if (reservStart == null) { cart.addOrIncreaseItem( productId, null, quantity, null, null, null, null, null, null, attributes, prodCatalogId, configWrapper, null, null, null, dispatcher); } else { cart.addOrIncreaseItem( productId, null, quantity, reservStart, reservLength, reservPersons, null, null, null, null, null, attributes, prodCatalogId, configWrapper, null, null, null, dispatcher); } Map<String, Object> messageMap = UtilMisc.<String, Object>toMap("productId", productId); errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.added_product_to_cart", messageMap, cart.getLocale()); eventMessage.append(errMsg).append("\n"); } catch (CartItemModifyException e) { Debug.logWarning( e, UtilProperties.getMessage( resource_error, "OrderProblemsAddingItemFromListToCart", cart.getLocale())); Map<String, Object> messageMap = UtilMisc.<String, Object>toMap("productId", productId); errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.problem_adding_product_to_cart", messageMap, cart.getLocale()); eventMessage.append(errMsg).append("\n"); } catch (ItemNotFoundException e) { Debug.logWarning( e, UtilProperties.getMessage(resource_error, "OrderProductNotFound", cart.getLocale())); Map<String, Object> messageMap = UtilMisc.<String, Object>toMap("productId", productId); errMsg = UtilProperties.getMessage( resource_error, "shoppinglistevents.problem_adding_product_to_cart", messageMap, cart.getLocale()); eventMessage.append(errMsg).append("\n"); } } if (eventMessage.length() > 0) { return eventMessage.toString(); } // all done return ""; // no message to return; will simply reply as success }
public static Map<String, Object> setExpressCheckout( DispatchContext dctx, Map<String, ? extends Object> context) { ShoppingCart cart = (ShoppingCart) context.get("cart"); Locale locale = cart.getLocale(); if (cart == null || cart.items().size() <= 0) { return ServiceUtil.returnError( UtilProperties.getMessage(resource, "AccountingPayPalShoppingCartIsEmpty", locale)); } GenericValue payPalConfig = getPaymentMethodGatewayPayPal(dctx, context, null); if (payPalConfig == null) { return ServiceUtil.returnError( UtilProperties.getMessage( resource, "AccountingPayPalPaymentGatewayConfigCannotFind", locale)); } NVPEncoder encoder = new NVPEncoder(); // Set Express Checkout Request Parameters encoder.add("METHOD", "SetExpressCheckout"); String token = (String) cart.getAttribute("payPalCheckoutToken"); if (UtilValidate.isNotEmpty(token)) { encoder.add("TOKEN", token); } encoder.add("RETURNURL", payPalConfig.getString("returnUrl")); encoder.add("CANCELURL", payPalConfig.getString("cancelReturnUrl")); if (!cart.shippingApplies()) { encoder.add("NOSHIPPING", "1"); } else { encoder.add("CALLBACK", payPalConfig.getString("shippingCallbackUrl")); encoder.add("CALLBACKTIMEOUT", "6"); // Default to no String reqConfirmShipping = "Y".equals(payPalConfig.getString("requireConfirmedShipping")) ? "1" : "0"; encoder.add("REQCONFIRMSHIPPING", reqConfirmShipping); // Default shipment method encoder.add("L_SHIPPINGOPTIONISDEFAULT0", "true"); encoder.add("L_SHIPPINGOPTIONNAME0", "Calculated Offline"); encoder.add("L_SHIPPINGOPTIONAMOUNT0", "0.00"); } encoder.add("ALLOWNOTE", "1"); encoder.add("INSURANCEOPTIONOFFERED", "false"); if (UtilValidate.isNotEmpty(payPalConfig.getString("imageUrl"))) ; encoder.add("PAYMENTACTION", "Order"); // Cart information try { addCartDetails(encoder, cart); } catch (GenericEntityException e) { Debug.logError(e, module); return ServiceUtil.returnError( UtilProperties.getMessage( resource, "AccountingPayPalErrorDuringRetrievingCartDetails", locale)); } NVPDecoder decoder; try { decoder = sendNVPRequest(payPalConfig, encoder); } catch (PayPalException e) { Debug.logError(e, module); return ServiceUtil.returnError(e.getMessage()); } Map<String, String> errorMessages = getErrorMessageMap(decoder); if (UtilValidate.isNotEmpty(errorMessages)) { if (errorMessages.containsKey("10411")) { // Token has expired, get a new one cart.setAttribute("payPalCheckoutToken", null); return PayPalServices.setExpressCheckout(dctx, context); } return ServiceUtil.returnError(UtilMisc.toList(errorMessages.values())); } token = decoder.get("TOKEN"); cart.setAttribute("payPalCheckoutToken", token); TokenWrapper tokenWrapper = new TokenWrapper(token); cart.setAttribute("payPalCheckoutTokenObj", tokenWrapper); PayPalServices.tokenCartMap.put(tokenWrapper, new WeakReference<ShoppingCart>(cart)); return ServiceUtil.returnSuccess(); }