Search in sources :

Example 1 with InventoryUnavailableException

use of org.broadleafcommerce.core.inventory.service.InventoryUnavailableException in project BroadleafCommerce by BroadleafCommerce.

the class DecrementInventoryRollbackHandler method rollbackState.

@Override
public void rollbackState(Activity<ProcessContext<CheckoutSeed>> activity, ProcessContext<CheckoutSeed> processContext, Map<String, Object> stateConfiguration) throws RollbackFailureException {
    if (shouldExecute(activity, processContext, stateConfiguration)) {
        String orderId = "(Not Known)";
        if (stateConfiguration.get(ROLLBACK_BLC_ORDER_ID) != null) {
            orderId = String.valueOf(stateConfiguration.get(ROLLBACK_BLC_ORDER_ID));
        }
        @SuppressWarnings("unchecked") Map<Sku, Integer> inventoryToIncrement = (Map<Sku, Integer>) stateConfiguration.get(ROLLBACK_BLC_INVENTORY_DECREMENTED);
        @SuppressWarnings("unchecked") Map<Sku, Integer> inventoryToDecrement = (Map<Sku, Integer>) stateConfiguration.get(ROLLBACK_BLC_INVENTORY_INCREMENTED);
        Map<String, Object> contextualInformation = new HashMap<>();
        contextualInformation.put(ContextualInventoryService.ROLLBACK_STATE_KEY, stateConfiguration.get(EXTENDED_ROLLBACK_STATE));
        contextualInformation.put(ContextualInventoryService.ORDER_KEY, processContext.getSeedData().getOrder());
        if (inventoryToIncrement != null && !inventoryToIncrement.isEmpty()) {
            try {
                inventoryService.incrementInventory(inventoryToIncrement, contextualInformation);
            } catch (Exception ex) {
                RollbackFailureException rfe = new RollbackFailureException("An unexpected error occured in the error handler of the checkout workflow trying to compensate for inventory. This happend for order ID: " + orderId + ". This should be corrected manually!", ex);
                rfe.setActivity(activity);
                rfe.setProcessContext(processContext);
                rfe.setStateItems(stateConfiguration);
                throw rfe;
            }
        }
        if (inventoryToDecrement != null && !inventoryToDecrement.isEmpty()) {
            try {
                inventoryService.decrementInventory(inventoryToDecrement, contextualInformation);
            } catch (InventoryUnavailableException e) {
                // This is an awkward, unlikely state.  I just added some inventory, but something happened, and I want to remove it, but it's already gone!
                RollbackFailureException rfe = new RollbackFailureException("While trying roll back (decrement) inventory, we found that there was none left decrement.", e);
                rfe.setActivity(activity);
                rfe.setProcessContext(processContext);
                rfe.setStateItems(stateConfiguration);
                throw rfe;
            } catch (RuntimeException ex) {
                LOG.error("An unexpected error occured in the error handler of the checkout workflow trying to compensate for inventory. This happend for order ID: " + StringUtil.sanitize(orderId) + ". This should be corrected manually!", ex);
                RollbackFailureException rfe = new RollbackFailureException("An unexpected error occured in the error handler of the checkout workflow " + "trying to compensate for inventory. This happend for order ID: " + orderId + ". This should be corrected manually!", ex);
                rfe.setActivity(activity);
                rfe.setProcessContext(processContext);
                rfe.setStateItems(stateConfiguration);
                throw rfe;
            }
        }
    }
}
Also used : RollbackFailureException(org.broadleafcommerce.core.workflow.state.RollbackFailureException) HashMap(java.util.HashMap) InventoryUnavailableException(org.broadleafcommerce.core.inventory.service.InventoryUnavailableException) InventoryUnavailableException(org.broadleafcommerce.core.inventory.service.InventoryUnavailableException) RollbackFailureException(org.broadleafcommerce.core.workflow.state.RollbackFailureException) Sku(org.broadleafcommerce.core.catalog.domain.Sku) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 Sku (org.broadleafcommerce.core.catalog.domain.Sku)1 InventoryUnavailableException (org.broadleafcommerce.core.inventory.service.InventoryUnavailableException)1 RollbackFailureException (org.broadleafcommerce.core.workflow.state.RollbackFailureException)1