use of org.apache.ofbiz.product.config.ProductConfigWrapper in project ofbiz-framework by apache.
the class ShoppingListEvents method addListToCart.
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 = new LinkedList<>();
}
// 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 configId = shoppingListItem.getString("configId");
try {
String listId = shoppingListItem.getString("shoppingListId");
String itemId = shoppingListItem.getString("shoppingListItemSeqId");
Map<String, Object> attributes = new HashMap<>();
// 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());
}
// 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();
}
// no message to return; will simply reply as success
return "";
}
use of org.apache.ofbiz.product.config.ProductConfigWrapper in project ofbiz-framework by apache.
the class ShoppingCartHelper method addToCartFromOrder.
public Map<String, Object> addToCartFromOrder(String catalogId, String orderId, String[] itemIds, boolean addAll, String itemGroupNumber) {
List<String> errorMsgs = new ArrayList<>();
Map<String, Object> result;
String errMsg = null;
if (UtilValidate.isEmpty(orderId)) {
errMsg = UtilProperties.getMessage(resource_error, "cart.order_not_specified_to_add_from", this.cart.getLocale());
result = ServiceUtil.returnError(errMsg);
return result;
}
boolean noItems = true;
List<? extends Object> itemIdList = null;
Iterator<? extends Object> itemIter = null;
OrderReadHelper orderHelper = new OrderReadHelper(delegator, orderId);
if (addAll) {
itemIdList = orderHelper.getOrderItems();
} else {
if (itemIds != null) {
itemIdList = Arrays.asList(itemIds);
}
}
if (UtilValidate.isNotEmpty(itemIdList)) {
itemIter = itemIdList.iterator();
}
String orderItemTypeId = null;
String productId = null;
if (itemIter != null && itemIter.hasNext()) {
while (itemIter.hasNext()) {
GenericValue orderItem = null;
Object value = itemIter.next();
if (value instanceof GenericValue) {
orderItem = (GenericValue) value;
} else {
String orderItemSeqId = (String) value;
orderItem = orderHelper.getOrderItem(orderItemSeqId);
}
// do not include PROMO items
if (orderItem.get("isPromo") != null && "Y".equals(orderItem.getString("isPromo"))) {
continue;
}
orderItemTypeId = orderItem.getString("orderItemTypeId");
productId = orderItem.getString("productId");
// do not store rental items
if ("RENTAL_ORDER_ITEM".equals(orderItemTypeId)) {
continue;
}
if (UtilValidate.isNotEmpty(productId) && orderItem.get("quantity") != null) {
BigDecimal amount = orderItem.getBigDecimal("selectedAmount");
ProductConfigWrapper configWrapper = null;
String aggregatedProdId = null;
if (EntityTypeUtil.hasParentType(delegator, "ProductType", "productTypeId", ProductWorker.getProductTypeId(delegator, productId), "parentTypeId", "AGGREGATED")) {
try {
GenericValue instanceProduct = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
String configId = instanceProduct.getString("configId");
aggregatedProdId = ProductWorker.getInstanceAggregatedId(delegator, productId);
configWrapper = ProductConfigWorker.loadProductConfigWrapper(delegator, dispatcher, configId, aggregatedProdId, cart.getProductStoreId(), catalogId, cart.getWebSiteId(), cart.getCurrency(), cart.getLocale(), cart.getAutoUserLogin());
} catch (GenericEntityException e) {
errorMsgs.add(e.getMessage());
}
}
try {
this.cart.addOrIncreaseItem(UtilValidate.isNotEmpty(aggregatedProdId) ? aggregatedProdId : productId, amount, orderItem.getBigDecimal("quantity"), null, null, null, null, null, null, null, catalogId, configWrapper, orderItemTypeId, itemGroupNumber, null, dispatcher);
noItems = false;
} catch (CartItemModifyException | ItemNotFoundException e) {
errorMsgs.add(e.getMessage());
}
}
}
if (errorMsgs.size() > 0) {
result = ServiceUtil.returnError(errorMsgs);
result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS);
// don't return error because this is a non-critical error and should go back to the same page
return result;
}
} else {
noItems = true;
}
if (noItems) {
result = ServiceUtil.returnSuccess();
result.put("_ERROR_MESSAGE_", UtilProperties.getMessage(resource_error, "OrderNoItemsFoundToAdd", this.cart.getLocale()));
// don't return error because this is a non-critical error and should go back to the same page
return result;
}
result = ServiceUtil.returnSuccess();
return result;
}
use of org.apache.ofbiz.product.config.ProductConfigWrapper in project ofbiz-framework by apache.
the class ShoppingCartServices method loadCartFromShoppingList.
public static Map<String, Object> loadCartFromShoppingList(DispatchContext dctx, Map<String, Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String shoppingListId = (String) context.get("shoppingListId");
String orderPartyId = (String) context.get("orderPartyId");
Locale locale = (Locale) context.get("locale");
// get the shopping list header
GenericValue shoppingList = null;
try {
shoppingList = EntityQuery.use(delegator).from("ShoppingList").where("shoppingListId", shoppingListId).queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// initial required cart info
String productStoreId = shoppingList.getString("productStoreId");
String currency = shoppingList.getString("currencyUom");
// If no currency has been set in the ShoppingList, use the ProductStore default currency
if (currency == null) {
try {
GenericValue productStore = shoppingList.getRelatedOne("ProductStore", false);
if (productStore != null) {
currency = productStore.getString("defaultCurrencyUomId");
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
// If we still have no currency, use the default from general.properties. Failing that, use USD
if (currency == null) {
currency = EntityUtilProperties.getPropertyValue("general", "currency.uom.id.default", "USD", delegator);
}
// create the cart
ShoppingCart cart = new ShoppingCart(delegator, productStoreId, locale, currency);
try {
cart.setUserLogin(userLogin, dispatcher);
} catch (CartItemModifyException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// set the role information
if (UtilValidate.isNotEmpty(orderPartyId)) {
cart.setOrderPartyId(orderPartyId);
} else {
cart.setOrderPartyId(shoppingList.getString("partyId"));
}
List<GenericValue> shoppingListItems = null;
try {
shoppingListItems = shoppingList.getRelated("ShoppingListItem", null, null, false);
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
long nextItemSeq = 0;
if (UtilValidate.isNotEmpty(shoppingListItems)) {
Pattern pattern = Pattern.compile("\\P{Digit}");
for (GenericValue shoppingListItem : shoppingListItems) {
// get the next item sequence id
String orderItemSeqId = shoppingListItem.getString("shoppingListItemSeqId");
Matcher pmatcher = pattern.matcher(orderItemSeqId);
orderItemSeqId = pmatcher.replaceAll("");
try {
long seq = Long.parseLong(orderItemSeqId);
if (seq > nextItemSeq) {
nextItemSeq = seq;
}
} catch (NumberFormatException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
BigDecimal modifiedPrice = shoppingListItem.getBigDecimal("modifiedPrice");
BigDecimal quantity = shoppingListItem.getBigDecimal("quantity");
if (quantity == null) {
quantity = BigDecimal.ZERO;
}
int itemIndex = -1;
if (shoppingListItem.get("productId") != null) {
// product item
String productId = shoppingListItem.getString("productId");
ProductConfigWrapper configWrapper = null;
if (UtilValidate.isNotEmpty(shoppingListItem.getString("configId"))) {
configWrapper = ProductConfigWorker.loadProductConfigWrapper(delegator, dispatcher, shoppingListItem.getString("configId"), productId, productStoreId, null, null, currency, locale, userLogin);
}
try {
itemIndex = cart.addItemToEnd(productId, null, quantity, null, null, null, null, null, configWrapper, dispatcher, Boolean.TRUE, Boolean.TRUE);
} catch (ItemNotFoundException | CartItemModifyException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// set the modified price
if (modifiedPrice != null && modifiedPrice.doubleValue() != 0) {
ShoppingCartItem item = cart.findCartItem(itemIndex);
if (item != null) {
item.setIsModifiedPrice(true);
item.setBasePrice(modifiedPrice);
}
}
}
// flag the item w/ the orderItemSeqId so we can reference it
ShoppingCartItem cartItem = cart.findCartItem(itemIndex);
cartItem.setOrderItemSeqId(orderItemSeqId);
// attach additional item information
cartItem.setShoppingList(shoppingListItem.getString("shoppingListId"), shoppingListItem.getString("shoppingListItemSeqId"));
}
}
// set the item seq in the cart
if (nextItemSeq > 0) {
try {
cart.setNextItemSeq(nextItemSeq + 1);
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
}
Map<String, Object> result = ServiceUtil.returnSuccess();
result.put("shoppingCart", cart);
return result;
}
use of org.apache.ofbiz.product.config.ProductConfigWrapper 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