Class ProductTypeServiceImpl
- java.lang.Object
-
- com.commercetools.sync.services.impl.ProductTypeServiceImpl
-
- All Implemented Interfaces:
ProductTypeService
public final class ProductTypeServiceImpl extends java.lang.Object implements ProductTypeService
-
-
Field Summary
Fields Modifier and Type Field Description protected com.github.benmanes.caffeine.cache.Cache<java.lang.String,java.lang.String>keyToIdCacheprotected static intMAXIMUM_ALLOWED_UPDATE_ACTIONS
-
Constructor Summary
Constructors Constructor Description ProductTypeServiceImpl(BaseSyncOptions syncOptions)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,java.lang.String>>cacheKeysToIds(java.util.Set<java.lang.String> keys)Filters out the keys which are already cached and fetches only the not-cached product type keys from the CTP project defined in an injectedSphereClientand stores a mapping for every productType to id in the cached map of keys -> ids and returns this cached map.java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.producttypes.ProductType>>createProductType(io.sphere.sdk.producttypes.ProductTypeDraft productTypeDraft)Given a resource draft of typeProductTypeDraft, this method attempts to create a resourceProductTypebased on it in the CTP project defined by the sync options.java.util.concurrent.CompletionStage<java.util.Optional<java.util.Map<java.lang.String,AttributeMetaData>>>fetchCachedProductAttributeMetaDataMap(java.lang.String productTypeId)Given aproductType, this method first checks if a cached map of ProductType ids -> map ofAttributeMetaDatais not empty.java.util.concurrent.CompletionStage<java.util.Optional<java.lang.String>>fetchCachedProductTypeId(java.lang.String key)Given akey, this method first checks if a cached map of ProductType keys -> ids contains the key.java.util.concurrent.CompletionStage<java.util.Set<io.sphere.sdk.producttypes.ProductType>>fetchMatchingProductTypesByKeys(java.util.Set<java.lang.String> keys)Given aSetof ProductType keys, this method fetches a set of all the ProductTypes, matching this given set of keys in the CTP project, defined in an injectedSphereClient.java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.producttypes.ProductType>>fetchProductType(java.lang.String key)Given a productType key, this method fetches a productType that matches this given key in the CTP project defined in an injectedSphereClient.java.util.concurrent.CompletionStage<io.sphere.sdk.producttypes.ProductType>updateProductType(io.sphere.sdk.producttypes.ProductType productType, java.util.List<io.sphere.sdk.commands.UpdateAction<io.sphere.sdk.producttypes.ProductType>> updateActions)Updates existing product type withupdateActions.
-
-
-
Field Detail
-
keyToIdCache
protected final com.github.benmanes.caffeine.cache.Cache<java.lang.String,java.lang.String> keyToIdCache
-
MAXIMUM_ALLOWED_UPDATE_ACTIONS
protected static final int MAXIMUM_ALLOWED_UPDATE_ACTIONS
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ProductTypeServiceImpl
public ProductTypeServiceImpl(@Nonnull BaseSyncOptions syncOptions)
-
-
Method Detail
-
cacheKeysToIds
@Nonnull public java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,java.lang.String>> cacheKeysToIds(@Nonnull java.util.Set<java.lang.String> keys)Description copied from interface:ProductTypeServiceFilters out the keys which are already cached and fetches only the not-cached product type keys from the CTP project defined in an injectedSphereClientand stores a mapping for every productType to id in the cached map of keys -> ids and returns this cached map.Note: If all the supplied keys are already cached, the cached map is returned right away with no request to CTP.
- Specified by:
cacheKeysToIdsin interfaceProductTypeService- Parameters:
keys- the productType keys to fetch and cache the ids for.- Returns:
CompletionStage<Map> in which the result of it's completion contains a map of all productType keys -> ids
-
fetchCachedProductTypeId
@Nonnull public java.util.concurrent.CompletionStage<java.util.Optional<java.lang.String>> fetchCachedProductTypeId(@Nonnull java.lang.String key)Description copied from interface:ProductTypeServiceGiven akey, this method first checks if a cached map of ProductType keys -> ids contains the key. If not, it returns a completed future that contains anOptionalthat contains what this key maps to in the cache. If the cache doesn't contain the key; this method attempts to fetch the id of the key from the CTP project, caches it and returns aCompletionStage<Optional<String>> in which the result of it's completion could contain anOptionalwith the id inside of it or an emptyOptionalif noProductTypewas found in the CTP project with this key.- Specified by:
fetchCachedProductTypeIdin interfaceProductTypeService- Parameters:
key- the key by which aProductTypeid should be fetched from the CTP project.- Returns:
CompletionStage<Optional<String>> in which the result of its completion could contain anOptionalwith the id inside of it or an emptyOptionalif noProductTypewas found in the CTP project with this key.
-
fetchCachedProductAttributeMetaDataMap
@Nonnull public java.util.concurrent.CompletionStage<java.util.Optional<java.util.Map<java.lang.String,AttributeMetaData>>> fetchCachedProductAttributeMetaDataMap(@Nonnull java.lang.String productTypeId)
Description copied from interface:ProductTypeServiceGiven aproductType, this method first checks if a cached map of ProductType ids -> map ofAttributeMetaDatais not empty. If not, it returns a completed future that contains an optional that contains what this productType id maps to in the cache. If the cache is empty, the method populates the cache with the mapping of all ProductType ids to maps of each product type's attributes'AttributeMetaDatain the CTP project, by querying the CTP project for all ProductTypes.After that, the method returns a
CompletionStage<Optional<String>> in which the result of it's completion could contain anOptionalwith a map of the attributes names ->AttributeMetaDatainside of it or an emptyOptionalif noProductTypewas found in the CTP project with this id.- Specified by:
fetchCachedProductAttributeMetaDataMapin interfaceProductTypeService- Parameters:
productTypeId- the id by which a a map of the attributes names ->AttributeMetaDatacorresponding to the product type should be fetched from the CTP project.- Returns:
CompletionStage<Optional<String>> in which the result of its completion could contain anOptionalwith the id inside of it or an emptyOptionalif noProductTypewas found in the CTP project with this key.
-
fetchMatchingProductTypesByKeys
@Nonnull public java.util.concurrent.CompletionStage<java.util.Set<io.sphere.sdk.producttypes.ProductType>> fetchMatchingProductTypesByKeys(@Nonnull java.util.Set<java.lang.String> keys)Description copied from interface:ProductTypeServiceGiven aSetof ProductType keys, this method fetches a set of all the ProductTypes, matching this given set of keys in the CTP project, defined in an injectedSphereClient. A mapping of the key to the id of the fetched ProductType is persisted in an in-memory map.- Specified by:
fetchMatchingProductTypesByKeysin interfaceProductTypeService- Parameters:
keys- set of ProductType keys to fetch matching ProductTypes by.- Returns:
CompletionStage<Map> in which the result of it's completion contains aSetof all matching ProductType.
-
createProductType
@Nonnull public java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.producttypes.ProductType>> createProductType(@Nonnull io.sphere.sdk.producttypes.ProductTypeDraft productTypeDraft)Description copied from interface:ProductTypeServiceGiven a resource draft of typeProductTypeDraft, this method attempts to create a resourceProductTypebased on it in the CTP project defined by the sync options.A completion stage containing an empty optional and the error callback will be triggered in those cases:
- the draft has a blank key
- the create request fails on CTP
On the other hand, if the resource gets created successfully on CTP, then the created resource's id and key are cached and the method returns a
CompletionStagein which the result of it's completion contains an instanceOptionalof the resource which was created.- Specified by:
createProductTypein interfaceProductTypeService- Parameters:
productTypeDraft- the resource draft to create a resource based off of.- Returns:
- a
CompletionStagecontaining an optional with the created resource if successful otherwise an empty optional.
-
updateProductType
@Nonnull public java.util.concurrent.CompletionStage<io.sphere.sdk.producttypes.ProductType> updateProductType(@Nonnull io.sphere.sdk.producttypes.ProductType productType, @Nonnull java.util.List<io.sphere.sdk.commands.UpdateAction<io.sphere.sdk.producttypes.ProductType>> updateActions)Description copied from interface:ProductTypeServiceUpdates existing product type withupdateActions.- Specified by:
updateProductTypein interfaceProductTypeService- Parameters:
productType- product type that should be updatedupdateActions-Listof actions that should be applied toproductType- Returns:
CompletionStagewith updatedProductType.
-
fetchProductType
@Nonnull public java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.producttypes.ProductType>> fetchProductType(@Nullable java.lang.String key)Description copied from interface:ProductTypeServiceGiven a productType key, this method fetches a productType that matches this given key in the CTP project defined in an injectedSphereClient. If there is no matching productType an emptyOptionalwill be returned in the returned future.- Specified by:
fetchProductTypein interfaceProductTypeService- Parameters:
key- the key of the product type to fetch.- Returns:
CompletionStage<Optional> in which the result of it's completion contains anOptionalthat contains the matchingProductTypeif exists, otherwise empty.
-
-