Skip to content

Commit

Permalink
[MODORDERS-1149] Async fix (#1004)
Browse files Browse the repository at this point in the history
* MODORDERS-1163. Async fix
  • Loading branch information
SerhiiNosko authored Aug 23, 2024
1 parent 4f10a04 commit a63deea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
16 changes: 16 additions & 0 deletions src/main/java/org/folio/orders/utils/HelperUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletionException;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -331,6 +332,21 @@ public static <T> Future<List<T>> collectResultsOnSuccess(Collection<Future<T>>
.map(CompositeFuture::list);
}

/**
* The method allows to compose any elements with the same action in sequence.
*
* @param list elements to be executed in sequence
* @param method action that will be executed sequentially based on the number of list items
* @return the last composed element(Feature result)
*/
public static <T, R> Future<R> chainCall(List<T> list, Function<T, Future<R>> method) {
Future<R> f = Future.succeededFuture();
for (T item : list) {
f = f.compose(r -> method.apply(item));
}
return f;
}

/**
* Transform list of id's to CQL query using 'or' operation
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Map;

import static org.folio.orders.utils.HelperUtils.chainCall;
import static org.folio.orders.utils.HelperUtils.extractCreatedDate;
import static org.folio.orders.utils.HelperUtils.extractId;
import static org.folio.service.inventory.InventoryUtils.INVENTORY_LOOKUP_ENDPOINTS;
Expand Down Expand Up @@ -63,19 +64,11 @@ public Future<Void> transferItemRequests(List<String> originItemIds, String dest
}

// Move requests to new item sequentially and then cancel unnecessary ones
return transferRequests(requestsToTransfer, destinationItemId, requestContext)
return chainCall(requestsToTransfer, (request) -> transferRequest(request, destinationItemId, requestContext))
.compose(v -> cancelRequests(requestsToCancel, requestContext));
});
}

private Future<Void> transferRequests(List<JsonObject> requests, String destinationItemId, RequestContext requestContext) {
return requests.stream().reduce(
Future.succeededFuture(),
(transfers, request) -> transfers.compose(v -> transferRequest(request, destinationItemId, requestContext)),
(transfers1, transfers2) -> transfers1.compose(v -> transfers2)
);
}

private Future<Void> transferRequest(JsonObject request, String itemId, RequestContext requestContext) {
String reqId = extractId(request);
JsonObject jsonObject = JsonObject.of(DESTINATION_ITEM_ID.getValue(), itemId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;
import static org.folio.orders.utils.HelperUtils.chainCall;
import static org.folio.orders.utils.HelperUtils.collectResultsOnSuccess;
import static org.folio.service.inventory.InventoryItemManager.ID;
import static org.folio.service.inventory.InventoryItemManager.ITEM_HOLDINGS_RECORD_ID;

Expand Down Expand Up @@ -194,12 +196,8 @@ private Future<Void> processLocations(OrderLineUpdateInstanceHolder holder,
Function<Location, Future<Void>> processFunction) {
return retrieveUniqueLocations(holder.getStoragePoLine(), requestContext)
.compose(tenantIdToLocationsMap ->
GenericCompositeFuture.all(tenantIdToLocationsMap.values().stream()
.map(locations ->
GenericCompositeFuture.join(locations.stream()
.map(processFunction)
.toList())
)
collectResultsOnSuccess(tenantIdToLocationsMap.values().stream()
.map(locations -> chainCall(locations, processFunction))
.collect(toList()))
)
.mapEmpty();
Expand Down

0 comments on commit a63deea

Please sign in to comment.