Skip to content

Commit

Permalink
MODDATAIMP-957: set external IDs before Record creation in SRS (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslav-epam committed Jan 15, 2024
1 parent 9ffdb9b commit 44fff6d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.folio.processing.mapping.mapper.MappingContext;
import org.folio.rest.client.SourceStorageRecordsClient;
import org.folio.rest.jaxrs.model.EntityType;
import org.folio.rest.jaxrs.model.ExternalIdsHolder;
import org.folio.rest.jaxrs.model.ParsedRecord;
import org.folio.rest.jaxrs.model.Record;

Expand All @@ -39,6 +40,7 @@

import static java.lang.String.format;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.apache.commons.lang.StringUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.folio.ActionProfile.Action.CREATE;
import static org.folio.ActionProfile.FolioRecord.INSTANCE;
Expand Down Expand Up @@ -132,7 +134,7 @@ public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload d
Instance mappedInstance = Instance.fromJson(instanceAsJson);
return addInstance(mappedInstance, instanceCollection)
.compose(createdInstance -> precedingSucceedingTitlesHelper.createPrecedingSucceedingTitles(mappedInstance, context).map(createdInstance))
.compose(createdInstance -> fill001And999IFieldsAndSetMatchedId(createdInstance, targetRecord))
.compose(createdInstance -> executeFieldsManipulation(createdInstance, targetRecord))
.compose(createdInstance -> saveRecordInSrsAndHandleResponse(dataImportEventPayload, targetRecord, createdInstance, instanceCollection));
})
.onSuccess(ar -> {
Expand Down Expand Up @@ -160,16 +162,43 @@ public CompletableFuture<DataImportEventPayload> handle(DataImportEventPayload d
return future;
}

private Future<Instance> fill001And999IFieldsAndSetMatchedId(Instance instance, Record record) {
private Future<Instance> executeFieldsManipulation(Instance instance, Record record) {
AdditionalFieldsUtil.fill001FieldInMarcRecord(record, instance.getHrid());
if (StringUtils.isBlank(record.getMatchedId())) {
record.setMatchedId(record.getId());
}
setExternalIds(record, instance);
return AdditionalFieldsUtil.addFieldToMarcRecord(record, TAG_999, 'i', instance.getId())
? Future.succeededFuture(instance)
: Future.failedFuture(format("Failed to add instance id '%s' to record with id '%s'", instance.getId(), record.getId()));
}

/**
* Adds specified externalId and externalHrid to record and additional custom field with externalId to parsed record.
*
* @param record record to update
* @param instance externalEntity in Json
*/
private void setExternalIds(Record record, Instance instance) {
if (record.getExternalIdsHolder() == null) {
record.setExternalIdsHolder(new ExternalIdsHolder());
}
String externalId = record.getExternalIdsHolder().getInstanceId();
String externalHrid = record.getExternalIdsHolder().getInstanceHrid();
if (isNotEmpty(externalId) || isNotEmpty(externalHrid)) {
if (AdditionalFieldsUtil.isFieldsFillingNeeded(record, instance)) {
setIdAndHrid(record, instance);
}
} else {
setIdAndHrid(record, instance);
}
}

private void setIdAndHrid(Record record, Instance instance) {
record.getExternalIdsHolder().setInstanceId(instance.getId());
record.getExternalIdsHolder().setInstanceHrid(instance.getHrid());
}

private Future<Instance> saveRecordInSrsAndHandleResponse(DataImportEventPayload payload, Record record,
Instance instance, InstanceCollection instanceCollection) {
Promise<Instance> promise = Promise.promise();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.inventory.domain.instances.Instance;
import org.folio.processing.exceptions.EventProcessingException;
import org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters;
import org.folio.rest.jaxrs.model.MarcFieldProtectionSetting;
Expand Down Expand Up @@ -466,6 +467,23 @@ private static boolean removeFieldByNameAndValue(org.marc4j.marc.Record marcReco
return isFieldFound;
}

/**
* Check if record should be filled with specific fields.
*
* @param record - source record.
* @param instance - instance.
* @return - true if filling needed.
*/
public static boolean isFieldsFillingNeeded(Record record, Instance instance) {
var externalIdsHolder = record.getExternalIdsHolder();
return isValidIdAndHrid(instance.getId(), instance.getHrid(),
externalIdsHolder.getInstanceId(), externalIdsHolder.getInstanceHrid());
}

private static boolean isValidIdAndHrid(String id, String hrid, String externalId, String externalHrid) {
return (isNotEmpty(externalId) && isNotEmpty(externalHrid)) && (id.equals(externalId) && !hrid.equals(externalHrid));
}

/**
* Adds new data field to marc record
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public void shouldProcessEventAndUpdate005Field() throws InterruptedException, E
assertNotNull(actualDate);
assertEquals(expectedDate.substring(0, 10), actualDate.substring(0, 10));
assertEquals(recordId, recordCaptor.getValue().getMatchedId());
assertEquals(instanceId, recordCaptor.getValue().getExternalIdsHolder().getInstanceId());
}

@Test(expected = ExecutionException.class)
Expand Down

0 comments on commit 44fff6d

Please sign in to comment.