diff --git a/mod-source-record-storage-server/src/main/java/org/folio/services/handlers/links/UpdateLinkProcessor.java b/mod-source-record-storage-server/src/main/java/org/folio/services/handlers/links/UpdateLinkProcessor.java index a1a4e114..7e6fec36 100644 --- a/mod-source-record-storage-server/src/main/java/org/folio/services/handlers/links/UpdateLinkProcessor.java +++ b/mod-source-record-storage-server/src/main/java/org/folio/services/handlers/links/UpdateLinkProcessor.java @@ -21,9 +21,14 @@ public class UpdateLinkProcessor implements LinkProcessor { @Override public Collection process(String fieldCode, List subfieldsChanges, List oldSubfields) { + if (isOnlyNaturalIdChanged(subfieldsChanges)) { + return processOnlyNaturalIdChange(oldSubfields, subfieldsChanges.get(0).getValue()); + } + var authorityIdSubfield = getAuthorityIdSubfield(oldSubfields); var authorityNaturalIdSubfield = getAuthorityNaturalIdSubfield(oldSubfields); - var controllableSubfields = new ArrayList(); + var controllableAlphaSubfields = new ArrayList(); + var controllableDigitSubfields = new ArrayList(); var controllableSubfieldCodes = initializeSubfieldCodes(); for (var subfieldsChange : subfieldsChanges) { @@ -34,30 +39,58 @@ public Collection process(String fieldCode, List 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 processOnlyNaturalIdChange(List 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 subfieldsChanges) { + return subfieldsChanges.size() == 1 + && subfieldsChanges.get(0).getCode().charAt(0) == AUTHORITY_NATURAL_ID_SUBFIELD; + } + private boolean isNotControllableSubfield(Subfield subfield, Set controllableSubfieldCodes) { return !controllableSubfieldCodes.contains(subfield.getCode()); } diff --git a/mod-source-record-storage-server/src/test/java/org/folio/services/handlers/links/UpdateLinkProcessorTest.java b/mod-source-record-storage-server/src/test/java/org/folio/services/handlers/links/UpdateLinkProcessorTest.java index 3ca7f622..8930ddc3 100644 --- a/mod-source-record-storage-server/src/test/java/org/folio/services/handlers/links/UpdateLinkProcessorTest.java +++ b/mod-source-record-storage-server/src/test/java/org/folio/services/handlers/links/UpdateLinkProcessorTest.java @@ -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 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)