Search in sources :

Example 1 with Holdings

use of org.folio.Holdings in project mod-inventory by folio-org.

the class CreateMarcHoldingsEventHandler method handle.

@Override
public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload dataImportEventPayload) {
    CompletableFuture<DataImportEventPayload> future = new CompletableFuture<>();
    try {
        dataImportEventPayload.setEventType(DI_INVENTORY_HOLDING_CREATED.value());
        HashMap<String, String> payloadContext = dataImportEventPayload.getContext();
        if (payloadContext == null || payloadContext.isEmpty() || StringUtils.isEmpty(payloadContext.get(MARC_HOLDINGS.value()))) {
            return CompletableFuture.failedFuture(new EventProcessingException(CONTEXT_EMPTY_ERROR_MESSAGE));
        }
        if (dataImportEventPayload.getCurrentNode().getChildSnapshotWrappers().isEmpty()) {
            LOGGER.error(ACTION_HAS_NO_MAPPING_MSG);
            return CompletableFuture.failedFuture(new EventProcessingException(ACTION_HAS_NO_MAPPING_MSG));
        }
        Context context = constructContext(dataImportEventPayload.getTenant(), dataImportEventPayload.getToken(), dataImportEventPayload.getOkapiUrl());
        Record targetRecord = new JsonObject(payloadContext.get(EntityType.MARC_HOLDINGS.value())).mapTo(Record.class);
        prepareEvent(dataImportEventPayload);
        String jobExecutionId = dataImportEventPayload.getJobExecutionId();
        String recordId = payloadContext.get(RECORD_ID_HEADER);
        String chunkId = payloadContext.get(CHUNK_ID_HEADER);
        Future<RecordToEntity> recordToHoldingsFuture = idStorageService.store(targetRecord.getId(), UUID.randomUUID().toString(), dataImportEventPayload.getTenant());
        recordToHoldingsFuture.onSuccess(res -> {
            String holdingsId = res.getEntityId();
            mappingMetadataCache.get(jobExecutionId, context).map(parametersOptional -> parametersOptional.orElseThrow(() -> new EventProcessingException(format(MAPPING_METADATA_NOT_FOUND_MSG, jobExecutionId, recordId, chunkId)))).onSuccess(mappingMetadata -> defaultMapRecordToHoldings(dataImportEventPayload, mappingMetadata)).map(v -> processMappingResult(dataImportEventPayload, holdingsId)).compose(holdingJson -> findInstanceIdByHrid(dataImportEventPayload, holdingJson, context).compose(instanceId -> {
                fillInstanceId(dataImportEventPayload, holdingJson, instanceId);
                var holdingsRecords = storage.getHoldingsRecordCollection(context);
                HoldingsRecord holding = Json.decodeValue(payloadContext.get(HOLDINGS.value()), HoldingsRecord.class);
                return addHoldings(holding, holdingsRecords);
            })).onSuccess(createdHoldings -> {
                LOGGER.info("Created Holding record by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}' ", jobExecutionId, recordId, chunkId);
                dataImportEventPayload.getContext().put(HOLDINGS.value(), Json.encodePrettily(createdHoldings));
                future.complete(dataImportEventPayload);
            }).onFailure(e -> {
                if (!(e instanceof DuplicateEventException)) {
                    LOGGER.error("Error creating Holding by jobExecutionId: '{}' and recordId: '{}' and chunkId: '{}' ", jobExecutionId, recordId, chunkId, e);
                }
                future.completeExceptionally(e);
            });
        }).onFailure(failure -> {
            LOGGER.error(format(CREATING_INVENTORY_RELATIONSHIP_ERROR_MESSAGE, jobExecutionId, recordId, chunkId), failure);
            future.completeExceptionally(failure);
        });
    } catch (Exception e) {
        LOGGER.error("Failed to create Holdings", e);
        future.completeExceptionally(e);
    }
    return future;
}
Also used : Context(org.folio.inventory.common.Context) EventHandlingUtil.constructContext(org.folio.inventory.dataimport.handlers.matching.util.EventHandlingUtil.constructContext) MappingMetadataDto(org.folio.MappingMetadataDto) Context(org.folio.inventory.common.Context) Json(io.vertx.core.json.Json) EventHandler(org.folio.processing.events.services.handler.EventHandler) DI_INVENTORY_HOLDING_CREATED(org.folio.DataImportEventTypes.DI_INVENTORY_HOLDING_CREATED) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ACTION_PROFILE(org.folio.rest.jaxrs.model.ProfileSnapshotWrapper.ContentType.ACTION_PROFILE) StringUtils(org.apache.commons.lang3.StringUtils) JsonMappingException(org.folio.inventory.validation.exceptions.JsonMappingException) RecordMapperBuilder(org.folio.processing.mapping.defaultmapper.RecordMapperBuilder) EventHandlingUtil.constructContext(org.folio.inventory.dataimport.handlers.matching.util.EventHandlingUtil.constructContext) HOLDINGS(org.folio.ActionProfile.FolioRecord.HOLDINGS) DI_INVENTORY_HOLDINGS_CREATED_READY_FOR_POST_PROCESSING(org.folio.DataImportEventTypes.DI_INVENTORY_HOLDINGS_CREATED_READY_FOR_POST_PROCESSING) ParsedRecordUtil.getControlFieldValue(org.folio.inventory.dataimport.util.ParsedRecordUtil.getControlFieldValue) StringUtils.isBlank(org.apache.commons.lang.StringUtils.isBlank) HoldingsRecordCollection(org.folio.inventory.domain.HoldingsRecordCollection) UNIQUE_ID_ERROR_MESSAGE(org.folio.inventory.dataimport.util.DataImportConstants.UNIQUE_ID_ERROR_MESSAGE) EntityType(org.folio.rest.jaxrs.model.EntityType) IdStorageService(org.folio.inventory.services.IdStorageService) JsonObject(io.vertx.core.json.JsonObject) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) ActionProfile(org.folio.ActionProfile) RecordToEntity(org.folio.inventory.domain.relationship.RecordToEntity) RecordMapper(org.folio.processing.mapping.defaultmapper.RecordMapper) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) Holdings(org.folio.Holdings) DuplicateEventException(org.folio.kafka.exception.DuplicateEventException) Record(org.folio.rest.jaxrs.model.Record) Promise(io.vertx.core.Promise) DataImportEventPayload(org.folio.DataImportEventPayload) MARC_HOLDINGS(org.folio.ActionProfile.FolioRecord.MARC_HOLDINGS) UUID(java.util.UUID) HoldingsRecord(org.folio.HoldingsRecord) Future(io.vertx.core.Future) String.format(java.lang.String.format) StringUtils.isNotBlank(org.apache.commons.lang.StringUtils.isNotBlank) Storage(org.folio.inventory.storage.Storage) Logger(org.apache.logging.log4j.Logger) EventProcessingException(org.folio.processing.exceptions.EventProcessingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) LogManager(org.apache.logging.log4j.LogManager) PagingParameters(org.folio.inventory.common.api.request.PagingParameters) MappingMetadataCache(org.folio.inventory.dataimport.cache.MappingMetadataCache) DuplicateEventException(org.folio.kafka.exception.DuplicateEventException) JsonObject(io.vertx.core.json.JsonObject) RecordToEntity(org.folio.inventory.domain.relationship.RecordToEntity) JsonMappingException(org.folio.inventory.validation.exceptions.JsonMappingException) DuplicateEventException(org.folio.kafka.exception.DuplicateEventException) EventProcessingException(org.folio.processing.exceptions.EventProcessingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DataImportEventPayload(org.folio.DataImportEventPayload) HoldingsRecord(org.folio.HoldingsRecord) CompletableFuture(java.util.concurrent.CompletableFuture) Record(org.folio.rest.jaxrs.model.Record) HoldingsRecord(org.folio.HoldingsRecord) EventProcessingException(org.folio.processing.exceptions.EventProcessingException)

Example 2 with Holdings

use of org.folio.Holdings in project mod-inventory by folio-org.

the class HoldingsUpdateDelegate method handle.

public Future<HoldingsRecord> handle(Map<String, String> eventPayload, Record marcRecord, Context context) {
    try {
        JsonObject mappingRules = new JsonObject(eventPayload.get(MAPPING_RULES_KEY));
        MappingParameters mappingParameters = new JsonObject(eventPayload.get(MAPPING_PARAMS_KEY)).mapTo(MappingParameters.class);
        JsonObject parsedRecord = retrieveParsedContent(marcRecord.getParsedRecord());
        String holdingsId = marcRecord.getExternalIdsHolder().getHoldingsId();
        RecordMapper<Holdings> recordMapper = RecordMapperBuilder.buildMapper(MARC_FORMAT);
        var mappedHoldings = recordMapper.mapRecord(parsedRecord, mappingParameters, mappingRules);
        HoldingsRecordCollection holdingsRecordCollection = storage.getHoldingsRecordCollection(context);
        return getHoldingsRecordById(holdingsId, holdingsRecordCollection).onSuccess(existingHoldingsRecord -> fillVersion(existingHoldingsRecord, eventPayload)).compose(existingHoldingsRecord -> mergeRecords(existingHoldingsRecord, mappedHoldings)).compose(updatedHoldingsRecord -> updateHoldingsRecord(updatedHoldingsRecord, holdingsRecordCollection));
    } catch (Exception e) {
        LOGGER.error("Error updating inventory holdings", e);
        return Future.failedFuture(e);
    }
}
Also used : Context(org.folio.inventory.common.Context) Holdings(org.folio.Holdings) Record(org.folio.rest.jaxrs.model.Record) Promise(io.vertx.core.Promise) HttpStatus(org.apache.http.HttpStatus) Future(io.vertx.core.Future) HoldingsRecord(org.folio.HoldingsRecord) String.format(java.lang.String.format) RecordMapperBuilder(org.folio.processing.mapping.defaultmapper.RecordMapperBuilder) Storage(org.folio.inventory.storage.Storage) Logger(org.apache.logging.log4j.Logger) HoldingsRecordCollection(org.folio.inventory.domain.HoldingsRecordCollection) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) OptimisticLockingException(org.folio.inventory.dataimport.exceptions.OptimisticLockingException) LogManager(org.apache.logging.log4j.LogManager) ParsedRecord(org.folio.rest.jaxrs.model.ParsedRecord) RecordMapper(org.folio.processing.mapping.defaultmapper.RecordMapper) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) HoldingsRecordCollection(org.folio.inventory.domain.HoldingsRecordCollection) Holdings(org.folio.Holdings) JsonObject(io.vertx.core.json.JsonObject) MappingParameters(org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters) OptimisticLockingException(org.folio.inventory.dataimport.exceptions.OptimisticLockingException)

Example 3 with Holdings

use of org.folio.Holdings in project mod-inventory by folio-org.

the class CreateMarcHoldingsEventHandler method defaultMapRecordToHoldings.

private void defaultMapRecordToHoldings(DataImportEventPayload dataImportEventPayload, MappingMetadataDto mappingMetadata) {
    try {
        HashMap<String, String> context = dataImportEventPayload.getContext();
        var mappingRules = new JsonObject(mappingMetadata.getMappingRules());
        var parsedRecord = new JsonObject((String) new JsonObject(context.get(MARC_HOLDINGS.value())).mapTo(Record.class).getParsedRecord().getContent());
        var mappingParameters = Json.decodeValue(mappingMetadata.getMappingParams(), MappingParameters.class);
        RecordMapper<Holdings> recordMapper = RecordMapperBuilder.buildMapper(MARC_FORMAT);
        var holdings = recordMapper.mapRecord(parsedRecord, mappingParameters, mappingRules);
        dataImportEventPayload.getContext().put(HOLDINGS.value(), Json.encode(new JsonObject().put(HOLDINGS_PATH, JsonObject.mapFrom(holdings))));
    } catch (Exception e) {
        LOGGER.error("Failed to map Record to Holdings", e);
        throw new JsonMappingException("Error in default mapper.", e);
    }
}
Also used : Holdings(org.folio.Holdings) JsonMappingException(org.folio.inventory.validation.exceptions.JsonMappingException) JsonObject(io.vertx.core.json.JsonObject) JsonMappingException(org.folio.inventory.validation.exceptions.JsonMappingException) DuplicateEventException(org.folio.kafka.exception.DuplicateEventException) EventProcessingException(org.folio.processing.exceptions.EventProcessingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)3 Holdings (org.folio.Holdings)3 Future (io.vertx.core.Future)2 Promise (io.vertx.core.Promise)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 String.format (java.lang.String.format)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 HoldingsRecord (org.folio.HoldingsRecord)2 Context (org.folio.inventory.common.Context)2 HoldingsRecordCollection (org.folio.inventory.domain.HoldingsRecordCollection)2 Storage (org.folio.inventory.storage.Storage)2 RecordMapper (org.folio.processing.mapping.defaultmapper.RecordMapper)2 RecordMapperBuilder (org.folio.processing.mapping.defaultmapper.RecordMapperBuilder)2 MappingParameters (org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters)2 Record (org.folio.rest.jaxrs.model.Record)2 Json (io.vertx.core.json.Json)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UUID (java.util.UUID)1