Skip to content

Commit ac54724

Browse files
committed
DEVX-813: fixing keyResolver
1 parent 215cb0c commit ac54724

5 files changed

Lines changed: 75 additions & 19 deletions

File tree

commercetools/commercetools-importapi-utils/src/main/java/com/commercetools/sdk/CategoryUtil.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313

1414
public class CategoryUtil {
1515

16+
private final KeyResolverService keyResolverService;
17+
private final CommonImportUtil util;
18+
public CategoryUtil() {
19+
keyResolverService = new ExpandObjResolverService();
20+
util = new CommonImportUtil(keyResolverService);
21+
}
22+
23+
public CategoryUtil(final KeyResolverService resolverService) {
24+
keyResolverService = resolverService;
25+
util = new CommonImportUtil(keyResolverService);
26+
}
27+
1628
public CategoryImport toCategoryImport(Category category) {
1729
return CategoryImport.builder()
1830
.key(category.getKey()) // required field
@@ -38,14 +50,14 @@ public CategoryImport toCategoryImport(Category category) {
3850
.map(LocalizedStringBuilder::build)
3951
.orElse(null))
4052
.assets(importAssets(category.getAssets()))
41-
.custom(getImportApiCustom(category.getCustom()))
53+
.custom(util.getImportApiCustom(category.getCustom()))
4254
.build();
4355
}
4456

4557
private CategoryKeyReference CategoryKeyReference(CategoryReference categoryReference) {
4658
if (categoryReference == null) {
4759
return null;
4860
}
49-
return CategoryKeyReference.builder().key(categoryReference.getId()).build();
61+
return CategoryKeyReference.builder().key(keyResolverService.resolveKey(categoryReference)).build();
5062
}
5163
}

commercetools/commercetools-importapi-utils/src/main/java/com/commercetools/sdk/CommonImportUtil.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919

2020
public class CommonImportUtil {
2121

22+
private final KeyResolverService keyResolverService;
23+
public CommonImportUtil() {
24+
keyResolverService = new ExpandObjResolverService();
25+
}
26+
27+
public CommonImportUtil(final KeyResolverService resolverService) {
28+
keyResolverService = resolverService;
29+
}
30+
2231
public static LocalizedStringBuilder getLocalizedStringBuilder(LocalizedString s) {
2332
return com.commercetools.importapi.models.common.LocalizedString.builder().values(s.values());
2433
}
@@ -48,7 +57,7 @@ public static Builder<? extends TypedMoney> importApiTypedMoney(com.commercetool
4857
.fractionDigits(p.getFractionDigits());
4958
}
5059

51-
public static com.commercetools.importapi.models.customfields.Custom getImportApiCustom(CustomFields customFields) {
60+
public com.commercetools.importapi.models.customfields.Custom getImportApiCustom(CustomFields customFields) {
5261
return com.commercetools.importapi.models.customfields.Custom.builder()
5362
.type(getTypeReference(customFields.getType()))
5463
.fields(getImportApiFields(customFields.getFields()))
@@ -108,7 +117,7 @@ static CustomField mapCustomField(Object value) {
108117
throw new IllegalArgumentException("Unsupported custom field type: " + value.getClass());
109118
}
110119

111-
private static com.commercetools.importapi.models.common.TypeKeyReference getTypeReference(TypeReference typeRef) {
112-
return TypeKeyReference.builder().key(typeRef.getId()).build();
120+
private com.commercetools.importapi.models.common.TypeKeyReference getTypeReference(TypeReference typeRef) {
121+
return TypeKeyReference.builder().key(keyResolverService.resolveKey(typeRef)).build();
113122
}
114123
}

commercetools/commercetools-importapi-utils/src/main/java/com/commercetools/sdk/CustomerUtil.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
package com.commercetools.sdk;
33

4-
import static com.commercetools.sdk.CommonImportUtil.getImportApiCustom;
54
import static java.lang.Integer.parseInt;
65

76
import java.util.List;
@@ -18,6 +17,19 @@
1817
import org.jetbrains.annotations.NotNull;
1918

2019
public class CustomerUtil {
20+
private final KeyResolverService keyResolverService;
21+
private final CommonImportUtil util;
22+
23+
public CustomerUtil() {
24+
keyResolverService = new ExpandObjResolverService();
25+
util = new CommonImportUtil(keyResolverService);
26+
}
27+
28+
public CustomerUtil(final KeyResolverService resolverService) {
29+
keyResolverService = resolverService;
30+
util = new CommonImportUtil(keyResolverService);
31+
}
32+
2133
public CustomerImport toCustomerImport(Customer customer) {
2234
return CustomerImport.builder()
2335
.key(customer.getKey()) // required field
@@ -42,7 +54,7 @@ public CustomerImport toCustomerImport(Customer customer) {
4254
.shippingAddresses(getAddressesIds(customer.getShippingAddresses()))
4355
.defaultShippingAddress(getAddressesId(customer.findDefaultShippingAddress().orElse(null)))
4456
.locale(customer.getLocale())
45-
.custom(getImportApiCustom(customer.getCustom())) // required field
57+
.custom(util.getImportApiCustom(customer.getCustom())) // required field
4658
.authenticationMode(toImportApiAuthenticationMode(customer.getAuthenticationMode()))
4759
.build();
4860
}
@@ -56,8 +68,8 @@ private AuthenticationMode toImportApiAuthenticationMode(
5668
return null;
5769
}
5870

59-
public static CustomerGroupKeyReference toCustomerGroupKeyReference(@NotNull CustomerGroupReference customerGroup) {
60-
return CustomerGroupKeyReference.builder().key(customerGroup.getId()).build();
71+
public CustomerGroupKeyReference toCustomerGroupKeyReference(@NotNull CustomerGroupReference customerGroup) {
72+
return CustomerGroupKeyReference.builder().key(keyResolverService.resolveKey(customerGroup)).build();
6173
}
6274

6375
private List<StoreKeyReference> toImportApiStoreKeyReferences(
@@ -106,7 +118,7 @@ private CustomerAddress toCustomerAddress(Address address) {
106118
.fax(address.getFax())
107119
.additionalAddressInfo(address.getAdditionalAddressInfo())
108120
.externalId(address.getExternalId())
109-
.custom(getImportApiCustom(address.getCustom())) // required field
121+
.custom(util.getImportApiCustom(address.getCustom())) // required field
110122
.build();
111123
}
112124
}
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11

22
package com.commercetools.sdk;
33

4-
import static com.commercetools.sdk.CommonImportUtil.getImportApiCustom;
54

65
import com.commercetools.api.models.channel.ChannelReference;
76
import com.commercetools.api.models.inventory.InventoryEntry;
87
import com.commercetools.importapi.models.common.ChannelKeyReference;
98
import com.commercetools.importapi.models.inventories.InventoryImport;
109

1110
public class InventoryUtil {
11+
private final KeyResolverService keyResolverService;
12+
private final CommonImportUtil util;
13+
public InventoryUtil() {
14+
keyResolverService = new ExpandObjResolverService();
15+
util = new CommonImportUtil(keyResolverService);
16+
}
17+
18+
public InventoryUtil(final KeyResolverService resolverService) {
19+
keyResolverService = resolverService;
20+
util = new CommonImportUtil(keyResolverService);
21+
}
22+
1223
public InventoryImport toInventoryImport(InventoryEntry entry) {
1324
return InventoryImport.builder()
1425
.key(entry.getKey()) // required field
@@ -18,14 +29,14 @@ public InventoryImport toInventoryImport(InventoryEntry entry) {
1829
.expectedDelivery(entry.getExpectedDelivery())
1930
.reservationExpirationInMinutes(entry.getReservationExpirationInMinutes())
2031
.supplyChannel(getImportAPISupplyChannel(entry.getSupplyChannel()))
21-
.custom(getImportApiCustom(entry.getCustom()))
32+
.custom(util.getImportApiCustom(entry.getCustom()))
2233
.build();
2334
}
2435

2536
private ChannelKeyReference getImportAPISupplyChannel(ChannelReference supplyChannel) {
2637
if (supplyChannel == null) {
2738
return null;
2839
}
29-
return ChannelKeyReference.builder().key(supplyChannel.getId()).build();
40+
return ChannelKeyReference.builder().key(keyResolverService.resolveKey(supplyChannel)).build();
3041
}
3142
}

commercetools/commercetools-importapi-utils/src/main/java/com/commercetools/sdk/StandalonePriceUtil.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
package com.commercetools.sdk;
33

4-
import static com.commercetools.sdk.CommonImportUtil.getImportApiCustom;
54
import static com.commercetools.sdk.CommonImportUtil.importApiTypedMoney;
65
import static com.commercetools.sdk.ProductUtil.toProductDiscountKeyReference;
76

@@ -15,19 +14,32 @@
1514
import com.commercetools.importapi.models.standalone_prices.StandalonePriceImport;
1615

1716
public class StandalonePriceUtil {
18-
public static StandalonePriceImport toStandalonePriceImport(StandalonePrice price) {
17+
private final KeyResolverService keyResolverService;
18+
private final CommonImportUtil util;
19+
20+
public StandalonePriceUtil() {
21+
keyResolverService = new ExpandObjResolverService();
22+
util = new CommonImportUtil(keyResolverService);
23+
}
24+
25+
public StandalonePriceUtil(final KeyResolverService resolverService) {
26+
keyResolverService = resolverService;
27+
util = new CommonImportUtil(keyResolverService);
28+
}
29+
30+
public StandalonePriceImport toStandalonePriceImport(StandalonePrice price) {
1931
return StandalonePriceImport.builder()
2032
.key(price.getKey()) // required field
2133
.sku(price.getSku()) // required field
2234
.value(v -> importApiTypedMoney(price.getValue(), v)) // required field
2335
.country(price.getCountry())
24-
.customerGroup(CustomerUtil.toCustomerGroupKeyReference(price.getCustomerGroup()))
36+
.customerGroup((new CustomerUtil(keyResolverService)).toCustomerGroupKeyReference(price.getCustomerGroup()))
2537
.channel(toImportApiChannelKeyReference(price.getChannel()))
2638
.validFrom(price.getValidFrom())
2739
.validUntil(price.getValidUntil())
2840
.tiers(toImportApiPriceTiers(price.getTiers()))
2941
.discounted(toImportApiDiscountedPrice(price.getDiscounted()))
30-
.custom(getImportApiCustom(price.getCustom()))
42+
.custom(util.getImportApiCustom(price.getCustom()))
3143
.active(price.getActive())
3244
.build();
3345
}
@@ -54,10 +66,10 @@ private static PriceTier toImportApiPriceTier(com.commercetools.api.models.commo
5466
.build();
5567
}
5668

57-
private static ChannelKeyReference toImportApiChannelKeyReference(ChannelReference channel) {
69+
private ChannelKeyReference toImportApiChannelKeyReference(ChannelReference channel) {
5870
if (channel == null) {
5971
return null;
6072
}
61-
return ChannelKeyReference.builder().key(channel.getId()).build();
73+
return ChannelKeyReference.builder().key(keyResolverService.resolveKey(channel)).build();
6274
}
6375
}

0 commit comments

Comments
 (0)