|
11 | 11 | import com.commercetools.api.models.inventory.InventoryEntry; |
12 | 12 | import com.commercetools.api.models.inventory.InventoryEntryDraft; |
13 | 13 | import com.commercetools.api.models.inventory.InventoryEntryDraftBuilder; |
| 14 | +import com.commercetools.api.models.type.CustomFieldsDraft; |
14 | 15 | import com.commercetools.api.models.type.Type; |
15 | 16 | import com.commercetools.api.models.type.TypeReference; |
16 | 17 | import com.commercetools.api.models.type.TypeResourceIdentifier; |
17 | 18 | import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache; |
18 | 19 | import java.util.List; |
19 | 20 | import javax.annotation.Nonnull; |
| 21 | +import javax.annotation.Nullable; |
20 | 22 |
|
21 | 23 | /** |
22 | 24 | * Util class which provides utilities that can be used when syncing resources from a source |
@@ -82,15 +84,40 @@ private static InventoryEntryDraft mapToInventoryEntryDraft( |
82 | 84 | inventoryEntry.getSupplyChannel(), |
83 | 85 | referenceIdToKeyCache, |
84 | 86 | (id, key) -> ChannelResourceIdentifierBuilder.of().key(key).id(id).build()); |
85 | | - return InventoryEntryDraftBuilder.of() |
86 | | - .sku(inventoryEntry.getSku()) |
87 | | - .quantityOnStock(inventoryEntry.getQuantityOnStock()) |
88 | | - .expectedDelivery(inventoryEntry.getExpectedDelivery()) |
89 | | - .restockableInDays(inventoryEntry.getRestockableInDays()) |
90 | | - .key(inventoryEntry.getKey()) |
91 | | - .custom(mapToCustomFieldsDraft(inventoryEntry, referenceIdToKeyCache)) |
92 | | - .supplyChannel(channelResourceIdentifier) |
93 | | - .build(); |
| 87 | + final CustomFieldsDraft customFieldsDraft = |
| 88 | + mapToCustomFieldsDraft(inventoryEntry, referenceIdToKeyCache); |
| 89 | + return getInventoryEntryDraft(inventoryEntry, customFieldsDraft, channelResourceIdentifier); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Creates a new {@link InventoryEntryDraft} from given {@link InventoryEntry}, already mapped |
| 94 | + * {@link CustomFieldsDraft} and channel as {@link ChannelResourceIdentifier}. |
| 95 | + * |
| 96 | + * @param inventoryEntry - a template inventoryEntry to build the draft from |
| 97 | + * @param mappedCustomFields - a customFieldsDraft or null |
| 98 | + * @param channel - a resource identifier representing the supply channel or null |
| 99 | + * @return a new {@link InventoryEntryDraft} with all fields copied from the {@param |
| 100 | + * inventoryEntry} and custom fields set {@param mappedCustomFields} and supply channel with |
| 101 | + * {@param channel} resource identifier - it will return empty InventoryEntryDraft if sku or |
| 102 | + * quantityOnStock are missing. |
| 103 | + */ |
| 104 | + private static InventoryEntryDraft getInventoryEntryDraft( |
| 105 | + @Nonnull final InventoryEntry inventoryEntry, |
| 106 | + @Nullable final CustomFieldsDraft mappedCustomFields, |
| 107 | + @Nullable final ChannelResourceIdentifier channel) { |
| 108 | + if (inventoryEntry.getSku() != null && inventoryEntry.getQuantityOnStock() != null) { |
| 109 | + return InventoryEntryDraftBuilder.of() |
| 110 | + .sku(inventoryEntry.getSku()) |
| 111 | + .quantityOnStock(inventoryEntry.getQuantityOnStock()) |
| 112 | + .expectedDelivery(inventoryEntry.getExpectedDelivery()) |
| 113 | + .restockableInDays(inventoryEntry.getRestockableInDays()) |
| 114 | + .key(inventoryEntry.getKey()) |
| 115 | + .custom(mappedCustomFields) |
| 116 | + .supplyChannel(channel) |
| 117 | + .build(); |
| 118 | + } else { |
| 119 | + return InventoryEntryDraft.of(); |
| 120 | + } |
94 | 121 | } |
95 | 122 |
|
96 | 123 | private InventoryReferenceResolutionUtils() {} |
|
0 commit comments