Search in sources :

Example 16 with ShoppingCartItem

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

the class ShoppingListEvents method addBulkFromCart.

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()));
            if (ServiceUtil.isError(newListResult)) {
                String errorMessage = ServiceUtil.getErrorMessage(newListResult);
                Debug.logError(errorMessage, module);
                throw new IllegalArgumentException(errorMessage);
            }
        } 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);
        }
        // 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 (String item2 : items) {
        Integer cartIdInt = null;
        try {
            cartIdInt = Integer.valueOf(item2);
        } 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());
                    if (item.getConfigWrapper() != null) {
                        ctx.put("configId", item.getConfigWrapper().getConfigId());
                    }
                    serviceResult = dispatcher.runSync("createShoppingListItem", ctx);
                    if (ServiceUtil.isError(serviceResult)) {
                        String errorMessage = ServiceUtil.getErrorMessage(serviceResult);
                        Debug.logError(errorMessage, module);
                        throw new IllegalArgumentException(errorMessage);
                    }
                } 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);
                }
            }
        }
    }
    // return the shoppinglist id
    return shoppingListId;
}
Also used : GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) ShoppingCartItem(org.apache.ofbiz.order.shoppingcart.ShoppingCartItem) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) CartItemModifyException(org.apache.ofbiz.order.shoppingcart.CartItemModifyException) ItemNotFoundException(org.apache.ofbiz.order.shoppingcart.ItemNotFoundException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GeneralException(org.apache.ofbiz.base.util.GeneralException)

Aggregations

ShoppingCartItem (org.apache.ofbiz.order.shoppingcart.ShoppingCartItem)16 BigDecimal (java.math.BigDecimal)12 GenericValue (org.apache.ofbiz.entity.GenericValue)11 HashMap (java.util.HashMap)8 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)8 CartItemModifyException (org.apache.ofbiz.order.shoppingcart.CartItemModifyException)8 Delegator (org.apache.ofbiz.entity.Delegator)7 ShoppingCart (org.apache.ofbiz.order.shoppingcart.ShoppingCart)7 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)7 Locale (java.util.Locale)5 Map (java.util.Map)5 GeneralException (org.apache.ofbiz.base.util.GeneralException)5 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)5 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 Timestamp (java.sql.Timestamp)3 CheckOutHelper (org.apache.ofbiz.order.shoppingcart.CheckOutHelper)3 ItemNotFoundException (org.apache.ofbiz.order.shoppingcart.ItemNotFoundException)3 Calendar (com.ibm.icu.util.Calendar)2 ArrayList (java.util.ArrayList)2