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())));
}
});
}
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)));
}
});
}
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);
}
});
}
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);
}
});
}
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);
}
});
}
Aggregations