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