From f8c1591d213facde781879f9953cd66f41779006 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 20 Jan 2021 16:44:48 -0500 Subject: [PATCH] post CDR review fixes --- .../jpa/mdm/svc/MdmControllerSvcImpl.java | 4 ++-- .../jpa/mdm/svc/MdmLinkUpdaterSvcImpl.java | 3 +-- .../uhn/fhir/mdm/api/IMdmControllerSvc.java | 2 +- .../uhn/fhir/mdm/api/IMdmLinkUpdaterSvc.java | 2 +- .../mdm/provider/MdmProviderDstu3Plus.java | 2 +- .../java/ca/uhn/fhir/mdm/util/TerserUtil.java | 23 +++++++++++++------ .../ca/uhn/fhir/mdm/util/TerserUtilTest.java | 10 +++++--- 7 files changed, 29 insertions(+), 17 deletions(-) diff --git a/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmControllerSvcImpl.java b/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmControllerSvcImpl.java index 209a986fa10..824e6ffd237 100644 --- a/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmControllerSvcImpl.java +++ b/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmControllerSvcImpl.java @@ -81,14 +81,14 @@ public class MdmControllerSvcImpl implements IMdmControllerSvc { } @Override - public IAnyResource updateLink(String theGoldenResourceId, String theSourceResourceId, IAnyResource theManuallyMergedGoldenResource, String theMatchResult, MdmTransactionContext theMdmTransactionContext) { + public IAnyResource updateLink(String theGoldenResourceId, String theSourceResourceId, String theMatchResult, MdmTransactionContext theMdmTransactionContext) { MdmMatchResultEnum matchResult = MdmControllerUtil.extractMatchResultOrNull(theMatchResult); IAnyResource goldenResource = myMdmControllerHelper.getLatestGoldenResourceFromIdOrThrowException(ProviderConstants.MDM_UPDATE_LINK_GOLDEN_RESOURCE_ID, theGoldenResourceId); IAnyResource source = myMdmControllerHelper.getLatestSourceFromIdOrThrowException(ProviderConstants.MDM_UPDATE_LINK_RESOURCE_ID, theSourceResourceId); myMdmControllerHelper.validateSameVersion(goldenResource, theGoldenResourceId); myMdmControllerHelper.validateSameVersion(source, theSourceResourceId); - return myIMdmLinkUpdaterSvc.updateLink(goldenResource, source, theManuallyMergedGoldenResource, matchResult, theMdmTransactionContext); + return myIMdmLinkUpdaterSvc.updateLink(goldenResource, source, matchResult, theMdmTransactionContext); } @Override diff --git a/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmLinkUpdaterSvcImpl.java b/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmLinkUpdaterSvcImpl.java index ce52001f4c5..20de6cd705d 100644 --- a/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmLinkUpdaterSvcImpl.java +++ b/hapi-fhir-jpaserver-mdm/src/main/java/ca/uhn/fhir/jpa/mdm/svc/MdmLinkUpdaterSvcImpl.java @@ -69,8 +69,7 @@ public class MdmLinkUpdaterSvcImpl implements IMdmLinkUpdaterSvc { @Transactional @Override - public IAnyResource updateLink(IAnyResource theGoldenResource, IAnyResource theSourceResource, IAnyResource theManuallyMergedGoldenResource, - MdmMatchResultEnum theMatchResult, MdmTransactionContext theMdmContext) { + public IAnyResource updateLink(IAnyResource theGoldenResource, IAnyResource theSourceResource, MdmMatchResultEnum theMatchResult, MdmTransactionContext theMdmContext) { String sourceType = myFhirContext.getResourceType(theSourceResource); validateUpdateLinkRequest(theGoldenResource, theSourceResource, theMatchResult, sourceType); diff --git a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/api/IMdmControllerSvc.java b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/api/IMdmControllerSvc.java index 0d2d4cb5762..fb0e9ebe981 100644 --- a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/api/IMdmControllerSvc.java +++ b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/api/IMdmControllerSvc.java @@ -36,5 +36,5 @@ public interface IMdmControllerSvc { IAnyResource mergeGoldenResources(String theFromGoldenResourceId, String theToGoldenResourceId, IAnyResource theManuallyMergedGoldenResource, MdmTransactionContext theMdmTransactionContext); - IAnyResource updateLink(String theGoldenResourceId, String theSourceResourceId, IAnyResource theManuallyMergedGoldenResource, String theMatchResult, MdmTransactionContext theMdmTransactionContext); + IAnyResource updateLink(String theGoldenResourceId, String theSourceResourceId, String theMatchResult, MdmTransactionContext theMdmTransactionContext); } diff --git a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/api/IMdmLinkUpdaterSvc.java b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/api/IMdmLinkUpdaterSvc.java index 27d1583d28b..0c5597e9b73 100644 --- a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/api/IMdmLinkUpdaterSvc.java +++ b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/api/IMdmLinkUpdaterSvc.java @@ -24,6 +24,6 @@ import ca.uhn.fhir.mdm.model.MdmTransactionContext; import org.hl7.fhir.instance.model.api.IAnyResource; public interface IMdmLinkUpdaterSvc { - IAnyResource updateLink(IAnyResource theGoldenResource, IAnyResource theSourceResource, IAnyResource theManuallyMergedGoldenResource, MdmMatchResultEnum theMatchResult, MdmTransactionContext theMdmContext); + IAnyResource updateLink(IAnyResource theGoldenResource, IAnyResource theSourceResource, MdmMatchResultEnum theMatchResult, MdmTransactionContext theMdmContext); void notDuplicateGoldenResource(IAnyResource theGoldenResource, IAnyResource theTargetGoldenResource, MdmTransactionContext theMdmContext); } diff --git a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/provider/MdmProviderDstu3Plus.java b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/provider/MdmProviderDstu3Plus.java index 56c0365a121..45f9d2031a4 100644 --- a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/provider/MdmProviderDstu3Plus.java +++ b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/provider/MdmProviderDstu3Plus.java @@ -161,7 +161,7 @@ public class MdmProviderDstu3Plus extends BaseMdmProvider { @OperationParam(name = ProviderConstants.MDM_UPDATE_LINK_MATCH_RESULT, min = 1, max = 1) IPrimitiveType theMatchResult, ServletRequestDetails theRequestDetails) { validateUpdateLinkParameters(theGoldenResourceId, theResourceId, theMatchResult); - return myMdmControllerSvc.updateLink(theGoldenResourceId.getValueAsString(), theResourceId.getValue(), null, + return myMdmControllerSvc.updateLink(theGoldenResourceId.getValueAsString(), theResourceId.getValue(), theMatchResult.getValue(), createMdmContext(theRequestDetails, MdmTransactionContext.OperationType.UPDATE_LINK, getResourceType(ProviderConstants.MDM_UPDATE_LINK_GOLDEN_RESOURCE_ID, theGoldenResourceId)) ); diff --git a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/util/TerserUtil.java b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/util/TerserUtil.java index a7135b35ffb..88c75228246 100644 --- a/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/util/TerserUtil.java +++ b/hapi-fhir-server-mdm/src/main/java/ca/uhn/fhir/mdm/util/TerserUtil.java @@ -185,12 +185,13 @@ public final class TerserUtil { } } - public static void replaceField(FhirContext theFhirContext, FhirTerser theTerser, String theFieldName, IBaseResource theFrom, IBaseResource theTo) { - FhirTerser terser = theFhirContext.newTerser(); + public static boolean fieldExists(FhirContext theFhirContext, String theFieldName, IBaseResource theInstance) { + RuntimeResourceDefinition definition = theFhirContext.getResourceDefinition(theInstance); + return definition.getChildByName(theFieldName) != null; + } - RuntimeResourceDefinition definition = theFhirContext.getResourceDefinition(theFrom); - BaseRuntimeChildDefinition childDefinition = definition.getChildByName(theFieldName); - replaceField(theFrom, theTo, childDefinition); + public static void replaceField(FhirContext theFhirContext, FhirTerser theTerser, String theFieldName, IBaseResource theFrom, IBaseResource theTo) { + replaceField(theFrom, theTo, getBaseRuntimeChildDefinition(theFhirContext, theFieldName, theFrom)); } private static void replaceField(IBaseResource theFrom, IBaseResource theTo, BaseRuntimeChildDefinition childDefinition) { @@ -231,8 +232,7 @@ public final class TerserUtil { * @param theTo */ public static void mergeField(FhirContext theFhirContext, FhirTerser theTerser, String theFieldName, IBaseResource theFrom, IBaseResource theTo) { - RuntimeResourceDefinition definition = theFhirContext.getResourceDefinition(theFrom); - BaseRuntimeChildDefinition childDefinition = definition.getChildByName(theFieldName); + BaseRuntimeChildDefinition childDefinition = getBaseRuntimeChildDefinition(theFhirContext, theFieldName, theFrom); List theFromFieldValues = childDefinition.getAccessor().getValues(theFrom); List theToFieldValues = childDefinition.getAccessor().getValues(theTo); @@ -240,6 +240,15 @@ public final class TerserUtil { mergeFields(theTerser, theTo, childDefinition, theFromFieldValues, theToFieldValues); } + private static BaseRuntimeChildDefinition getBaseRuntimeChildDefinition(FhirContext theFhirContext, String theFieldName, IBaseResource theFrom) { + RuntimeResourceDefinition definition = theFhirContext.getResourceDefinition(theFrom); + BaseRuntimeChildDefinition childDefinition = definition.getChildByName(theFieldName); + if (childDefinition == null) { + throw new IllegalStateException(String.format("Field %s does not exist", theFieldName)); + } + return childDefinition; + } + private static void mergeFields(FhirTerser theTerser, IBaseResource theTo, BaseRuntimeChildDefinition childDefinition, List theFromFieldValues, List theToFieldValues) { for (IBase theFromFieldValue : theFromFieldValues) { if (contains(theFromFieldValue, theToFieldValues)) { diff --git a/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/util/TerserUtilTest.java b/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/util/TerserUtilTest.java index 5ed84e4f207..c0eb2a2b09a 100644 --- a/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/util/TerserUtilTest.java +++ b/hapi-fhir-server-mdm/src/test/java/ca/uhn/fhir/mdm/util/TerserUtilTest.java @@ -8,9 +8,7 @@ import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasSize; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; class TerserUtilTest extends BaseR4Test { @@ -30,6 +28,12 @@ class TerserUtilTest extends BaseR4Test { assertEquals(p1.getIdentifier().get(0).getValue(), p2.getIdentifier().get(0).getValue()); } + @Test + void testFieldExists() { + assertTrue(TerserUtil.fieldExists(ourFhirContext, "identifier", new Patient())); + assertFalse(TerserUtil.fieldExists(ourFhirContext, "randomFieldName", new Patient())); + } + @Test void testCloneFields() { Patient p1 = buildJohny();