Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(marc-fields-order): Fix bib fields order on link update event #662

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>();
psmagin marked this conversation as resolved.
Show resolved Hide resolved
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