use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.
the class ItemStorageAPI method deleteItemStorageItems.
@Validate
@Override
public void deleteItemStorageItems(@DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
String tenantId = okapiHeaders.get(TENANT_HEADER);
try {
vertxContext.runOnContext(v -> {
PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
postgresClient.mutate(String.format("TRUNCATE TABLE %s_%s.item", tenantId, "mod_inventory_storage"), reply -> {
if (reply.succeeded()) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsResponse.noContent().build()));
} else {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
});
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(ItemStorageResource.DeleteItemStorageItemsResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.annotations.Validate 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.annotations.Validate in project mod-inventory-storage by folio-org.
the class MaterialTypeAPI method deleteMaterialTypesByMaterialtypeId.
@Validate
@Override
public void deleteMaterialTypesByMaterialtypeId(String materialtypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
vertxContext.runOnContext(v -> {
String tenantId = TenantTool.calculateTenantId(okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT));
try {
Item item = new Item();
item.setMaterialTypeId(materialtypeId);
/**
* check if any item is using this material type *
*/
try {
PostgresClient.getInstance(vertxContext.owner(), tenantId).get(ItemStorageAPI.ITEM_TABLE, item, new String[] { idFieldName }, true, false, 0, 1, replyHandler -> {
if (replyHandler.succeeded()) {
List<Item> mtypeList = (List<Item>) replyHandler.result().getResults();
if (mtypeList.size() > 0) {
String message = "Can not delete material type, " + materialtypeId + ". " + mtypeList.size() + " items associated with it";
log.error(message);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainBadRequest(message)));
return;
} else {
log.info("Attemping delete of unused material type, " + materialtypeId);
}
try {
PostgresClient.getInstance(vertxContext.owner(), tenantId).delete(MATERIAL_TYPE_TABLE, materialtypeId, reply -> {
try {
if (reply.succeeded()) {
if (reply.result().getUpdated() == 1) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withNoContent()));
} else {
log.error(messages.getMessage(lang, MessageConsts.DeletedCountError, 1, reply.result().getUpdated()));
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainNotFound(messages.getMessage(lang, MessageConsts.DeletedCountError, 1, reply.result().getUpdated()))));
}
} else {
log.error(reply.cause().getMessage(), reply.cause());
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
});
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
} else {
log.error(replyHandler.cause().getMessage(), replyHandler.cause());
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
});
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(DeleteMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
});
}
use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.
the class LoanTypeAPI method getLoanTypesByLoantypeId.
@Validate
@Override
public void getLoanTypesByLoantypeId(String loantypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
vertxContext.runOnContext(v -> {
try {
Criterion c = new Criterion(new Criteria().addField(idFieldName).setJSONB(false).setOperation("=").setValue("'" + loantypeId + "'"));
getPostgresClient(vertxContext, okapiHeaders).get(LOAN_TYPE_TABLE, Loantype.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(GetLoanTypesByLoantypeIdResponse.withPlainNotFound(msg)));
return;
}
@SuppressWarnings("unchecked") List<Loantype> userGroup = (List<Loantype>) reply.result().getResults();
if (userGroup.isEmpty()) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetLoanTypesByLoantypeIdResponse.withPlainNotFound(loantypeId)));
} else {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetLoanTypesByLoantypeIdResponse.withJsonOK(userGroup.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 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);
}
});
}
Aggregations