diff --git a/src/main/java/org/folio/inventory/dataimport/util/AdditionalFieldsUtil.java b/src/main/java/org/folio/inventory/dataimport/util/AdditionalFieldsUtil.java index 2676ea120..94da11dab 100644 --- a/src/main/java/org/folio/inventory/dataimport/util/AdditionalFieldsUtil.java +++ b/src/main/java/org/folio/inventory/dataimport/util/AdditionalFieldsUtil.java @@ -68,12 +68,11 @@ public final class AdditionalFieldsUtil { public static final String TAG_001 = "001"; private static final String TAG_003 = "003"; public static final String TAG_035 = "035"; - public static final char SUBFIELD_A = 'a'; - public static final char SUBFIELD_B = 'b'; - public static final char SUBFIELD_I = 'i'; + public static final char TAG_035_SUB = 'a'; private static final char TAG_035_IND = ' '; private static final String ANY_STRING = "*"; private static final char INDICATOR = 'f'; + public static final char SUBFIELD_I = 'i'; private static final String HR_ID_FIELD = "hrid"; private static final CacheLoader parsedRecordContentCacheLoader; private static final LoadingCache parsedRecordContentCache; @@ -263,15 +262,17 @@ public static void move001To035(Record srcRecord) { if (StringUtils.isNotEmpty(valueFrom001)) { String valueFrom003 = getValueFromControlledField(srcRecord, TAG_003); String new035Value = mergeFieldsFor035(valueFrom003, valueFrom001); - if (!isFieldExist(srcRecord, TAG_035, SUBFIELD_A, new035Value)) { - addDataFieldToMarcRecord(srcRecord, TAG_035, TAG_035_IND, TAG_035_IND, SUBFIELD_A, new035Value); + if (!isFieldExist(srcRecord, TAG_035, TAG_035_SUB, new035Value)) { + addDataFieldToMarcRecord(srcRecord, TAG_035, TAG_035_IND, TAG_035_IND, + TAG_035_SUB, new035Value); } } removeField(srcRecord, TAG_003); } public static void normalize035(Record srsRecord) { - List subfields = get035SubfieldOclcValues(srsRecord, TAG_035, SUBFIELD_A); + List subfields = get035SubfieldOclcValues(srsRecord, TAG_035, + TAG_035_SUB); if (!subfields.isEmpty()) { Set normalized035Subfields = formatOclc(subfields); @@ -739,7 +740,7 @@ public static void remove035FieldWhenRecordContainsHrId(Record srcRecord) { } public static void remove035WithActualHrId(Record srcRecord, String actualHrId) { - removeField(srcRecord, TAG_035, SUBFIELD_A, actualHrId); + removeField(srcRecord, TAG_035, TAG_035_SUB, actualHrId); } /** @@ -755,9 +756,9 @@ public static void fillHrIdFieldInMarcRecord(Pair recordInst String originalHrIdPrefix = AdditionalFieldsUtil.getValueFromControlledField(recordInstancePair.getKey(), TAG_003); String originalHrId = AdditionalFieldsUtil.mergeFieldsFor035(originalHrIdPrefix, valueFrom001); if (!AdditionalFieldsUtil.isFieldExist(recordInstancePair.getKey(), TAG_035, - SUBFIELD_A, originalHrId)) { + TAG_035_SUB, originalHrId)) { AdditionalFieldsUtil.addDataFieldToMarcRecord(recordInstancePair.getKey(), TAG_035, TAG_035_IND, TAG_035_IND, - SUBFIELD_A, originalHrId); + TAG_035_SUB, originalHrId); } } AdditionalFieldsUtil.removeField(recordInstancePair.getKey(), TAG_001); diff --git a/src/main/java/org/folio/inventory/instanceingress/handler/CreateInstanceIngressEventHandler.java b/src/main/java/org/folio/inventory/instanceingress/handler/CreateInstanceIngressEventHandler.java index a13f13292..db94c27b5 100644 --- a/src/main/java/org/folio/inventory/instanceingress/handler/CreateInstanceIngressEventHandler.java +++ b/src/main/java/org/folio/inventory/instanceingress/handler/CreateInstanceIngressEventHandler.java @@ -2,19 +2,13 @@ import static java.lang.String.format; import static java.util.Objects.isNull; -import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.SUBFIELD_A; -import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.SUBFIELD_B; -import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.SUBFIELD_I; import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.TAG_035; -import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.TAG_999; +import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.TAG_035_SUB; import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.reorderMarcRecordFields; import static org.folio.inventory.dataimport.util.MappingConstants.INSTANCE_REQUIRED_FIELDS; import static org.folio.inventory.dataimport.util.MappingConstants.MARC_BIB_RECORD_FORMAT; import static org.folio.inventory.dataimport.util.MappingConstants.MARC_BIB_RECORD_TYPE; -import static org.folio.inventory.domain.instances.Instance.INSTANCE_TYPE_ID_KEY; -import static org.folio.inventory.domain.instances.Instance.SOURCE_KEY; import static org.folio.rest.jaxrs.model.EntityType.MARC_BIBLIOGRAPHIC; -import static org.folio.rest.jaxrs.model.InstanceIngressPayload.SourceType; import static org.folio.rest.jaxrs.model.Record.RecordType.MARC_BIB; import static org.folio.rest.jaxrs.model.Snapshot.Status.PROCESSING_IN_PROGRESS; @@ -27,7 +21,6 @@ import java.util.List; import java.util.Optional; import java.util.concurrent.CompletableFuture; -import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.folio.HttpStatus; @@ -57,7 +50,7 @@ public class CreateInstanceIngressEventHandler extends CreateInstanceEventHandler implements InstanceIngressEventHandler { private static final Logger LOGGER = LogManager.getLogger(CreateInstanceIngressEventHandler.class); - private static final String BIBFRAME = " (bibframe)"; + private static final String BIBFRAME = "(bibframe) "; private final Context context; private final InstanceCollection instanceCollection; @@ -99,36 +92,23 @@ private Future prepareAndExecuteMapping(MappingMetadataDto m return postSnapshotInSrsAndHandleResponse(event.getId()) .compose(snapshot -> { try { - LOGGER.info("Constructing a Record from InstanceIngressEvent with id '{}'", event.getId()); - var marcBibRecord = new org.folio.rest.jaxrs.model.Record() - .withId(event.getId()) - .withRecordType(MARC_BIB) - .withSnapshotId(event.getId()) - .withRawRecord(new RawRecord() - .withId(event.getId()) - .withContent(event.getEventPayload().getSourceRecordObject()) - ) - .withParsedRecord(new ParsedRecord() - .withId(event.getId()) - .withContent(event.getEventPayload().getSourceRecordObject()) - ); - event.getEventPayload() - .withAdditionalProperty(MARC_BIBLIOGRAPHIC.value(), marcBibRecord); + var marcBibRecord = constructMarcBibRecord(event); LOGGER.info("Manipulating fields of a Record from InstanceIngressEvent with id '{}'", event.getId()); var mappingParameters = Json.decodeValue(mappingMetadata.getMappingParams(), MappingParameters.class); AdditionalFieldsUtil.updateLatestTransactionDate(marcBibRecord, mappingParameters); AdditionalFieldsUtil.move001To035(marcBibRecord); AdditionalFieldsUtil.normalize035(marcBibRecord); - AdditionalFieldsUtil.addFieldToMarcRecord(marcBibRecord, TAG_035, SUBFIELD_A, event.getEventPayload().getSourceRecordIdentifier() + BIBFRAME); + AdditionalFieldsUtil.addFieldToMarcRecord(marcBibRecord, TAG_035, TAG_035_SUB, + BIBFRAME + event.getEventPayload().getSourceRecordIdentifier()); LOGGER.info("Mapping a Record from InstanceIngressEvent with id '{}' into an Instance", event.getId()); var parsedRecord = new JsonObject((String) marcBibRecord.getParsedRecord().getContent()); RecordMapper recordMapper = RecordMapperBuilder.buildMapper(MARC_BIB_RECORD_FORMAT); var instance = recordMapper.mapRecord(parsedRecord, mappingParameters, new JsonObject(mappingMetadata.getMappingRules())); + instance.setSource(event.getEventPayload().getSourceType().value()); LOGGER.info("Mapped Instance from InstanceIngressEvent with id '{}': {}", event.getId(), instance); - return super.idStorageService.store(event.getId(), instance.getId(), - context.getTenantId()) + return super.idStorageService.store(marcBibRecord.getId(), instance.getId(), context.getTenantId()) .map(r -> instance) .onFailure(e -> LOGGER.error("Error creating relationship of inventory recordId '{} and instanceId '{}'", event.getId(), instance.getId())); } catch (Exception e) { @@ -138,10 +118,29 @@ private Future prepareAndExecuteMapping(MappingMetadataDto m }); } + private Record constructMarcBibRecord(InstanceIngressEvent event) { + LOGGER.info("Constructing a Record from InstanceIngressEvent with id '{}'", event.getId()); + var marcBibRecord = new org.folio.rest.jaxrs.model.Record() + .withId(event.getId()) + .withRecordType(MARC_BIB) + .withSnapshotId(event.getId()) + .withRawRecord(new RawRecord() + .withId(event.getId()) + .withContent(event.getEventPayload().getSourceRecordObject()) + ) + .withParsedRecord(new ParsedRecord() + .withId(event.getId()) + .withContent(event.getEventPayload().getSourceRecordObject()) + ); + event.getEventPayload() + .withAdditionalProperty(MARC_BIBLIOGRAPHIC.value(), marcBibRecord); + return marcBibRecord; + } + private Future validateInstance(org.folio.Instance instance, InstanceIngressEvent event) { try { LOGGER.info("Validating Instance from InstanceIngressEvent with id '{}':", event.getId()); - var instanceAsJson = prepareInstance(instance, event.getEventPayload().getSourceType()); + var instanceAsJson = JsonObject.mapFrom(instance); var errors = EventHandlingUtil.validateJsonByRequiredFields(instanceAsJson, INSTANCE_REQUIRED_FIELDS); return failIfErrors(errors, event.getId()) .orElseGet(() -> { @@ -164,24 +163,13 @@ private static Optional> failIfErrors(List errors, Stri return Optional.empty(); } - private JsonObject prepareInstance(org.folio.Instance instance, SourceType sourceType) { - var instanceAsJson = JsonObject.mapFrom(instance); - instanceAsJson.put(SOURCE_KEY, sourceType.value()); - if (isNull(instanceAsJson.getString(INSTANCE_TYPE_ID_KEY))) { - //q is that ok? - LOGGER.warn("No instance type provided in mapped instance with id '{}', setting 'unspecified'", instance.getId()); - instanceAsJson.put(INSTANCE_TYPE_ID_KEY, "30fffe0e-e985-4144-b2e2-1e8179bdb41f"); - } - return instanceAsJson; - } - private Future saveInstance(Instance instance, InstanceIngressEvent event) { - LOGGER.info("Saving Instance from InstanceIngressEvent with id '{}':", event.getId()); + LOGGER.info("Saving Instance from InstanceIngressEvent with id '{}': {}", event.getId(), instance); var targetRecord = (Record) event.getEventPayload().getAdditionalProperties().get(MARC_BIBLIOGRAPHIC.value()); var sourceContent = targetRecord.getParsedRecord().getContent().toString(); super.addInstance(instance, instanceCollection) .compose(createdInstance -> getPrecedingSucceedingTitlesHelper().createPrecedingSucceedingTitles(instance, context).map(createdInstance)) - .compose(createdInstance -> executeFieldsManipulation(createdInstance, targetRecord, event)) + .compose(createdInstance -> executeFieldsManipulation(createdInstance, targetRecord)) .compose(createdInstance -> { var targetContent = targetRecord.getParsedRecord().getContent().toString(); var content = reorderMarcRecordFields(sourceContent, targetContent); @@ -191,21 +179,6 @@ private Future saveInstance(Instance instance, InstanceIngressEvent event) return Future.succeededFuture(); } - private Future executeFieldsManipulation(Instance instance, Record srcRecord, InstanceIngressEvent event) { - LOGGER.info("executeFieldsManipulation for an Instance with id '{}':", instance.getId()); - AdditionalFieldsUtil.fill001FieldInMarcRecord(srcRecord, instance.getHrid()); - if (StringUtils.isBlank(srcRecord.getMatchedId())) { - srcRecord.setMatchedId(srcRecord.getId()); - } - super.setExternalIds(srcRecord, instance); - boolean bibframeIdSetTo999 = AdditionalFieldsUtil.addFieldToMarcRecord(srcRecord, TAG_999, SUBFIELD_B, - event.getEventPayload().getSourceRecordIdentifier()); - boolean instanceIdSetTo999 = AdditionalFieldsUtil.addFieldToMarcRecord(srcRecord, TAG_999, SUBFIELD_I, instance.getId()); - return bibframeIdSetTo999 && instanceIdSetTo999 - ? Future.succeededFuture(instance) - : Future.failedFuture(format("Failed to add instance id '%s' to record with id '%s'", instance.getId(), srcRecord.getId())); - } - protected Future saveRecordInSrsAndHandleResponse(InstanceIngressEvent event, Record srcRecord, Instance instance) { LOGGER.info("Saving record in SRS and handling a response for an Instance with id '{}':", instance.getId()); Promise promise = Promise.promise(); @@ -229,7 +202,6 @@ protected Future saveRecordInSrsAndHandleResponse(InstanceIngressEvent } private Future postSnapshotInSrsAndHandleResponse(String id) { - //q is that ok? var snapshot = new Snapshot() .withJobExecutionId(id) .withProcessingStartedDate(new Date()) diff --git a/src/test/java/org/folio/inventory/dataimport/util/AdditionalFieldsUtilTest.java b/src/test/java/org/folio/inventory/dataimport/util/AdditionalFieldsUtilTest.java index 01e2447c7..0b145639a 100644 --- a/src/test/java/org/folio/inventory/dataimport/util/AdditionalFieldsUtilTest.java +++ b/src/test/java/org/folio/inventory/dataimport/util/AdditionalFieldsUtilTest.java @@ -1,9 +1,9 @@ package org.folio.inventory.dataimport.util; -import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.SUBFIELD_A; import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.TAG_001; import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.TAG_005; import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.TAG_035; +import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.TAG_035_SUB; import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.TAG_999; import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.addControlledFieldToMarcRecord; import static org.folio.inventory.dataimport.util.AdditionalFieldsUtil.addDataFieldToMarcRecord; @@ -483,7 +483,7 @@ public void shouldReturnSubfieldIfOclcExist() { .withExternalIdsHolder(new ExternalIdsHolder().withInstanceId("001").withInstanceHrid("in001")); // when - var subfields = get035SubfieldOclcValues(record, TAG_035, SUBFIELD_A).stream().map(Subfield::getData).toList(); + var subfields = get035SubfieldOclcValues(record, TAG_035, TAG_035_SUB).stream().map(Subfield::getData).toList(); // then Assert.assertEquals(expectedSubfields.size(), subfields.size()); Assert.assertEquals(expectedSubfields.get(0), subfields.get(0)); @@ -504,7 +504,7 @@ public void shouldNotReturnSubfieldIfOclcNotExist() { .withExternalIdsHolder(new ExternalIdsHolder().withInstanceId("001").withInstanceHrid("in001")); // when - var subfields = get035SubfieldOclcValues(record, TAG_035, SUBFIELD_A).stream().map(Subfield::getData).toList(); + var subfields = get035SubfieldOclcValues(record, TAG_035, TAG_035_SUB).stream().map(Subfield::getData).toList(); // then Assert.assertEquals(0, subfields.size()); }