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;
}
Aggregations