Search in sources :

Example 1 with RequestMetadataLb

use of org.folio.rest.jooq.tables.pojos.RequestMetadataLb in project mod-oai-pmh by folio-org.

the class InstancesDaoImpl method updateRequestUpdatedDate.

@Override
public Future<RequestMetadataLb> updateRequestUpdatedDate(String requestId, OffsetDateTime lastUpdatedDate, String tenantId) {
    RequestMetadataLb requestMetadataLb = new RequestMetadataLb();
    requestMetadataLb.setRequestId(UUID.fromString(requestId)).setLastUpdatedDate(lastUpdatedDate);
    return getQueryExecutor(tenantId).transaction(queryExecutor -> queryExecutor.executeAny(dslContext -> dslContext.update(REQUEST_METADATA_LB).set(REQUEST_METADATA_LB.LAST_UPDATED_DATE, lastUpdatedDate).where(REQUEST_METADATA_LB.REQUEST_ID.eq(UUID.fromString(requestId))).returning()).map(this::toOptionalRequestMetadata).map(optional -> {
        if (optional.isPresent()) {
            return optional.get();
        }
        throw new NotFoundException(String.format(REQUEST_METADATA_WITH_ID_DOES_NOT_EXIST, requestId));
    }));
}
Also used : RequestMetadataLb(org.folio.rest.jooq.tables.pojos.RequestMetadataLb) InstancesDao(org.folio.oaipmh.dao.InstancesDao) ZonedDateTime(java.time.ZonedDateTime) RequestMetadataLbRecord(org.folio.rest.jooq.tables.records.RequestMetadataLbRecord) StringUtils(org.apache.commons.lang3.StringUtils) RowMappers(org.folio.rest.jooq.tables.mappers.RowMappers) PostgresClientFactory(org.folio.oaipmh.dao.PostgresClientFactory) RowSet(io.vertx.sqlclient.RowSet) Repository(org.springframework.stereotype.Repository) Record(org.jooq.Record) INSTANCES(org.folio.rest.jooq.tables.Instances.INSTANCES) REQUEST_METADATA_LB(org.folio.rest.jooq.tables.RequestMetadataLb.REQUEST_METADATA_LB) Instances(org.folio.rest.jooq.tables.pojos.Instances) ReactiveClassicGenericQueryExecutor(io.github.jklingsporn.vertx.jooq.classic.reactivepg.ReactiveClassicGenericQueryExecutor) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) InsertValuesStep3(org.jooq.InsertValuesStep3) QueryResult(io.github.jklingsporn.vertx.jooq.shared.internal.QueryResult) Future(io.vertx.core.Future) ZoneId(java.time.ZoneId) NotFoundException(javax.ws.rs.NotFoundException) Objects(java.util.Objects) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Logger(org.apache.logging.log4j.Logger) InstancesRecord(org.folio.rest.jooq.tables.records.InstancesRecord) Row(io.vertx.sqlclient.Row) Optional(java.util.Optional) LogManager(org.apache.logging.log4j.LogManager) NotFoundException(javax.ws.rs.NotFoundException) RequestMetadataLb(org.folio.rest.jooq.tables.pojos.RequestMetadataLb)

Example 2 with RequestMetadataLb

use of org.folio.rest.jooq.tables.pojos.RequestMetadataLb in project mod-oai-pmh by folio-org.

the class InstancesServiceImplTest method shouldReturnFailedFuture_whenSaveRequestMetadataWithEmptyRequestId.

@Test
void shouldReturnFailedFuture_whenSaveRequestMetadataWithEmptyRequestId(VertxTestContext testContext) {
    testContext.verify(() -> {
        RequestMetadataLb requestMetadataLb = new RequestMetadataLb().setLastUpdatedDate(OffsetDateTime.now());
        instancesService.saveRequestMetadata(requestMetadataLb, OAI_TEST_TENANT).onComplete(testContext.failing(throwable -> {
            assertTrue(throwable instanceof IllegalStateException);
            testContext.completeNow();
        }));
    });
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) RequestMetadataLb(org.folio.rest.jooq.tables.pojos.RequestMetadataLb) InstancesDao(org.folio.oaipmh.dao.InstancesDao) PostgresTesterContainer(org.folio.postgres.testing.PostgresTesterContainer) Autowired(org.springframework.beans.factory.annotation.Autowired) Context(io.vertx.core.Context) SpringContextUtil(org.folio.spring.SpringContextUtil) OAI_TEST_TENANT(org.folio.rest.impl.OkapiMockServer.OAI_TEST_TENANT) AfterAll(org.junit.jupiter.api.AfterAll) PostgresClientFactory(org.folio.oaipmh.dao.PostgresClientFactory) TestInstance(org.junit.jupiter.api.TestInstance) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) AbstractInstancesTest(org.folio.oaipmh.common.AbstractInstancesTest) TestUtil(org.folio.oaipmh.common.TestUtil) ModuleName(org.folio.rest.tools.utils.ModuleName) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Vertx(io.vertx.core.Vertx) UUID(java.util.UUID) OkapiMockServer(org.folio.rest.impl.OkapiMockServer) VertxExtension(io.vertx.junit5.VertxExtension) NotFoundException(javax.ws.rs.NotFoundException) PostgresClient(org.folio.rest.persist.PostgresClient) NetworkUtils(org.folio.rest.tools.utils.NetworkUtils) Test(org.junit.jupiter.api.Test) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Logger(org.apache.logging.log4j.Logger) WebClientProvider(org.folio.oaipmh.WebClientProvider) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) ApplicationConfig(org.folio.config.ApplicationConfig) LogManager(org.apache.logging.log4j.LogManager) RequestMetadataLb(org.folio.rest.jooq.tables.pojos.RequestMetadataLb) AbstractInstancesTest(org.folio.oaipmh.common.AbstractInstancesTest) Test(org.junit.jupiter.api.Test)

Example 3 with RequestMetadataLb

use of org.folio.rest.jooq.tables.pojos.RequestMetadataLb in project mod-oai-pmh by folio-org.

the class InstancesDaoImpl method saveRequestMetadata.

@Override
public Future<RequestMetadataLb> saveRequestMetadata(RequestMetadataLb requestMetadata, String tenantId) {
    UUID uuid = requestMetadata.getRequestId();
    requestMetadata.setStreamEnded(false);
    if (Objects.isNull(uuid) || StringUtils.isEmpty(uuid.toString())) {
        return Future.failedFuture(new IllegalStateException("Cannot save request metadata, request metadata entity must contain requestId"));
    }
    return getQueryExecutor(tenantId).transaction(queryExecutor -> queryExecutor.executeAny(dslContext -> dslContext.insertInto(REQUEST_METADATA_LB).set(toDatabaseRecord(requestMetadata))).map(raw -> requestMetadata));
}
Also used : RequestMetadataLb(org.folio.rest.jooq.tables.pojos.RequestMetadataLb) InstancesDao(org.folio.oaipmh.dao.InstancesDao) ZonedDateTime(java.time.ZonedDateTime) RequestMetadataLbRecord(org.folio.rest.jooq.tables.records.RequestMetadataLbRecord) StringUtils(org.apache.commons.lang3.StringUtils) RowMappers(org.folio.rest.jooq.tables.mappers.RowMappers) PostgresClientFactory(org.folio.oaipmh.dao.PostgresClientFactory) RowSet(io.vertx.sqlclient.RowSet) Repository(org.springframework.stereotype.Repository) Record(org.jooq.Record) INSTANCES(org.folio.rest.jooq.tables.Instances.INSTANCES) REQUEST_METADATA_LB(org.folio.rest.jooq.tables.RequestMetadataLb.REQUEST_METADATA_LB) Instances(org.folio.rest.jooq.tables.pojos.Instances) ReactiveClassicGenericQueryExecutor(io.github.jklingsporn.vertx.jooq.classic.reactivepg.ReactiveClassicGenericQueryExecutor) UUID(java.util.UUID) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) InsertValuesStep3(org.jooq.InsertValuesStep3) QueryResult(io.github.jklingsporn.vertx.jooq.shared.internal.QueryResult) Future(io.vertx.core.Future) ZoneId(java.time.ZoneId) NotFoundException(javax.ws.rs.NotFoundException) Objects(java.util.Objects) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) Logger(org.apache.logging.log4j.Logger) InstancesRecord(org.folio.rest.jooq.tables.records.InstancesRecord) Row(io.vertx.sqlclient.Row) Optional(java.util.Optional) LogManager(org.apache.logging.log4j.LogManager) UUID(java.util.UUID)

Example 4 with RequestMetadataLb

use of org.folio.rest.jooq.tables.pojos.RequestMetadataLb in project mod-oai-pmh by folio-org.

the class InstancesDaoImpl method toOptionalRequestMetadata.

private Optional<RequestMetadataLb> toOptionalRequestMetadata(RowSet<Row> rows) {
    if (rows.rowCount() == 1) {
        Row row = rows.iterator().next();
        RequestMetadataLb requestMetadataLb = RowMappers.getRequestMetadataLbMapper().apply(row);
        return Optional.of(requestMetadataLb);
    }
    return Optional.empty();
}
Also used : Row(io.vertx.sqlclient.Row) RequestMetadataLb(org.folio.rest.jooq.tables.pojos.RequestMetadataLb)

Example 5 with RequestMetadataLb

use of org.folio.rest.jooq.tables.pojos.RequestMetadataLb in project mod-oai-pmh by folio-org.

the class MarcWithHoldingsRequestHelper method handle.

/**
 * Handle MarcWithHoldings request
 */
@Override
public Future<Response> handle(Request request, Context vertxContext) {
    Promise<Response> oaipmhResponsePromise = Promise.promise();
    metricsCollectingService.startMetric(request.getRequestId(), SEND_REQUEST);
    try {
        String resumptionToken = request.getResumptionToken();
        List<OAIPMHerrorType> errors = validateListRequest(request);
        if (!errors.isEmpty()) {
            return buildResponseWithErrors(request, oaipmhResponsePromise, errors);
        }
        String requestId;
        OffsetDateTime lastUpdateDate = OffsetDateTime.now(ZoneId.systemDefault());
        RequestMetadataLb requestMetadata = new RequestMetadataLb().setLastUpdatedDate(lastUpdateDate);
        Future<RequestMetadataLb> updateRequestMetadataFuture;
        if (resumptionToken == null) {
            requestId = request.getRequestId();
            requestMetadata.setRequestId(UUID.fromString(requestId));
            updateRequestMetadataFuture = instancesService.saveRequestMetadata(requestMetadata, request.getTenant());
        } else {
            requestId = request.getRequestId();
            updateRequestMetadataFuture = instancesService.updateRequestUpdatedDate(requestId, lastUpdateDate, request.getTenant());
        }
        updateRequestMetadataFuture.onSuccess(res -> {
            boolean isFirstBatch = resumptionToken == null;
            processBatch(request, vertxContext, oaipmhResponsePromise, requestId, isFirstBatch);
            if (isFirstBatch) {
                saveInstancesExecutor.executeBlocking(downloadInstancesPromise -> downloadInstances(request, oaipmhResponsePromise, downloadInstancesPromise, downloadContext), downloadInstancesResult -> {
                    instancesService.updateRequestStreamEnded(requestId, true, request.getTenant());
                    if (downloadInstancesResult.succeeded()) {
                        logger.info("Downloading instances complete.");
                    } else {
                        logger.error("Downloading instances was canceled due to the error. ", downloadInstancesResult.cause());
                        if (!oaipmhResponsePromise.future().isComplete()) {
                            oaipmhResponsePromise.fail(new IllegalStateException(downloadInstancesResult.cause()));
                        }
                    }
                });
            }
        }).onFailure(th -> handleException(oaipmhResponsePromise, th));
    } catch (Exception e) {
        handleException(oaipmhResponsePromise, e);
    }
    return oaipmhResponsePromise.future().onComplete(responseAsyncResult -> metricsCollectingService.endMetric(request.getRequestId(), SEND_REQUEST));
}
Also used : Response(javax.ws.rs.core.Response) HttpResponse(io.vertx.ext.web.client.HttpResponse) CONTENT_TYPE(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE) OFFSET_PARAM(org.folio.oaipmh.Constants.OFFSET_PARAM) DecodeException(io.vertx.core.json.DecodeException) Date(java.util.Date) NEXT_INSTANCE_PK_VALUE(org.folio.oaipmh.Constants.NEXT_INSTANCE_PK_VALUE) StatusType(org.openarchives.oai._2.StatusType) Autowired(org.springframework.beans.factory.annotation.Autowired) NEXT_RECORD_ID_PARAM(org.folio.oaipmh.Constants.NEXT_RECORD_ID_PARAM) SEND_REQUEST(org.folio.oaipmh.service.MetricsCollectingService.MetricOperation.SEND_REQUEST) RESUMPTION_TOKEN_TIMEOUT(org.folio.oaipmh.Constants.RESUMPTION_TOKEN_TIMEOUT) Context(io.vertx.core.Context) WorkerExecutor(io.vertx.core.WorkerExecutor) Tuple(io.vertx.sqlclient.Tuple) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) ResumptionTokenType(org.openarchives.oai._2.ResumptionTokenType) Request(org.folio.oaipmh.Request) NAME(org.folio.oaipmh.helpers.records.RecordMetadataManager.NAME) OAIPMH(org.openarchives.oai._2.OAIPMH) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) BigInteger(java.math.BigInteger) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) AbstractHelper(org.folio.oaipmh.helpers.AbstractHelper) UNTIL_PARAM(org.folio.oaipmh.Constants.UNTIL_PARAM) EXPIRATION_DATE_RESUMPTION_TOKEN_PARAM(org.folio.oaipmh.Constants.EXPIRATION_DATE_RESUMPTION_TOKEN_PARAM) OKAPI_TENANT(org.folio.oaipmh.Constants.OKAPI_TENANT) InstancesService(org.folio.oaipmh.service.InstancesService) ITEMS(org.folio.oaipmh.helpers.records.RecordMetadataManager.ITEMS) MetricsCollectingService(org.folio.oaipmh.service.MetricsCollectingService) UUID(java.util.UUID) ITEMS_AND_HOLDINGS_FIELDS(org.folio.oaipmh.helpers.records.RecordMetadataManager.ITEMS_AND_HOLDINGS_FIELDS) Instant(java.time.Instant) Future(io.vertx.core.Future) TenantTool(org.folio.rest.tools.utils.TenantTool) Collectors(java.util.stream.Collectors) PostgresClient(org.folio.rest.persist.PostgresClient) ZoneId(java.time.ZoneId) String.format(java.lang.String.format) RepositoryConfigurationUtil(org.folio.oaipmh.helpers.RepositoryConfigurationUtil) REQUEST_ID_PARAM(org.folio.oaipmh.Constants.REQUEST_ID_PARAM) CALL_NUMBER(org.folio.oaipmh.helpers.records.RecordMetadataManager.CALL_NUMBER) Objects(java.util.Objects) List(java.util.List) Logger(org.apache.logging.log4j.Logger) OffsetDateTime(java.time.OffsetDateTime) Buffer(io.vertx.core.buffer.Buffer) Response(javax.ws.rs.core.Response) WebClientProvider(org.folio.oaipmh.WebClientProvider) RETRY_ATTEMPTS(org.folio.oaipmh.Constants.RETRY_ATTEMPTS) STATUS_MESSAGE(org.folio.oaipmh.Constants.STATUS_MESSAGE) ChronoField(java.time.temporal.ChronoField) HttpResponse(io.vertx.ext.web.client.HttpResponse) SourceStorageSourceRecordsClientWrapper(org.folio.oaipmh.service.SourceStorageSourceRecordsClientWrapper) RequestMetadataLb(org.folio.rest.jooq.tables.pojos.RequestMetadataLb) BodyCodec(io.vertx.ext.web.codec.BodyCodec) INSTANCES_PROCESSING(org.folio.oaipmh.service.MetricsCollectingService.MetricOperation.INSTANCES_PROCESSING) HashMap(java.util.HashMap) OKAPI_TOKEN(org.folio.oaipmh.Constants.OKAPI_TOKEN) REPOSITORY_MAX_RECORDS_PER_RESPONSE(org.folio.oaipmh.Constants.REPOSITORY_MAX_RECORDS_PER_RESPONSE) JsonEvent(io.vertx.core.parsetools.JsonEvent) CollectionUtils(org.apache.commons.collections4.CollectionUtils) SpringContextUtil(org.folio.spring.SpringContextUtil) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) RepositoryConfigurationUtil.getBooleanProperty(org.folio.oaipmh.helpers.RepositoryConfigurationUtil.getBooleanProperty) HttpRequestImpl(io.vertx.ext.web.client.impl.HttpRequestImpl) AsyncResult(io.vertx.core.AsyncResult) STATUS_CODE(org.folio.oaipmh.Constants.STATUS_CODE) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Instances(org.folio.rest.jooq.tables.pojos.Instances) RecordMetadataManager(org.folio.oaipmh.helpers.records.RecordMetadataManager) Maps(com.google.common.collect.Maps) HttpRequest(io.vertx.ext.web.client.HttpRequest) JsonArray(io.vertx.core.json.JsonArray) ResponseHelper(org.folio.oaipmh.helpers.response.ResponseHelper) PgConnection(io.vertx.pgclient.PgConnection) Collectors.toList(java.util.stream.Collectors.toList) RecordType(org.openarchives.oai._2.RecordType) REPOSITORY_SRS_HTTP_REQUEST_RETRY_ATTEMPTS(org.folio.oaipmh.Constants.REPOSITORY_SRS_HTTP_REQUEST_RETRY_ATTEMPTS) ACCEPT(javax.ws.rs.core.HttpHeaders.ACCEPT) LOCATION(org.folio.oaipmh.Constants.LOCATION) REPOSITORY_SUPPRESSED_RECORDS_PROCESSING(org.folio.oaipmh.Constants.REPOSITORY_SUPPRESSED_RECORDS_PROCESSING) ListRecordsType(org.openarchives.oai._2.ListRecordsType) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) OAIPMHerrorType(org.openarchives.oai._2.OAIPMHerrorType) OffsetDateTime(java.time.OffsetDateTime) RequestMetadataLb(org.folio.rest.jooq.tables.pojos.RequestMetadataLb) DecodeException(io.vertx.core.json.DecodeException)

Aggregations

RequestMetadataLb (org.folio.rest.jooq.tables.pojos.RequestMetadataLb)6 OffsetDateTime (java.time.OffsetDateTime)5 List (java.util.List)5 UUID (java.util.UUID)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 NotFoundException (javax.ws.rs.NotFoundException)4 InstancesDao (org.folio.oaipmh.dao.InstancesDao)4 PostgresClientFactory (org.folio.oaipmh.dao.PostgresClientFactory)4 Context (io.vertx.core.Context)3 Future (io.vertx.core.Future)3 Vertx (io.vertx.core.Vertx)3 Row (io.vertx.sqlclient.Row)3 Instant (java.time.Instant)3 ZoneId (java.time.ZoneId)3 Objects (java.util.Objects)3 Collectors (java.util.stream.Collectors)3 WebClientProvider (org.folio.oaipmh.WebClientProvider)3 Instances (org.folio.rest.jooq.tables.pojos.Instances)3 PostgresClient (org.folio.rest.persist.PostgresClient)3