All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.commercetools.sync.services.impl.ChannelServiceImpl Maven / Gradle / Ivy

package com.commercetools.sync.services.impl;

import com.commercetools.api.client.ByProjectKeyChannelsByIDGet;
import com.commercetools.api.client.ByProjectKeyChannelsGet;
import com.commercetools.api.client.ByProjectKeyChannelsPost;
import com.commercetools.api.models.channel.Channel;
import com.commercetools.api.models.channel.ChannelDraft;
import com.commercetools.api.models.channel.ChannelDraftBuilder;
import com.commercetools.api.models.channel.ChannelPagedQueryResponse;
import com.commercetools.api.models.channel.ChannelRoleEnum;
import com.commercetools.api.predicates.query.channel.ChannelQueryBuilderDsl;
import com.commercetools.sync.commons.BaseSyncOptions;
import com.commercetools.sync.commons.models.GraphQlQueryResource;
import com.commercetools.sync.services.ChannelService;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import javax.annotation.Nonnull;

public final class ChannelServiceImpl
    extends BaseService<
        BaseSyncOptions,
        Channel,
        ChannelDraft,
        ByProjectKeyChannelsGet,
        ChannelPagedQueryResponse,
        ByProjectKeyChannelsByIDGet,
        Channel,
        ChannelQueryBuilderDsl,
        ByProjectKeyChannelsPost>
    implements ChannelService {

  private final Set channelRoles;

  public ChannelServiceImpl(
      @Nonnull final BaseSyncOptions syncOptions,
      @Nonnull final Set channelRoles) {
    super(syncOptions);
    this.channelRoles = channelRoles;
  }

  @Nonnull
  @Override
  public CompletionStage> cacheKeysToIds(
      @Nonnull final Set channelKeys) {
    return super.cacheKeysToIdsUsingGraphQl(channelKeys, GraphQlQueryResource.CHANNELS);
  }

  @Nonnull
  @Override
  public CompletionStage> fetchCachedChannelId(@Nonnull final String key) {
    ByProjectKeyChannelsGet query =
        syncOptions
            .getCtpClient()
            .channels()
            .get()
            .withWhere("key = :key")
            .withPredicateVar("key", key);

    return fetchCachedResourceId(key, resource -> resource.getKey(), query);
  }

  @Nonnull
  @Override
  public CompletionStage> createChannel(@Nonnull final String key) {
    ChannelDraft channelDraft =
        ChannelDraftBuilder.of().key(key).roles(List.copyOf(channelRoles)).build();

    return super.createResource(
        channelDraft,
        ChannelDraft::getKey,
        Channel::getId,
        Function.identity(),
        () -> syncOptions.getCtpClient().channels().post(channelDraft));
  }

  @Nonnull
  @Override
  public CompletionStage> createAndCacheChannel(@Nonnull final String key) {

    return createChannel(key)
        .thenApply(
            channelOptional -> {
              channelOptional.ifPresent(channel -> keyToIdCache.put(key, channel.getId()));
              return channelOptional;
            });
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy