Class CategoryServiceImpl

  • All Implemented Interfaces:
    CategoryService

    public final class CategoryServiceImpl
    extends java.lang.Object
    implements CategoryService
    Implementation of CategoryService interface.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected com.github.benmanes.caffeine.cache.Cache<java.lang.String,​java.lang.String> keyToIdCache  
    • 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> categoryKeys)
      Filters out the keys which are already cached and fetches only the not-cached category keys from the CTP project defined in an injected SphereClient and stores a mapping for every category to id in the cached map of keys -> ids and returns this cached map.
      java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.categories.Category>> createCategory​(io.sphere.sdk.categories.CategoryDraft categoryDraft)
      Given a resource draft of type CategoryDraft, this method attempts to create a resource Category based on it in the CTP project defined by the sync options.
      java.util.concurrent.CompletionStage<java.util.Optional<java.lang.String>> fetchCachedCategoryId​(java.lang.String key)
      Given a key, this method first checks if cached map of category keys -> ids contains the key.
      java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.categories.Category>> fetchCategory​(java.lang.String key)
      Given a category key, this method fetches a category that matches this given key in the CTP project defined in a potentially injected SphereClient.
      java.util.concurrent.CompletionStage<java.util.Set<io.sphere.sdk.categories.Category>> fetchMatchingCategoriesByKeys​(java.util.Set<java.lang.String> categoryKeys)
      Given a Set of category keys, this method fetches a set of all the categories, matching this given set of keys in the CTP project, defined in an injected SphereClient.
      java.util.concurrent.CompletionStage<io.sphere.sdk.categories.Category> updateCategory​(io.sphere.sdk.categories.Category category, java.util.List<io.sphere.sdk.commands.UpdateAction<io.sphere.sdk.categories.Category>> updateActions)
      Given a Category and a List<UpdateAction<Category>>, this method issues an update request with these update actions on this Category in the CTP project defined in a potentially injected SphereClient.
      • 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
    • Constructor Detail

      • CategoryServiceImpl

        public CategoryServiceImpl​(@Nonnull
                                   CategorySyncOptions 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> categoryKeys)
        Description copied from interface: CategoryService
        Filters out the keys which are already cached and fetches only the not-cached category keys from the CTP project defined in an injected SphereClient and stores a mapping for every category to id in the cached map of keys -> ids and returns this cached map.
        Specified by:
        cacheKeysToIds in interface CategoryService
        Parameters:
        categoryKeys - - a set category keys to fetch and cache the ids for
        Returns:
        CompletionStage<Map> in which the result of it's completion contains a map of requested category keys -> ids
      • fetchMatchingCategoriesByKeys

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Set<io.sphere.sdk.categories.Category>> fetchMatchingCategoriesByKeys​(@Nonnull
                                                                                                                                    java.util.Set<java.lang.String> categoryKeys)
        Description copied from interface: CategoryService
        Given a Set of category keys, this method fetches a set of all the categories, matching this given set of keys in the CTP project, defined in an injected SphereClient. A mapping of the key to the id of the fetched categories is persisted in an in-memory map.
        Specified by:
        fetchMatchingCategoriesByKeys in interface CategoryService
        Parameters:
        categoryKeys - set of category keys to fetch matching categories by.
        Returns:
        CompletionStage<Map> in which the result of it's completion contains a Set of all matching categories.
      • fetchCategory

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.categories.Category>> fetchCategory​(@Nullable
                                                                                                                         java.lang.String key)
        Description copied from interface: CategoryService
        Given a category key, this method fetches a category that matches this given key in the CTP project defined in a potentially injected SphereClient. If there is no matching category 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:
        fetchCategory in interface CategoryService
        Parameters:
        key - the key of the category to fetch.
        Returns:
        CompletionStage<Optional> in which the result of it's completion contains an Optional that contains the matching Category if exists, otherwise empty.
      • fetchCachedCategoryId

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Optional<java.lang.String>> fetchCachedCategoryId​(@Nonnull
                                                                                                                java.lang.String key)
        Description copied from interface: CategoryService
        Given a key, this method first checks if cached map of category 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 Category was found in the CTP project with this key.
        Specified by:
        fetchCachedCategoryId in interface CategoryService
        Parameters:
        key - the key by which a Category 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 Category was found in the CTP project with this key.
      • createCategory

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Optional<io.sphere.sdk.categories.Category>> createCategory​(@Nonnull
                                                                                                                          io.sphere.sdk.categories.CategoryDraft categoryDraft)
        Description copied from interface: CategoryService
        Given a resource draft of type CategoryDraft, this method attempts to create a resource Category 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:
        createCategory in interface CategoryService
        Parameters:
        categoryDraft - 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.
      • updateCategory

        @Nonnull
        public java.util.concurrent.CompletionStage<io.sphere.sdk.categories.Category> updateCategory​(@Nonnull
                                                                                                      io.sphere.sdk.categories.Category category,
                                                                                                      @Nonnull
                                                                                                      java.util.List<io.sphere.sdk.commands.UpdateAction<io.sphere.sdk.categories.Category>> updateActions)
        Description copied from interface: CategoryService
        Given a Category and a List<UpdateAction<Category>>, this method issues an update request with these update actions on this Category in the CTP project defined in a potentially injected SphereClient. This method returns CompletionStage<Category> in which the result of it's completion contains an instance of the Category which was updated in the CTP project.
        Specified by:
        updateCategory in interface CategoryService
        Parameters:
        category - the Category to update.
        updateActions - the update actions to update the Category with.
        Returns:
        CompletionStage<Category> containing as a result of it's completion an instance of the Category which was updated in the CTP project or a SphereException.