Skip to content

Commit

Permalink
MODINV-986: fix instance source mapped to MARC, code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
PBobylev committed Jun 5, 2024
1 parent ed195ba commit 9faf9a6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, org.marc4j.marc.Record> parsedRecordContentCacheLoader;
private static final LoadingCache<String, org.marc4j.marc.Record> parsedRecordContentCache;
Expand Down Expand Up @@ -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<Subfield> subfields = get035SubfieldOclcValues(srsRecord, TAG_035, SUBFIELD_A);
List<Subfield> subfields = get035SubfieldOclcValues(srsRecord, TAG_035,
TAG_035_SUB);
if (!subfields.isEmpty()) {
Set<String> normalized035Subfields = formatOclc(subfields);

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -755,9 +756,9 @@ public static void fillHrIdFieldInMarcRecord(Pair<Record, JsonObject> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -99,36 +92,23 @@ private Future<org.folio.Instance> 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<org.folio.Instance> 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) {
Expand All @@ -138,10 +118,29 @@ private Future<org.folio.Instance> 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<Instance> 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(() -> {
Expand All @@ -164,24 +163,13 @@ private static Optional<Future<Instance>> failIfErrors(List<String> 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<Void> 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);
Expand All @@ -191,21 +179,6 @@ private Future<Void> saveInstance(Instance instance, InstanceIngressEvent event)
return Future.succeededFuture();
}

private Future<Instance> 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<Instance> 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<Instance> promise = Promise.promise();
Expand All @@ -229,7 +202,6 @@ protected Future<Instance> saveRecordInSrsAndHandleResponse(InstanceIngressEvent
}

private Future<Snapshot> postSnapshotInSrsAndHandleResponse(String id) {
//q is that ok?
var snapshot = new Snapshot()
.withJobExecutionId(id)
.withProcessingStartedDate(new Date())
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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());
}
Expand Down

0 comments on commit 9faf9a6

Please sign in to comment.