|
| 1 | +package com.commercetools.sdk; |
| 2 | + |
| 3 | +import com.commercetools.api.models.common.Address; |
| 4 | +import com.commercetools.api.models.common.BaseAddress; |
| 5 | +import com.commercetools.api.models.customer.Customer; |
| 6 | +import com.commercetools.api.models.customer_group.CustomerGroupReference; |
| 7 | +import com.commercetools.importapi.models.common.CustomerGroupKeyReference; |
| 8 | +import com.commercetools.importapi.models.common.StoreKeyReference; |
| 9 | +import com.commercetools.importapi.models.customers.AuthenticationMode; |
| 10 | +import com.commercetools.importapi.models.customers.CustomerAddress; |
| 11 | +import com.commercetools.importapi.models.customers.CustomerImport; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static com.commercetools.sdk.CommonImportUtil.getImportApiCustom; |
| 17 | +import static java.lang.Integer.parseInt; |
| 18 | + |
| 19 | +public class CustomerUtil { |
| 20 | + public CustomerImport toCustomerImport(Customer customer) { |
| 21 | + return CustomerImport.builder() |
| 22 | + .key(customer.getKey()) // required field |
| 23 | + .customerNumber(customer.getCustomerNumber()) |
| 24 | + .email(customer.getEmail()) // required field |
| 25 | + .password(customer.getPassword()) |
| 26 | + .stores(toImportApiStoreKeyReferences(customer.getStores())) // required field |
| 27 | + .firstName(customer.getFirstName()) |
| 28 | + .lastName(customer.getLastName()) |
| 29 | + .middleName(customer.getMiddleName()) |
| 30 | + .title(customer.getTitle()) |
| 31 | + .salutation(customer.getSalutation()) |
| 32 | + .externalId(customer.getExternalId()) |
| 33 | + .dateOfBirth(customer.getDateOfBirth()) |
| 34 | + .companyName(customer.getCompanyName()) |
| 35 | + .vatId(customer.getVatId()) |
| 36 | + .isEmailVerified(customer.getIsEmailVerified()) |
| 37 | + .customerGroup(toCustomerGroupKeyReference(customer.getCustomerGroup())) // required field |
| 38 | + .addresses(mapToCustomerAddresses(customer.getAddresses())) // required field |
| 39 | + .defaultBillingAddress(getAddressesId(customer.getDefaultBillingAddress())) |
| 40 | + .billingAddresses(getAddressesIds(customer.getBillingAddresses())) |
| 41 | + .shippingAddresses(getAddressesIds(customer.getShippingAddresses())) |
| 42 | + .defaultShippingAddress(getAddressesId(customer.findDefaultShippingAddress().orElse(null))) |
| 43 | + .locale(customer.getLocale()) |
| 44 | + .custom(getImportApiCustom(customer.getCustom())) // required field |
| 45 | + .authenticationMode(toImportApiAuthenticationMode(customer.getAuthenticationMode())) |
| 46 | + .build(); |
| 47 | + } |
| 48 | + |
| 49 | + private AuthenticationMode toImportApiAuthenticationMode( |
| 50 | + com.commercetools.api.models.customer.AuthenticationMode authenticationMode) { |
| 51 | + if (authenticationMode instanceof com.commercetools.api.models.customer.AuthenticationMode.AuthenticationModeEnum) { |
| 52 | + return AuthenticationMode.AuthenticationModeEnum.valueOf(authenticationMode.name()); |
| 53 | + } |
| 54 | + else return null; |
| 55 | + } |
| 56 | + |
| 57 | + private CustomerGroupKeyReference toCustomerGroupKeyReference( |
| 58 | + @NotNull CustomerGroupReference customerGroup) { |
| 59 | + return CustomerGroupKeyReference.builder().key(customerGroup.getId()).build(); |
| 60 | + } |
| 61 | + |
| 62 | + private List<StoreKeyReference> toImportApiStoreKeyReferences( |
| 63 | + @NotNull |
| 64 | + List<com.commercetools.api.models.store.StoreKeyReference> stores) { |
| 65 | + return stores.stream().map(x -> StoreKeyReference.builder().key(x.getKey()).build()).toList(); |
| 66 | + } |
| 67 | + |
| 68 | + private List<Integer> getAddressesIds(List<Address> shippingAddresses) { |
| 69 | + return shippingAddresses.stream().map(CustomerUtil::getAddressesId).toList(); |
| 70 | + } |
| 71 | + |
| 72 | + private static Integer getAddressesId(Address shippingAddress) { |
| 73 | + if (shippingAddress == null) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + return parseInt(shippingAddress.getId()); |
| 77 | + } |
| 78 | + |
| 79 | + private List<CustomerAddress> mapToCustomerAddresses(List<Address> shippingAddresses) { |
| 80 | + return shippingAddresses.stream().map(this::toCustomerAddress).toList(); |
| 81 | + } |
| 82 | + |
| 83 | + private CustomerAddress toCustomerAddress(Address address) { |
| 84 | + return CustomerAddress.builder() |
| 85 | + .key(address.getKey()) // required field |
| 86 | + .country(address.getCountry()) // required field |
| 87 | + .title(address.getTitle()) |
| 88 | + .salutation(address.getSalutation()) |
| 89 | + .firstName(address.getFirstName()) |
| 90 | + .lastName(address.getLastName()) |
| 91 | + .streetName(address.getStreetName()) |
| 92 | + .streetNumber(address.getStreetNumber()) |
| 93 | + .additionalStreetInfo(address.getAdditionalStreetInfo()) |
| 94 | + .postalCode(address.getPostalCode()) |
| 95 | + .city(address.getCity()) |
| 96 | + .region(address.getRegion()) |
| 97 | + .state(address.getState()) |
| 98 | + .company(address.getCompany()) |
| 99 | + .department(address.getDepartment()) |
| 100 | + .building(address.getBuilding()) |
| 101 | + .apartment(address.getApartment()) |
| 102 | + .pOBox(address.getPOBox()) |
| 103 | + .phone(address.getPhone()) |
| 104 | + .mobile(address.getMobile()) |
| 105 | + .email(address.getEmail()) |
| 106 | + .fax(address.getFax()) |
| 107 | + .additionalAddressInfo(address.getAdditionalAddressInfo()) |
| 108 | + .externalId(address.getExternalId()) |
| 109 | + .custom(getImportApiCustom(address.getCustom())) // required field |
| 110 | + .build(); |
| 111 | + } |
| 112 | +} |
0 commit comments