Class CustomObjectServiceImpl

  • All Implemented Interfaces:
    CustomObjectService

    public class CustomObjectServiceImpl
    extends java.lang.Object
    implements CustomObjectService
    Implementation of CustomObjectService interface.
    • 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<CustomObjectCompositeIdentifier> identifiers)
      Filters out the custom object identifiers which are already cached and fetches only the not-cached custom object identifiers from the CTP project defined in an injected ProjectApiRoot and stores a mapping for every custom object 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<java.lang.String>> fetchCachedCustomObjectId​(CustomObjectCompositeIdentifier identifier)
      Given an identifier, this method first checks if identifier#toString() is contained in a cached map of CustomObjectCompositeIdentifier.toString() -> ids .
      java.util.concurrent.CompletionStage<java.util.Optional<com.commercetools.api.models.custom_object.CustomObject>> fetchCustomObject​(CustomObjectCompositeIdentifier identifier)
      Given a CustomObjectCompositeIdentifier identify which includes key and container of CustomObject, this method fetches a CustomObject that matches this given identifier in the CTP project defined in an injected ProjectApiRoot.
      java.util.concurrent.CompletionStage<java.util.Set<com.commercetools.api.models.custom_object.CustomObject>> fetchMatchingCustomObjects​(java.util.Set<CustomObjectCompositeIdentifier> identifiers)
      Given a Set of CustomObjectCompositeIdentifier, this method fetches a set of all the CustomObjects, matching this given set of CustomObjectCompositeIdentifiers in the CTP project defined in an injected ProjectApiRoot.
      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<java.util.Optional<com.commercetools.api.models.custom_object.CustomObject>> upsertCustomObject​(com.commercetools.api.models.custom_object.CustomObjectDraft customObjectDraft)
      Given a resource draft of CustomObject CustomObjectDraft, this method attempts to create or update a resource CustomObject based on it in the CTP project defined by the sync options.
      • 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

    • Method Detail

      • cacheKeysToIds

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Map<java.lang.String,​java.lang.String>> cacheKeysToIds​(@Nonnull
                                                                                                                           java.util.Set<CustomObjectCompositeIdentifier> identifiers)
        Description copied from interface: CustomObjectService
        Filters out the custom object identifiers which are already cached and fetches only the not-cached custom object identifiers from the CTP project defined in an injected ProjectApiRoot and stores a mapping for every custom object to id in the cached map of keys -> ids and returns this cached map.
        Specified by:
        cacheKeysToIds in interface CustomObjectService
        Parameters:
        identifiers - - a set custom object identifiers to fetch and cache the ids for
        Returns:
        CompletionStage<Map> in which the result of it's completion contains a map of requested custom object identifiers -> ids
      • fetchCachedCustomObjectId

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Optional<java.lang.String>> fetchCachedCustomObjectId​(@Nonnull
                                                                                                                    CustomObjectCompositeIdentifier identifier)
        Description copied from interface: CustomObjectService
        Given an identifier, this method first checks if identifier#toString() is contained in a cached map of CustomObjectCompositeIdentifier.toString() -> ids . If it contains, it returns a CompletionStage<Optional<String>> in which String is what this identifier maps to in the cache. If the cache doesn't contain the identifier, this method attempts to fetch the id of the identifier from the CTP project, caches it and returns a CompletionStage<Optional<String>> in which the Optional could contain the id inside of it.
        Specified by:
        fetchCachedCustomObjectId in interface CustomObjectService
        Parameters:
        identifier - the identifier object containing CustomObject key and container, by which a CustomObject id should be fetched from the CTP project.
        Returns:
        CompletionStage<Optional<String>> in which the result of its completion could contain an Optional with the id inside of it or an empty Optional if no CustomObject was found in the CTP project with this identifier.
      • fetchMatchingCustomObjects

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Set<com.commercetools.api.models.custom_object.CustomObject>> fetchMatchingCustomObjects​(@Nonnull
                                                                                                                                                       java.util.Set<CustomObjectCompositeIdentifier> identifiers)
        Description copied from interface: CustomObjectService
        Given a Set of CustomObjectCompositeIdentifier, this method fetches a set of all the CustomObjects, matching this given set of CustomObjectCompositeIdentifiers in the CTP project defined in an injected ProjectApiRoot. A mapping of the CustomObjectCompositeIdentifier to the id of the fetched CustomObject is persisted in an in-memory map.
        Specified by:
        fetchMatchingCustomObjects in interface CustomObjectService
        Parameters:
        identifiers - set of CustomObjectCompositeIdentifiers. Each identifier includes key and container to fetch matching CustomObject.
        Returns:
        CompletionStage<Map> in which the result of its completion contains a Set of all matching CustomObjects.
      • fetchCustomObject

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Optional<com.commercetools.api.models.custom_object.CustomObject>> fetchCustomObject​(@Nonnull
                                                                                                                                                   CustomObjectCompositeIdentifier identifier)
        Description copied from interface: CustomObjectService
        Given a CustomObjectCompositeIdentifier identify which includes key and container of CustomObject, this method fetches a CustomObject that matches this given identifier in the CTP project defined in an injected ProjectApiRoot. If there is no matching CustomObject an empty Optional will be returned in the returned future.
        Specified by:
        fetchCustomObject in interface CustomObjectService
        Parameters:
        identifier - the identifier of the CustomObject to fetch.
        Returns:
        CompletionStage<Optional> in which the result of its completion contains an Optional that contains the matching CustomObject if exists, otherwise empty.
      • upsertCustomObject

        @Nonnull
        public java.util.concurrent.CompletionStage<java.util.Optional<com.commercetools.api.models.custom_object.CustomObject>> upsertCustomObject​(@Nonnull
                                                                                                                                                    com.commercetools.api.models.custom_object.CustomObjectDraft customObjectDraft)
        Description copied from interface: CustomObjectService
        Given a resource draft of CustomObject CustomObjectDraft, this method attempts to create or update a resource CustomObject based 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 or updated successfully on CTP, then the created resource's id and key/container wrapped by CustomObjectCompositeIdentifier are cached and the method returns a CompletionStage in which the result of its completion contains an instance Optional of the resource which was created or updated.

        If an object with the given container/key exists on CTP, the object will be replaced with the new value and the version is incremente.

        Specified by:
        upsertCustomObject in interface CustomObjectService
        Parameters:
        customObjectDraft - the resource draft to create or update a resource based off of.
        Returns:
        a CompletionStage containing an optional with the created/updated resource if successful otherwise an empty optional.
      • 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)