|
| 1 | +package com.commercetools.sdk; |
| 2 | + |
| 3 | +import com.commercetools.api.models.common.LocalizedString; |
| 4 | +import com.commercetools.api.models.type.CustomFields; |
| 5 | +import com.commercetools.api.models.type.FieldContainer; |
| 6 | +import com.commercetools.api.models.type.TypeReference; |
| 7 | +import com.commercetools.importapi.models.common.*; |
| 8 | +import com.commercetools.importapi.models.customfields.CustomField; |
| 9 | +import io.vrap.rmf.base.client.Builder; |
| 10 | + |
| 11 | +import java.time.LocalDate; |
| 12 | +import java.time.LocalTime; |
| 13 | +import java.time.ZonedDateTime; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.stream.Collectors; |
| 17 | + |
| 18 | +public class CommonImportUtil { |
| 19 | + |
| 20 | + public static LocalizedStringBuilder getLocalizedStringBuilder(LocalizedString s) { |
| 21 | + return com.commercetools.importapi.models.common.LocalizedString.builder().values(s.values()); |
| 22 | + } |
| 23 | + |
| 24 | + public static List<Asset> importAssets( |
| 25 | + List<com.commercetools.api.models.common.Asset> assets) { |
| 26 | + if (assets == null) { |
| 27 | + return null; |
| 28 | + } |
| 29 | + return assets.stream() |
| 30 | + .map(a -> com.commercetools.importapi.models.common.Asset.builder() |
| 31 | + .key(a.getKey()) |
| 32 | + .name(getLocalizedStringBuilder(a.getName()).build()) |
| 33 | + .build()) |
| 34 | + .collect(Collectors.toList()); |
| 35 | + } |
| 36 | + |
| 37 | + public static Builder<? extends TypedMoney> importApiTypedMoney(com.commercetools.api.models.common.TypedMoney p, |
| 38 | + TypedMoneyBuilder v) { |
| 39 | + return (p instanceof HighPrecisionMoney) |
| 40 | + ? v.highPrecisionBuilder() |
| 41 | + .centAmount(p.getCentAmount()) |
| 42 | + .currencyCode(p.getCurrencyCode()) |
| 43 | + .preciseAmount(((com.commercetools.api.models.common.HighPrecisionMoney) p).getPreciseAmount()) |
| 44 | + : v.centPrecisionBuilder() |
| 45 | + .centAmount(p.getCentAmount()) |
| 46 | + .currencyCode(p.getCurrencyCode()) |
| 47 | + .fractionDigits(p.getFractionDigits()); |
| 48 | + } |
| 49 | + |
| 50 | + public static com.commercetools.importapi.models.customfields.Custom getImportApiCustom(CustomFields customFields) { |
| 51 | + return com.commercetools.importapi.models.customfields.Custom.builder() |
| 52 | + .type(getTypeReference(customFields.getType())) |
| 53 | + .fields(getImportApiFields(customFields.getFields())) |
| 54 | + .build(); |
| 55 | + } |
| 56 | + |
| 57 | + private static com.commercetools.importapi.models.customfields.FieldContainer getImportApiFields( |
| 58 | + FieldContainer fields) { |
| 59 | + return com.commercetools.importapi.models.customfields.FieldContainer.builder() |
| 60 | + .values(mapCustomField(fields.values())) |
| 61 | + .build(); |
| 62 | + } |
| 63 | + |
| 64 | + static Map<String, CustomField> mapCustomField( |
| 65 | + Map<String, Object> customFieldsValues) { |
| 66 | + if (customFieldsValues == null) return null; |
| 67 | + return customFieldsValues.entrySet() |
| 68 | + .stream() |
| 69 | + .collect(Collectors.toMap( |
| 70 | + Map.Entry::getKey, |
| 71 | + e -> mapCustomField(e.getValue()) |
| 72 | + )); |
| 73 | + } |
| 74 | + |
| 75 | + static CustomField mapCustomField(Object value) { |
| 76 | + if (value instanceof String) { |
| 77 | + return CustomField.stringBuilder().value((String) value).build(); |
| 78 | + } |
| 79 | + if (value instanceof Boolean) { |
| 80 | + return CustomField.booleanBuilder().value((Boolean) value).build(); |
| 81 | + } |
| 82 | + if (value instanceof Double) { |
| 83 | + return CustomField.numberBuilder().value((Double) value).build(); |
| 84 | + } |
| 85 | + if (value instanceof Integer) { |
| 86 | + return CustomField.numberBuilder().value(((Integer) value).doubleValue()).build(); |
| 87 | + } |
| 88 | + if (value instanceof Long) { |
| 89 | + return CustomField.numberBuilder().value(((Long) value).doubleValue()).build(); |
| 90 | + } |
| 91 | + if (value instanceof LocalizedString) { |
| 92 | + return CustomField.localizedStringBuilder() |
| 93 | + .value(getLocalizedStringBuilder((LocalizedString) value).build()) |
| 94 | + .build(); |
| 95 | + } |
| 96 | + if (value instanceof LocalDate) { |
| 97 | + return CustomField.dateBuilder().value((LocalDate) value).build(); |
| 98 | + } |
| 99 | + if (value instanceof ZonedDateTime) { |
| 100 | + return CustomField.dateTimeBuilder().value((ZonedDateTime) value).build(); |
| 101 | + } |
| 102 | + if (value instanceof LocalTime) { |
| 103 | + return CustomField.timeBuilder().value((LocalTime) value).build(); |
| 104 | + } |
| 105 | + if (value instanceof Money) { |
| 106 | + return CustomField.moneyBuilder() |
| 107 | + .value(v -> importApiTypedMoney( |
| 108 | + (com.commercetools.api.models.common.TypedMoney) value, v)) |
| 109 | + .build(); |
| 110 | + } |
| 111 | + throw new IllegalArgumentException("Unsupported custom field type: " + value.getClass()); |
| 112 | + } |
| 113 | + |
| 114 | + private static com.commercetools.importapi.models.common.TypeKeyReference getTypeReference(TypeReference typeRef) { |
| 115 | + return TypeKeyReference.builder() |
| 116 | + .key(typeRef.getId()) |
| 117 | + .build(); |
| 118 | + } |
| 119 | +} |
0 commit comments