Class ExternalInventoryProvider
- java.lang.Object
-
- com.broadleafcommerce.cartoperation.service.provider.external.AbstractExternalProvider
-
- com.broadleafcommerce.cartoperation.service.provider.external.ExternalInventoryProvider
-
- All Implemented Interfaces:
InventoryProvider
public class ExternalInventoryProvider extends AbstractExternalProvider implements InventoryProvider
Component that, by default, attempts to invoke Broadleaf Inventorry Services to reserve inventory.- Author:
- Kelly Tisdell (ktisdell)
- See Also:
InventoryProvider,EmptyInventoryProvider
-
-
Field Summary
Fields Modifier and Type Field Description protected static StringAVAILABILITY_CHECK_UNEXPECTED_FAILURE_ERROR_MSGprotected static StringCANNOT_DETERMINE_CART_ITEM_INVENTORY_ERROR_MSGprotected static StringDEFAULT_RESERVATION_REQUEST_TYPEprotected static StringDEFAULT_SKU_REF_FIELD_TYPEprotected static StringINVENTORY_UNAVAILABLEprotected static StringUNAVAILABLE_CART_ITEM_INVENTORY_ERROR_MSG
-
Constructor Summary
Constructors Constructor Description ExternalInventoryProvider(org.springframework.web.reactive.function.client.WebClient webClient, com.fasterxml.jackson.databind.ObjectMapper objectMapper, com.broadleafcommerce.common.extension.TypeFactory typeFactory, ExternalInventoryProperties properties)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected InventoryUnavailableExceptionbuildExceptionForUnexpectedReservationError(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull Exception ex)protected Optional<SkuInventoryReservationRequest>buildInventoryReservationRequest(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)This allows implementors to build aSkuInventoryReservationRequestor return an emptyOptionalif no external request should be made (e.g.protected ExceptionbuildReservationResponseException(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull org.springframework.web.reactive.function.client.WebClientResponseException ex)Builds the appropriate exception that should be thrown when a request to reserve inventory fails.protected List<SkuInventoryAvailabilityRequest>buildSkuInventoryAvailabilityRequests(Map<String,Integer> skuCodesAndQuantity)Builds a listSkuInventoryAvailabilityRequestto verify the availability of the SKU for the provided quantity.protected org.springframework.util.MultiValueMap<String,String>buildSkuToCartItemIdMap(@NonNull Collection<com.broadleafcommerce.cart.client.domain.CartItem> cartItems)protected StringgetAvailabilityCheckUri(@NonNull String skuCode, int quantity, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Return the GET URI for Inventory Availability.protected StringgetBulkAvailabilityCheckUri(com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Return the GET URI for Inventory Availability.protected com.jayway.jsonpath.DocumentContextgetDocumentContext(@NonNull String payload)protected StringgetErrorType(@NonNull org.springframework.web.reactive.function.client.WebClientResponseException ex)protected List<SkuInventoryAvailabilitySummary>getInventorySummaries(Map<String,Integer> skuCodesAndQuantity, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Map<String,List<SkuInventoryAvailabilitySummary>>getInventorySummariesBySku(Map<String,Integer> skuCodesAndQuantity, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Gets a map ofSkuInventoryAvailabilitySummariesby SKU.protected org.springframework.core.ParameterizedTypeReference<List<SkuInventoryAvailabilitySummary>>getListType()Gets the type reference for a list of sku availability.protected ObjectgetProductType(com.broadleafcommerce.cart.client.domain.CartItem cartItem)protected ExternalInventoryPropertiesgetProperties()protected StringgetReservationRequestType(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Indicates the reservation request type (e.g.protected StringgetReservationUri(com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Return the POST URI for Inventory Reservations.protected StringgetServiceClient()protected StringgetSkuReferenceFieldType(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)SkuInventory records can be looked up or reference by a number of field types, including:
SKU_CODE - Identifies aProduct.getSku()orVariant.getSku()by a unique SKU code
SKU_EXTERNAL_ID - An ID in another 1st or 3rd party system (e.g.protected List<String>getSkuReferences(@NonNull org.springframework.web.reactive.function.client.WebClientResponseException ex)booleanisInventoryAvailable(String skuCode, int quantity, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Determines whether Inventory is available for a particular SKU.Map<String,Boolean>isInventoryAvailable(Map<String,Integer> skuCodesAndQuantity, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Determines whether Inventory is available for a list of SKUs.protected booleanisInventoryUnavailableResponse(@NonNull org.springframework.web.reactive.function.client.WebClientResponseException ex)protected voidpostProcessReservationResponse(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull SkuInventoryReservationResponse response, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Arbitrary hook point to allow for post processing of theCartand/or theSkuInventoryReservationResponse.voidreserveInventory(@NonNull com.broadleafcommerce.cart.client.domain.Cart cart, com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Decrement or otherwise "reserve" inventory for a cart during checkout.-
Methods inherited from class com.broadleafcommerce.cartoperation.service.provider.external.AbstractExternalProvider
executeRequest, getHeaders, getObjectMapper, getTypeFactory, getWebClient, pageableToParams, uriVars
-
-
-
-
Field Detail
-
DEFAULT_RESERVATION_REQUEST_TYPE
protected static final String DEFAULT_RESERVATION_REQUEST_TYPE
- See Also:
- Constant Field Values
-
DEFAULT_SKU_REF_FIELD_TYPE
protected static final String DEFAULT_SKU_REF_FIELD_TYPE
- See Also:
- Constant Field Values
-
INVENTORY_UNAVAILABLE
protected static final String INVENTORY_UNAVAILABLE
- See Also:
- Constant Field Values
-
CANNOT_DETERMINE_CART_ITEM_INVENTORY_ERROR_MSG
protected static final String CANNOT_DETERMINE_CART_ITEM_INVENTORY_ERROR_MSG
- See Also:
- Constant Field Values
-
UNAVAILABLE_CART_ITEM_INVENTORY_ERROR_MSG
protected static final String UNAVAILABLE_CART_ITEM_INVENTORY_ERROR_MSG
- See Also:
- Constant Field Values
-
AVAILABILITY_CHECK_UNEXPECTED_FAILURE_ERROR_MSG
protected static final String AVAILABILITY_CHECK_UNEXPECTED_FAILURE_ERROR_MSG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ExternalInventoryProvider
public ExternalInventoryProvider(org.springframework.web.reactive.function.client.WebClient webClient, com.fasterxml.jackson.databind.ObjectMapper objectMapper, com.broadleafcommerce.common.extension.TypeFactory typeFactory, ExternalInventoryProperties properties)
-
-
Method Detail
-
isInventoryAvailable
public boolean isInventoryAvailable(String skuCode, int quantity, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:InventoryProviderDetermines whether Inventory is available for a particular SKU.- Specified by:
isInventoryAvailablein interfaceInventoryProvider- Parameters:
skuCode- the sku code for which to check inventory availabilityquantity- the quantity of the sku code that will be requestedcontextInfo- context information surrounding sandboxing and multitenant state- Returns:
trueif inventory is available for the SKU and quantity,falseotherwise
-
isInventoryAvailable
public Map<String,Boolean> isInventoryAvailable(Map<String,Integer> skuCodesAndQuantity, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:InventoryProviderDetermines whether Inventory is available for a list of SKUs.By default, if the SKU is available for one of the SKU locations or vendors, then it's considered available overall.
Use
InventoryProvider.getInventorySummariesBySku(Map, ContextInfo)for custom logic on SKU inventory availability.- Specified by:
isInventoryAvailablein interfaceInventoryProvider- Parameters:
skuCodesAndQuantity- a map of the sku codes and quantity for which to check inventory availabilitycontextInfo- context information surrounding sandboxing and multitenant state- Returns:
- a map of the sku code with
trueif inventory is available for the SKU and quantity, andfalseotherwise
-
getInventorySummariesBySku
public Map<String,List<SkuInventoryAvailabilitySummary>> getInventorySummariesBySku(Map<String,Integer> skuCodesAndQuantity, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Description copied from interface:InventoryProviderGets a map ofSkuInventoryAvailabilitySummariesby SKU.There can be multiple
SkuInventoryAvailabilitySummariesfor one SKU, if there are SKU inventories set up at multiple inventory locations or vendors.This is useful for custom SKU inventory availability logic.
- Specified by:
getInventorySummariesBySkuin interfaceInventoryProvider- Parameters:
skuCodesAndQuantity- a map of the sku codes and quantity for which to check inventory availabilitycontextInfo- context information surrounding sandboxing and multitenant state- Returns:
- a map of
SkuInventoryAvailabilitySummariesby SKU
-
getInventorySummaries
protected List<SkuInventoryAvailabilitySummary> getInventorySummaries(Map<String,Integer> skuCodesAndQuantity, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
-
reserveInventory
public void reserveInventory(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Description copied from interface:InventoryProviderDecrement or otherwise "reserve" inventory for a cart during checkout.- Specified by:
reserveInventoryin interfaceInventoryProvider- Parameters:
cart- The cart for which to decrement inventory.contextInfo- Context information around sandbox and multitenant state.
-
buildReservationResponseException
protected Exception buildReservationResponseException(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull @NonNull org.springframework.web.reactive.function.client.WebClientResponseException ex)
Builds the appropriate exception that should be thrown when a request to reserve inventory fails.By default, all errors (even those caused by unexpected issues) will result in an
InventoryUnavailableException.- Parameters:
cart- the cart for which reservation was requestedex- the error that was encountered when making the request- Returns:
- the appropriate exception that should be thrown when a request to reserve inventory fails
-
buildExceptionForUnexpectedReservationError
protected InventoryUnavailableException buildExceptionForUnexpectedReservationError(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull @NonNull Exception ex)
- Parameters:
cart- the cart for which inventory reservation was requestedex- the unexpected error that was encountered- Returns:
- an exception that can be thrown when an inventory reservation request encounters an unexpected error
-
isInventoryUnavailableResponse
protected boolean isInventoryUnavailableResponse(@NonNull @NonNull org.springframework.web.reactive.function.client.WebClientResponseException ex)
-
getErrorType
@Nullable protected String getErrorType(@NonNull @NonNull org.springframework.web.reactive.function.client.WebClientResponseException ex)
-
buildSkuToCartItemIdMap
protected org.springframework.util.MultiValueMap<String,String> buildSkuToCartItemIdMap(@NonNull @NonNull Collection<com.broadleafcommerce.cart.client.domain.CartItem> cartItems)
-
getSkuReferences
protected List<String> getSkuReferences(@NonNull @NonNull org.springframework.web.reactive.function.client.WebClientResponseException ex)
-
getDocumentContext
protected com.jayway.jsonpath.DocumentContext getDocumentContext(@NonNull @NonNull String payload)
-
buildInventoryReservationRequest
protected Optional<SkuInventoryReservationRequest> buildInventoryReservationRequest(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
This allows implementors to build aSkuInventoryReservationRequestor return an emptyOptionalif no external request should be made (e.g. if all items in the cart are considered implicitly available.- Parameters:
cart-contextInfo-- Returns:
- Throws:
InventoryUnavailableException- - if one or more items are in the cart that are unavailable.
-
getProductType
@Nullable protected Object getProductType(com.broadleafcommerce.cart.client.domain.CartItem cartItem)
-
getReservationUri
protected String getReservationUri(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Return the POST URI for Inventory Reservations.- Parameters:
contextInfo-- Returns:
-
getAvailabilityCheckUri
protected String getAvailabilityCheckUri(@NonNull @NonNull String skuCode, int quantity, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Return the GET URI for Inventory Availability. By default, this looks for shipping locations only for the providedskuCode.- Parameters:
contextInfo-- Returns:
-
getBulkAvailabilityCheckUri
protected String getBulkAvailabilityCheckUri(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Return the GET URI for Inventory Availability. By default, this looks for shipping locations only for the providedskuCodes.- Parameters:
contextInfo- Context information around sandbox and multitenant state.- Returns:
-
postProcessReservationResponse
protected void postProcessReservationResponse(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @NonNull @NonNull SkuInventoryReservationResponse response, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)Arbitrary hook point to allow for post processing of theCartand/or theSkuInventoryReservationResponse.- Parameters:
cart-response-contextInfo-
-
getReservationRequestType
protected String getReservationRequestType(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
Indicates the reservation request type (e.g. HARD or SOFT). The default is 'SOFT', meaning that the inventory reservation will be temporary to allow for a hard reservation later - usually after order submittal - which makes the reservation permanent.- Parameters:
cart-contextInfo-- Returns:
-
getSkuReferenceFieldType
protected String getSkuReferenceFieldType(@NonNull @NonNull com.broadleafcommerce.cart.client.domain.Cart cart, @Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo)
SkuInventory records can be looked up or reference by a number of field types, including:
SKU_CODE - Identifies aProduct.getSku()orVariant.getSku()by a unique SKU code
SKU_EXTERNAL_ID - An ID in another 1st or 3rd party system (e.g. in an ERP)
SKU_CODE - Arbitrary SKU code UPC - Universal Product
Code EAN - European Article Number (aka International Article Number)
GTIN - Global Trade Item Number
The default is SKU_CODE.- Parameters:
cart-contextInfo-- Returns:
-
getListType
protected org.springframework.core.ParameterizedTypeReference<List<SkuInventoryAvailabilitySummary>> getListType()
Gets the type reference for a list of sku availability.- Returns:
- type reference for a list of sku availability
-
buildSkuInventoryAvailabilityRequests
protected List<SkuInventoryAvailabilityRequest> buildSkuInventoryAvailabilityRequests(Map<String,Integer> skuCodesAndQuantity)
Builds a listSkuInventoryAvailabilityRequestto verify the availability of the SKU for the provided quantity.- Returns:
- a list of sku inventory availability requests
-
getServiceClient
protected String getServiceClient()
-
getProperties
protected ExternalInventoryProperties getProperties()
-
-