Skip to content

Commit

Permalink
fix(marc-fields-order): Don't group fields with same tags together (#722
Browse files Browse the repository at this point in the history
)

Closes: MODINV-1026
  • Loading branch information
viacheslavkol authored May 21, 2024
1 parent 9af7ad0 commit 0ecb85a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ForkJoinPool;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -771,60 +768,65 @@ public static void fillHrIdFieldInMarcRecord(Pair<Record, JsonObject> recordInst
}

/**
* Reorders MARC record fields
* Take field values from system modified record content while preserving incoming record content`s field order.
* Put system fields (001, 005) first, regardless of incoming record fields order.
*
* @param sourceContent source parsed record
* @param targetContent target parsed record
* @return MARC txt
* @param sourceOrderContent content with incoming record fields order
* @param systemOrderContent system modified record content with reordered fields
* @return MARC record parsed content with desired fields order
*/
public static String reorderMarcRecordFields(String sourceContent, String targetContent) {
public static String reorderMarcRecordFields(String sourceOrderContent, String systemOrderContent) {
try {
var parsedContent = objectMapper.readTree(targetContent);
var parsedContent = objectMapper.readTree(systemOrderContent);
var fieldsArrayNode = (ArrayNode) parsedContent.path(FIELDS);

var jsonNodesByTag = groupNodesByTag(fieldsArrayNode);
var sourceFields = getSourceFields(sourceContent);
var rearrangedArray = objectMapper.createArrayNode();
var nodes = toNodeList(fieldsArrayNode);
var sourceOrderTags = getSourceFields(sourceOrderContent);
var reorderedFields = objectMapper.createArrayNode();

var nodes001 = jsonNodesByTag.get(TAG_001);
if (nodes001 != null && !nodes001.isEmpty()) {
rearrangedArray.addAll(nodes001);
jsonNodesByTag.remove(TAG_001);
var node001 = removeAndGetNodeByTag(nodes, TAG_001);
if (node001 != null && !node001.isEmpty()) {
reorderedFields.add(node001);
}

var nodes005 = jsonNodesByTag.get(TAG_005);
if (nodes005 != null && !nodes005.isEmpty()) {
rearrangedArray.addAll(nodes005);
jsonNodesByTag.remove(TAG_005);
var node005 = removeAndGetNodeByTag(nodes, TAG_005);
if (node005 != null && !node005.isEmpty()) {
reorderedFields.add(node005);
}

for (String tag : sourceFields) {
Queue<JsonNode> nodes = jsonNodesByTag.get(tag);
if (nodes != null && !nodes.isEmpty()) {
rearrangedArray.addAll(nodes);
jsonNodesByTag.remove(tag);
for (String tag : sourceOrderTags) {
var node = removeAndGetNodeByTag(nodes, tag);
if (node != null && !node.isEmpty()) {
reorderedFields.add(node);
}

}

jsonNodesByTag.values().forEach(rearrangedArray::addAll);
reorderedFields.addAll(nodes);

((ObjectNode) parsedContent).set(FIELDS, rearrangedArray);
((ObjectNode) parsedContent).set(FIELDS, reorderedFields);
return parsedContent.toString();
} catch (Exception e) {
LOGGER.error("An error occurred while reordering Marc record fields: {}", e.getMessage(), e);
return targetContent;
return systemOrderContent;
}
}

private static List<JsonNode> toNodeList(ArrayNode fieldsArrayNode) {
var nodes = new LinkedList<JsonNode>();
for (var node : fieldsArrayNode) {
nodes.add(node);
}
return nodes;
}

private static Map<String, Queue<JsonNode>> groupNodesByTag(ArrayNode fieldsArrayNode) {
var jsonNodesByTag = new LinkedHashMap<String, Queue<JsonNode>>();
for (JsonNode node : fieldsArrayNode) {
var tag = getTagFromNode(node);
jsonNodesByTag.putIfAbsent(tag, new LinkedList<>());
jsonNodesByTag.get(tag).add(node);
private static JsonNode removeAndGetNodeByTag(List<JsonNode> nodes, String tag) {
for (int i = 0; i < nodes.size(); i++) {
var nodeTag = getTagFromNode(nodes.get(i));
if (nodeTag.equals(tag)) {
return nodes.remove(i);
}
}
return jsonNodesByTag;
return null;
}

private static String getTagFromNode(JsonNode node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,14 +804,14 @@ public void shouldRemove035() {

@Test
public void shouldReorderMarcRecordFields() throws IOException, MarcException {
var reorderedRecordContent = readFileFromPath(PARSED_RECORD);
var sourceRecordContent = readFileFromPath(REORDERED_PARSED_RECORD);
var reorderingResultRecord = readFileFromPath(REORDERING_RESULT_RECORD);
var systemReorderedRecordContent = readFileFromPath(PARSED_RECORD);
var userOrderRecordContent = readFileFromPath(REORDERED_PARSED_RECORD);
var expectedOrderRecord = readFileFromPath(REORDERING_RESULT_RECORD);

var resultContent = AdditionalFieldsUtil.reorderMarcRecordFields(sourceRecordContent, reorderedRecordContent);
var actualOrderRecord = AdditionalFieldsUtil.reorderMarcRecordFields(userOrderRecordContent, systemReorderedRecordContent);

assertNotNull(resultContent);
assertEquals(formatContent(resultContent), formatContent(reorderingResultRecord));
assertNotNull(actualOrderRecord);
assertEquals(formatContent(expectedOrderRecord), formatContent(actualOrderRecord));
}

private static String readFileFromPath(String path) throws IOException {
Expand Down
30 changes: 15 additions & 15 deletions src/test/resources/marc/reorderedParsedRecord.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,53 +31,53 @@
"008":"120329s2011 sz a ob 001 0 eng d"
},
{
"020":{
"050":{
"subfields":[
{
"a":"2940447241 (electronic bk.)"
"a":"Z246"
},
{
"b":".A43 2011"
}
],
"ind1":" ",
"ind2":" "
"ind2":"4"
}
},
{
"020":{
"subfields":[
{
"a":"9782940447244 (electronic bk.)"
"a":"2940447241 (electronic bk.)"
}
],
"ind1":" ",
"ind2":" "
}
},
{
"050":{
"082":{
"subfields":[
{
"a":"Z246"
"a":"686.22"
},
{
"b":".A43 2011"
"2":"22"
}
],
"ind1":" ",
"ind1":"0",
"ind2":"4"
}
},
{
"082":{
"020":{
"subfields":[
{
"a":"686.22"
},
{
"2":"22"
"a":"9782940447244 (electronic bk.)"
}
],
"ind1":"0",
"ind2":"4"
"ind1":" ",
"ind2":" "
}
},
{
Expand Down
30 changes: 15 additions & 15 deletions src/test/resources/marc/reorderingResultRecord.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,53 +34,53 @@
"008": "120329s2011 sz a ob 001 0 eng d"
},
{
"020": {
"050": {
"subfields": [
{
"a": "2940447241 (electronic bk.)"
"a": "Z246"
},
{
"b": ".A43 2011"
}
],
"ind1": " ",
"ind2": " "
"ind2": "4"
}
},
{
"020": {
"subfields": [
{
"a": "9782940447244 (electronic bk.)"
"a": "2940447241 (electronic bk.)"
}
],
"ind1": " ",
"ind2": " "
}
},
{
"050": {
"082": {
"subfields": [
{
"a": "Z246"
"a": "686.22"
},
{
"b": ".A43 2011"
"2": "22"
}
],
"ind1": " ",
"ind1": "0",
"ind2": "4"
}
},
{
"082": {
"020": {
"subfields": [
{
"a": "686.22"
},
{
"2": "22"
"a": "9782940447244 (electronic bk.)"
}
],
"ind1": "0",
"ind2": "4"
"ind1": " ",
"ind2": " "
}
},
{
Expand Down

0 comments on commit 0ecb85a

Please sign in to comment.