Class ProductServiceImpl

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected com.github.benmanes.caffeine.cache.Cache<java.lang.String,​java.lang.String> keyToIdCache  
      protected static int MAXIMUM_ALLOWED_UPDATE_ACTIONS  
    • 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> productKeys)
      Filters out the keys which are already cached and fetches only the not-cached product keys from the CTP project defined in an injected ProjectApiRoot and stores a mapping for every product to id in the cached map of keys -> ids and returns this cached map.
      java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,​java.lang.String>> cacheKeysToIdsUsingGraphQl​(java.util.Set<java.lang.String> keysToCache, GraphQlQueryResource queryResource)  
      java.util.concurrent.CompletionStage<java.util.Optional<com.commercetools.api.models.product.ProductProjection>> createProduct​(com.commercetools.api.models.product.ProductDraft productDraft)
      Given a resource draft of type ProductDraft, this method attempts to create a resource ProductProjection based on it in the CTP project defined by the sync options.
      java.util.concurrent.CompletionStage<java.util.Set<com.commercetools.api.models.product.ProductProjection>> fetchMatchingProductsByKeys​(java.util.Set<java.lang.String> productKeys)
      Given a Set of product keys, this method fetches a set of all the products, matching this given set of keys in the CTP project, defined in an injected ProjectApiRoot.
      java.util.concurrent.CompletionStage<java.util.Optional<com.commercetools.api.models.product.ProductProjection>> fetchProduct​(java.lang.String key)
      Given a product key, this method fetches a product that matches this given key in the CTP project defined in a potentially injected ProjectApiRoot.
      java.util.concurrent.CompletionStage<java.util.Optional<java.lang.String>> getIdFromCacheOrFetch​(java.lang.String key)
      Given a key, if it is blank (null/empty), a completed future with an empty optional is returned.
      protected java.util.Set<java.lang.String> getKeysNotCached​(java.util.Set<java.lang.String> keys)
      Given a set of keys this method collects all keys which aren't already contained in the cache keyToIdCache
      java.util.concurrent.CompletionStage<com.commercetools.api.models.product.ProductProjection> updateProduct​(com.commercetools.api.models.product.ProductProjection productProjection, java.util.List<com.commercetools.api.models.product.ProductUpdateAction> updateActions)
      Given a ProductProjection and a List<ProductUpdateAction>, this method issues an update request with these update actions on this ProductProjection in the CTP project defined in a potentially injected ProjectApiRoot.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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

      • ProductServiceImpl

        public ProductServiceImpl​(@Nonnull
                                  ProductSyncOptions syncOptions)
    • Method Detail

      • getIdFromCacheOrFetch

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Optional<java.lang.String>> getIdFromCacheOrFetch​(@Nullable
                                                                                                                java.lang.String key)
        Description copied from interface: ProductService
        Given a key, if it is blank (null/empty), a completed future with an empty optional is returned. This method then checks if the cached map of product keys -> ids contains the key. If it does, then an optional containing the mapped id is returned. 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 a CompletionStage<Optional<String>> in which the result of it's completion could contain an Optional with the id inside of it or an empty Optional if no ProductProjection was found in the CTP project with this key.
        Specified by:
        getIdFromCacheOrFetch in interface ProductService
        Parameters:
        key - the key by which a ProductProjection id should be fetched from the CTP project.
        Returns:
        CompletionStage<Optional<String>> in which the result of it's completion could contain an Optional with the id inside of it or an empty Optional if no ProductProjection was found in the CTP project with this key.
      • cacheKeysToIds

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,​java.lang.String>> cacheKeysToIds​(@Nonnull
                                                                                                                           java.util.Set<java.lang.String> productKeys)
        Description copied from interface: ProductService
        Filters out the keys which are already cached and fetches only the not-cached product keys from the CTP project defined in an injected ProjectApiRoot and stores a mapping for every product 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:
        cacheKeysToIds in interface ProductService
        Parameters:
        productKeys - the product keys to fetch and cache the ids for.
        Returns:
        CompletionStage<Map> in which the result of it's completion contains a map of all product keys -> ids
      • fetchMatchingProductsByKeys

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Set<com.commercetools.api.models.product.ProductProjection>> fetchMatchingProductsByKeys​(@Nonnull
                                                                                                                                                       java.util.Set<java.lang.String> productKeys)
        Description copied from interface: ProductService
        Given a Set of product keys, this method fetches a set of all the products, matching this given set of keys in the CTP project, defined in an injected ProjectApiRoot. A mapping of the key to the id of the fetched products is persisted in an in-memory map.
        Specified by:
        fetchMatchingProductsByKeys in interface ProductService
        Parameters:
        productKeys - set of product keys to fetch matching products by.
        Returns:
        CompletionStage<Map> in which the result of it's completion contains a Set of all matching products.
      • fetchProduct

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Optional<com.commercetools.api.models.product.ProductProjection>> fetchProduct​(@Nullable
                                                                                                                                             java.lang.String key)
        Description copied from interface: ProductService
        Given a product key, this method fetches a product that matches this given key in the CTP project defined in a potentially injected ProjectApiRoot. If there is no matching product an empty Optional will be returned in the returned future. A mapping of the key to the id of the fetched category is persisted in an in -memory map.
        Specified by:
        fetchProduct in interface ProductService
        Parameters:
        key - the key of the product to fetch.
        Returns:
        CompletionStage<Optional> in which the result of it's completion contains an Optional that contains the matching ProductProjection if exists, otherwise empty.
      • createProduct

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Optional<com.commercetools.api.models.product.ProductProjection>> createProduct​(@Nonnull
                                                                                                                                              com.commercetools.api.models.product.ProductDraft productDraft)
        Description copied from interface: ProductService
        Given a resource draft of type ProductDraft, this method attempts to create a resource ProductProjection based on it in the CTP project defined by the sync options.

        A completion stage containing an empty option 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 CompletionStage in which the result of it's completion contains an instance Optional of the resource which was created.

        Specified by:
        createProduct in interface ProductService
        Parameters:
        productDraft - the resource draft to create a resource based off of.
        Returns:
        a CompletionStage containing an optional with the created resource if successful otherwise an empty optional.
      • updateProduct

        @Nonnull
        public java.util.concurrent.CompletionStage<com.commercetools.api.models.product.ProductProjection> updateProduct​(@Nonnull
                                                                                                                          com.commercetools.api.models.product.ProductProjection productProjection,
                                                                                                                          @Nonnull
                                                                                                                          java.util.List<com.commercetools.api.models.product.ProductUpdateAction> updateActions)
        Description copied from interface: ProductService
        Given a ProductProjection and a List<ProductUpdateAction>, this method issues an update request with these update actions on this ProductProjection in the CTP project defined in a potentially injected ProjectApiRoot. This method returns CompletionStage<ProductProjection> in which the result of it's completion contains an instance of the ProductProjection which was updated in the CTP project.
        Specified by:
        updateProduct in interface ProductService
        Parameters:
        productProjection - the ProductProjection to update.
        updateActions - the update actions to update the ProductProjection with.
        Returns:
        CompletionStage<ProductProjection> containing as a result of it's completion an instance of the ProductProjection which was updated in the CTP project or a CompletionException.
      • getKeysNotCached

        @Nonnull
        protected java.util.Set<java.lang.String> getKeysNotCached​(@Nonnull
                                                                   java.util.Set<java.lang.String> keys)
        Given a set of keys this method collects all keys which aren't already contained in the cache keyToIdCache
        Parameters:
        keys - Set of keys
        Returns:
        a Set of keys which aren't already contained in the cache or empty
      • cacheKeysToIdsUsingGraphQl

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,​java.lang.String>> cacheKeysToIdsUsingGraphQl​(@Nonnull
                                                                                                                                       java.util.Set<java.lang.String> keysToCache,
                                                                                                                                       @Nonnull
                                                                                                                                       GraphQlQueryResource queryResource)