diff --git a/NEWS.md b/NEWS.md index f07a97d7a..be8973475 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,7 @@ * [MODINV-1044](https://folio-org.atlassian.net/browse/MODINV-1044) Additional Requirements - Update Data Import logic to normalize OCLC 035 values * [MODSOURMAN-1200](https://folio-org.atlassian.net/browse/MODSOURMAN-1200) Find record by match id on update generation * [MODINV-1049](https://folio-org.atlassian.net/browse/MODINV-1049) Existing "035" field is not retained the original position in imported record +* [MODSOURCE-785](https://folio-org.atlassian.net/browse/MODSOURCE-785) Update 005 field when set MARC for deletion ## 2024-03-20 5.8.0 * [MODSOURCE-733](https://issues.folio.org/browse/MODSOURCE-733) Reduce Memory Allocation of Strings diff --git a/mod-source-record-storage-server/src/main/java/org/folio/dao/util/ParsedRecordDaoUtil.java b/mod-source-record-storage-server/src/main/java/org/folio/dao/util/ParsedRecordDaoUtil.java index 167160881..4f8979624 100644 --- a/mod-source-record-storage-server/src/main/java/org/folio/dao/util/ParsedRecordDaoUtil.java +++ b/mod-source-record-storage-server/src/main/java/org/folio/dao/util/ParsedRecordDaoUtil.java @@ -258,5 +258,4 @@ public static JsonObject normalize(Object content) { ? new JsonObject((String) content) : JsonObject.mapFrom(content); } - } diff --git a/mod-source-record-storage-server/src/main/java/org/folio/services/RecordServiceImpl.java b/mod-source-record-storage-server/src/main/java/org/folio/services/RecordServiceImpl.java index b47400c15..b61a2b7cf 100644 --- a/mod-source-record-storage-server/src/main/java/org/folio/services/RecordServiceImpl.java +++ b/mod-source-record-storage-server/src/main/java/org/folio/services/RecordServiceImpl.java @@ -4,6 +4,7 @@ import static java.util.Objects.nonNull; import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; +import static org.folio.dao.util.MarcUtil.reorderMarcRecordFields; import static org.folio.dao.util.RecordDaoUtil.RECORD_NOT_FOUND_TEMPLATE; import static org.folio.dao.util.RecordDaoUtil.ensureRecordForeignKeys; import static org.folio.dao.util.RecordDaoUtil.ensureRecordHasId; @@ -55,6 +56,7 @@ import org.folio.rest.jaxrs.model.RecordMatchingDto; import org.folio.rest.jaxrs.model.RecordsIdentifiersCollection; import org.folio.services.exceptions.DuplicateRecordException; +import org.folio.services.util.AdditionalFieldsUtil; import org.folio.services.util.TypeConnection; import org.jooq.Condition; import org.jooq.OrderField; @@ -342,6 +344,7 @@ public Future deleteRecordById(String id, IdType idType, String tenantId) return recordDao.getRecordByExternalId(id, idType, tenantId) .map(recordOptional -> recordOptional.orElseThrow(() -> new NotFoundException(format(NOT_FOUND_MESSAGE, Record.class.getSimpleName(), id)))) .map(record -> { + update005field(record); record.withState(Record.State.DELETED); record.setAdditionalInfo(record.getAdditionalInfo().withSuppressDiscovery(true)); ParsedRecordDaoUtil.updateLeaderStatus(record.getParsedRecord(), DELETED_LEADER_RECORD_STATUS); @@ -496,4 +499,14 @@ private Future processDefaultMatchField(MatchField .withIdentifiers(identifiers).withTotalRecords(recordCollection.getTotalRecords())))); } + private static void update005field(Record targetRecord) { + if (targetRecord.getParsedRecord() != null && targetRecord.getParsedRecord().getContent() != null) { + AdditionalFieldsUtil.updateLatestTransactionDate(targetRecord); + var sourceContent = targetRecord.getParsedRecord().getContent().toString(); + var targetContent = targetRecord.getParsedRecord().getContent().toString(); + var content = reorderMarcRecordFields(sourceContent, targetContent); + targetRecord.getParsedRecord().setContent(content); + } + } + } diff --git a/mod-source-record-storage-server/src/main/java/org/folio/services/util/AdditionalFieldsUtil.java b/mod-source-record-storage-server/src/main/java/org/folio/services/util/AdditionalFieldsUtil.java index aebfedae4..8cb8d8ff3 100644 --- a/mod-source-record-storage-server/src/main/java/org/folio/services/util/AdditionalFieldsUtil.java +++ b/mod-source-record-storage-server/src/main/java/org/folio/services/util/AdditionalFieldsUtil.java @@ -623,17 +623,25 @@ private static VariableField getSingleFieldByIndicators(List list /** * Updates field 005 for case when this field is not protected. * - * @param record record to update + * @param targetRecord record to update * @param mappingParameters mapping parameters */ - public static void updateLatestTransactionDate(Record record, MappingParameters mappingParameters) { - if (isField005NeedToUpdate(record, mappingParameters)) { + public static void updateLatestTransactionDate(Record targetRecord, MappingParameters mappingParameters) { + if (isField005NeedToUpdate(targetRecord, mappingParameters)) { + updateLatestTransactionDate(targetRecord); + } + } + + /** + * Updates field 005. + * @param targetRecord record to update + */ + public static void updateLatestTransactionDate(Record targetRecord) { String date = AdditionalFieldsUtil.dateTime005Formatter.format(ZonedDateTime.ofInstant(Instant.now(), ZoneId.systemDefault())); - boolean isLatestTransactionDateUpdated = AdditionalFieldsUtil.addControlledFieldToMarcRecord(record, AdditionalFieldsUtil.TAG_005, date, true); + boolean isLatestTransactionDateUpdated = AdditionalFieldsUtil.addControlledFieldToMarcRecord(targetRecord, AdditionalFieldsUtil.TAG_005, date, true); if (!isLatestTransactionDateUpdated) { - throw new PostProcessingException(format("Failed to update field '005' to record with id '%s'", record.getId())); + throw new PostProcessingException(format("Failed to update field '005' to record with id '%s'", targetRecord.getId())); } - } } /** diff --git a/mod-source-record-storage-server/src/test/java/org/folio/rest/impl/RecordApiTest.java b/mod-source-record-storage-server/src/test/java/org/folio/rest/impl/RecordApiTest.java index d4d38b08b..369d52cfc 100644 --- a/mod-source-record-storage-server/src/test/java/org/folio/rest/impl/RecordApiTest.java +++ b/mod-source-record-storage-server/src/test/java/org/folio/rest/impl/RecordApiTest.java @@ -9,6 +9,8 @@ import static org.hamcrest.Matchers.nullValue; import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; import java.util.UUID; @@ -832,15 +834,18 @@ public void shouldDeleteExistingMarcRecordOnDeleteByRecordId(TestContext testCon } @Test - public void shouldDeleteExistingMarcRecordOnDeleteByInstanceId(TestContext testContext) { + public void shouldDeleteExistingMarcRecordOnDeleteByInstanceIdAndUpdate005FieldWithCurrentDate(TestContext testContext) { postSnapshots(testContext, snapshot_1); String srsId = UUID.randomUUID().toString(); String instanceId = UUID.randomUUID().toString(); + String currentDate = "20240718132044.6"; ParsedRecord parsedRecord = new ParsedRecord().withId(srsId) .withContent(new JsonObject().put("leader", "01542ccm a2200361 4500") - .put("fields", new JsonArray().add(new JsonObject().put("999", new JsonObject() + .put("fields", new JsonArray() + .add(new JsonObject().put("005", currentDate)) + .add(new JsonObject().put("999", new JsonObject() .put("subfields", new JsonArray().add(new JsonObject().put("s", srsId)).add(new JsonObject().put("i", instanceId))))))); Record newRecord = new Record() @@ -887,6 +892,13 @@ public void shouldDeleteExistingMarcRecordOnDeleteByInstanceId(TestContext testC Assert.assertEquals(true, deletedRecord.getAdditionalInfo().getSuppressDiscovery()); Assert.assertEquals("d", ParsedRecordDaoUtil.getLeaderStatus(deletedRecord.getParsedRecord())); + //Complex verifying "005" field is NOT empty inside parsed record. + LinkedHashMap>> content = (LinkedHashMap>>) deletedRecord.getParsedRecord().getContent(); + LinkedHashMap map = content.get("fields").get(0); + String resulted005FieldValue = map.get("005"); + Assert.assertNotNull(resulted005FieldValue); + Assert.assertNotEquals(currentDate, resulted005FieldValue); + async.complete(); }