use of org.folio.models.PieceItemPair in project mod-orders by folio-org.
the class InventoryManager method handleItemRecords.
/**
* Handles Inventory items for passed list of locations. Items are either retrieved from Inventory or new ones are created
* if no corresponding item records exist yet.
* Returns list of {@link Piece} records with populated item id (and other info) corresponding to given PO line.
*
* @param compPOL PO line to retrieve/create Item Records for
* @param holder pair of new location provided from POl and location from storage
* @return future with list of piece objects
*/
public CompletableFuture<List<Piece>> handleItemRecords(CompositePoLine compPOL, PoLineUpdateHolder holder, RequestContext requestContext) {
List<Location> polLocations = compPOL.getLocations().stream().filter(location -> location.getLocationId().equals(holder.getNewLocationId())).collect(toList());
Map<Piece.Format, Integer> piecesWithItemsQuantities = HelperUtils.calculatePiecesWithItemIdQuantity(compPOL, polLocations);
int piecesWithItemsQty = IntStreamEx.of(piecesWithItemsQuantities.values()).sum();
String polId = compPOL.getId();
logger.debug("Handling {} items for PO Line with id={} and holdings with id={}", piecesWithItemsQty, polId, holder.getOldHoldingId());
if (piecesWithItemsQty == 0) {
return completedFuture(Collections.emptyList());
}
return pieceStorageService.getExpectedPiecesByLineId(compPOL.getId(), requestContext).thenApply(existingPieces -> {
List<Piece> needUpdatePieces = new ArrayList<>();
List<Piece> pieces = existingPieces.getPieces().stream().filter(piece -> piece.getLocationId().equals(holder.getOldLocationId())).map(piece -> piece.withLocationId(holder.getNewLocationId())).collect(toList());
if (!pieces.isEmpty()) {
needUpdatePieces.addAll(pieces);
}
return needUpdatePieces;
}).thenCompose(needUpdatePieces -> {
if (!needUpdatePieces.isEmpty()) {
return getItemRecordsByIds(needUpdatePieces.stream().map(Piece::getItemId).collect(toList()), requestContext).thenApply(items -> buildPieceItemPairList(needUpdatePieces, items));
}
return completedFuture(Collections.<PieceItemPair>emptyList());
}).thenCompose(pieceItemPairs -> {
List<CompletableFuture<String>> updatedItemIds = new ArrayList<>(pieceItemPairs.size());
pieceItemPairs.forEach(pair -> {
JsonObject item = pair.getItem();
if (isLocationContainsItemLocation(polLocations, item)) {
item.put(ITEM_HOLDINGS_RECORD_ID, holder.getNewHoldingId());
updatedItemIds.add(saveItem(item, requestContext));
}
});
// Wait for all items to be created and corresponding updatedItemIds are built
return collectResultsOnSuccess(updatedItemIds).thenApply(results -> {
validateItemsCreation(compPOL.getId(), updatedItemIds.size(), results.size());
return pieceItemPairs.stream().map(PieceItemPair::getPiece).collect(toList());
});
});
}
Aggregations