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

com.commercetools.sync.customers.utils.CustomerTransformUtils Maven / Gradle / Ivy

Go to download

Java Library used to import and/or sync (taking care of changes) data into one or more commercetools projects from external sources such as CSV, XML, JSON, etc.. or even from an already existing commercetools project.

The newest version!
package com.commercetools.sync.customers.utils;

import static java.util.stream.Collectors.toSet;

import com.commercetools.api.client.ProjectApiRoot;
import com.commercetools.api.models.common.Reference;
import com.commercetools.api.models.customer.Customer;
import com.commercetools.api.models.customer.CustomerDraft;
import com.commercetools.api.models.type.CustomFields;
import com.commercetools.sync.commons.models.GraphQlQueryResource;
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache;
import com.commercetools.sync.services.impl.BaseTransformServiceImpl;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import javax.annotation.Nonnull;

public final class CustomerTransformUtils {

  /**
   * Transforms customers by resolving the references and map them to CustomerDrafts.
   *
   * 

This method resolves(fetch key values for the reference id's) non null and unexpanded * references of the customer{@link Customer} by using cache. * *

If the reference ids are already cached, key values are pulled from the cache, otherwise it * executes the query to fetch the key value for the reference id's and store the idToKey value * pair in the cache for reuse. * *

Then maps the Customer to CustomerDraft by performing reference resolution considering * idToKey value from the cache. * * @param client commercetools client. * @param referenceIdToKeyCache the instance that manages cache. * @param customers the customers to resolve the references. * @return a new list which contains customerDrafts which have all their references resolved. */ @Nonnull public static CompletableFuture> toCustomerDrafts( @Nonnull final ProjectApiRoot client, @Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache, @Nonnull final List customers) { final CustomerTransformUtils.CustomerTransformServiceImpl customerTransformService = new CustomerTransformUtils.CustomerTransformServiceImpl(client, referenceIdToKeyCache); return customerTransformService.toCustomerDrafts(customers); } private static class CustomerTransformServiceImpl extends BaseTransformServiceImpl { public CustomerTransformServiceImpl( @Nonnull final ProjectApiRoot ctpClient, @Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) { super(ctpClient, referenceIdToKeyCache); } @Nonnull public CompletableFuture> toCustomerDrafts( @Nonnull final List customers) { final List> transformReferencesToRunParallel = new ArrayList<>(); transformReferencesToRunParallel.add(this.transformCustomTypeReference(customers)); transformReferencesToRunParallel.add(this.transformCustomerGroupReference(customers)); return CompletableFuture.allOf( transformReferencesToRunParallel.stream().toArray(CompletableFuture[]::new)) .thenApply( ignore -> CustomerReferenceResolutionUtils.mapToCustomerDrafts( customers, referenceIdToKeyCache)); } @Nonnull private CompletableFuture transformCustomTypeReference( @Nonnull final List customers) { final Set setOfTypeIds = customers.stream() .map(Customer::getCustom) .filter(Objects::nonNull) .map(CustomFields::getType) .map(Reference::getId) .collect(toSet()); return fetchAndFillReferenceIdToKeyCache(setOfTypeIds, GraphQlQueryResource.TYPES); } @Nonnull private CompletableFuture transformCustomerGroupReference( @Nonnull final List customers) { final Set customerGroupIds = customers.stream() .map(Customer::getCustomerGroup) .filter(Objects::nonNull) .map(Reference::getId) .collect(toSet()); return fetchAndFillReferenceIdToKeyCache( customerGroupIds, GraphQlQueryResource.CUSTOMER_GROUPS); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy