Search in sources :

Example 6 with ItemNotFoundException

use of org.apache.ofbiz.order.shoppingcart.ItemNotFoundException in project ofbiz-framework by apache.

the class ShoppingListServices method makeShoppingListCart.

/**
 * Add a shoppinglist to an existing shoppingcart
 *
 * @param listCart the shopping cart list
 * @param dispatcher the local dispatcher
 * @param shoppingList a GenericValue object of the shopping list
 * @param locale the locale in use
 * @return the modified shopping cart adding the shopping list elements
 */
public static ShoppingCart makeShoppingListCart(ShoppingCart listCart, LocalDispatcher dispatcher, GenericValue shoppingList, Locale locale) {
    Delegator delegator = dispatcher.getDelegator();
    if (shoppingList != null && shoppingList.get("productStoreId") != null) {
        String productStoreId = shoppingList.getString("productStoreId");
        String currencyUom = shoppingList.getString("currencyUom");
        if (currencyUom == null) {
            GenericValue productStore = ProductStoreWorker.getProductStore(productStoreId, delegator);
            if (productStore == null) {
                return null;
            }
            currencyUom = productStore.getString("defaultCurrencyUomId");
        }
        if (locale == null) {
            locale = Locale.getDefault();
        }
        List<GenericValue> items = null;
        try {
            items = shoppingList.getRelated("ShoppingListItem", null, UtilMisc.toList("shoppingListItemSeqId"), false);
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
        if (UtilValidate.isNotEmpty(items)) {
            if (listCart == null) {
                listCart = new ShoppingCart(delegator, productStoreId, locale, currencyUom);
                listCart.setOrderPartyId(shoppingList.getString("partyId"));
                listCart.setAutoOrderShoppingListId(shoppingList.getString("shoppingListId"));
            } else {
                if (!listCart.getPartyId().equals(shoppingList.getString("partyId"))) {
                    Debug.logError("CANNOT add shoppingList: " + shoppingList.getString("shoppingListId") + " of partyId: " + shoppingList.getString("partyId") + " to a shoppingcart with a different orderPartyId: " + listCart.getPartyId(), module);
                    return listCart;
                }
            }
            ProductConfigWrapper configWrapper = null;
            for (GenericValue shoppingListItem : items) {
                String productId = shoppingListItem.getString("productId");
                BigDecimal quantity = shoppingListItem.getBigDecimal("quantity");
                Timestamp reservStart = shoppingListItem.getTimestamp("reservStart");
                BigDecimal reservLength = null;
                String configId = shoppingListItem.getString("configId");
                if (shoppingListItem.get("reservLength") != null) {
                    reservLength = shoppingListItem.getBigDecimal("reservLength");
                }
                BigDecimal reservPersons = null;
                if (shoppingListItem.get("reservPersons") != null) {
                    reservPersons = shoppingListItem.getBigDecimal("reservPersons");
                }
                if (UtilValidate.isNotEmpty(productId) && quantity != null) {
                    if (UtilValidate.isNotEmpty(configId)) {
                        configWrapper = ProductConfigWorker.loadProductConfigWrapper(delegator, dispatcher, configId, productId, listCart.getProductStoreId(), null, listCart.getWebSiteId(), listCart.getCurrency(), listCart.getLocale(), listCart.getAutoUserLogin());
                    }
                    // list items are noted in the shopping cart
                    String listId = shoppingListItem.getString("shoppingListId");
                    String itemId = shoppingListItem.getString("shoppingListItemSeqId");
                    Map<String, Object> attributes = UtilMisc.<String, Object>toMap("shoppingListId", listId, "shoppingListItemSeqId", itemId);
                    try {
                        listCart.addOrIncreaseItem(productId, null, quantity, reservStart, reservLength, reservPersons, null, null, null, null, null, attributes, null, configWrapper, null, null, null, dispatcher);
                    } catch (CartItemModifyException e) {
                        Debug.logError(e, "Unable to add product to List Cart - " + productId, module);
                    } catch (ItemNotFoundException e) {
                        Debug.logError(e, "Product not found - " + productId, module);
                    }
                }
            }
            if (listCart.size() > 0) {
                if (UtilValidate.isNotEmpty(shoppingList.get("paymentMethodId"))) {
                    listCart.addPayment(shoppingList.getString("paymentMethodId"));
                }
                if (UtilValidate.isNotEmpty(shoppingList.get("contactMechId"))) {
                    listCart.setAllShippingContactMechId(shoppingList.getString("contactMechId"));
                }
                if (UtilValidate.isNotEmpty(shoppingList.get("shipmentMethodTypeId"))) {
                    listCart.setAllShipmentMethodTypeId(shoppingList.getString("shipmentMethodTypeId"));
                }
                if (UtilValidate.isNotEmpty(shoppingList.get("carrierPartyId"))) {
                    listCart.setAllCarrierPartyId(shoppingList.getString("carrierPartyId"));
                }
                if (UtilValidate.isNotEmpty(shoppingList.getString("productPromoCodeId"))) {
                    listCart.addProductPromoCode(shoppingList.getString("productPromoCodeId"), dispatcher);
                }
            }
        }
    }
    return listCart;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ProductConfigWrapper(org.apache.ofbiz.product.config.ProductConfigWrapper) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Delegator(org.apache.ofbiz.entity.Delegator) ShoppingCart(org.apache.ofbiz.order.shoppingcart.ShoppingCart) CartItemModifyException(org.apache.ofbiz.order.shoppingcart.CartItemModifyException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ItemNotFoundException(org.apache.ofbiz.order.shoppingcart.ItemNotFoundException)

Aggregations

CartItemModifyException (org.apache.ofbiz.order.shoppingcart.CartItemModifyException)6 ItemNotFoundException (org.apache.ofbiz.order.shoppingcart.ItemNotFoundException)6 GenericValue (org.apache.ofbiz.entity.GenericValue)5 ShoppingCart (org.apache.ofbiz.order.shoppingcart.ShoppingCart)5 Locale (java.util.Locale)4 Delegator (org.apache.ofbiz.entity.Delegator)4 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)4 BigDecimal (java.math.BigDecimal)3 Timestamp (java.sql.Timestamp)3 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)3 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)3 LinkedList (java.util.LinkedList)2 CheckOutHelper (org.apache.ofbiz.order.shoppingcart.CheckOutHelper)2 ShoppingCartItem (org.apache.ofbiz.order.shoppingcart.ShoppingCartItem)2 ProductConfigWrapper (org.apache.ofbiz.product.config.ProductConfigWrapper)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1