Search in sources :

Example 11 with Validate

use of org.folio.rest.annotations.Validate in project raml-module-builder by folio-org.

the class TenantAPI method deleteTenant.

@Validate
@Override
public void deleteTenant(Map<String, String> headers, Handler<AsyncResult<Response>> handlers, Context context) throws Exception {
    context.runOnContext(v -> {
        try {
            String tenantId = TenantTool.calculateTenantId(headers.get(ClientGenerator.OKAPI_HEADER_TENANT));
            log.info("sending... deleteTenant for " + tenantId);
            tenantExists(context, tenantId, h -> {
                boolean exists = false;
                if (h.succeeded()) {
                    exists = h.result();
                    if (!exists) {
                        handlers.handle(io.vertx.core.Future.succeededFuture(DeleteTenantResponse.withPlainInternalServerError("Tenant does not exist: " + tenantId)));
                        log.error("Can not delete. Tenant does not exist: " + tenantId);
                        return;
                    } else {
                        log.info("Deleting tenant " + tenantId);
                    }
                } else {
                    handlers.handle(io.vertx.core.Future.failedFuture(h.cause().getMessage()));
                    log.error(h.cause().getMessage(), h.cause());
                    return;
                }
                String sqlFile = null;
                try {
                    /*              InputStream is = TenantAPI.class.getClassLoader().getResourceAsStream(DELETE_JSON);
              if(is == null){
                log.info("No delete json to use for deleting tenant " + tenantId);
                handlers.handle(io.vertx.core.Future.succeededFuture(DeleteTenantResponse.withNoContent()));
                return;
              }
              sqlFile = IOUtils.toString(is);*/
                    SchemaMaker sMaker = new SchemaMaker(tenantId, PostgresClient.getModuleName(), TenantOperation.DELETE, null, PomReader.INSTANCE.getRmbVersion());
                    sqlFile = sMaker.generateDDL();
                } catch (Exception e1) {
                    handlers.handle(io.vertx.core.Future.failedFuture(e1.getMessage()));
                    log.error(e1.getMessage(), e1);
                    return;
                }
                log.info("Attempting to run delete script for: " + tenantId);
                log.debug("GENERATED SCHEMA " + sqlFile);
                /* connect as user in postgres-conf.json file (super user) - so that all commands will be available */
                PostgresClient.getInstance(context.owner()).runSQLFile(sqlFile, true, reply -> {
                    try {
                        String res = "";
                        if (reply.succeeded()) {
                            res = new JsonArray(reply.result()).encodePrettily();
                            if (reply.result().size() > 0) {
                                log.error("Unable to run the following commands during tenant delete: ");
                                reply.result().forEach(System.out::println);
                                handlers.handle(io.vertx.core.Future.succeededFuture(DeleteTenantResponse.withPlainBadRequest(res)));
                            } else {
                                OutStream os = new OutStream();
                                os.setData(res);
                                handlers.handle(io.vertx.core.Future.succeededFuture(DeleteTenantResponse.withNoContent()));
                            }
                        } else {
                            log.error(reply.cause().getMessage(), reply.cause());
                            handlers.handle(io.vertx.core.Future.succeededFuture(DeleteTenantResponse.withPlainInternalServerError(reply.cause().getMessage())));
                        }
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                        handlers.handle(io.vertx.core.Future.succeededFuture(DeleteTenantResponse.withPlainInternalServerError(e.getMessage())));
                    }
                });
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            handlers.handle(io.vertx.core.Future.succeededFuture(DeleteTenantResponse.withPlainInternalServerError(e.getMessage())));
        }
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) OutStream(org.folio.rest.tools.utils.OutStream) SchemaMaker(org.folio.rest.persist.ddlgen.SchemaMaker) Validate(org.folio.rest.annotations.Validate)

Example 12 with Validate

use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.

the class PlatformAPI method getPlatforms.

@Validate
@Override
public void getPlatforms(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    /**
     * http://host:port/platforms
     */
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.tenantId(okapiHeaders);
            CQLWrapper cql = getCQL(query, limit, offset);
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(PLATFORM_TABLE, Platform.class, new String[] { "*" }, cql, true, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        Platforms instanceTypes = new Platforms();
                        @SuppressWarnings("unchecked") List<Platform> instanceType = (List<Platform>) reply.result().getResults();
                        instanceTypes.setPlatforms(instanceType);
                        instanceTypes.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PlatformsResource.GetPlatformsResponse.withJsonOK(instanceTypes)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PlatformsResource.GetPlatformsResponse.withPlainBadRequest(reply.cause().getMessage())));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PlatformsResource.GetPlatformsResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String message = messages.getMessage(lang, MessageConsts.InternalServerError);
            if (e.getCause() instanceof CQLParseException) {
                message = " CQL parse error " + e.getLocalizedMessage();
            }
            asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PlatformsResource.GetPlatformsResponse.withPlainInternalServerError(message)));
        }
    });
}
Also used : Platforms(org.folio.rest.jaxrs.model.Platforms) Platform(org.folio.rest.jaxrs.model.Platform) List(java.util.List) CQLParseException(org.z3950.zing.cql.CQLParseException) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 13 with Validate

use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.

the class PlatformAPI method postPlatforms.

@Validate
@Override
public void postPlatforms(String lang, Platform 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(PLATFORM_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(PlatformsResource.PostPlatformsResponse.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(PlatformsResource.PostPlatformsResponse.withPlainBadRequest(msg)));
                    }
                } catch (Exception e) {
                    internalServerErrorDuringPost(e, lang, asyncResultHandler);
                }
            });
        } catch (Exception e) {
            internalServerErrorDuringPost(e, lang, asyncResultHandler);
        }
    });
}
Also used : OutStream(org.folio.rest.tools.utils.OutStream) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 14 with Validate

use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.

the class PlatformAPI method getPlatformsByPlatformId.

@Validate
@Override
public void getPlatformsByPlatformId(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);
            Criterion c = new Criterion(new Criteria().addField(idFieldName).setJSONB(false).setOperation("=").setValue("'" + instanceTypeId + "'"));
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(PLATFORM_TABLE, Platform.class, c, true, reply -> {
                try {
                    if (reply.failed()) {
                        String msg = PgExceptionUtil.badRequestMessage(reply.cause());
                        if (msg == null) {
                            internalServerErrorDuringGetById(reply.cause(), lang, asyncResultHandler);
                            return;
                        }
                        log.info(msg);
                        asyncResultHandler.handle(Future.succeededFuture(PlatformsResource.GetPlatformsByPlatformIdResponse.withPlainNotFound(msg)));
                        return;
                    }
                    @SuppressWarnings("unchecked") List<Platform> instanceType = (List<Platform>) reply.result().getResults();
                    if (instanceType.isEmpty()) {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PlatformsResource.GetPlatformsByPlatformIdResponse.withPlainNotFound(instanceTypeId)));
                    } else {
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PlatformsResource.GetPlatformsByPlatformIdResponse.withJsonOK(instanceType.get(0))));
                    }
                } catch (Exception e) {
                    internalServerErrorDuringGetById(e, lang, asyncResultHandler);
                }
            });
        } catch (Exception e) {
            internalServerErrorDuringGetById(e, lang, asyncResultHandler);
        }
    });
}
Also used : Platform(org.folio.rest.jaxrs.model.Platform) Criterion(org.folio.rest.persist.Criteria.Criterion) List(java.util.List) Criteria(org.folio.rest.persist.Criteria.Criteria) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Example 15 with Validate

use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.

the class IdentifierTypeAPI method postIdentifierTypes.

@Validate
@Override
public void postIdentifierTypes(String lang, IdentifierType 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(IDENTIFIER_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(PostIdentifierTypesResponse.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(PostIdentifierTypesResponse.withPlainBadRequest(msg)));
                    }
                } catch (Exception e) {
                    internalServerErrorDuringPost(e, lang, asyncResultHandler);
                }
            });
        } catch (Exception e) {
            internalServerErrorDuringPost(e, lang, asyncResultHandler);
        }
    });
}
Also used : OutStream(org.folio.rest.tools.utils.OutStream) CQLParseException(org.z3950.zing.cql.CQLParseException) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException) Validate(org.folio.rest.annotations.Validate)

Aggregations

Validate (org.folio.rest.annotations.Validate)66 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)42 CQLParseException (org.z3950.zing.cql.CQLParseException)32 List (java.util.List)27 OutStream (org.folio.rest.tools.utils.OutStream)27 Criteria (org.folio.rest.persist.Criteria.Criteria)16 Criterion (org.folio.rest.persist.Criteria.Criterion)16 PostgresClient (org.folio.rest.persist.PostgresClient)16 CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)13 JsonObject (io.vertx.core.json.JsonObject)4 Map (java.util.Map)4 Response (javax.ws.rs.core.Response)4 TenantTool (org.folio.rest.tools.utils.TenantTool)4 InputStream (java.io.InputStream)3 BodyPart (javax.mail.BodyPart)3 io.vertx.core (io.vertx.core)2 AsyncResult (io.vertx.core.AsyncResult)2 Context (io.vertx.core.Context)2 Handler (io.vertx.core.Handler)2 Logger (io.vertx.core.logging.Logger)2