feat(person): add upsertBySourceRef with human-edit-preserve precedence
Idempotent person upsert keyed on the normalizer person_id (source_ref), for the Phase-3 canonical importer. Re-import precedence (Resolved decision #1): a non-blank existing field is never overwritten, blank fields are filled from canonical, and provisional is monotonic — once a human confirms a person (false) it never reverts to true. New importer-created persons carry provisional=true; register persons false. Maiden name is stored as a MAIDEN_NAME PersonNameAlias, matching the existing findOrCreateByAlias behaviour. Refs #669 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -32,6 +32,9 @@ public interface PersonRepository extends JpaRepository<Person, UUID> {
|
||||
// Lookup by full alias string, used during ODS mass import
|
||||
Optional<Person> findByAliasIgnoreCase(String alias);
|
||||
|
||||
// Lookup by the normalizer person_id, used for idempotent canonical re-import (Phase 3).
|
||||
Optional<Person> findBySourceRef(String sourceRef);
|
||||
|
||||
// Exact first+last name match, used for filename-based sender lookup
|
||||
Optional<Person> findByFirstNameIgnoreCaseAndLastNameIgnoreCase(String firstName, String lastName);
|
||||
|
||||
|
||||
@@ -115,6 +115,69 @@ public class PersonService {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Idempotent upsert keyed on {@code sourceRef} (the normalizer person_id) for the
|
||||
* canonical importer (Phase 3, ADR-025). On first import the canonical fields are
|
||||
* written verbatim. On re-import the human-edit-preserve precedence applies:
|
||||
* a non-blank existing field is never overwritten, and {@code provisional} never
|
||||
* flips back to true once a human has confirmed the person.
|
||||
*/
|
||||
@Transactional
|
||||
public Person upsertBySourceRef(PersonUpsertCommand cmd) {
|
||||
return personRepository.findBySourceRef(cmd.sourceRef())
|
||||
.map(existing -> personRepository.save(mergeCanonical(existing, cmd)))
|
||||
.orElseGet(() -> fromCanonical(cmd));
|
||||
}
|
||||
|
||||
private Person fromCanonical(PersonUpsertCommand cmd) {
|
||||
Person person = personRepository.save(Person.builder()
|
||||
.sourceRef(cmd.sourceRef())
|
||||
.firstName(blankToNull(cmd.firstName()))
|
||||
.lastName(cmd.lastName())
|
||||
.notes(blankToNull(cmd.notes()))
|
||||
.birthYear(cmd.birthYear())
|
||||
.deathYear(cmd.deathYear())
|
||||
.familyMember(cmd.familyMember())
|
||||
.personType(cmd.personType() == null ? PersonType.PERSON : cmd.personType())
|
||||
.provisional(cmd.provisional())
|
||||
.build());
|
||||
String maiden = blankToNull(cmd.maidenName());
|
||||
if (maiden != null) {
|
||||
int nextSortOrder = aliasRepository.findMaxSortOrder(person.getId()) + 1;
|
||||
aliasRepository.save(PersonNameAlias.builder()
|
||||
.person(person)
|
||||
.lastName(maiden)
|
||||
.type(PersonNameAliasType.MAIDEN_NAME)
|
||||
.sortOrder(nextSortOrder)
|
||||
.build());
|
||||
}
|
||||
return person;
|
||||
}
|
||||
|
||||
private Person mergeCanonical(Person existing, PersonUpsertCommand cmd) {
|
||||
existing.setFirstName(preferHuman(existing.getFirstName(), cmd.firstName()));
|
||||
existing.setLastName(preferHuman(existing.getLastName(), cmd.lastName()));
|
||||
existing.setNotes(preferHuman(existing.getNotes(), cmd.notes()));
|
||||
if (existing.getBirthYear() == null) existing.setBirthYear(cmd.birthYear());
|
||||
if (existing.getDeathYear() == null) existing.setDeathYear(cmd.deathYear());
|
||||
if (cmd.personType() != null && existing.getPersonType() == PersonType.PERSON) {
|
||||
existing.setPersonType(cmd.personType());
|
||||
}
|
||||
// provisional is monotonic: once a human confirms a person (false) it never reverts.
|
||||
if (existing.isProvisional()) {
|
||||
existing.setProvisional(cmd.provisional());
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
|
||||
private static String preferHuman(String existing, String canonical) {
|
||||
return (existing == null || existing.isBlank()) ? blankToNull(canonical) : existing;
|
||||
}
|
||||
|
||||
private static String blankToNull(String s) {
|
||||
return (s == null || s.isBlank()) ? null : s.trim();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Person createPerson(String firstName, String lastName, String alias) {
|
||||
Person person = Person.builder()
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.raddatz.familienarchiv.person;
|
||||
|
||||
import lombok.Builder;
|
||||
|
||||
/**
|
||||
* Importer → {@link PersonService} command for an idempotent upsert keyed on
|
||||
* {@code sourceRef} (the normalizer's stable person_id). Carries only the canonical
|
||||
* fields the importer owns; the service applies the human-edit-preserve precedence
|
||||
* (see ADR-025): non-blank existing fields are never overwritten, and {@code provisional}
|
||||
* never flips back to true once a human has confirmed a person.
|
||||
*/
|
||||
@Builder
|
||||
public record PersonUpsertCommand(
|
||||
String sourceRef,
|
||||
String firstName,
|
||||
String lastName,
|
||||
String maidenName,
|
||||
String notes,
|
||||
Integer birthYear,
|
||||
Integer deathYear,
|
||||
boolean familyMember,
|
||||
PersonType personType,
|
||||
boolean provisional
|
||||
) {}
|
||||
Reference in New Issue
Block a user