Skip to content

Commit

Permalink
Merge branch 'master' into MODINV-1031
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanChernetskyi authored Jun 14, 2024
2 parents 02ee878 + 8d0546e commit 0959ee2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -829,6 +836,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
55 changes: 33 additions & 22 deletions src/test/resources/marc/parsedRecord.json
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,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":" "
}
},
{
Expand All @@ -83,6 +83,17 @@
"ind2":"4"
}
},
{
"020":{
"subfields":[
{
"a":"9782940447244 (electronic bk.)"
}
],
"ind1":" ",
"ind2":" "
}
},
{
"100":{
"subfields":[
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/marc/reorderingResultRecord.json
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 0959ee2

Please sign in to comment.