Merge branch 'gg_20201105-remove-person-references' of github.com:jamesagnew/hapi-fhir into gg_20201105-remove-person-references

This commit is contained in:
Tadgh 2020-11-11 14:15:58 -05:00
commit 3d34502fae
11 changed files with 132 additions and 133 deletions

View File

@ -156,7 +156,8 @@ public class EmpiLink {
}
public EmpiLink setSourceResourcePid(Long theSourceResourcePid) {
myPersonPid = theSourceResourcePid;
setPersonPid(theSourceResourcePid);
mySourceResourcePid = theSourceResourcePid;
return this;
}

View File

@ -47,6 +47,7 @@ import java.util.Set;
import java.util.stream.Collectors;
public class EmpiLinkDaoSvc {
private static final Logger ourLog = Logs.getEmpiTroubleshootingLog();
@Autowired
@ -330,33 +331,4 @@ public class EmpiLinkDaoSvc {
return myEmpiLinkFactory.newEmpiLink();
}
// @Transactional
// public List<EmpiLink> deleteEmpiLinks(IAnyResource theSourceResource, IAnyResource theTargetResource) {
// Long sourceResourcePid = myIdHelperService.getPidOrNull(theSourceResource);
// if (sourceResourcePid == null) {
// if (ourLog.isDebugEnabled()) {
// ourLog.debug(String.format("Unable to find source resource for ID %s, no links deleted", sourceResourcePid));
// }
// return new ArrayList<>();
// }
//
// Long targetResourcePid = myIdHelperService.getPidOrNull(theSourceResource);
// if (targetResourcePid == null) {
// if (ourLog.isDebugEnabled()) {
// ourLog.debug(String.format("Unable to find target resource for ID %s, no links deleted", targetResourcePid));
// }
//
// return new ArrayList<>();
// }
//
// EmpiLink exampleLink = myEmpiLinkFactory
// .newEmpiLink()
// .setSourceResourcePid(sourceResourcePid)
// .setTargetPid(targetResourcePid);
//
// Example<EmpiLink> example = Example.of(exampleLink);
// List<EmpiLink> linksToDelete = myEmpiLinkDao.findAll(example);
// myEmpiLinkDao.deleteAll(linksToDelete);
// return linksToDelete;
// }
}

View File

@ -102,26 +102,26 @@ public class EmpiLinkSvcImpl implements IEmpiLinkSvc {
myEmpiLinkDaoSvc.getEmpiLinksByPersonPidTargetPidAndMatchResult(targetId, personId, EmpiMatchResultEnum.NO_MATCH).isPresent();
}
@Override
@Transactional
public void syncEmpiLinksToPersonLinks(IAnyResource thePersonResource, EmpiTransactionContext theEmpiTransactionContext) {
// int origLinkCount = myPersonHelper.getLinkCount(thePersonResource);
int origLinkCount = myEmpiLinkDaoSvc.findEmpiMatchLinksBySource(thePersonResource).size();
List<EmpiLink> empiLinks = myEmpiLinkDaoSvc.findEmpiLinksBySourceResource(thePersonResource);
List<IBaseBackboneElement> newLinks = empiLinks.stream()
.filter(link -> link.isMatch() || link.isPossibleMatch() || link.isRedirect())
.map(this::personLinkFromEmpiLink)
.collect(Collectors.toList());
myPersonHelper.setLinks(thePersonResource, newLinks);
if (newLinks.size() > origLinkCount) {
log(theEmpiTransactionContext, thePersonResource.getIdElement().toVersionless() + " links increased from " + origLinkCount + " to " + newLinks.size());
} else if (newLinks.size() < origLinkCount) {
log(theEmpiTransactionContext, thePersonResource.getIdElement().toVersionless() + " links decreased from " + origLinkCount + " to " + newLinks.size());
}
}
// @Override
// @Transactional
// public void syncEmpiLinksToPersonLinks(IAnyResource thePersonResource, EmpiTransactionContext theEmpiTransactionContext) {
// // int origLinkCount = myPersonHelper.getLinkCount(thePersonResource);
// int origLinkCount = myEmpiLinkDaoSvc.findEmpiMatchLinksBySource(thePersonResource).size();
//
// List<EmpiLink> empiLinks = myEmpiLinkDaoSvc.findEmpiLinksBySourceResource(thePersonResource);
//
// List<IBaseBackboneElement> newLinks = empiLinks.stream()
// .filter(link -> link.isMatch() || link.isPossibleMatch() || link.isRedirect())
// .map(this::personLinkFromEmpiLink)
// .collect(Collectors.toList());
// myPersonHelper.setLinks(thePersonResource, newLinks);
// if (newLinks.size() > origLinkCount) {
// log(theEmpiTransactionContext, thePersonResource.getIdElement().toVersionless() + " links increased from " + origLinkCount + " to " + newLinks.size());
// } else if (newLinks.size() < origLinkCount) {
// log(theEmpiTransactionContext, thePersonResource.getIdElement().toVersionless() + " links decreased from " + origLinkCount + " to " + newLinks.size());
// }
//
// }
@Override
public void deleteLink(IAnyResource theSourceResource, IAnyResource theTargetResource, EmpiTransactionContext theEmpiTransactionContext) {

View File

@ -81,7 +81,7 @@ public class EmpiLinkUpdaterSvcImpl implements IEmpiLinkUpdaterSvc {
empiLink.setMatchResult(theMatchResult);
empiLink.setLinkSource(EmpiLinkSourceEnum.MANUAL);
myEmpiLinkDaoSvc.save(empiLink);
myEmpiLinkSvc.syncEmpiLinksToPersonLinks(thePerson, theEmpiContext);
// myEmpiLinkSvc.syncEmpiLinksToPersonLinks(thePerson, theEmpiContext);
myEmpiResourceDaoSvc.upsertSourceResource(thePerson, theEmpiContext.getResourceType());
if (theMatchResult == EmpiMatchResultEnum.NO_MATCH) {
// Need to find a new Person to link this target to

View File

@ -57,21 +57,21 @@ public class EmpiPersonMergerSvcImpl implements IEmpiPersonMergerSvc {
@Override
@Transactional
public IAnyResource mergePersons(IAnyResource theFromPerson, IAnyResource theToPerson, EmpiTransactionContext theEmpiTransactionContext) {
Long toPersonPid = myIdHelperService.getPidOrThrowException(theToPerson);
public IAnyResource mergePersons(IAnyResource theFrom, IAnyResource theTo, EmpiTransactionContext theEmpiTransactionContext) {
Long toPid = myIdHelperService.getPidOrThrowException(theTo);
// TODO NG - Revisit when merge rules are defined
// myPersonHelper.mergeFields(theFrom, theTo);
mergeLinks(theFrom, theTo, toPid, theEmpiTransactionContext);
refreshLinksAndUpdatePerson(theTo, theEmpiTransactionContext);
myPersonHelper.mergePersonFields(theFromPerson, theToPerson);
mergeLinks(theFromPerson, theToPerson, toPersonPid, theEmpiTransactionContext);
refreshLinksAndUpdatePerson(theToPerson, theEmpiTransactionContext);
Long fromPersonPid = myIdHelperService.getPidOrThrowException(theFrom);
addMergeLink(fromPersonPid, toPid);
myPersonHelper.deactivateResource(theFrom);
Long fromPersonPid = myIdHelperService.getPidOrThrowException(theFromPerson);
addMergeLink(fromPersonPid, toPersonPid);
myPersonHelper.deactivatePerson(theFromPerson);
refreshLinksAndUpdatePerson(theFrom, theEmpiTransactionContext);
refreshLinksAndUpdatePerson(theFromPerson, theEmpiTransactionContext);
log(theEmpiTransactionContext, "Merged " + theFromPerson.getIdElement().toVersionless() + " into " + theToPerson.getIdElement().toVersionless());
return theToPerson;
log(theEmpiTransactionContext, "Merged " + theFrom.getIdElement().toVersionless() + " into " + theTo.getIdElement().toVersionless());
return theTo;
}
private void addMergeLink(Long theDeactivatedPersonPid, Long theActivePersonPid) {
@ -84,7 +84,7 @@ public class EmpiPersonMergerSvcImpl implements IEmpiPersonMergerSvc {
}
private void refreshLinksAndUpdatePerson(IAnyResource theToPerson, EmpiTransactionContext theEmpiTransactionContext) {
myEmpiLinkSvc.syncEmpiLinksToPersonLinks(theToPerson, theEmpiTransactionContext);
// myEmpiLinkSvc.syncEmpiLinksToPersonLinks(theToPerson, theEmpiTransactionContext);
myEmpiResourceDaoSvc.upsertSourceResource(theToPerson, theEmpiTransactionContext.getResourceType());
}

View File

@ -61,7 +61,7 @@ public class EmpiProviderUpdateLinkR4Test extends BaseLinkR4Test {
@Test
public void testUpdateLinkTwiceWorksWhenNoVersionProvided() {
myEmpiProviderR4.updateLink(mySourcePatientId, myPatientId, MATCH_RESULT, myRequestDetails);
Person person = (Person)myEmpiProviderR4.updateLink(myVersionlessPersonId, myPatientId, NO_MATCH_RESULT, myRequestDetails);
Person person = (Person) myEmpiProviderR4.updateLink(myVersionlessPersonId, myPatientId, NO_MATCH_RESULT, myRequestDetails);
assertThat(person.getLink(), hasSize(0));
}

View File

@ -143,7 +143,7 @@ public class EmpiLinkSvcTest extends BaseEmpiR4Test {
// Test: it should be impossible to have a AUTO NO_MATCH record. The only NO_MATCH records in the system must be MANUAL.
try {
myEmpiLinkSvc.updateLink(sourcePatient, patient, EmpiMatchOutcome.NO_MATCH, EmpiLinkSourceEnum.AUTO, null);
myEmpiLinkSvc.updateLink(sourcePatient, patient, EmpiMatchOutcome.NO_MATCH, EmpiLinkSourceEnum.AUTO, createContextForUpdate("Patient"));
fail();
} catch (InternalErrorException e) {
assertThat(e.getMessage(), is(equalTo("EMPI system is not allowed to automatically NO_MATCH a resource")));
@ -159,7 +159,7 @@ public class EmpiLinkSvcTest extends BaseEmpiR4Test {
myEmpiLinkDaoSvc.createOrUpdateLinkEntity(sourcePatient, patient1, EmpiMatchOutcome.NEW_PERSON_MATCH, EmpiLinkSourceEnum.MANUAL, createContextForCreate("Patient"));
myEmpiLinkDaoSvc.createOrUpdateLinkEntity(sourcePatient, patient2, EmpiMatchOutcome.NO_MATCH, EmpiLinkSourceEnum.MANUAL, createContextForCreate("Patient"));
myEmpiLinkSvc.syncEmpiLinksToPersonLinks(sourcePatient, createContextForCreate("Patient"));
// myEmpiLinkSvc.syncEmpiLinksToPersonLinks(sourcePatient, createContextForCreate("Patient"));
assertTrue(sourcePatient.hasLink());
//TODO GGG update this test once we decide what has to happen here. There is no more "syncing links"
//assertEquals(patient1.getIdElement().toVersionless().getValue(), sourcePatient.getLinkFirstRep().getTarget().getReference());

View File

@ -114,12 +114,19 @@ public class EmpiPersonMergerSvcTest extends BaseEmpiR4Test {
}
private EmpiTransactionContext createEmpiContext() {
return new EmpiTransactionContext(TransactionLogMessages.createFromTransactionGuid(UUID.randomUUID().toString()), EmpiTransactionContext.OperationType.MERGE_PERSONS);
EmpiTransactionContext empiTransactionContext = new EmpiTransactionContext(TransactionLogMessages.createFromTransactionGuid(UUID.randomUUID().toString()), EmpiTransactionContext.OperationType.MERGE_PERSONS);
empiTransactionContext.setResourceType("Patient");
return empiTransactionContext;
}
@Test
public void mergeRemovesPossibleDuplicatesLink() {
EmpiLink empiLink = myEmpiLinkDaoSvc.newEmpiLink().setSourceResourcePid(myToSourcePatientPid).setTargetPid(myFromSourcePatientPid).setMatchResult(EmpiMatchResultEnum.POSSIBLE_DUPLICATE).setLinkSource(EmpiLinkSourceEnum.AUTO);
EmpiLink empiLink = myEmpiLinkDaoSvc.newEmpiLink()
.setSourceResourcePid(myToSourcePatientPid)
.setTargetPid(myFromSourcePatientPid)
.setMatchResult(EmpiMatchResultEnum.POSSIBLE_DUPLICATE)
.setLinkSource(EmpiLinkSourceEnum.AUTO);
saveLink(empiLink);
{
@ -142,10 +149,11 @@ public class EmpiPersonMergerSvcTest extends BaseEmpiR4Test {
populatePerson(myFromSourcePatient);
Patient mergedSourcePatient = mergeSourcePatients();
HumanName returnedName = mergedSourcePatient.getNameFirstRep();
assertEquals(GIVEN_NAME, returnedName.getGivenAsSingleString());
assertEquals(FAMILY_NAME, returnedName.getFamily());
assertEquals(POSTAL_CODE, mergedSourcePatient.getAddressFirstRep().getPostalCode());
// TODO NG - Revisit when rules are ready
// HumanName returnedName = mergedSourcePatient.getNameFirstRep();
// assertEquals(GIVEN_NAME, returnedName.getGivenAsSingleString());
// assertEquals(FAMILY_NAME, returnedName.getFamily());
// assertEquals(POSTAL_CODE, mergedSourcePatient.getAddressFirstRep().getPostalCode());
}
@Test
@ -351,37 +359,39 @@ public class EmpiPersonMergerSvcTest extends BaseEmpiR4Test {
@Test
public void testMergeNames() {
myFromSourcePatient.addName().addGiven("Jim");
myFromSourcePatient.getNameFirstRep().addGiven("George");
assertThat(myFromSourcePatient.getName(), hasSize(1));
assertThat(myFromSourcePatient.getName().get(0).getGiven(), hasSize(2));
myToSourcePatient.addName().addGiven("Jeff");
myToSourcePatient.getNameFirstRep().addGiven("George");
assertThat(myToSourcePatient.getName(), hasSize(1));
assertThat(myToSourcePatient.getName().get(0).getGiven(), hasSize(2));
Patient mergedSourcePatient = mergeSourcePatients();
assertThat(mergedSourcePatient.getName(), hasSize(2));
assertThat(mergedSourcePatient.getName().get(0).getGiven(), hasSize(2));
assertThat(mergedSourcePatient.getName().get(1).getGiven(), hasSize(2));
// TODO NG - Revisit when rules are available
// myFromSourcePatient.addName().addGiven("Jim");
// myFromSourcePatient.getNameFirstRep().addGiven("George");
// assertThat(myFromSourcePatient.getName(), hasSize(1));
// assertThat(myFromSourcePatient.getName().get(0).getGiven(), hasSize(2));
//
// myToSourcePatient.addName().addGiven("Jeff");
// myToSourcePatient.getNameFirstRep().addGiven("George");
// assertThat(myToSourcePatient.getName(), hasSize(1));
// assertThat(myToSourcePatient.getName().get(0).getGiven(), hasSize(2));
//
// Patient mergedSourcePatient = mergeSourcePatients();
// assertThat(mergedSourcePatient.getName(), hasSize(2));
// assertThat(mergedSourcePatient.getName().get(0).getGiven(), hasSize(2));
// assertThat(mergedSourcePatient.getName().get(1).getGiven(), hasSize(2));
}
@Test
public void testMergeNamesAllSame() {
myFromSourcePatient.addName().addGiven("Jim");
myFromSourcePatient.getNameFirstRep().addGiven("George");
assertThat(myFromSourcePatient.getName(), hasSize(1));
assertThat(myFromSourcePatient.getName().get(0).getGiven(), hasSize(2));
myToSourcePatient.addName().addGiven("Jim");
myToSourcePatient.getNameFirstRep().addGiven("George");
assertThat(myToSourcePatient.getName(), hasSize(1));
assertThat(myToSourcePatient.getName().get(0).getGiven(), hasSize(2));
mergeSourcePatients();
assertThat(myToSourcePatient.getName(), hasSize(1));
assertThat(myToSourcePatient.getName().get(0).getGiven(), hasSize(2));
// TODO NG - Revisit when rules are available
// myFromSourcePatient.addName().addGiven("Jim");
// myFromSourcePatient.getNameFirstRep().addGiven("George");
// assertThat(myFromSourcePatient.getName(), hasSize(1));
// assertThat(myFromSourcePatient.getName().get(0).getGiven(), hasSize(2));
//
// myToSourcePatient.addName().addGiven("Jim");
// myToSourcePatient.getNameFirstRep().addGiven("George");
// assertThat(myToSourcePatient.getName(), hasSize(1));
// assertThat(myToSourcePatient.getName().get(0).getGiven(), hasSize(2));
//
// mergeSourcePatients();
// assertThat(myToSourcePatient.getName(), hasSize(1));
// assertThat(myToSourcePatient.getName().get(0).getGiven(), hasSize(2));
}
@Test

View File

@ -32,7 +32,7 @@ public class EmpiResourceDaoSvcTest extends BaseEmpiR4Test {
badSourcePatient.setActive(false);
myPatientDao.update(badSourcePatient);
Optional<IAnyResource> foundPerson = myResourceDaoSvc.searchSourceResourceByEID(TEST_EID, "Person");
Optional<IAnyResource> foundPerson = myResourceDaoSvc.searchSourceResourceByEID(TEST_EID, "Patient");
assertTrue(foundPerson.isPresent());
assertThat(foundPerson.get().getIdElement().toUnqualifiedVersionless().getValue(), is(goodSourcePatient.getIdElement().toUnqualifiedVersionless().getValue()));
}

View File

@ -36,11 +36,11 @@ public interface IEmpiLinkSvc {
*/
void updateLink(IAnyResource thePerson, IAnyResource theTargetResource, EmpiMatchOutcome theMatchResult, EmpiLinkSourceEnum theLinkSource, EmpiTransactionContext theEmpiTransactionContext);
/**
* Replace Person.link values from what they should be based on EmpiLink values
* @param thePersonResource the person to correct the links on.
*/
void syncEmpiLinksToPersonLinks(IAnyResource thePersonResource, EmpiTransactionContext theEmpiTransactionContext);
// /**
// * Replace Person.link values from what they should be based on EmpiLink values
// * @param thePersonResource the person to correct the links on.
// */
// void syncEmpiLinksToPersonLinks(IAnyResource thePersonResource, EmpiTransactionContext theEmpiTransactionContext);
/**
* Delete a link between given Person and target patient/practitioner

View File

@ -102,20 +102,24 @@ public class PersonHelper {
addHapiEidIfNoExternalEidIsPresent(newSourceResource, sourceResourceIdentifier, theIncomingResource);
setActive(newSourceResource, resourceDefinition);
setActive(newSourceResource, resourceDefinition, true);
EmpiUtil.setEmpiManaged(newSourceResource);
return (T) newSourceResource;
}
private void setActive(IBaseResource theNewSourceResource, RuntimeResourceDefinition theResourceDefinition) {
private void setActive(IBaseResource theResource, boolean theActiveFlag) {
setActive(theResource, myFhirContext.getResourceDefinition(theResource), theActiveFlag);
}
private void setActive(IBaseResource theNewSourceResource, RuntimeResourceDefinition theResourceDefinition, boolean theActiveFlag) {
BaseRuntimeChildDefinition activeChildDefinition = theResourceDefinition.getChildByName("active");
if (activeChildDefinition == null) {
ourLog.warn(String.format("Unable to set active flag on the provided source resource %s.", theNewSourceResource));
return;
}
activeChildDefinition.getMutator().setValue(theNewSourceResource, toBooleanType(true));
activeChildDefinition.getMutator().setValue(theNewSourceResource, toBooleanType(theActiveFlag));
}
/**
@ -299,6 +303,16 @@ public class PersonHelper {
throw new IllegalStateException("Unsupported FHIR version " + myFhirContext.getVersion().getVersion());
}
private <T extends IBase> boolean fromBooleanType(T theFlag) {
switch (myFhirContext.getVersion().getVersion()) {
case R4:
return ((BooleanType)theFlag).booleanValue();
case DSTU3:
return ((org.hl7.fhir.dstu3.model.BooleanType)theFlag).booleanValue();
}
throw new IllegalStateException("Unsupported FHIR version " + myFhirContext.getVersion().getVersion());
}
/**
* To avoid adding duplicate
*
@ -315,7 +329,7 @@ public class PersonHelper {
}
}
public void mergePersonFields(IBaseResource theFromPerson, IBaseResource theToPerson) {
public void mergeFields(IBaseResource theFromPerson, IBaseResource theToPerson) {
switch (myFhirContext.getVersion().getVersion()) {
case R4:
mergeR4PersonFields(theFromPerson, theToPerson);
@ -460,31 +474,33 @@ public class PersonHelper {
}
}
public void deactivatePerson(IAnyResource thePerson) {
switch (myFhirContext.getVersion().getVersion()) {
case R4:
Person personR4 = (Person) thePerson;
personR4.setActive(false);
break;
case DSTU3:
org.hl7.fhir.dstu3.model.Person personStu3 = (org.hl7.fhir.dstu3.model.Person) thePerson;
personStu3.setActive(false);
break;
default:
throw new UnsupportedOperationException("Version not supported: " + myFhirContext.getVersion().getVersion());
}
public void deactivateResource(IAnyResource theResource) {
// get a ref to the actual ID Field
setActive(theResource, myFhirContext.getResourceDefinition(theResource), false);
}
public boolean isDeactivated(IBaseResource thePerson) {
switch (myFhirContext.getVersion().getVersion()) {
case R4:
Person personR4 = (Person) thePerson;
return !personR4.getActive();
case DSTU3:
org.hl7.fhir.dstu3.model.Person personStu3 = (org.hl7.fhir.dstu3.model.Person) thePerson;
return !personStu3.getActive();
default:
throw new UnsupportedOperationException("Version not supported: " + myFhirContext.getVersion().getVersion());
}
RuntimeResourceDefinition resourceDefinition = myFhirContext.getResourceDefinition(thePerson);
BaseRuntimeChildDefinition activeChildDefinition = resourceDefinition.getChildByName("active");
Optional<IBase> value = activeChildDefinition.getAccessor().getFirstValueOrNull(thePerson);
return value.map(v -> {
return !fromBooleanType(v);
}).orElseThrow(
() -> new UnsupportedOperationException(String.format("Resource %s does not support deactivation", resourceDefinition.getName()))
);
//
// }
// switch (myFhirContext.getVersion().getVersion()) {
// case R4:
// Person personR4 = (Person) thePerson;
// return !personR4.getActive();
// case DSTU3:
// org.hl7.fhir.dstu3.model.Person personStu3 = (org.hl7.fhir.dstu3.model.Person) thePerson;
// return !personStu3.getActive();
// default:
// throw
// }
}
}