Search in sources :

Example 6 with CQLWrapper

use of org.folio.rest.persist.cql.CQLWrapper 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())));
    }
}
Also used : CQL2PgJSON(org.z3950.zing.cql.cql2pgjson.CQL2PgJSON) HoldingsRecord(org.folio.rest.jaxrs.model.HoldingsRecord) PostgresClient(org.folio.rest.persist.PostgresClient) List(java.util.List) OutStream(org.folio.rest.tools.utils.OutStream) Limit(org.folio.rest.persist.Criteria.Limit) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) Offset(org.folio.rest.persist.Criteria.Offset)

Example 7 with CQLWrapper

use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.

the class IdentifierTypeAPI method getIdentifierTypes.

@Validate
@Override
public void getIdentifierTypes(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    /**
     * http://host:port/identifier-types
     */
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.tenantId(okapiHeaders);
            CQLWrapper cql = getCQL(query, limit, offset);
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(IDENTIFIER_TYPE_TABLE, IdentifierType.class, new String[] { "*" }, cql, true, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        IdentifierTypes identifierTypes = new IdentifierTypes();
                        @SuppressWarnings("unchecked") List<IdentifierType> identifierType = (List<IdentifierType>) reply.result().getResults();
                        identifierTypes.setIdentifierTypes(identifierType);
                        identifierTypes.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetIdentifierTypesResponse.withJsonOK(identifierTypes)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetIdentifierTypesResponse.withPlainBadRequest(reply.cause().getMessage())));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetIdentifierTypesResponse.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(GetIdentifierTypesResponse.withPlainInternalServerError(message)));
        }
    });
}
Also used : IdentifierTypes(org.folio.rest.jaxrs.model.IdentifierTypes) List(java.util.List) IdentifierType(org.folio.rest.jaxrs.model.IdentifierType) 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 8 with CQLWrapper

use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.

the class InstanceFormatAPI method getInstanceFormats.

@Validate
@Override
public void getInstanceFormats(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
    /**
     * http://host:port/instance-formats
     */
    vertxContext.runOnContext(v -> {
        try {
            String tenantId = TenantTool.tenantId(okapiHeaders);
            CQLWrapper cql = getCQL(query, limit, offset);
            PostgresClient.getInstance(vertxContext.owner(), tenantId).get(INSTANCE_FORMAT_TABLE, InstanceFormat.class, new String[] { "*" }, cql, true, true, reply -> {
                try {
                    if (reply.succeeded()) {
                        InstanceFormats instanceFormats = new InstanceFormats();
                        @SuppressWarnings("unchecked") List<InstanceFormat> instanceFormat = (List<InstanceFormat>) reply.result().getResults();
                        instanceFormats.setInstanceFormats(instanceFormat);
                        instanceFormats.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceFormatsResponse.withJsonOK(instanceFormats)));
                    } else {
                        log.error(reply.cause().getMessage(), reply.cause());
                        asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceFormatsResponse.withPlainBadRequest(reply.cause().getMessage())));
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetInstanceFormatsResponse.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(GetInstanceFormatsResponse.withPlainInternalServerError(message)));
        }
    });
}
Also used : InstanceFormats(org.folio.rest.jaxrs.model.InstanceFormats) 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) InstanceFormat(org.folio.rest.jaxrs.model.InstanceFormat) Validate(org.folio.rest.annotations.Validate)

Example 9 with CQLWrapper

use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.

the class InstanceStorageAPI method putInstanceStorageInstancesByInstanceId.

@Override
public void putInstanceStorageInstancesByInstanceId(@NotNull String instanceId, @DefaultValue("en") @Pattern(regexp = "[a-zA-Z]{2}") String lang, Instance 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 = { "*" };
                CQLWrapper cql = handleCQL(String.format("id==%s", instanceId), 1, 0);
                postgresClient.get(tableName, Instance.class, fieldList, cql, true, false, reply -> {
                    if (reply.succeeded()) {
                        List<Instance> instancesList = (List<Instance>) reply.result().getResults();
                        if (instancesList.size() == 1) {
                            try {
                                postgresClient.update(tableName, entity, entity.getId(), update -> {
                                    try {
                                        if (update.succeeded()) {
                                            OutStream stream = new OutStream();
                                            stream.setData(entity);
                                            asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withNoContent()));
                                        } else {
                                            asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(update.cause().getMessage())));
                                        }
                                    } catch (Exception e) {
                                        asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
                                    }
                                });
                            } catch (Exception e) {
                                asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
                            }
                        } else {
                            try {
                                postgresClient.save(tableName, entity.getId(), entity, save -> {
                                    try {
                                        if (save.succeeded()) {
                                            OutStream stream = new OutStream();
                                            stream.setData(entity);
                                            asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withNoContent()));
                                        } else {
                                            asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(save.cause().getMessage())));
                                        }
                                    } catch (Exception e) {
                                        asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
                                    }
                                });
                            } catch (Exception e) {
                                asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
                            }
                        }
                    } else {
                        asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(reply.cause().getMessage())));
                    }
                });
            } catch (Exception e) {
                asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
            }
        });
    } catch (Exception e) {
        asyncResultHandler.handle(Future.succeededFuture(PutInstanceStorageInstancesByInstanceIdResponse.withPlainInternalServerError(e.getMessage())));
    }
}
Also used : Instance(org.folio.rest.jaxrs.model.Instance) PostgresClient(org.folio.rest.persist.PostgresClient) List(java.util.List) OutStream(org.folio.rest.tools.utils.OutStream) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException)

Example 10 with CQLWrapper

use of org.folio.rest.persist.cql.CQLWrapper in project mod-inventory-storage by folio-org.

the class LocationAPI method locationInUse.

Future<Boolean> locationInUse(String locationId, String tenantId, Context vertxContext) {
    Future<Boolean> future = Future.future();
    // Get all items where the temporary future or permanent future is this location id
    String query = "permanentLocation == " + locationId + " OR temporarylocation == " + locationId;
    try {
        CQLWrapper cql = getCQL(query, 10, 0, ItemStorageAPI.ITEM_TABLE);
        String[] fieldList = { "*" };
        PostgresClient.getInstance(vertxContext.owner(), tenantId).get(ItemStorageAPI.ITEM_TABLE, Item.class, fieldList, cql, true, false, getReply -> {
            if (getReply.failed()) {
                future.fail(getReply.cause());
            } else {
                List<Item> itemList = (List<Item>) getReply.result().getResults();
                if (itemList.isEmpty()) {
                    future.complete(false);
                } else {
                    future.complete(true);
                }
            }
        });
    } catch (Exception e) {
        future.fail(e);
    }
    return future;
}
Also used : Item(org.folio.rest.jaxrs.model.Item) List(java.util.List) CQLWrapper(org.folio.rest.persist.cql.CQLWrapper) FieldException(org.z3950.zing.cql.cql2pgjson.FieldException)

Aggregations

CQLWrapper (org.folio.rest.persist.cql.CQLWrapper)32 List (java.util.List)24 FieldException (org.z3950.zing.cql.cql2pgjson.FieldException)22 Validate (org.folio.rest.annotations.Validate)12 CQL2PgJSON (org.z3950.zing.cql.cql2pgjson.CQL2PgJSON)12 PostgresClient (org.folio.rest.persist.PostgresClient)9 CQLParseException (org.z3950.zing.cql.CQLParseException)8 Test (org.junit.Test)6 Limit (org.folio.rest.persist.Criteria.Limit)4 Offset (org.folio.rest.persist.Criteria.Offset)4 HoldingsRecord (org.folio.rest.jaxrs.model.HoldingsRecord)3 Instance (org.folio.rest.jaxrs.model.Instance)3 ArrayList (java.util.ArrayList)2 OutStream (org.folio.rest.tools.utils.OutStream)2 io.vertx.core (io.vertx.core)1 Logger (io.vertx.core.logging.Logger)1 LoggerFactory (io.vertx.core.logging.LoggerFactory)1 Async (io.vertx.ext.unit.Async)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1