Search in sources :

Example 1 with Tenant

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()));
            }
        });
    }));
}
Also used : TemplateException(freemarker.template.TemplateException) SchemaMaker(org.folio.rest.persist.ddlgen.SchemaMaker) HashMap(java.util.HashMap) Context(io.vertx.core.Context) Tuple(io.vertx.sqlclient.Tuple) TenantOperation(org.folio.dbschema.TenantOperation) ObjectMapperTool(org.folio.dbschema.ObjectMapperTool) Map(java.util.Map) Schema(org.folio.dbschema.Schema) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) LinkedList(java.util.LinkedList) TenantJob(org.folio.rest.jaxrs.model.TenantJob) Tenant(org.folio.rest.jaxrs.resource.Tenant) Promise(io.vertx.core.Promise) IOException(java.io.IOException) UUID(java.util.UUID) Future(io.vertx.core.Future) Validate(org.folio.rest.annotations.Validate) TenantTool(org.folio.rest.tools.utils.TenantTool) StandardCharsets(java.nio.charset.StandardCharsets) PostgresClient(org.folio.rest.persist.PostgresClient) ResponseException(org.folio.rest.tools.client.exceptions.ResponseException) UncheckedIOException(java.io.UncheckedIOException) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Response(javax.ws.rs.core.Response) TenantAttributes(org.folio.rest.jaxrs.model.TenantAttributes) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) InputStream(java.io.InputStream) TenantJob(org.folio.rest.jaxrs.model.TenantJob)

Aggregations

TemplateException (freemarker.template.TemplateException)1 AsyncResult (io.vertx.core.AsyncResult)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Promise (io.vertx.core.Promise)1 JsonObject (io.vertx.core.json.JsonObject)1 Tuple (io.vertx.sqlclient.Tuple)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 Response (javax.ws.rs.core.Response)1 IOUtils (org.apache.commons.io.IOUtils)1 LogManager (org.apache.logging.log4j.LogManager)1