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 6e8c27dee..568657bdc 100644 --- a/src/main/java/org/folio/inventory/dataimport/util/AdditionalFieldsUtil.java +++ b/src/main/java/org/folio/inventory/dataimport/util/AdditionalFieldsUtil.java @@ -63,6 +63,7 @@ public final class AdditionalFieldsUtil { private static final Logger LOGGER = LogManager.getLogger(); public static final DateTimeFormatter dateTime005Formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss.S"); + 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_001 = "001"; @@ -781,24 +782,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, TAG_001); + var node001 = removeAndGetNodeByTag(nodes00X, TAG_001); 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); - } + for (var tag : sourceOrderTags) { + 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); @@ -829,6 +836,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/src/test/resources/marc/parsedRecord.json b/src/test/resources/marc/parsedRecord.json index 4c6d25e55..b33b499ef 100644 --- a/src/test/resources/marc/parsedRecord.json +++ b/src/test/resources/marc/parsedRecord.json @@ -5,10 +5,10 @@ "001":"ybp7406411" }, { - "005":"20120404100627.6" + "003":"NhCcYBP" }, { - "003":"NhCcYBP" + "005":"20120404100627.6" }, { "006":"m||||||||d|||||||" @@ -20,53 +20,53 @@ "008":"120329s2011 sz a ob 001 0 eng d" }, { - "020":{ - "subfields":[ + "035": { + "subfields": [ { - "a":"2940447241 (electronic bk.)" + "a": "(OCoLC)63611770" } ], - "ind1":" ", - "ind2":" " + "ind1": " ", + "ind2": " " } }, { - "020":{ - "subfields":[ + "040": { + "subfields": [ { - "a":"9782940447244 (electronic bk.)" + "a": "NhCcYBP" + }, + { + "c": "NhCcYBP" } ], - "ind1":" ", - "ind2":" " + "ind1": " ", + "ind2": " " } }, { - "040":{ + "050":{ "subfields":[ { - "a":"NhCcYBP" + "a":"Z246" }, { - "c":"NhCcYBP" + "b":".A43 2011" } ], "ind1":" ", - "ind2":" " + "ind2":"4" } }, { - "050":{ + "020":{ "subfields":[ { - "a":"Z246" - }, - { - "b":".A43 2011" + "a":"2940447241 (electronic bk.)" } ], "ind1":" ", - "ind2":"4" + "ind2":" " } }, { @@ -83,6 +83,17 @@ "ind2":"4" } }, + { + "020":{ + "subfields":[ + { + "a":"9782940447244 (electronic bk.)" + } + ], + "ind1":" ", + "ind2":" " + } + }, { "100":{ "subfields":[ diff --git a/src/test/resources/marc/reorderingResultRecord.json b/src/test/resources/marc/reorderingResultRecord.json index c1ba56cb0..8f17c58ea 100644 --- a/src/test/resources/marc/reorderingResultRecord.json +++ b/src/test/resources/marc/reorderingResultRecord.json @@ -7,6 +7,17 @@ { "005": "20120404100627.6" }, + { + "035": { + "subfields": [ + { + "a": "(OCoLC)63611770" + } + ], + "ind1": " ", + "ind2": " " + } + }, { "040": { "subfields": [