use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class InstanceStorageAPI method postInstanceStorageInstances.
@Override
public void postInstanceStorageInstances(@DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, Instance 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));
vertxContext.runOnContext(v -> {
try {
if (entity.getId() == null) {
entity.setId(UUID.randomUUID().toString());
} else {
if (isUUID(entity.getId())) {
io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withPlainBadRequest("ID must be a UUID"));
}
}
postgresClient.save("instance", entity.getId(), entity, reply -> {
try {
if (reply.succeeded()) {
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withJsonCreated(reply.result(), stream)));
} else {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withPlainBadRequest(reply.cause().getMessage())));
}
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.PostInstanceStorageInstancesResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class InstanceTypeAPI method deleteInstanceTypesByInstanceTypeId.
@Validate
@Override
public void deleteInstanceTypesByInstanceTypeId(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(INSTANCE_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(DeleteInstanceTypesByInstanceTypeIdResponse.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(DeleteInstanceTypesByInstanceTypeIdResponse.withPlainNotFound(msg)));
return;
}
asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceTypesByInstanceTypeIdResponse.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 ItemStorageAPI method getItemStorageItemsByItemId.
@Validate
@Override
public void getItemStorageItemsByItemId(@PathParam("itemId") @NotNull String itemId, @QueryParam("lang") @DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, java.util.Map<String, String> okapiHeaders, io.vertx.core.Handler<io.vertx.core.AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
String tenantId = okapiHeaders.get(TENANT_HEADER);
try {
PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
String[] fieldList = { "*" };
String query = String.format("id==%s", itemId);
CQLWrapper cql = getCQL(query, 1, 0);
log.info(String.format("SQL generated from CQL: %s", cql.toString()));
vertxContext.runOnContext(v -> {
try {
postgresClient.get(getTableName(query), Item.class, fieldList, cql, true, false, reply -> {
try {
if (reply.succeeded()) {
List<Item> itemList = (List<Item>) reply.result().getResults();
if (itemList.size() == 1) {
Item item = itemList.get(0);
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withJsonOK(item)));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainNotFound("Not Found")));
}
} else {
Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(reply.cause().getMessage()));
}
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class ItemStorageAPI method getItemStorageItems.
@Validate
@Override
public void getItemStorageItems(int offset, int limit, String query, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
String tenantId = okapiHeaders.get(TENANT_HEADER);
try {
vertxContext.runOnContext(v -> {
try {
PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
String[] fieldList = { "*" };
CQLWrapper cql = getCQL(query, limit, offset);
log.info(String.format("SQL generated by CQL query (%s): %s", query, cql.toString()));
postgresClient.get(getTableName(query), Item.class, fieldList, cql, true, false, reply -> {
try {
if (reply.succeeded()) {
List<Item> items = (List<Item>) reply.result().getResults();
Items itemList = new Items();
itemList.setItems(items);
itemList.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withJsonOK(itemList)));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
} catch (Exception e) {
if (e.getCause() != null && e.getCause().getClass().getSimpleName().contains("CQLParseException")) {
asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainBadRequest("CQL Parsing Error for '" + query + "': " + e.getLocalizedMessage())));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError("Error")));
}
}
});
} catch (IllegalStateException e) {
asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainInternalServerError("CQL State Error for '" + query + "': " + e.getLocalizedMessage())));
} catch (Exception e) {
if (e.getCause() != null && e.getCause().getClass().getSimpleName().contains("CQLParseException")) {
asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainBadRequest("CQL Parsing Error for '" + query + "': " + e.getLocalizedMessage())));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError("Error")));
}
}
});
} catch (Exception e) {
if (e.getCause() != null && e.getCause().getClass().getSimpleName().contains("CQLParseException")) {
asyncResultHandler.handle(Future.succeededFuture(GetItemStorageItemsResponse.withPlainBadRequest("CQL Parsing Error for '" + query + "': " + e.getLocalizedMessage())));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.GetItemStorageItemsResponse.withPlainInternalServerError("Error")));
}
}
}
use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class ItemStorageAPI method putItemStorageItemsByItemId.
@Validate
@Override
public void putItemStorageItemsByItemId(@PathParam("itemId") @NotNull String itemId, @QueryParam("lang") @DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, Item entity, java.util.Map<String, String> okapiHeaders, io.vertx.core.Handler<io.vertx.core.AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
String tenantId = okapiHeaders.get(TENANT_HEADER);
try {
PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
vertxContext.runOnContext(v -> {
try {
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(PutItemStorageItemsByItemIdResponse.withPlainBadRequest(message)));
} else if (res == -1) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.PostItemStorageItemsResponse.withPlainInternalServerError("")));
} else {
try {
String[] fieldList = { "*" };
String query = String.format("id==%s", itemId);
CQLWrapper cql = getCQL(query, 1, 0);
log.info(String.format("SQL generated from CQL: %s", cql.toString()));
postgresClient.get(getTableName(query), Item.class, fieldList, cql, true, false, reply -> {
if (reply.succeeded()) {
List<Item> itemList = (List<Item>) reply.result().getResults();
if (itemList.size() == 1) {
try {
postgresClient.update("item", entity, entity.getId(), update -> {
try {
if (update.succeeded()) {
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withNoContent()));
} else {
String message = PgExceptionUtil.badRequestMessage(update.cause());
if (message != null) {
asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainBadRequest(message)));
} else {
asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainInternalServerError(update.cause().getMessage())));
}
}
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PostItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
}
} else {
try {
postgresClient.save("item", entity.getId(), entity, save -> {
try {
if (save.succeeded()) {
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withNoContent()));
} else {
asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainInternalServerError(save.cause().getMessage())));
}
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PostItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.withPlainInternalServerError(e.getMessage())));
}
}
} else {
asyncResultHandler.handle(Future.succeededFuture(PutItemStorageItemsByItemIdResponse.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())));
}
}
Aggregations