Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/folio/rest/core/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ public <T> Future<Void> put(String endpoint, T dataObject, RequestContext reques
}

public <T> Future<Void> patch(RequestEntry requestEntry, T dataObject, RequestContext requestContext) {
String endpoint = requestEntry.buildEndpoint();
var endpoint = requestEntry.buildEndpoint();
return patch(endpoint, dataObject, requestContext);
}

public <T> Future<Void> patch(String endpoint, T dataObject, RequestContext requestContext) {
var caseInsensitiveHeader = convertToCaseInsensitiveMap(requestContext.getHeaders());
Promise<Void> promise = Promise.promise();
var promise = Promise.<Void>promise();
return getVertxWebClient(requestContext.getContext())
.patchAbs(buildAbsEndpoint(caseInsensitiveHeader, endpoint))
.putHeaders(caseInsensitiveHeader)
Expand Down
25 changes: 12 additions & 13 deletions src/main/java/org/folio/rest/impl/PoLineAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.vertx.core.Vertx;

public class PoLineAPI extends BaseApi implements OrdersOrderLines {

private static final Logger logger = LogManager.getLogger();

@Autowired
Expand All @@ -58,7 +59,7 @@ public PoLineAPI() {
@Override
@Validate
public void getOrdersOrderLines(String totalRecords, int offset, int limit, String query, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
helper.getOrderLines(query, offset, limit, new RequestContext(vertxContext, okapiHeaders))
.onSuccess(lines -> asyncResultHandler.handle(succeededFuture(buildOkResponse(lines))))
.onFailure(t -> handleErrorResponse(asyncResultHandler, t));
Expand All @@ -67,7 +68,7 @@ public void getOrdersOrderLines(String totalRecords, int offset, int limit, Stri
@Override
@Validate
public void postOrdersOrderLines(PoLine poLine, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
RequestContext requestContext = new RequestContext(vertxContext, okapiHeaders);
commonSettingsCache.loadSettings(requestContext)
.compose(tenantConfig -> helper.createPoLine(poLine, tenantConfig, requestContext))
Expand All @@ -82,8 +83,8 @@ public void postOrdersOrderLines(PoLine poLine, Map<String, String> okapiHeaders
@Override
@Validate
public void getOrdersOrderLinesById(String lineId, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
logger.debug("Started Invocation of POLine Request with id = {}", lineId);
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
logger.debug("getOrdersOrderLinesById:: Handling GET Order Line operation with id={}", lineId);
helper.getPoLine(lineId, new RequestContext(vertxContext, okapiHeaders))
.onSuccess(poLine -> asyncResultHandler.handle(succeededFuture(buildOkResponse(poLine))))
.onFailure(t -> handleErrorResponse(asyncResultHandler, t));
Expand All @@ -92,7 +93,7 @@ public void getOrdersOrderLinesById(String lineId, Map<String, String> okapiHead
@Override
@Validate
public void deleteOrdersOrderLinesById(String lineId, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
helper.deleteLine(lineId, new RequestContext(vertxContext, okapiHeaders))
.onSuccess(v -> asyncResultHandler.handle(succeededFuture(buildNoContentResponse())))
.onFailure(t -> handleErrorResponse(asyncResultHandler, t));
Expand All @@ -101,8 +102,8 @@ public void deleteOrdersOrderLinesById(String lineId, Map<String, String> okapiH
@Override
@Validate
public void putOrdersOrderLinesById(String lineId, PoLine poLine, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
logger.debug("Handling PUT Order Line operation...");
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
logger.debug("putOrdersOrderLinesById:: Handling PUT Order Line operation...");
// Set id if this is available only in path
RequestContext requestContext = new RequestContext(vertxContext, okapiHeaders);
if (StringUtils.isEmpty(poLine.getId())) {
Expand Down Expand Up @@ -135,14 +136,12 @@ public void putOrdersOrderLinesById(String lineId, PoLine poLine, Map<String, St

@Override
@Validate
public void patchOrdersOrderLinesById(String lineId, PatchOrderLineRequest request,
Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler,
Context vertxContext) {
public void patchOrdersOrderLinesById(String lineId, PatchOrderLineRequest request, Map<String, String> okapiHeaders,
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
RequestContext requestContext = new RequestContext(vertxContext, okapiHeaders);

orderLinePatchOperationService.patch(lineId, request, requestContext)
.onSuccess(v -> asyncResultHandler.handle(succeededFuture(buildNoContentResponse())))
.onFailure(t -> handleErrorResponse(asyncResultHandler, t));
.onSuccess(v -> asyncResultHandler.handle(succeededFuture(buildNoContentResponse())))
.onFailure(t -> handleErrorResponse(asyncResultHandler, t));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class InventoryItemManager {

public static final String BARCODE_ALREADY_EXIST_ERROR = "lower(jsonb ->> 'barcode'::text) value already exists in table item";
private static final String LOOKUP_ITEM_QUERY = "purchaseOrderLineIdentifier==%s and holdingsRecordId==%s";
private static final String ITEM_STOR_ENDPOINT = "/item-storage/items";
private static final String ITEM_STORAGE_ENDPOINT = "/item-storage/items";
private static final String BUILDING_PIECE_MESSAGE = "Building {} {} piece(s) for PO Line with id={}";

private final RestClient restClient;
Expand Down Expand Up @@ -109,7 +109,7 @@ public Future<List<JsonObject>> getItemsByHoldingIdAndOrderLineId(String holding
}

public Future<List<JsonObject>> getItemsByPoLineIdsAndStatus(List<String> poLineIds, String itemStatus, RequestContext requestContext) {
logger.debug("getItemsByStatus start");
logger.debug("getItemsByPoLineIdsAndStatus:: Started");
List<Future<List<JsonObject>>> futures = StreamEx
.ofSubLists(poLineIds, MAX_IDS_FOR_GET_RQ_15)
.map(ids -> {
Expand Down Expand Up @@ -235,7 +235,7 @@ public Future<List<Piece>> handleItemRecords(CompositePurchaseOrder comPO, PoLin
private Future<List<JsonObject>> searchStorageExistingItems(String poLineId, String holdingId, int expectedQuantity,
RequestContext requestContext) {
String query = String.format(LOOKUP_ITEM_QUERY, poLineId, holdingId);
RequestEntry requestEntry = new RequestEntry(ITEM_STOR_ENDPOINT).withQuery(query).withOffset(0).withLimit(expectedQuantity);
RequestEntry requestEntry = new RequestEntry(ITEM_STORAGE_ENDPOINT).withQuery(query).withOffset(0).withLimit(expectedQuantity);
return restClient.getAsJsonObject(requestEntry, requestContext)
.map(itemsCollection -> {
List<JsonObject> items = extractEntities(itemsCollection);
Expand Down Expand Up @@ -318,11 +318,11 @@ public Future<String> openOrderCreateItemRecord(CompositePurchaseOrder compPO, P
Piece pieceWithHoldingId = new Piece().withHoldingId(holdingId);
if (poLine.getOrderFormat() == ELECTRONIC_RESOURCE) {
createMissingElectronicItems(compPO, poLine, pieceWithHoldingId, ITEM_QUANTITY, requestContext)
.onSuccess(idS -> itemFuture.complete(idS.get(0)))
.onSuccess(idS -> itemFuture.complete(idS.getFirst()))
.onFailure(itemFuture::fail);
} else {
createMissingPhysicalItems(compPO, poLine, pieceWithHoldingId, ITEM_QUANTITY, requestContext)
.onSuccess(idS -> itemFuture.complete(idS.get(0)))
.onSuccess(idS -> itemFuture.complete(idS.getFirst()))
.onFailure(itemFuture::fail);
}
} catch (Exception e) {
Expand Down Expand Up @@ -436,9 +436,9 @@ private Future<List<String>> createItemRecords(JsonObject itemRecord, int expect

private Future<String> createItemInInventory(JsonObject itemData, RequestContext requestContext) {
Promise<String> promise = Promise.promise();
RequestEntry requestEntry = new RequestEntry(ITEM_STOR_ENDPOINT);
RequestEntry requestEntry = new RequestEntry(ITEM_STORAGE_ENDPOINT);
String tenantId = TenantTool.tenantId(requestContext.getHeaders());
logger.info("Trying to create Item in inventory in tenant: {}", tenantId);
logger.info("createItemInInventory:: Trying to create Item in inventory in tenant: {}", tenantId);
restClient.postJsonObjectAndGetId(requestEntry, itemData, requestContext)
.onSuccess(promise::complete)
// In case item creation failed, return null instead of id
Expand All @@ -463,4 +463,13 @@ private List<JsonObject> extractEntities(JsonObject entries) {
.orElseGet(List::of);
}

public Future<Void> batchUpdatePartialItems(List<JsonObject> items, RequestContext requestContext) {
logger.info("batchUpdatePartialItems:: Batch updating partial items={}", items.size());
var requestEntry = new RequestEntry(ITEM_STORAGE_ENDPOINT);
var tenantId = TenantTool.tenantId(requestContext.getHeaders());
logger.info("batchUpdatePartialItems:: Trying to batch update partial items in inventory for tenant: {}", tenantId);
var payload = new JsonObject()
.put(InventoryUtils.ITEMS, items);
return restClient.patch(requestEntry, payload, requestContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public OrderLinePatchOperationService(RestClient restClient,
}

public Future<Void> patch(String lineId, PatchOrderLineRequest request, RequestContext requestContext) {
log.info("patch:: start patching operation: {} for poLineId: {}", request.getOperation(), lineId);
log.info("patch:: Start patching operation: {} for poLineId: {}", request.getOperation(), lineId);
var newInstanceId = request.getReplaceInstanceRef().getNewInstanceId();
return inventoryInstanceManager.createShadowInstanceIfNeeded(newInstanceId, requestContext)
.compose(v -> purchaseOrderLineService.getOrderLineById(lineId, requestContext))
.compose(poLine -> patchOrderLine(request, poLine, requestContext))
.onSuccess(v -> log.info("patch:: successfully patched operation: {} for poLineId: {}", request.getOperation(), lineId))
.onSuccess(v -> log.info("patch:: Successfully patched operation: {} for poLineId: {}", request.getOperation(), lineId))
.onFailure(e -> log.error("Failed to patch operation: {} for poLineId: {}", request.getOperation(), lineId, e))
.mapEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.folio.rest.jaxrs.model.CreateInventoryType;

public class OrderLineUpdateInstanceStrategyResolver {

private final Map<CreateInventoryType, OrderLineUpdateInstanceStrategy> strategies;

public OrderLineUpdateInstanceStrategyResolver(Map<CreateInventoryType, OrderLineUpdateInstanceStrategy> strategies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public class WithHoldingOrderLineUpdateInstanceStrategy extends BaseOrderLineUpd

private static final String HOLDINGS_ITEMS = "holdingsItems";
private static final String BARE_HOLDINGS_ITEMS = "bareHoldingsItems";
private static final String VERSION = "_version";
private static final String ITEM_ID = "itemId";
private static final String TENANT_ID = "tenantId";
private static final String ORIGINAL_ERROR = "originalError";

private final PieceStorageService pieceStorageService;
private final PurchaseOrderLineService purchaseOrderLineService;

Expand Down Expand Up @@ -234,24 +239,27 @@ private Future<Void> updateItemsHolding(String holdingId, String newHoldingId, S
}

private Future<Void> updateItemsInInventory(List<JsonObject> items, String newHoldingId, RequestContext requestContext) {
items.forEach(item -> item.put(ITEM_HOLDINGS_RECORD_ID, newHoldingId));
List<Parameter> parameters = new ArrayList<>();
return Future.join(
items
.stream()
.map(item -> inventoryItemManager.updateItem(item, requestContext)
.otherwise(ex -> {
var itemIdParam = new Parameter().withKey("itemId").withValue(item.getString(ID));
if (ex.getCause() instanceof HttpException httpException) {
itemIdParam.withAdditionalProperty("originalError", httpException.getError().getMessage());
}
var tenantIdParam = new Parameter().withKey("tenantId").withValue(TenantTool.tenantId(requestContext.getHeaders()));
parameters.add(itemIdParam);
parameters.add(tenantIdParam);
return null;
}))
.toList())
.mapEmpty()
log.info("updateItemsInInventory:: Updating items in batch, items={}, new holding id={}", items.size(), newHoldingId);
var partialItems = items.stream()
.map(item -> new JsonObject()
.put(ID, item.getString(ID))
.put(ITEM_HOLDINGS_RECORD_ID, newHoldingId)
.put(VERSION, item.getString(VERSION)))
.toList();
var parameters = new ArrayList<Parameter>();
return inventoryItemManager.batchUpdatePartialItems(partialItems, requestContext)
.otherwise(ex -> {
items.forEach(item -> {
var itemIdParam = new Parameter().withKey(ITEM_ID).withValue(item.getString(ID));
if (ex.getCause() instanceof HttpException httpException) {
itemIdParam.withAdditionalProperty(ORIGINAL_ERROR, httpException.getError().getMessage());
}
var tenantIdParam = new Parameter().withKey(TENANT_ID).withValue(TenantTool.tenantId(requestContext.getHeaders()));
parameters.add(itemIdParam);
parameters.add(tenantIdParam);
});
return null;
})
.compose(v -> CollectionUtils.isNotEmpty(parameters)
? Future.failedFuture(new HttpException(500, ErrorCodes.ITEM_UPDATE_FAILED.toError().withParameters(parameters)))
: Future.succeededFuture());
Expand Down Expand Up @@ -286,5 +294,4 @@ private Future<List<Location>> getDeletableHoldings(OrderLineUpdateInstanceHolde
.filter(location -> !usedHoldingIds.contains(location.getHoldingId()))
.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ Future<Void> processHoldings(OrderLineUpdateInstanceHolder holder, RequestContex
? Future.succeededFuture()
: deleteAbandonedHoldings(holder.getStoragePoLine().getLocations(), requestContext).mapEmpty();
}

}
Loading