use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class LocationUnitAPI method deleteLocationUnitsLibraries.
// //////////////////////////////////////////
@Override
public void deleteLocationUnitsLibraries(String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
String tenantId = TenantTool.tenantId(okapiHeaders);
PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
postgresClient.mutate(String.format("DELETE FROM %s_%s.%s", tenantId, MOD_NAME, LIBRARY_TABLE), reply -> {
if (reply.succeeded()) {
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.DeleteLocationUnitsLibrariesResponse.noContent().build()));
} else {
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.DeleteLocationUnitsLibrariesResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
});
}
use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class LocationUnitAPI method deleteLocationUnitsInstitutions.
@Override
public void deleteLocationUnitsInstitutions(String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
String tenantId = TenantTool.tenantId(okapiHeaders);
try {
PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
postgresClient.mutate(String.format("DELETE FROM %s_%s.%s", tenantId, MOD_NAME, INSTITUTION_TABLE), reply -> {
if (reply.succeeded()) {
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.DeleteLocationUnitsInstitutionsResponse.noContent().build()));
} else {
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.DeleteLocationUnitsInstitutionsResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(LocationUnitsResource.DeleteLocationUnitsInstitutionsResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class HoldingsStorageAPI method putHoldingsStorageHoldingsByHoldingsRecordId.
@Override
public void putHoldingsStorageHoldingsByHoldingsRecordId(@NotNull String holdingsRecordId, @DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, HoldingsRecord entity, 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));
vertxContext.runOnContext(v -> {
try {
String[] fieldList = { "*" };
CQL2PgJSON cql2pgJson = new CQL2PgJSON(HOLDINGS_RECORD_TABLE + ".jsonb");
CQLWrapper cql = new CQLWrapper(cql2pgJson, String.format("id==%s", holdingsRecordId)).setLimit(new Limit(1)).setOffset(new Offset(0));
log.info(String.format("SQL generated from CQL: %s", cql.toString()));
postgresClient.get(HOLDINGS_RECORD_TABLE, HoldingsRecord.class, fieldList, cql, true, false, reply -> {
if (reply.succeeded()) {
List<HoldingsRecord> itemList = (List<HoldingsRecord>) reply.result().getResults();
if (itemList.size() == 1) {
try {
postgresClient.update(HOLDINGS_RECORD_TABLE, entity, entity.getId(), update -> {
try {
if (update.succeeded()) {
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withNoContent()));
} else {
asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(update.cause().getMessage())));
}
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
}
} else {
try {
postgresClient.save(HOLDINGS_RECORD_TABLE, entity.getId(), entity, save -> {
try {
if (save.succeeded()) {
OutStream stream = new OutStream();
stream.setData(entity);
asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withNoContent()));
} else {
asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(save.cause().getMessage())));
}
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
}
}
} else {
asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(reply.cause().getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
}
});
} catch (Exception e) {
asyncResultHandler.handle(Future.succeededFuture(PutHoldingsStorageHoldingsByHoldingsRecordIdResponse.withPlainInternalServerError(e.getMessage())));
}
}
use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class InstanceFormatAPI method deleteInstanceFormatsByInstanceFormatId.
@Validate
@Override
public void deleteInstanceFormatsByInstanceFormatId(String instanceFormatId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
vertxContext.runOnContext(v -> {
try {
String tenantId = TenantTool.tenantId(okapiHeaders);
PostgresClient postgres = PostgresClient.getInstance(vertxContext.owner(), tenantId);
postgres.delete(INSTANCE_FORMAT_TABLE, instanceFormatId, reply -> {
try {
if (reply.failed()) {
String msg = PgExceptionUtil.badRequestMessage(reply.cause());
if (msg == null) {
internalServerErrorDuringDelete(reply.cause(), lang, asyncResultHandler);
return;
}
log.info(msg);
asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceFormatsByInstanceFormatIdResponse.withPlainBadRequest(msg)));
return;
}
int updated = reply.result().getUpdated();
if (updated != 1) {
String msg = messages.getMessage(lang, MessageConsts.DeletedCountError, 1, updated);
log.error(msg);
asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceFormatsByInstanceFormatIdResponse.withPlainNotFound(msg)));
return;
}
asyncResultHandler.handle(Future.succeededFuture(DeleteInstanceFormatsByInstanceFormatIdResponse.withNoContent()));
} catch (Exception e) {
internalServerErrorDuringDelete(e, lang, asyncResultHandler);
}
});
} catch (Exception e) {
internalServerErrorDuringDelete(e, lang, asyncResultHandler);
}
});
}
use of org.folio.rest.persist.PostgresClient in project mod-inventory-storage by folio-org.
the class InstanceStorageAPI method deleteInstanceStorageInstances.
@Override
public void deleteInstanceStorageInstances(@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);
vertxContext.runOnContext(v -> {
try {
PostgresClient postgresClient = PostgresClient.getInstance(vertxContext.owner(), TenantTool.calculateTenantId(tenantId));
postgresClient.mutate(String.format("TRUNCATE TABLE %s_%s.instance", tenantId, "mod_inventory_storage"), reply -> {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.DeleteInstanceStorageInstancesResponse.noContent().build()));
});
} catch (Exception e) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(InstanceStorageResource.DeleteInstanceStorageInstancesResponse.withPlainInternalServerError(e.getMessage())));
}
});
}
Aggregations