use of org.folio.rest.tools.utils.OutStream in project mod-inventory-storage by folio-org.
the class LocationUnitAPI method postLocationUnitsInstitutions.
@Override
public void postLocationUnitsInstitutions(String lang, Locinst entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
String tenantId = getTenant(okapiHeaders);
String id = entity.getId();
if (id == null) {
id = UUID.randomUUID().toString();
entity.setId(id);
}
PostgresClient.getInstance(vertxContext.owner(), tenantId).save(INSTITUTION_TABLE, id, entity, reply -> {
if (reply.failed()) {
String message = logAndSaveError(reply.cause());
if (isDuplicate(message)) {
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PostLocationUnitsInstitutionsResponse.withJsonUnprocessableEntity(ValidationHelper.createValidationErrorMessage("locinst", entity.getId(), "Institution already exists"))));
} else {
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PostLocationUnitsInstitutionsResponse.withPlainInternalServerError(message)));
}
} else {
Object responseObject = reply.result();
entity.setId((String) responseObject);
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PostLocationUnitsInstitutionsResponse.withJsonCreated(URL_PREFIX + responseObject, stream)));
}
});
}
use of org.folio.rest.tools.utils.OutStream in project mod-inventory-storage by folio-org.
the class HoldingsStorageAPI method postHoldingsStorageHoldings.
@Override
public void postHoldingsStorageHoldings(@DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, HoldingsRecord 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(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withPlainBadRequest("ID must be a UUID"));
}
}
postgresClient.save(HOLDINGS_RECORD_TABLE, entity.getId(), entity, reply -> {
try {
if (reply.succeeded()) {
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withJsonCreated(reply.result(), stream)));
} else {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withPlainBadRequest(reply.cause().getMessage())));
}
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
e.printStackTrace();
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(HoldingsStorageResource.PostHoldingsStorageHoldingsResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.tools.utils.OutStream in project mod-inventory-storage by folio-org.
the class InstanceFormatAPI method postInstanceFormats.
@Validate
@Override
public void postInstanceFormats(String lang, InstanceFormat entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
vertxContext.runOnContext(v -> {
try {
String id = entity.getId();
if (id == null) {
id = UUID.randomUUID().toString();
entity.setId(id);
}
String tenantId = TenantTool.tenantId(okapiHeaders);
PostgresClient.getInstance(vertxContext.owner(), tenantId).save(INSTANCE_FORMAT_TABLE, id, entity, reply -> {
try {
if (reply.succeeded()) {
Object ret = reply.result();
entity.setId((String) ret);
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PostInstanceFormatsResponse.withJsonCreated(LOCATION_PREFIX + ret, stream)));
} else {
String msg = PgExceptionUtil.badRequestMessage(reply.cause());
if (msg == null) {
internalServerErrorDuringPost(reply.cause(), lang, asyncResultHandler);
return;
}
log.info(msg);
asyncResultHandler.handle(Future.succeededFuture(PostInstanceFormatsResponse.withPlainBadRequest(msg)));
}
} catch (Exception e) {
internalServerErrorDuringPost(e, lang, asyncResultHandler);
}
});
} catch (Exception e) {
internalServerErrorDuringPost(e, lang, asyncResultHandler);
}
});
}
use of org.folio.rest.tools.utils.OutStream 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.tools.utils.OutStream 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