use of org.folio.rest.persist.Criteria.Criteria in project mod-inventory-storage by folio-org.
the class InstanceStorageAPI method deleteInstanceStorageInstancesByInstanceId.
@Override
public void deleteInstanceStorageInstancesByInstanceId(@NotNull String instanceId, @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 {
PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
Criteria a = new Criteria();
a.addField("'id'");
a.setOperation("=");
a.setValue(instanceId);
Criterion criterion = new Criterion(a);
vertxContext.runOnContext(v -> {
try {
postgresClient.delete("instance", criterion, reply -> {
if (reply.succeeded()) {
asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceStorageInstancesByInstanceIdResponse.withNoContent()));
} else {
asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.persist.Criteria.Criteria in project mod-inventory-storage by folio-org.
the class InstanceTypeAPI method getInstanceTypesByInstanceTypeId.
@Validate
@Override
public void getInstanceTypesByInstanceTypeId(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(INSTANCE_TYPE_TABLE, InstanceType.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(GetInstanceTypesByInstanceTypeIdResponse.withPlainNotFound(msg)));
return;
}
@SuppressWarnings("unchecked") List<InstanceType> instanceType = (List<InstanceType>) reply.result().getResults();
if (instanceType.isEmpty()) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceTypesByInstanceTypeIdResponse.withPlainNotFound(instanceTypeId)));
} else {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceTypesByInstanceTypeIdResponse.withJsonOK(instanceType.get(0))));
}
} catch (Exception e) {
internalServerErrorDuringGetById(e, lang, asyncResultHandler);
}
});
} catch (Exception e) {
internalServerErrorDuringGetById(e, lang, asyncResultHandler);
}
});
}
use of org.folio.rest.persist.Criteria.Criteria in project mod-inventory-storage by folio-org.
the class LocationAPI method deleteLocationsById.
@Override
public void deleteLocationsById(String id, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
String tenantId = getTenant(okapiHeaders);
Criterion criterion;
try {
Criteria criteria = new Criteria(LOCATION_SCHEMA_PATH);
criteria.addField(ID_FIELD_NAME);
criteria.setOperation("=");
criteria.setValue(id);
criterion = new Criterion(criteria);
} catch (Exception e) {
String message = logAndSaveError(e);
asyncResultHandler.handle(Future.succeededFuture(GetLocationsByIdResponse.withPlainInternalServerError(message)));
return;
}
locationInUse(id, tenantId, vertxContext).setHandler(res -> {
if (res.failed()) {
String message = logAndSaveError(res.cause());
DeleteLocationsByIdResponse.withPlainInternalServerError(message);
} else {
if (res.result()) {
asyncResultHandler.handle(Future.succeededFuture(DeleteLocationsByIdResponse.withPlainBadRequest("Cannot delete location, as it is in use")));
} else {
PostgresClient.getInstance(vertxContext.owner(), tenantId).delete(LOCATION_TABLE, criterion, deleteReply -> {
if (deleteReply.failed()) {
logAndSaveError(deleteReply.cause());
asyncResultHandler.handle(Future.succeededFuture(DeleteLocationsByIdResponse.withPlainNotFound("Not found")));
} else {
asyncResultHandler.handle(Future.succeededFuture(DeleteLocationsByIdResponse.withNoContent()));
}
});
}
}
});
}
use of org.folio.rest.persist.Criteria.Criteria in project mod-inventory-storage by folio-org.
the class LocationAPI method putLocationsById.
@Override
public void putLocationsById(String id, String lang, Location entity, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
if (!id.equals(entity.getId())) {
String message = "Illegal operation: id cannot be changed";
asyncResultHandler.handle(Future.succeededFuture(PutLocationsByIdResponse.withPlainBadRequest(message)));
return;
}
String tenantId = getTenant(okapiHeaders);
Criterion criterion;
try {
Criteria criteria = new Criteria(LOCATION_SCHEMA_PATH);
criteria.addField(ID_FIELD_NAME);
criteria.setOperation("=");
criteria.setValue(id);
criterion = new Criterion(criteria);
} catch (Exception e) {
String message = logAndSaveError(e);
asyncResultHandler.handle(Future.succeededFuture(PutLocationsByIdResponse.withPlainInternalServerError(message)));
return;
}
PostgresClient.getInstance(vertxContext.owner(), tenantId).update(LOCATION_TABLE, entity, criterion, false, updateReply -> {
if (updateReply.failed()) {
String message = logAndSaveError(updateReply.cause());
asyncResultHandler.handle(Future.succeededFuture(PutLocationsByIdResponse.withPlainInternalServerError(message)));
} else {
if (updateReply.result().getUpdated() == 0) {
asyncResultHandler.handle(Future.succeededFuture(PutLocationsByIdResponse.withPlainNotFound("Not found")));
// Not found
} else {
asyncResultHandler.handle(Future.succeededFuture(PutLocationsByIdResponse.withNoContent()));
}
}
});
}
use of org.folio.rest.persist.Criteria.Criteria in project mod-inventory-storage by folio-org.
the class MaterialTypeAPI method getMaterialTypesByMaterialtypeId.
@Validate
@Override
public void getMaterialTypesByMaterialtypeId(String materialtypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
vertxContext.runOnContext(v -> {
try {
String tenantId = TenantTool.calculateTenantId(okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT));
Criterion c = new Criterion(new Criteria().addField(idFieldName).setJSONB(false).setOperation("=").setValue("'" + materialtypeId + "'"));
PostgresClient.getInstance(vertxContext.owner(), tenantId).get(MATERIAL_TYPE_TABLE, Mtype.class, c, true, reply -> {
try {
if (reply.succeeded()) {
@SuppressWarnings("unchecked") List<Mtype> userGroup = (List<Mtype>) reply.result().getResults();
if (userGroup.isEmpty()) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withPlainNotFound(materialtypeId)));
} else {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withJsonOK(userGroup.get(0))));
}
} else {
log.error(reply.cause().getMessage(), reply.cause());
if (isInvalidUUID(reply.cause().getMessage())) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withPlainNotFound(materialtypeId)));
} else {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
});
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetMaterialTypesByMaterialtypeIdResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
});
}
Aggregations