From dd267e7cf8ee89f71260e8ee343b3a84afea7478 Mon Sep 17 00:00:00 2001 From: Viacheslav Kolesnyk <94473337+viacheslavkol@users.noreply.github.com> Date: Fri, 14 Jun 2024 12:35:32 +0200 Subject: [PATCH] fix(marc-fields-order): Reorder only 00X fields while preserving system order for all other (#627) - System algorithms reorder only 00X while also adding f.e. 035 so no need to reorder system records fields apart from 00X, which will also allow to preserve system generated 035 field order Closes: MODSOURCE-780 --- .../java/org/folio/dao/util/MarcUtil.java | 32 ++++++++++--- .../services/util/AdditionalFieldsUtil.java | 1 + .../parsedRecords/parsedRecord.json | 47 ++++++++++++------- .../parsedRecords/reorderingResultRecord.json | 11 +++++ 4 files changed, 67 insertions(+), 24 deletions(-) diff --git a/mod-source-record-storage-server/src/main/java/org/folio/dao/util/MarcUtil.java b/mod-source-record-storage-server/src/main/java/org/folio/dao/util/MarcUtil.java index bc5255d79..c295502cc 100644 --- a/mod-source-record-storage-server/src/main/java/org/folio/dao/util/MarcUtil.java +++ b/mod-source-record-storage-server/src/main/java/org/folio/dao/util/MarcUtil.java @@ -3,6 +3,7 @@ import static java.lang.String.format; import static org.folio.services.util.AdditionalFieldsUtil.HR_ID_FROM_FIELD; import static org.folio.services.util.AdditionalFieldsUtil.TAG_005; +import static org.folio.services.util.AdditionalFieldsUtil.TAG_00X_PREFIX; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; @@ -153,24 +154,30 @@ public static String reorderMarcRecordFields(String sourceOrderContent, String s var fieldsArrayNode = (ArrayNode) parsedContent.path(FIELDS); var nodes = toNodeList(fieldsArrayNode); + var nodes00X = removeAndGetNodesByTagPrefix(nodes, TAG_00X_PREFIX); var sourceOrderTags = getSourceFields(sourceOrderContent); var reorderedFields = objectMapper.createArrayNode(); - var node001 = removeAndGetNodeByTag(nodes, HR_ID_FROM_FIELD); + var node001 = removeAndGetNodeByTag(nodes00X, HR_ID_FROM_FIELD); if (node001 != null && !node001.isEmpty()) { reorderedFields.add(node001); } - var node005 = removeAndGetNodeByTag(nodes, TAG_005); + var node005 = removeAndGetNodeByTag(nodes00X, TAG_005); if (node005 != null && !node005.isEmpty()) { reorderedFields.add(node005); } for (String tag : sourceOrderTags) { - var node = removeAndGetNodeByTag(nodes, tag); - if (node != null && !node.isEmpty()) { - reorderedFields.add(node); - } + var nodeTag = tag; + //loop will add system generated fields that are absent in initial record, preserving their order, f.e. 035 + do { + var node = tag.startsWith(TAG_00X_PREFIX) ? removeAndGetNodeByTag(nodes00X, tag) : nodes.remove(0); + if (node != null && !node.isEmpty()) { + nodeTag = getTagFromNode(node); + reorderedFields.add(node); + } + } while (!tag.equals(nodeTag) && !nodes.isEmpty()); } reorderedFields.addAll(nodes); @@ -201,6 +208,19 @@ private static JsonNode removeAndGetNodeByTag(List nodes, String tag) return null; } + private static List removeAndGetNodesByTagPrefix(List nodes, String prefix) { + var startsWithNodes = new LinkedList(); + for (int i = 0; i < nodes.size(); i++) { + var nodeTag = getTagFromNode(nodes.get(i)); + if (nodeTag.startsWith(prefix)) { + startsWithNodes.add(nodes.get(i)); + } + } + + nodes.removeAll(startsWithNodes); + return startsWithNodes; + } + private static String getTagFromNode(JsonNode node) { return node.fieldNames().next(); } 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 b97ec623b..0d967bb14 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 @@ -56,6 +56,7 @@ */ public final class AdditionalFieldsUtil { + public static final String TAG_00X_PREFIX = "00"; public static final String TAG_005 = "005"; public static final String TAG_999 = "999"; public static final String TAG_035 = "035"; diff --git a/mod-source-record-storage-server/src/test/resources/mock/sourceRecords/parsedRecords/parsedRecord.json b/mod-source-record-storage-server/src/test/resources/mock/sourceRecords/parsedRecords/parsedRecord.json index 752f36761..9484ff805 100644 --- a/mod-source-record-storage-server/src/test/resources/mock/sourceRecords/parsedRecords/parsedRecord.json +++ b/mod-source-record-storage-server/src/test/resources/mock/sourceRecords/parsedRecords/parsedRecord.json @@ -5,10 +5,10 @@ "001":"ybp7406411" }, { - "005":"20120404100627.6" + "003":"NhCcYBP" }, { - "003":"NhCcYBP" + "005":"20120404100627.6" }, { "006":"m||||||||d|||||||" @@ -20,25 +20,14 @@ "008":"120329s2011 sz a ob 001 0 eng d" }, { - "020":{ - "subfields":[ - { - "a":"2940447241 (electronic bk.)" - } - ], - "ind1":" ", - "ind2":" " - } - }, - { - "020":{ - "subfields":[ + "035": { + "subfields": [ { - "a":"9782940447244 (electronic bk.)" + "a": "(OCoLC)63611770" } ], - "ind1":" ", - "ind2":" " + "ind1": " ", + "ind2": " " } }, { @@ -69,6 +58,17 @@ "ind2":"4" } }, + { + "020":{ + "subfields":[ + { + "a":"2940447241 (electronic bk.)" + } + ], + "ind1":" ", + "ind2":" " + } + }, { "082":{ "subfields":[ @@ -83,6 +83,17 @@ "ind2":"4" } }, + { + "020":{ + "subfields":[ + { + "a":"9782940447244 (electronic bk.)" + } + ], + "ind1":" ", + "ind2":" " + } + }, { "100":{ "subfields":[ diff --git a/mod-source-record-storage-server/src/test/resources/mock/sourceRecords/parsedRecords/reorderingResultRecord.json b/mod-source-record-storage-server/src/test/resources/mock/sourceRecords/parsedRecords/reorderingResultRecord.json index 09fc0e0cd..c74eb2a7f 100644 --- a/mod-source-record-storage-server/src/test/resources/mock/sourceRecords/parsedRecords/reorderingResultRecord.json +++ b/mod-source-record-storage-server/src/test/resources/mock/sourceRecords/parsedRecords/reorderingResultRecord.json @@ -7,6 +7,17 @@ { "005": "20120404100627.6" }, + { + "035": { + "subfields": [ + { + "a": "(OCoLC)63611770" + } + ], + "ind1": " ", + "ind2": " " + } + }, { "040": { "subfields": [