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

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

package com.commercetools.sync.customers.utils;

import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.StringUtils.isBlank;

import com.commercetools.api.models.common.Address;
import com.commercetools.api.models.common.AddressDraftBuilder;
import com.commercetools.api.models.common.BaseAddress;
import com.commercetools.api.models.customer.Customer;
import com.commercetools.api.models.customer.CustomerDraft;
import com.commercetools.api.models.customer.CustomerDraftBuilder;
import com.commercetools.api.models.customer_group.CustomerGroup;
import com.commercetools.api.models.customer_group.CustomerGroupReference;
import com.commercetools.api.models.customer_group.CustomerGroupResourceIdentifier;
import com.commercetools.api.models.customer_group.CustomerGroupResourceIdentifierBuilder;
import com.commercetools.api.models.store.StoreKeyReference;
import com.commercetools.api.models.store.StoreResourceIdentifier;
import com.commercetools.api.models.store.StoreResourceIdentifierBuilder;
import com.commercetools.api.models.type.CustomFields;
import com.commercetools.api.models.type.CustomFieldsDraft;
import com.commercetools.api.models.type.CustomFieldsDraftBuilder;
import com.commercetools.api.models.type.Type;
import com.commercetools.api.models.type.TypeReference;
import com.commercetools.api.models.type.TypeResourceIdentifier;
import com.commercetools.api.models.type.TypeResourceIdentifierBuilder;
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * Util class which provides utilities that can be used when syncing resources from a source
 * commercetools project to a target one.
 */
public final class CustomerReferenceResolutionUtils {

  /**
   * Returns a {@link List}<{@link CustomerDraft}> consisting of the results of applying the
   * mapping from {@link Customer} to {@link CustomerDraft} with considering reference resolution.
   *
   * 
   *   
   *   
   *     
   *       
   *       
   *       
   *     
   *   
   *   
   *     
   *        
   *        
   *        
   *     
   *     
   *        
   *        
   *        
   *     
   *     
   *        
   *        
   *        
   *     
   *   
   * 
Mapping of Reference fields for the reference resolution
Reference fieldfromto
customerGroup{@link CustomerGroupReference}{@link CustomerGroupResourceIdentifier}
stores{@link List}<{@link StoreKeyReference}>{@link List}<{@link StoreResourceIdentifier}>
custom.type{@link TypeReference}{@link TypeResourceIdentifier}
* *

Note: The {@link CustomerGroup} and {@link Type} references should contain Id in the * map(cache) with a key value. Any reference, which have its id in place and not replaced by the * key, it would not be found in the map. In this case, this reference will be considered as * existing resources on the target commercetools project and the library will issues an * update/create API request without reference resolution. * * @param customers the customers without expansion of references. * @param referenceIdToKeyCache the instance that manages cache. * @return a {@link List} of {@link CustomerDraft} built from the supplied {@link List} of {@link * Customer}. */ @Nonnull public static List mapToCustomerDrafts( @Nonnull final List customers, @Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) { return customers.stream() .map(customer -> mapToCustomerDraft(customer, referenceIdToKeyCache)) .collect(toList()); } @Nonnull private static CustomerDraft mapToCustomerDraft( @Nonnull final Customer customer, @Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) { return CustomerDraftBuilder.of() .email(customer.getEmail()) .password(customer.getPassword()) .customerNumber(customer.getCustomerNumber()) .key(customer.getKey()) .firstName(customer.getFirstName()) .lastName(customer.getLastName()) .middleName(customer.getMiddleName()) .title(customer.getTitle()) .externalId(customer.getExternalId()) .companyName(customer.getCompanyName()) .customerGroup( mapToCustomerGroupResourceIdentifier( customer.getCustomerGroup(), referenceIdToKeyCache)) .dateOfBirth(customer.getDateOfBirth()) .isEmailVerified(customer.getIsEmailVerified()) .vatId(customer.getVatId()) .addresses(mapToAddressesDraft(customer.getAddresses())) .defaultBillingAddress( getAddressIndex(customer.getAddresses(), customer.getDefaultBillingAddressId())) .billingAddresses( getAddressIndexList(customer.getAddresses(), customer.getBillingAddressIds())) .defaultShippingAddress( getAddressIndex(customer.getAddresses(), customer.getDefaultShippingAddressId())) .shippingAddresses( getAddressIndexList(customer.getAddresses(), customer.getShippingAddressIds())) .custom(mapToCustomFieldsDraft(customer.getCustom(), referenceIdToKeyCache)) .locale(customer.getLocale()) .salutation(customer.getSalutation()) .stores(mapToStores(customer)) .build(); } private static CustomerGroupResourceIdentifier mapToCustomerGroupResourceIdentifier( @Nullable CustomerGroupReference reference, @Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) { if (reference != null) { CustomerGroupResourceIdentifierBuilder builder = new CustomerGroupResourceIdentifierBuilder(); final String id = reference.getId(); if (referenceIdToKeyCache.containsKey(id)) { builder.key(referenceIdToKeyCache.get(id)); } else { builder.id(id); } return builder.build(); } return null; } private static List mapToAddressesDraft(@Nonnull List

addresses) { if (addresses.isEmpty()) { return emptyList(); } return addresses.stream() .map( address -> { final AddressDraftBuilder builder = AddressDraftBuilder.of() .id(address.getId()) .key(address.getKey()) .title(address.getTitle()) .salutation(address.getSalutation()) .firstName(address.getFirstName()) .lastName(address.getLastName()) .streetName(address.getStreetName()) .streetNumber(address.getStreetNumber()) .additionalAddressInfo(address.getAdditionalAddressInfo()) .postalCode(address.getPostalCode()) .city(address.getCity()) .region(address.getRegion()) .country(address.getCountry()) .company(address.getCompany()) .department(address.getDepartment()) .building(address.getBuilding()) .apartment(address.getApartment()) .pOBox(address.getPOBox()) .phone(address.getPhone()) .mobile(address.getMobile()) .email(address.getEmail()) .fax(address.getFax()) .externalId(address.getExternalId()); if (address.getCustom() != null) { builder.custom( CustomFieldsDraftBuilder.of().fields(address.getCustom().getFields()).build()); } return builder.build(); }) .collect(Collectors.toList()); } @Nullable private static Integer getAddressIndex( @Nullable final List
allAddresses, @Nullable final String addressId) { if (allAddresses == null) { return null; } if (isBlank(addressId)) { return null; } for (int i = 0; i < allAddresses.size(); i++) { String id = allAddresses.get(i).getId(); if (id != null && id.equals(addressId)) { return i; } } return null; } private static List getAddressIndexList( @Nullable final List
allAddresses, @Nullable final List addressIds) { if (allAddresses == null || addressIds == null) { return emptyList(); } final List indexes = new ArrayList<>(); for (String addressId : addressIds) { indexes.add(getAddressIndex(allAddresses, addressId)); } return indexes; } @Nullable private static CustomFieldsDraft mapToCustomFieldsDraft( @Nullable final CustomFields customFields, @Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) { if (customFields != null) { final String typeId = customFields.getType().getId(); CustomFieldsDraftBuilder customFieldsDraftBuilder = CustomFieldsDraftBuilder.of(); if (referenceIdToKeyCache.containsKey(typeId)) { customFieldsDraftBuilder.type( TypeResourceIdentifierBuilder.of().key(referenceIdToKeyCache.get(typeId)).build()); } else { customFieldsDraftBuilder.type(TypeResourceIdentifierBuilder.of().id(typeId).build()); } customFieldsDraftBuilder.fields(customFields.getFields()); return customFieldsDraftBuilder.build(); } return null; } private static List mapToStores(@Nonnull final Customer customer) { final List storeReferences = customer.getStores(); if (storeReferences != null) { return storeReferences.stream() .map( storeKeyReference -> StoreResourceIdentifierBuilder.of().key(storeKeyReference.getKey()).build()) .collect(toList()); } return emptyList(); } private CustomerReferenceResolutionUtils() {} }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy