use of org.folio.rest.tools.utils.OutStream in project mod-inventory-storage by folio-org.
the class InstanceStorageAPI method putInstanceStorageInstancesByInstanceId.
@Override
public void putInstanceStorageInstancesByInstanceId(@NotNull String instanceId, @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 {
String[] fieldList = { "*" };
CQLWrapper cql = handleCQL(String.format("id==%s", instanceId), 1, 0);
postgresClient.get(tableName, Instance.class, fieldList, cql, true, false, reply -> {
if (reply.succeeded()) {
List<Instance> instancesList = (List<Instance>) reply.result().getResults();
if (instancesList.size() == 1) {
try {
postgresClient.update(tableName, entity, entity.getId(), update -> {
try {
if (update.succeeded()) {
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withNoContent()));
} else {
asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(update.cause().getMessage())));
}
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
}
} else {
try {
postgresClient.save(tableName, entity.getId(), entity, save -> {
try {
if (save.succeeded()) {
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withNoContent()));
} else {
asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(save.cause().getMessage())));
}
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
}
}
} else {
asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.tools.utils.OutStream in project mod-inventory-storage by folio-org.
the class InstanceTypeAPI method postInstanceTypes.
@Validate
@Override
public void postInstanceTypes(String lang, InstanceType 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_TYPE_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(PostInstanceTypesResponse.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(PostInstanceTypesResponse.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 MaterialTypeAPI method postMaterialTypes.
@Validate
@Override
public void postMaterialTypes(String lang, Mtype entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
vertxContext.runOnContext(v -> {
try {
String id = UUID.randomUUID().toString();
if (entity.getId() == null) {
entity.setId(id);
} else {
id = entity.getId();
}
String tenantId = TenantTool.calculateTenantId(okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT));
PostgresClient.getInstance(vertxContext.owner(), tenantId).save(MATERIAL_TYPE_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(PostMaterialTypesResponse.withJsonCreated(LOCATION_PREFIX + ret, stream)));
} else {
log.error(reply.cause().getMessage(), reply.cause());
if (isDuplicate(reply.cause().getMessage())) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PostMaterialTypesResponse.withJsonUnprocessableEntity(org.folio.rest.tools.utils.ValidationHelper.createValidationErrorMessage("name", entity.getName(), "Material Type exists"))));
} else {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PostMaterialTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PostMaterialTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
});
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PostMaterialTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
});
}
use of org.folio.rest.tools.utils.OutStream in project mod-inventory-storage by folio-org.
the class LoanTypeAPI method postLoanTypes.
@Validate
@Override
public void postLoanTypes(String lang, Loantype 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);
}
getPostgresClient(vertxContext, okapiHeaders).save(LOAN_TYPE_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(PostLoanTypesResponse.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(PostLoanTypesResponse.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 LocationUnitAPI method postLocationUnitsLibraries.
@Override
public void postLocationUnitsLibraries(String lang, Loclib 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(LIBRARY_TABLE, id, entity, reply -> {
if (reply.failed()) {
String message = logAndSaveError(reply.cause());
if (isDuplicate(message)) {
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PostLocationUnitsLibrariesResponse.withJsonUnprocessableEntity(ValidationHelper.createValidationErrorMessage("loclib", entity.getId(), "Library already exists"))));
} else {
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PostLocationUnitsLibrariesResponse.withPlainInternalServerError(message)));
}
} else {
Object responseObject = reply.result();
entity.setId((String) responseObject);
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.PostLocationUnitsLibrariesResponse.withJsonCreated(URL_PREFIX + responseObject, stream)));
}
});
}
Aggregations