Interface InventoryService
- All Known Subinterfaces:
ContextualInventoryService
- All Known Implementing Classes:
InventoryServiceImpl
This basic inventory service checks and adjusts the current inventory of a sku. All Skus will be considered
generally unavailable from an inventory perspective if Sku#isAvaliable() returns false or if Sku.isActive()
returns false.
Skus with an InventoryType of null or 'ALWAYS_AVAILABLE' will be considered undefined from an inventory perspective, and will generally
be considered available. However, a request for available quantities of Skus with a null or 'ALWAYS_AVAILABLE' inventory type will
return null (as the Sku is available but no inventory strategy is defined).
For most implementations outside of the very basic inventory case, you will actually want to use the ContextualInventoryService.
This is the version of the service that is invoked from the checkout workflow in DecrementInventoryActivity and
where the main checks for inventory are in the CheckAddAvailabilityActivity
- Author:
- Kelly Tisdell, Phillip Verheyden (phillipuniverse)
-
Method Summary
Modifier and TypeMethodDescriptionbooleanWithout worrying about quantities, just checks to see if the given Sku is available.voiddecrementInventory(Map<Sku, Integer> skuQuantities) Attempts to decrement inventory for a map of Skus and quantitiesvoiddecrementInventory(Sku sku, int quantity) Attempts to decrement inventory if it is available.voidincrementInventory(Map<Sku, Integer> skuQuantities) Attempts to increment inventory for a map of Skus and quantities.voidincrementInventory(Sku sku, int quantity) Attempts to increment inventory.booleanisAvailable(Sku sku, int quantity) Indicates whether the given quantity is available for the particular skuId.Retrieves the quantity available for a given sku.Retrieves the quantity available for a given sku.
-
Method Details
-
retrieveQuantityAvailable
Retrieves the quantity available for a given sku. May return null if no inventory is maintained for the given sku when Sku.getInventoryType() == null or the
InventoryTypeof the given sku isInventoryType.ALWAYS_AVAILABLE. Effectively, if the quantity returned is null, inventory is undefined, which most likely means it is available. However, rather than returning an arbitrary integer values (like Integer.MAX_VALUE), which has specific meaning, we return null as this can be interpreted by the client to mean whatever they define it as (including infinitely available), which is the most likely scenario.In practice, this is a convenience method to wrap
retrieveQuantitiesAvailable(Collection)- Returns:
- null if there is no inventory strategy defined (meaning,
Sku.getInventoryType()is null orInventoryType.ALWAYS_AVAILABLE). Otherwise, this returns the quantity of theSku
-
retrieveQuantitiesAvailable
Retrieves the quantity available for a given sku. May return null if no inventory is maintained for the given sku when Sku.getInventoryType() == null or the
InventoryTypeof the given sku isInventoryType.ALWAYS_AVAILABLE. Effectively, if the quantity returned is null, inventory is undefined, which most likely means it is available. However, rather than returning an arbitrary integer values (like Integer.MAX_VALUE), which has specific meaning, we return null as this can be interpreted by the client to mean whatever they define it as (including infinitely available), which is the most likely scenario.- Parameters:
skus- the set ofSkus to return inventory for- Returns:
- a map of the given set of skus to the quantity as represented in the inventory system. The
Map.keySet()is the same collection of given skus
-
isAvailable
Indicates whether the given quantity is available for the particular skuId. The result will be true if Sku.getInventoryType() == null or Sku.getInventoryType().equals(InventoryType.ALWAYS_AVAILABLE).
The result will be false if the
checkBasicAvailablility(Sku)returns false, or if the quantity field is null, or if the quantity requested exceeds the quantity available.- Parameters:
sku- theSkuto see if enough quantity is availablequantity- the quantity to check for the given sku- Returns:
- true if there is available quantity
-
checkBasicAvailablility
Without worrying about quantities, just checks to see if the given Sku is available. ASkuis generally available if any of these is true:Sku.getInventoryType()is nullSku.getInventoryType()is anything butInventoryType.UNAVAILABLESku.isActive()is is true or null- Does not consider the now-deprecated
Sku.isAvailable()
* @param sku the
Skuwhose availability is being checked- Returns:
- true or false according to the rules above
-
decrementInventory
Attempts to decrement inventory if it is available. If the Sku is marked as
InventoryType.ALWAYS_AVAILABLEthen this will be a no-op.This method is a convenience method to wrap
decrementInventory(Map)- Parameters:
sku- theSkuto decrement inventory fromquantity- the quantity to take inventory from- Throws:
InventoryUnavailableException- if there is not enough of the given quantity for the given skuIllegalArgumentException- if the given quantity is not greater than zero
-
decrementInventory
Attempts to decrement inventory for a map of Skus and quantities
Quantities must be greater than zero or an IllegalArgumentException will be thrown.
If any of the given
Skus inventory type is notInventoryType.CHECK_QUANTITYthen this is a no-op and nothing actually happens- Parameters:
skuQuantities- a map from aSkuto the quantity attempting to decrement- Throws:
InventoryUnavailableException- if there is not enough inventory to decrement from any of the given skus or ifcheckBasicAvailablility(Sku)returns falseIllegalArgumentException- if any of the quantities of the given skus are less than zero
-
incrementInventory
Attempts to increment inventory. Quantity must be greater than zero or an IllegalArgumentException will be thrown.
This is a convenience method to wrap
incrementInventory(Map)- Parameters:
sku- theSkuwhose inventory should be incrementedquantity- greater than zero- Throws:
IllegalArgumentException- if quantity is less than zero orretrieveQuantityAvailable(Sku)for the given sku returns null
-
incrementInventory
Attempts to increment inventory for a map of Skus and quantities. All must not be null and must be greater than zero or an IllegalArgumentException will be thrown.If any of the given
Skus inventory type is notInventoryType.CHECK_QUANTITYthen this is a no-op and nothing actually happens- Parameters:
skuQuantities- the map of aSkuto the quantity that should be incremented- Throws:
IllegalArgumentException- if any of the quantities in the map values are null or less than zero, or ifretrieveQuantityAvailable(Sku)for theSkus in the map is null
-