Search in sources :

Example 1 with Try

use of org.folio.dataimport.util.Try in project mod-source-record-manager by folio-org.

the class JobExecutionServiceImpl method updateSnapshotStatus.

private Future<JobExecution> updateSnapshotStatus(JobExecution jobExecution, OkapiConnectionParams params) {
    Promise<JobExecution> promise = Promise.promise();
    Snapshot snapshot = new Snapshot().withJobExecutionId(jobExecution.getId()).withStatus(Snapshot.Status.fromValue(jobExecution.getStatus().name()));
    SourceStorageSnapshotsClient client = new SourceStorageSnapshotsClient(params.getOkapiUrl(), params.getTenantId(), params.getToken());
    try {
        client.putSourceStorageSnapshotsByJobExecutionId(jobExecution.getId(), null, snapshot, response -> {
            if (response.result().statusCode() == HttpStatus.HTTP_OK.toInt()) {
                promise.complete(jobExecution);
            } else {
                jobExecutionDao.updateBlocking(jobExecution.getId(), jobExec -> {
                    Promise<JobExecution> jobExecutionPromise = Promise.promise();
                    jobExec.setErrorStatus(JobExecution.ErrorStatus.SNAPSHOT_UPDATE_ERROR);
                    jobExec.setStatus(JobExecution.Status.ERROR);
                    jobExec.setUiStatus(JobExecution.UiStatus.ERROR);
                    jobExec.setCompletedDate(new Date());
                    jobExecutionPromise.complete(jobExec);
                    return jobExecutionPromise.future();
                }, params.getTenantId()).onComplete(jobExecutionUpdate -> {
                    String message = "Couldn't update snapshot status for jobExecution with id " + jobExecution.getId();
                    LOGGER.error(message);
                    promise.fail(message);
                });
            }
        });
    } catch (Exception e) {
        LOGGER.error("Error during update for Snapshot with id {}", jobExecution.getId(), e);
        promise.fail(e);
    }
    return promise.future();
}
Also used : JobExecution(org.folio.rest.jaxrs.model.JobExecution) DataImportProfilesClient(org.folio.rest.client.DataImportProfilesClient) Arrays(java.util.Arrays) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) UserInfo(org.folio.rest.jaxrs.model.UserInfo) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) JobDuplicateUpdateException(org.folio.services.exceptions.JobDuplicateUpdateException) JobExecutionSourceChunkDao(org.folio.dao.JobExecutionSourceChunkDao) JobExecution(org.folio.rest.jaxrs.model.JobExecution) JobProfileInfo(org.folio.rest.jaxrs.model.JobProfileInfo) InitJobExecutionsRsDto(org.folio.rest.jaxrs.model.InitJobExecutionsRsDto) ERROR(org.folio.rest.jaxrs.model.StatusDto.Status.ERROR) JsonObject(io.vertx.core.json.JsonObject) BadRequestException(javax.ws.rs.BadRequestException) HttpException(io.vertx.ext.web.handler.HttpException) GenericCompositeFuture(org.folio.okapi.common.GenericCompositeFuture) StatusDto(org.folio.rest.jaxrs.model.StatusDto) UUID(java.util.UUID) Future(io.vertx.core.Future) Try(org.folio.dataimport.util.Try) OkapiConnectionParams(org.folio.dataimport.util.OkapiConnectionParams) NotFoundException(javax.ws.rs.NotFoundException) String.format(java.lang.String.format) Objects(java.util.Objects) List(java.util.List) HTTP_OK(org.folio.HttpStatus.HTTP_OK) Logger(org.apache.logging.log4j.Logger) JobExecutionFilter(org.folio.dao.JobExecutionFilter) JobExecutionDtoCollection(org.folio.rest.jaxrs.model.JobExecutionDtoCollection) HTTP_CREATED(org.folio.HttpStatus.HTTP_CREATED) Optional(java.util.Optional) RestUtil(org.folio.dataimport.util.RestUtil) JobProfile(org.folio.rest.jaxrs.model.JobProfile) FilenameUtils(org.apache.commons.io.FilenameUtils) Progress(org.folio.rest.jaxrs.model.Progress) JobExecutionDao(org.folio.dao.JobExecutionDao) CANCELLED(org.folio.rest.jaxrs.model.StatusDto.Status.CANCELLED) DeleteJobExecutionsResp(org.folio.rest.jaxrs.model.DeleteJobExecutionsResp) ArrayList(java.util.ArrayList) SourceStorageSnapshotsClient(org.folio.rest.client.SourceStorageSnapshotsClient) Service(org.springframework.stereotype.Service) InitJobExecutionsRqDto(org.folio.rest.jaxrs.model.InitJobExecutionsRqDto) Promise(io.vertx.core.Promise) COMMITTED(org.folio.rest.jaxrs.model.JobExecution.Status.COMMITTED) RunBy(org.folio.rest.jaxrs.model.RunBy) File(org.folio.rest.jaxrs.model.File) SortField(org.folio.dao.util.SortField) HttpMethod(io.vertx.core.http.HttpMethod) JobExecutionUserInfoCollection(org.folio.rest.jaxrs.model.JobExecutionUserInfoCollection) JobProfileInfoCollection(org.folio.rest.jaxrs.model.JobProfileInfoCollection) HttpStatus(org.folio.HttpStatus) PROFILE_SNAPSHOT_CREATING_ERROR(org.folio.rest.jaxrs.model.StatusDto.ErrorStatus.PROFILE_SNAPSHOT_CREATING_ERROR) LogManager(org.apache.logging.log4j.LogManager) Snapshot(org.folio.rest.jaxrs.model.Snapshot) Collections(java.util.Collections) Snapshot(org.folio.rest.jaxrs.model.Snapshot) Promise(io.vertx.core.Promise) SourceStorageSnapshotsClient(org.folio.rest.client.SourceStorageSnapshotsClient) Date(java.util.Date) JobDuplicateUpdateException(org.folio.services.exceptions.JobDuplicateUpdateException) BadRequestException(javax.ws.rs.BadRequestException) HttpException(io.vertx.ext.web.handler.HttpException) NotFoundException(javax.ws.rs.NotFoundException)

Example 2 with Try

use of org.folio.dataimport.util.Try in project mod-source-record-manager by folio-org.

the class ParsedRecordServiceImpl method getRecordByExternalId.

@Override
public Future<ParsedRecordDto> getRecordByExternalId(String externalId, OkapiConnectionParams params) {
    Promise<ParsedRecordDto> promise = Promise.promise();
    var client = new SourceStorageSourceRecordsClient(params.getOkapiUrl(), params.getTenantId(), params.getToken());
    try {
        client.getSourceStorageSourceRecordsById(externalId, "EXTERNAL", response -> {
            if (HTTP_OK.toInt() == response.result().statusCode()) {
                Buffer bodyAsBuffer = response.result().bodyAsBuffer();
                Try.itGet(() -> mapSourceRecordToParsedRecordDto(bodyAsBuffer)).compose(parsedRecordDto -> sourceRecordStateService.get(parsedRecordDto.getId(), params.getTenantId()).map(sourceRecordStateOptional -> sourceRecordStateOptional.orElse(new SourceRecordState().withRecordState(SourceRecordState.RecordState.ACTUAL))).compose(sourceRecordState -> Future.succeededFuture(parsedRecordDto.withRecordState(ParsedRecordDto.RecordState.valueOf(sourceRecordState.getRecordState().name()))))).onComplete(parsedRecordDtoAsyncResult -> {
                    if (parsedRecordDtoAsyncResult.succeeded()) {
                        promise.complete(parsedRecordDtoAsyncResult.result());
                    } else {
                        promise.fail(parsedRecordDtoAsyncResult.cause());
                    }
                });
            } else {
                String message = format("Error retrieving Record by externalId: '%s', response code %s, %s", externalId, response.result().statusCode(), response.result().statusMessage());
                if (HTTP_NOT_FOUND.toInt() == response.result().statusCode()) {
                    promise.fail(new NotFoundException(message));
                } else {
                    promise.fail(message);
                }
            }
        });
    } catch (Exception e) {
        log.error("Failed to GET Record from SRS", e);
        promise.fail(e);
    }
    return promise.future();
}
Also used : Buffer(io.vertx.core.buffer.Buffer) MappingParametersProvider(org.folio.services.mappers.processor.MappingParametersProvider) Json(io.vertx.core.json.Json) HashMap(java.util.HashMap) Value(org.springframework.beans.factory.annotation.Value) QM_RECORD_UPDATED(org.folio.verticle.consumers.util.QMEventTypes.QM_RECORD_UPDATED) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Service(org.springframework.stereotype.Service) MappingRuleCacheKey(org.folio.services.entity.MappingRuleCacheKey) JsonObject(io.vertx.core.json.JsonObject) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) SourceRecord(org.folio.rest.jaxrs.model.SourceRecord) Promise(io.vertx.core.Promise) HTTP_NOT_FOUND(org.folio.HttpStatus.HTTP_NOT_FOUND) Future.failedFuture(io.vertx.core.Future.failedFuture) UUID(java.util.UUID) Future(io.vertx.core.Future) Try(org.folio.dataimport.util.Try) ParsedRecordDto(org.folio.rest.jaxrs.model.ParsedRecordDto) String.format(java.lang.String.format) NotFoundException(javax.ws.rs.NotFoundException) OkapiConnectionParams(org.folio.dataimport.util.OkapiConnectionParams) HTTP_OK(org.folio.HttpStatus.HTTP_OK) Buffer(io.vertx.core.buffer.Buffer) SourceStorageSourceRecordsClient(org.folio.rest.client.SourceStorageSourceRecordsClient) Log4j2(lombok.extern.log4j.Log4j2) KafkaHeaderUtils(org.folio.kafka.KafkaHeaderUtils) SourceRecordState(org.folio.rest.jaxrs.model.SourceRecordState) SourceRecordState(org.folio.rest.jaxrs.model.SourceRecordState) ParsedRecordDto(org.folio.rest.jaxrs.model.ParsedRecordDto) SourceStorageSourceRecordsClient(org.folio.rest.client.SourceStorageSourceRecordsClient) NotFoundException(javax.ws.rs.NotFoundException) NotFoundException(javax.ws.rs.NotFoundException)

Example 3 with Try

use of org.folio.dataimport.util.Try in project mod-source-record-manager by folio-org.

the class JobExecutionServiceImpl method updateJobExecutionStatus.

@Override
public Future<JobExecution> updateJobExecutionStatus(String jobExecutionId, StatusDto status, OkapiConnectionParams params) {
    if (JobExecution.Status.PARENT.name().equals(status.getStatus().name())) {
        String errorMessage = "Cannot update JobExecution status to PARENT";
        LOGGER.error(errorMessage);
        return Future.failedFuture(new BadRequestException(errorMessage));
    } else {
        return jobExecutionDao.updateBlocking(jobExecutionId, jobExecution -> {
            Promise<JobExecution> promise = Promise.promise();
            try {
                if (JobExecution.Status.PARENT.name().equals(jobExecution.getStatus().name())) {
                    String message = format("JobExecution %s current status is PARENT and cannot be updated", jobExecutionId);
                    LOGGER.error(message);
                    promise.fail(new BadRequestException(message));
                } else {
                    jobExecution.setStatus(JobExecution.Status.fromValue(status.getStatus().name()));
                    jobExecution.setUiStatus(JobExecution.UiStatus.fromValue(Status.valueOf(status.getStatus().name()).getUiStatus()));
                    updateJobExecutionIfErrorExist(status, jobExecution);
                    promise.complete(jobExecution);
                }
            } catch (Exception e) {
                String errorMessage = "Error updating JobExecution with id " + jobExecutionId;
                LOGGER.error(errorMessage, e);
                promise.fail(errorMessage);
            }
            return promise.future();
        }, params.getTenantId()).compose(jobExecution -> updateSnapshotStatus(jobExecution, params));
    }
}
Also used : DataImportProfilesClient(org.folio.rest.client.DataImportProfilesClient) Arrays(java.util.Arrays) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) UserInfo(org.folio.rest.jaxrs.model.UserInfo) ProfileSnapshotWrapper(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper) JobDuplicateUpdateException(org.folio.services.exceptions.JobDuplicateUpdateException) JobExecutionSourceChunkDao(org.folio.dao.JobExecutionSourceChunkDao) JobExecution(org.folio.rest.jaxrs.model.JobExecution) JobProfileInfo(org.folio.rest.jaxrs.model.JobProfileInfo) InitJobExecutionsRsDto(org.folio.rest.jaxrs.model.InitJobExecutionsRsDto) ERROR(org.folio.rest.jaxrs.model.StatusDto.Status.ERROR) JsonObject(io.vertx.core.json.JsonObject) BadRequestException(javax.ws.rs.BadRequestException) HttpException(io.vertx.ext.web.handler.HttpException) GenericCompositeFuture(org.folio.okapi.common.GenericCompositeFuture) StatusDto(org.folio.rest.jaxrs.model.StatusDto) UUID(java.util.UUID) Future(io.vertx.core.Future) Try(org.folio.dataimport.util.Try) OkapiConnectionParams(org.folio.dataimport.util.OkapiConnectionParams) NotFoundException(javax.ws.rs.NotFoundException) String.format(java.lang.String.format) Objects(java.util.Objects) List(java.util.List) HTTP_OK(org.folio.HttpStatus.HTTP_OK) Logger(org.apache.logging.log4j.Logger) JobExecutionFilter(org.folio.dao.JobExecutionFilter) JobExecutionDtoCollection(org.folio.rest.jaxrs.model.JobExecutionDtoCollection) HTTP_CREATED(org.folio.HttpStatus.HTTP_CREATED) Optional(java.util.Optional) RestUtil(org.folio.dataimport.util.RestUtil) JobProfile(org.folio.rest.jaxrs.model.JobProfile) FilenameUtils(org.apache.commons.io.FilenameUtils) Progress(org.folio.rest.jaxrs.model.Progress) JobExecutionDao(org.folio.dao.JobExecutionDao) CANCELLED(org.folio.rest.jaxrs.model.StatusDto.Status.CANCELLED) DeleteJobExecutionsResp(org.folio.rest.jaxrs.model.DeleteJobExecutionsResp) ArrayList(java.util.ArrayList) SourceStorageSnapshotsClient(org.folio.rest.client.SourceStorageSnapshotsClient) Service(org.springframework.stereotype.Service) InitJobExecutionsRqDto(org.folio.rest.jaxrs.model.InitJobExecutionsRqDto) Promise(io.vertx.core.Promise) COMMITTED(org.folio.rest.jaxrs.model.JobExecution.Status.COMMITTED) RunBy(org.folio.rest.jaxrs.model.RunBy) File(org.folio.rest.jaxrs.model.File) SortField(org.folio.dao.util.SortField) HttpMethod(io.vertx.core.http.HttpMethod) JobExecutionUserInfoCollection(org.folio.rest.jaxrs.model.JobExecutionUserInfoCollection) JobProfileInfoCollection(org.folio.rest.jaxrs.model.JobProfileInfoCollection) HttpStatus(org.folio.HttpStatus) PROFILE_SNAPSHOT_CREATING_ERROR(org.folio.rest.jaxrs.model.StatusDto.ErrorStatus.PROFILE_SNAPSHOT_CREATING_ERROR) LogManager(org.apache.logging.log4j.LogManager) Snapshot(org.folio.rest.jaxrs.model.Snapshot) Collections(java.util.Collections) Promise(io.vertx.core.Promise) BadRequestException(javax.ws.rs.BadRequestException) JobDuplicateUpdateException(org.folio.services.exceptions.JobDuplicateUpdateException) BadRequestException(javax.ws.rs.BadRequestException) HttpException(io.vertx.ext.web.handler.HttpException) NotFoundException(javax.ws.rs.NotFoundException)

Aggregations

Future (io.vertx.core.Future)3 Promise (io.vertx.core.Promise)3 JsonObject (io.vertx.core.json.JsonObject)3 String.format (java.lang.String.format)3 UUID (java.util.UUID)3 NotFoundException (javax.ws.rs.NotFoundException)3 HTTP_OK (org.folio.HttpStatus.HTTP_OK)3 OkapiConnectionParams (org.folio.dataimport.util.OkapiConnectionParams)3 Try (org.folio.dataimport.util.Try)3 HttpMethod (io.vertx.core.http.HttpMethod)2 HttpException (io.vertx.ext.web.handler.HttpException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Date (java.util.Date)2 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 BadRequestException (javax.ws.rs.BadRequestException)2 FilenameUtils (org.apache.commons.io.FilenameUtils)2