use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class ItemStorageAPI method postItemStorageItems.
@Validate
@Override
public void postItemStorageItems(@DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, Item entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
String tenantId = okapiHeaders.get(TENANT_HEADER);
try {
PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
if (entity.getId() == null) {
entity.setId(UUID.randomUUID().toString());
}
vertxContext.runOnContext(v -> {
try {
/**
*This should be replaced with a foreign key / cache since a lookup into the MT table
* every time an item is inserted is wasteful and slows down the insert process
*/
getMaterialType(vertxContext.owner(), tenantId, entity, replyHandler -> {
int res = replyHandler.result();
if (res == 0) {
String message = "Can not add " + entity.getMaterialTypeId() + ". Material type not found";
log.error(message);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainBadRequest(message)));
return;
} else if (res == -1) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError("")));
return;
} else {
Future<Location> temporaryLocationFuture;
if (entity.getTemporaryLocationId() != null) {
temporaryLocationFuture = getShelfLocation(vertxContext.owner(), tenantId, entity.getTemporaryLocationId());
} else {
temporaryLocationFuture = Future.succeededFuture();
}
temporaryLocationFuture.setHandler(compRes -> {
if (compRes.failed()) {
String message = "Attempting to specify non-existent location";
log.error(message);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainBadRequest(message)));
} else {
try {
postgresClient.save("item", entity.getId(), entity, reply -> {
try {
if (reply.succeeded()) {
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withJsonCreated(reply.result(), stream)));
} else {
String message = PgExceptionUtil.badRequestMessage(reply.cause());
if (message != null) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainBadRequest(message)));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
}
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
}
}
});
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class MaterialTypeAPI method deleteMaterialTypes.
@Override
public void deleteMaterialTypes(String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
String tenantId = TenantTool.tenantId(okapiHeaders);
try {
vertxContext.runOnContext(v -> {
PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
postgresClient.mutate(String.format("DELETE FROM %s_%s.%s", tenantId, "mod_inventory_storage", MATERIAL_TYPE_TABLE), reply -> {
if (reply.succeeded()) {
asyncResultHandler.handle(Future.succeededFuture(MaterialTypesResource.DeleteMaterialTypesResponse.noContent().build()));
} else {
asyncResultHandler.handle(Future.succeededFuture(MaterialTypesResource.DeleteMaterialTypesResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
});
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(MaterialTypesResource.DeleteMaterialTypesResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class ClassificationTypeAPI method deleteClassificationTypesByClassificationTypeId.
@Validate
@Override
public void deleteClassificationTypesByClassificationTypeId(String instanceTypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
vertxContext.runOnContext(v -> {
try {
String tenantId = TenantTool.tenantId(okapiHeaders);
PostgresClient postgres = PostgresClient.getInstance(vertxContext.owner(), tenantId);
postgres.delete(CLASSIFICATION_TYPE_TABLE, instanceTypeId, reply -> {
try {
if (reply.failed()) {
String msg = PgExceptionUtil.badRequestMessage(reply.cause());
if (msg == null) {
internalServerErrorDuringDelete(reply.cause(), lang, asyncResultHandler);
return;
}
log.info(msg);
asyncResultHandler.handle(Future.succeededFuture(DeleteClassificationTypesByClassificationTypeIdResponse.withPlainBadRequest(msg)));
return;
}
int updated = reply.result().getUpdated();
if (updated != 1) {
String msg = messages.getMessage(lang, MessageConsts.DeletedCountError, 1, updated);
log.error(msg);
asyncResultHandler.handle(Future.succeededFuture(DeleteClassificationTypesByClassificationTypeIdResponse.withPlainNotFound(msg)));
return;
}
asyncResultHandler.handle(Future.succeededFuture(DeleteClassificationTypesByClassificationTypeIdResponse.withNoContent()));
} catch (Exception e) {
internalServerErrorDuringDelete(e, lang, asyncResultHandler);
}
});
} catch (Exception e) {
internalServerErrorDuringDelete(e, lang, asyncResultHandler);
}
});
}
use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class ContributorTypeAPI method deleteContributorTypesByContributorTypeId.
@Validate
@Override
public void deleteContributorTypesByContributorTypeId(String contributorTypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
vertxContext.runOnContext(v -> {
try {
String tenantId = TenantTool.tenantId(okapiHeaders);
PostgresClient postgres = PostgresClient.getInstance(vertxContext.owner(), tenantId);
postgres.delete(CONTRIBUTOR_TYPE_TABLE, contributorTypeId, reply -> {
try {
if (reply.failed()) {
String msg = PgExceptionUtil.badRequestMessage(reply.cause());
if (msg == null) {
internalServerErrorDuringDelete(reply.cause(), lang, asyncResultHandler);
return;
}
log.info(msg);
asyncResultHandler.handle(Future.succeededFuture(DeleteContributorTypesByContributorTypeIdResponse.withPlainBadRequest(msg)));
return;
}
int updated = reply.result().getUpdated();
if (updated != 1) {
String msg = messages.getMessage(lang, MessageConsts.DeletedCountError, 1, updated);
log.error(msg);
asyncResultHandler.handle(Future.succeededFuture(DeleteContributorTypesByContributorTypeIdResponse.withPlainNotFound(msg)));
return;
}
asyncResultHandler.handle(Future.succeededFuture(DeleteContributorTypesByContributorTypeIdResponse.withNoContent()));
} catch (Exception e) {
internalServerErrorDuringDelete(e, lang, asyncResultHandler);
}
});
} catch (Exception e) {
internalServerErrorDuringDelete(e, lang, asyncResultHandler);
}
});
}
use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class StorageTestSuite method getRecordsWithUnmatchedIds.
private static ResultSet getRecordsWithUnmatchedIds(String tenantId, String tableName) throws InterruptedException, ExecutionException, TimeoutException {
PostgresClient dbClient = PostgresClient.getInstance(getVertx(), tenantId);
CompletableFuture<ResultSet> selectCompleted = new CompletableFuture<>();
String sql = String.format("SELECT null FROM %s_%s.%s" + " WHERE CAST(_id AS VARCHAR(50)) != jsonb->>'id'", tenantId, "mod_inventory_storage", tableName);
dbClient.select(sql, result -> {
if (result.succeeded()) {
selectCompleted.complete(result.result());
} else {
selectCompleted.completeExceptionally(result.cause());
}
});
return selectCompleted.get(5, TimeUnit.SECONDS);
}
Aggregations