-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MODORDERS-344 poline update batch API
- Loading branch information
Showing
9 changed files
with
225 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#%RAML 1.0 | ||
title: "mod-orders" | ||
baseUri: http://github.com/folio-org/mod-orders-storage | ||
version: v9.3 | ||
|
||
documentation: | ||
- title: PO Line | ||
content: <b>This module implements the CRUD interface. This API is intended for internal use only. Please use the /orders/order-lines API provided by mod-orders instead.</b> | ||
|
||
types: | ||
po-line-collection: !include acq-models/mod-orders-storage/schemas/po_line_collection.json | ||
UUID: | ||
type: string | ||
pattern: ^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$ | ||
|
||
resourceTypes: | ||
collection: !include raml-util/rtypes/collection.raml | ||
collection-item: !include raml-util/rtypes/item-collection.raml | ||
traits: | ||
|
||
|
||
/orders-storage/po-lines-batch: | ||
displayName: Finance release encumbrance | ||
description: Finance release encumbrance APIs | ||
put: | ||
description: "asd" | ||
body: | ||
application/json: | ||
type: po-line-collection | ||
example: | ||
strict: false | ||
value: !include acq-models/mod-orders-storage/examples/po_line_collection.sample | ||
responses: | ||
204: | ||
description: "Item successfully updated" | ||
404: | ||
description: "Item with a given ID not found" | ||
body: | ||
text/plain: | ||
example: "Bad request" | ||
400: | ||
description: "Bad request, e.g. malformed request body or query parameter. Details of the error (e.g. name of the parameter or line/character number with malformed data) provided in the response." | ||
body: | ||
text/plain: | ||
example: "unable to update <<resourcePathName|!singularize>> -- malformed JSON at 13:4" | ||
500: | ||
description: "Internal server error, e.g. due to misconfiguration" | ||
body: | ||
text/plain: | ||
example: "internal server error, contact administrator" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.folio.rest.impl; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.ws.rs.core.Response; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.folio.dao.PostgresClientFactory; | ||
import org.folio.event.service.AuditOutboxService; | ||
import org.folio.rest.core.BaseApi; | ||
import org.folio.rest.jaxrs.model.PoLine; | ||
import org.folio.rest.jaxrs.model.PoLineCollection; | ||
import org.folio.rest.jaxrs.resource.OrdersStoragePoLinesBatch; | ||
import org.folio.rest.persist.HelperUtils; | ||
import org.folio.rest.persist.PostgresClient; | ||
import org.folio.services.lines.PoLinesBatchService; | ||
import org.folio.spring.SpringContextUtil; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Context; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.Vertx; | ||
|
||
public class PoLineBatchAPI extends BaseApi implements OrdersStoragePoLinesBatch { | ||
private static final Logger log = LogManager.getLogger(); | ||
private final PostgresClient pgClient; | ||
@Autowired | ||
PoLinesBatchService poLinesBatchService; | ||
@Autowired | ||
private PostgresClientFactory pgClientFactory; | ||
@Autowired | ||
private AuditOutboxService auditOutboxService; | ||
|
||
public PoLineBatchAPI(Vertx vertx, String tenantId) { | ||
SpringContextUtil.autowireDependencies(this, Vertx.currentContext()); | ||
pgClient = pgClientFactory.createInstance(tenantId); | ||
} | ||
|
||
@Override | ||
public void putOrdersStoragePoLinesBatch(PoLineCollection poLineCollection, Map<String, String> okapiHeaders, | ||
Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) { | ||
poLinesBatchService.poLinesBatchUpdate(poLineCollection.getPoLines(), pgClient, okapiHeaders, vertxContext) | ||
.onComplete(ar -> { | ||
if (ar.failed()) { | ||
log.error(getPoLineIdsForLogMessage(poLineCollection.getPoLines()), ar.cause()); | ||
asyncResultHandler.handle(buildErrorResponse(ar.cause())); | ||
} else { | ||
log.info(getPoLineIdsForLogMessage(poLineCollection.getPoLines())); | ||
auditOutboxService.processOutboxEventLogs(okapiHeaders); | ||
asyncResultHandler.handle(buildNoContentResponse()); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected String getEndpoint(Object entity) { | ||
return HelperUtils.getEndpoint(OrdersStoragePoLinesBatch.class); | ||
} | ||
|
||
private String getPoLineIdsForLogMessage(List<PoLine> polines) { | ||
return "putOrdersStoragePoLinesBatch completed, PO line ids: " + polines.stream() | ||
.map(PoLine::getId) | ||
.collect(Collectors.joining(", ")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/org/folio/services/lines/PoLinesBatchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.folio.services.lines; | ||
|
||
import static org.folio.models.TableNames.PO_LINE_TABLE; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.folio.event.service.AuditOutboxService; | ||
import org.folio.okapi.common.GenericCompositeFuture; | ||
import org.folio.rest.core.models.RequestContext; | ||
import org.folio.rest.jaxrs.model.OrderLineAuditEvent; | ||
import org.folio.rest.jaxrs.model.PoLine; | ||
import org.folio.rest.persist.Conn; | ||
import org.folio.rest.persist.PostgresClient; | ||
|
||
import io.vertx.core.CompositeFuture; | ||
import io.vertx.core.Context; | ||
import io.vertx.core.Future; | ||
|
||
public class PoLinesBatchService { | ||
private static final Logger log = LogManager.getLogger(); | ||
private final AuditOutboxService auditOutboxService; | ||
private final PoLinesService poLinesService; | ||
|
||
|
||
public PoLinesBatchService(AuditOutboxService auditOutboxService, PoLinesService poLinesService) { | ||
this.auditOutboxService = auditOutboxService; | ||
this.poLinesService = poLinesService; | ||
} | ||
|
||
public Future<Void> poLinesBatchUpdate(List<PoLine> poLines, PostgresClient pgClient, Map<String, String> okapiHeaders, | ||
Context vertxContext) { | ||
|
||
if (CollectionUtils.isEmpty(poLines)) { | ||
log.warn("poLinesBatchUpdate:: po line list is empty"); | ||
return Future.succeededFuture(); | ||
} | ||
|
||
return pgClient.withTrans(conn -> | ||
conn.updateBatch(PO_LINE_TABLE, poLines) | ||
.compose(rowSet -> updatePoLinesWithTitle(conn, poLines, okapiHeaders, vertxContext)) | ||
.compose(rowSet -> auditOutboxService.saveOrderLinesOutboxLogs(conn, poLines, OrderLineAuditEvent.Action.EDIT, okapiHeaders)) | ||
.mapEmpty() | ||
); | ||
|
||
} | ||
|
||
private CompositeFuture updatePoLinesWithTitle(Conn conn, List<PoLine> poLines, Map<String, String> okapiHeaders, Context vertxContext) { | ||
var futures = poLines.stream() | ||
.filter(poLine -> !poLine.getIsPackage()) | ||
.map(poLine -> poLinesService.updatePoLineWithTitle(conn, poLine.getId(), poLine, new RequestContext(vertxContext, okapiHeaders))) | ||
.toList(); | ||
return GenericCompositeFuture.join(futures); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters