use of org.folio.rest.jaxrs.resource.Tenant in project raml-module-builder by folio-org.
the class TenantAPI method postTenant.
private void postTenant(boolean async, TenantAttributes tenantAttributes, Map<String, String> headers, Handler<AsyncResult<Response>> handler, Context context) {
String tenantId = TenantTool.tenantId(headers);
String id = UUID.randomUUID().toString();
TenantJob job = new TenantJob();
job.setId(id);
job.setTenant(tenantId);
job.setTenantAttributes(tenantAttributes);
job.setComplete(false);
String location = "/_/tenant/" + id;
tenantExists(context, tenantId).compose(exists -> sqlFile(context, tenantId, tenantAttributes, exists)).onFailure(cause -> {
log.error(cause.getMessage(), cause);
handler.handle(Future.succeededFuture(PostTenantResponse.respond400WithTextPlain(cause.getMessage())));
}).onSuccess(files -> postgresClient(context).runSQLFile(files[0], true).compose(res -> {
if (!res.isEmpty()) {
return Future.failedFuture(res.get(0));
}
if (files.length == 1) {
// not saving job for disable or purge
return Future.succeededFuture();
}
return saveJob(job, tenantId, id, context);
}).onFailure(cause -> {
log.error(cause.getMessage(), cause);
handler.handle(Future.succeededFuture(PostTenantResponse.respond400WithTextPlain(cause.getMessage())));
}).onSuccess(result -> {
if (files.length == 1) {
// disable or purge?
PostgresClient.closeAllClients(tenantId);
handler.handle(Future.succeededFuture(PostTenantResponse.respond204()));
return;
}
if (async) {
handler.handle(Future.succeededFuture(PostTenantResponse.respond201WithApplicationJson(job, PostTenantResponse.headersFor201().withLocation(location))));
}
runAsync(tenantAttributes, files[1], job, headers, context).onComplete(res -> {
log.info("job {} completed", id);
if (async) {
completeJob(job, context);
} else {
if (job.getError() != null) {
handler.handle(Future.succeededFuture(PostTenantResponse.respond400WithTextPlain(job.getError())));
return;
}
handler.handle(Future.succeededFuture(PostTenantResponse.respond204()));
}
});
}));
}
Aggregations