Skip to content

Commit

Permalink
fix(marc-fields-order): Reorder only 00X fields while preserving syst…
Browse files Browse the repository at this point in the history
…em 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
  • Loading branch information
viacheslavkol authored Jun 14, 2024
1 parent 755b9cf commit dd267e7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -201,6 +208,19 @@ private static JsonNode removeAndGetNodeByTag(List<JsonNode> nodes, String tag)
return null;
}

private static List<JsonNode> removeAndGetNodesByTagPrefix(List<JsonNode> nodes, String prefix) {
var startsWithNodes = new LinkedList<JsonNode>();
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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"001":"ybp7406411"
},
{
"005":"20120404100627.6"
"003":"NhCcYBP"
},
{
"003":"NhCcYBP"
"005":"20120404100627.6"
},
{
"006":"m||||||||d|||||||"
Expand All @@ -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": " "
}
},
{
Expand Down Expand Up @@ -69,6 +58,17 @@
"ind2":"4"
}
},
{
"020":{
"subfields":[
{
"a":"2940447241 (electronic bk.)"
}
],
"ind1":" ",
"ind2":" "
}
},
{
"082":{
"subfields":[
Expand All @@ -83,6 +83,17 @@
"ind2":"4"
}
},
{
"020":{
"subfields":[
{
"a":"9782940447244 (electronic bk.)"
}
],
"ind1":" ",
"ind2":" "
}
},
{
"100":{
"subfields":[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
{
"005": "20120404100627.6"
},
{
"035": {
"subfields": [
{
"a": "(OCoLC)63611770"
}
],
"ind1": " ",
"ind2": " "
}
},
{
"040": {
"subfields": [
Expand Down

0 comments on commit dd267e7

Please sign in to comment.