Skip to content

Commit 4ea9d67

Browse files
fix: store in shipping-lists
1 parent 47364d1 commit 4ea9d67

File tree

4 files changed

+94
-5
lines changed

4 files changed

+94
-5
lines changed

src/main/java/com/commercetools/sync/shoppinglists/helpers/ShoppingListReferenceResolver.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import com.commercetools.api.models.customer.CustomerResourceIdentifierBuilder;
1212
import com.commercetools.api.models.shopping_list.ShoppingListDraft;
1313
import com.commercetools.api.models.shopping_list.ShoppingListDraftBuilder;
14+
import com.commercetools.api.models.store.StoreResourceIdentifier;
15+
import com.commercetools.api.models.store.StoreResourceIdentifierBuilder;
1416
import com.commercetools.sync.commons.exceptions.ReferenceResolutionException;
1517
import com.commercetools.sync.commons.helpers.CustomReferenceResolver;
1618
import com.commercetools.sync.services.CustomerService;
@@ -32,6 +34,9 @@ public final class ShoppingListReferenceResolver
3234
"Failed to resolve customer resource identifier on "
3335
+ "ShoppingListDraft with key:'%s'. Reason: %s";
3436
static final String CUSTOMER_DOES_NOT_EXIST = "Customer with key '%s' doesn't exist.";
37+
static final String FAILED_TO_RESOLVE_STORE_REFERENCE =
38+
"Failed to resolve store resource identifier on "
39+
+ "ShoppingListDraft with key:'%s'. Reason: %s";
3540
static final String FAILED_TO_RESOLVE_CUSTOM_TYPE =
3641
"Failed to resolve custom type reference on ShoppingListDraft with key:'%s'. ";
3742

@@ -66,9 +71,9 @@ public ShoppingListReferenceResolver(
6671
}
6772

6873
/**
69-
* Given a {@link ShoppingListDraft} this method attempts to resolve the customer and custom type
70-
* references to return a {@link java.util.concurrent.CompletionStage} which contains a new
71-
* instance of the draft with the resolved references.
74+
* Given a {@link ShoppingListDraft} this method attempts to resolve the customer, store and
75+
* custom type references to return a {@link java.util.concurrent.CompletionStage} which contains
76+
* a new instance of the draft with the resolved references.
7277
*
7378
* @param shoppingListDraft the shoppingListDraft to resolve its references.
7479
* @return a {@link java.util.concurrent.CompletionStage} that contains as a result a new
@@ -79,6 +84,7 @@ public ShoppingListReferenceResolver(
7984
public CompletionStage<ShoppingListDraft> resolveReferences(
8085
@Nonnull final ShoppingListDraft shoppingListDraft) {
8186
return resolveCustomerReference(ShoppingListDraftBuilder.of(shoppingListDraft))
87+
.thenCompose(this::resolveStoreReference)
8288
.thenCompose(this::resolveCustomTypeReference)
8389
.thenCompose(this::resolveLineItemReferences)
8490
.thenCompose(this::resolveTextLineItemReferences)
@@ -136,6 +142,32 @@ private CompletionStage<ShoppingListDraftBuilder> fetchAndResolveCustomerReferen
136142
}));
137143
}
138144

145+
@Nonnull
146+
protected CompletionStage<ShoppingListDraftBuilder> resolveStoreReference(
147+
@Nonnull final ShoppingListDraftBuilder draftBuilder) {
148+
149+
final StoreResourceIdentifier storeResourceIdentifier = draftBuilder.getStore();
150+
if (storeResourceIdentifier != null && storeResourceIdentifier.getId() == null) {
151+
try {
152+
final String storeKey = getKeyFromResourceIdentifier(storeResourceIdentifier);
153+
return completedFuture(
154+
draftBuilder.store(StoreResourceIdentifierBuilder.of().key(storeKey).build()));
155+
} catch (ReferenceResolutionException referenceResolutionException) {
156+
return exceptionallyCompletedFuture(
157+
new ReferenceResolutionException(
158+
format(
159+
FAILED_TO_RESOLVE_STORE_REFERENCE,
160+
draftBuilder.getKey(),
161+
referenceResolutionException.getMessage())));
162+
}
163+
} else if (storeResourceIdentifier != null && storeResourceIdentifier.getId() != null) {
164+
return completedFuture(
165+
draftBuilder.store(
166+
StoreResourceIdentifierBuilder.of().id(storeResourceIdentifier.getId()).build()));
167+
}
168+
return completedFuture(draftBuilder);
169+
}
170+
139171
@Nonnull
140172
protected CompletionStage<ShoppingListDraftBuilder> resolveCustomTypeReference(
141173
@Nonnull final ShoppingListDraftBuilder draftBuilder) {

src/main/java/com/commercetools/sync/shoppinglists/utils/ShoppingListReferenceResolutionUtils.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import com.commercetools.api.models.shopping_list.TextLineItem;
1616
import com.commercetools.api.models.shopping_list.TextLineItemDraft;
1717
import com.commercetools.api.models.shopping_list.TextLineItemDraftBuilder;
18+
import com.commercetools.api.models.store.StoreResourceIdentifier;
19+
import com.commercetools.api.models.store.StoreResourceIdentifierBuilder;
1820
import com.commercetools.sync.commons.utils.ReferenceIdToKeyCache;
1921
import java.util.List;
2022
import java.util.Objects;
@@ -48,6 +50,11 @@ public final class ShoppingListReferenceResolutionUtils {
4850
* <td>{@link com.commercetools.api.models.customer.CustomerResourceIdentifier}</td>
4951
* </tr>
5052
* <tr>
53+
* <td>store</td>
54+
* <td>{@link com.commercetools.api.models.store.StoreKeyReference}</td>
55+
* <td>{@link com.commercetools.api.models.store.StoreResourceIdentifier}</td>
56+
* </tr>
57+
* <tr>
5158
* <td>custom.type</td>
5259
* <td>{@link com.commercetools.api.models.type.TypeReference}</td>
5360
* <td>{@link com.commercetools.api.models.type.TypeResourceIdentifier}</td>
@@ -107,6 +114,11 @@ public static List<ShoppingListDraft> mapToShoppingListDrafts(
107114
* <td>{@link com.commercetools.api.models.customer.CustomerResourceIdentifier}</td>
108115
* </tr>
109116
* <tr>
117+
* <td>store</td>
118+
* <td>{@link com.commercetools.api.models.store.StoreKeyReference}</td>
119+
* <td>{@link com.commercetools.api.models.store.StoreResourceIdentifier}</td>
120+
* </tr>
121+
* <tr>
110122
* <td>custom.type</td>
111123
* <td>{@link com.commercetools.api.models.type.TypeReference}</td>
112124
* <td>{@link com.commercetools.api.models.type.TypeResourceIdentifier}</td>
@@ -139,16 +151,18 @@ public static ShoppingListDraft mapToShoppingListDraft(
139151
@Nonnull final ShoppingList shoppingList,
140152
@Nonnull final ReferenceIdToKeyCache referenceIdToKeyCache) {
141153

142-
final CustomerResourceIdentifier resourceIdentifierWithKey =
154+
final CustomerResourceIdentifier customerResourceIdentifierWithKey =
143155
getResourceIdentifierWithKey(
144156
shoppingList.getCustomer(),
145157
referenceIdToKeyCache,
146158
(id, key) -> CustomerResourceIdentifierBuilder.of().id(id).key(key).build());
159+
147160
return ShoppingListDraftBuilder.of()
148161
.name(shoppingList.getName())
149162
.description(shoppingList.getDescription())
150163
.key(shoppingList.getKey())
151-
.customer(resourceIdentifierWithKey)
164+
.customer(customerResourceIdentifierWithKey)
165+
.store(mapToStoreResourceIdentifier(shoppingList))
152166
.slug(shoppingList.getSlug())
153167
.lineItems(mapToLineItemDrafts(shoppingList.getLineItems(), referenceIdToKeyCache))
154168
.textLineItems(
@@ -217,5 +231,14 @@ private static TextLineItemDraft mapToTextLineItemDraft(
217231
.build();
218232
}
219233

234+
@Nullable
235+
private static StoreResourceIdentifier mapToStoreResourceIdentifier(
236+
@Nonnull final ShoppingList shoppingList) {
237+
if (shoppingList.getStore() != null) {
238+
return StoreResourceIdentifierBuilder.of().key(shoppingList.getStore().getKey()).build();
239+
}
240+
return null;
241+
}
242+
220243
private ShoppingListReferenceResolutionUtils() {}
221244
}

src/main/java/com/commercetools/sync/shoppinglists/utils/ShoppingListSyncUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public static List<ShoppingListUpdateAction> buildActions(
4646
buildSetDescriptionUpdateAction(oldShoppingList, newShoppingList),
4747
buildSetAnonymousIdUpdateAction(oldShoppingList, newShoppingList),
4848
buildSetCustomerUpdateAction(oldShoppingList, newShoppingList),
49+
buildSetStoreUpdateAction(oldShoppingList, newShoppingList),
4950
buildSetDeleteDaysAfterLastModificationUpdateAction(oldShoppingList, newShoppingList));
5051

5152
updateActions.addAll(

src/main/java/com/commercetools/sync/shoppinglists/utils/ShoppingListUpdateActionUtils.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.commercetools.api.models.shopping_list.ShoppingListSetDeleteDaysAfterLastModificationActionBuilder;
1212
import com.commercetools.api.models.shopping_list.ShoppingListSetDescriptionActionBuilder;
1313
import com.commercetools.api.models.shopping_list.ShoppingListSetSlugActionBuilder;
14+
import com.commercetools.api.models.shopping_list.ShoppingListSetStoreActionBuilder;
1415
import com.commercetools.api.models.shopping_list.ShoppingListUpdateAction;
1516
import java.util.Optional;
1617
import javax.annotation.Nonnull;
@@ -116,6 +117,38 @@ public static Optional<ShoppingListUpdateAction> buildSetCustomerUpdateAction(
116117
.build());
117118
}
118119

120+
/**
121+
* Compares the store references of a {@link ShoppingList} and a {@link ShoppingListDraft} and
122+
* returns an {@link ShoppingListUpdateAction} as a result in an {@link java.util.Optional}. If
123+
* both the {@link ShoppingList} and the {@link ShoppingListDraft} have the same store, then no
124+
* update action is needed and hence an empty {@link java.util.Optional} is returned.
125+
*
126+
* <p>Note: This method compares store keys since {@link
127+
* com.commercetools.api.models.store.StoreKeyReference} contains the key directly.
128+
*
129+
* @param oldShoppingList the shopping list which should be updated.
130+
* @param newShoppingList the shopping list draft which holds the new store.
131+
* @return A filled optional with the update action or an empty optional if the stores are
132+
* identical.
133+
*/
134+
@Nonnull
135+
public static Optional<ShoppingListUpdateAction> buildSetStoreUpdateAction(
136+
@Nonnull final ShoppingList oldShoppingList,
137+
@Nonnull final ShoppingListDraft newShoppingList) {
138+
139+
final String oldStoreKey =
140+
oldShoppingList.getStore() != null ? oldShoppingList.getStore().getKey() : null;
141+
final String newStoreKey =
142+
newShoppingList.getStore() != null && newShoppingList.getStore().getKey() != null
143+
? newShoppingList.getStore().getKey()
144+
: (newShoppingList.getStore() != null ? newShoppingList.getStore().getId() : null);
145+
146+
return buildUpdateAction(
147+
oldStoreKey,
148+
newStoreKey,
149+
() -> ShoppingListSetStoreActionBuilder.of().store(newShoppingList.getStore()).build());
150+
}
151+
119152
/**
120153
* Compares the anonymousIds of {@link ShoppingList} and a {@link ShoppingListDraft} and returns
121154
* an {@link ShoppingListUpdateAction} as a result in an {@link java.util.Optional}. If both the

0 commit comments

Comments
 (0)