Skip to content

Commit

Permalink
fix(marc-fields-order): Fix bib fields order on link update event (#662)
Browse files Browse the repository at this point in the history
- Handle only authority natural id change
- Put uncontroller alphabetic fields before controlled number fields

Closes: MODSOURCE-842
(cherry picked from commit 1f8d4c0)
  • Loading branch information
viacheslavkol committed Jan 24, 2025
1 parent ce32959 commit 8995ba2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ public class UpdateLinkProcessor implements LinkProcessor {
@Override
public Collection<Subfield> process(String fieldCode, List<org.folio.rest.jaxrs.model.Subfield> subfieldsChanges,
List<Subfield> oldSubfields) {
if (isOnlyNaturalIdChanged(subfieldsChanges)) {
return processOnlyNaturalIdChange(oldSubfields, subfieldsChanges.get(0).getValue());
}

var authorityIdSubfield = getAuthorityIdSubfield(oldSubfields);
var authorityNaturalIdSubfield = getAuthorityNaturalIdSubfield(oldSubfields);
var controllableSubfields = new ArrayList<Subfield>();
var controllableAlphaSubfields = new ArrayList<Subfield>();
var controllableDigitSubfields = new ArrayList<Subfield>();
var controllableSubfieldCodes = initializeSubfieldCodes();

for (var subfieldsChange : subfieldsChanges) {
Expand All @@ -34,30 +39,58 @@ public Collection<Subfield> process(String fieldCode, List<org.folio.rest.jaxrs.
case AUTHORITY_NATURAL_ID_SUBFIELD -> authorityNaturalIdSubfield = Optional.of(new SubfieldImpl(code, value));
default -> {
if (isNotBlank(value)) {
controllableSubfields.add(new SubfieldImpl(code, value));
if (Character.isAlphabetic(code)) {
controllableAlphaSubfields.add(new SubfieldImpl(code, value));
} else {
controllableDigitSubfields.add(new SubfieldImpl(code, value));
}
}
}
}
controllableSubfieldCodes.add(code);
}

// add controllable subfields
var result = new ArrayList<>(controllableSubfields);
// add controllable alphabetic subfields
var result = new ArrayList<>(controllableAlphaSubfields);

// add special subfields
// add uncontrollable alphabetic subfields
var uncontrolledSubfields = oldSubfields.stream()
.filter(subfield -> isNotControllableSubfield(subfield, controllableSubfieldCodes))
.toList();
uncontrolledSubfields.forEach(subfield -> {
if (Character.isAlphabetic(subfield.getCode())) {
result.add(subfield);
}
});

//add special/digit controlled subfields
authorityNaturalIdSubfield.ifPresent(result::add);
authorityIdSubfield.ifPresent(result::add);
result.addAll(controllableDigitSubfields);

// add uncontrollable subfields
for (var oldSubfield : oldSubfields) {
if (isNotControllableSubfield(oldSubfield, controllableSubfieldCodes)) {
result.add(oldSubfield);
// add uncontrollable digit subfields
uncontrolledSubfields.forEach(subfield -> {
if (!Character.isAlphabetic(subfield.getCode())) {
result.add(subfield);
}
}
});

return result;
}

private Collection<Subfield> processOnlyNaturalIdChange(List<Subfield> oldSubfields, String newNaturalIdSubfield) {
oldSubfields.stream()
.filter(subfield -> subfield.getCode() == AUTHORITY_NATURAL_ID_SUBFIELD)
.findFirst()
.ifPresent(subfield -> subfield.setData(newNaturalIdSubfield));
return oldSubfields;
}

private boolean isOnlyNaturalIdChanged(List<org.folio.rest.jaxrs.model.Subfield> subfieldsChanges) {
return subfieldsChanges.size() == 1
&& subfieldsChanges.get(0).getCode().charAt(0) == AUTHORITY_NATURAL_ID_SUBFIELD;
}

private boolean isNotControllableSubfield(Subfield subfield, Set<Character> controllableSubfieldCodes) {
return !controllableSubfieldCodes.contains(subfield.getCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@ public void process_positive_updateSubfieldsAndOrderIt() {
oldSubfields.get(0),
new SubfieldImpl('z', "z-data-new1"),
new SubfieldImpl('z', "z-data-new2"),
oldSubfields.get(5),
new SubfieldImpl('0', "0-data-new"),
oldSubfields.get(4)
);

assertEquals(expected.toString(), actual.toString());
}

@Test
public void process_positive_onlyNaturalIdChanged() {
List<Subfield> oldSubfields = List.of(
new SubfieldImpl('a', "a-data"),
new SubfieldImpl('b', "b-data"),
new SubfieldImpl('z', "z-data"),
new SubfieldImpl('0', "0-data"),
new SubfieldImpl('9', "9-data"),
new SubfieldImpl('k', "k-data")
);
var subfieldsChanges = List.of(
new org.folio.rest.jaxrs.model.Subfield().withCode("0").withValue("0-data-new")
);

var actual = processor.process("100", subfieldsChanges, oldSubfields);


var expected = List.of(
oldSubfields.get(0),
oldSubfields.get(1),
oldSubfields.get(2),
new SubfieldImpl('0', "0-data-new"),
oldSubfields.get(4),
oldSubfields.get(5)
Expand Down

0 comments on commit 8995ba2

Please sign in to comment.