3163 refactor some base mdm stuff (#3166)

Co-authored-by: leif stawnyczy <leifstawnyczy@leifs-MacBook-Pro.local>
This commit is contained in:
TipzCM 2021-11-16 10:47:44 -05:00 committed by GitHub
parent 843517f7ba
commit 7e291bb469
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 107 additions and 69 deletions

View File

@ -257,8 +257,16 @@ public class MdmConsumerConfig {
}
@Bean
MdmControllerHelper mdmProviderHelper(FhirContext theFhirContext, IResourceLoader theResourceLoader, IMdmSettings theMdmSettings, MessageHelper messageHelper) {
return new MdmControllerHelper(theFhirContext, theResourceLoader, theMdmSettings, messageHelper);
MdmControllerHelper mdmProviderHelper(FhirContext theFhirContext,
IResourceLoader theResourceLoader,
IMdmSettings theMdmSettings,
IMdmMatchFinderSvc theMdmMatchFinderSvc,
MessageHelper messageHelper) {
return new MdmControllerHelper(theFhirContext,
theResourceLoader,
theMdmMatchFinderSvc,
theMdmSettings,
messageHelper);
}
@Bean

View File

@ -5,6 +5,7 @@ import ca.uhn.fhir.mdm.api.IMdmClearJobSubmitter;
import ca.uhn.fhir.mdm.api.IMdmControllerSvc;
import ca.uhn.fhir.mdm.api.IMdmMatchFinderSvc;
import ca.uhn.fhir.mdm.api.IMdmSubmitSvc;
import ca.uhn.fhir.mdm.provider.MdmControllerHelper;
import ca.uhn.fhir.mdm.provider.MdmProviderDstu3Plus;
import ca.uhn.fhir.mdm.rules.config.MdmSettings;
import ca.uhn.fhir.mdm.util.MessageHelper;
@ -29,8 +30,6 @@ import java.util.List;
public abstract class BaseProviderR4Test extends BaseMdmR4Test {
MdmProviderDstu3Plus myMdmProvider;
@Autowired
private IMdmMatchFinderSvc myMdmMatchFinderSvc;
@Autowired
private IMdmControllerSvc myMdmControllerSvc;
@Autowired
private IMdmClearJobSubmitter myMdmClearJobSubmitter;
@ -39,6 +38,8 @@ public abstract class BaseProviderR4Test extends BaseMdmR4Test {
@Autowired
private MdmSettings myMdmSettings;
@Autowired
private MdmControllerHelper myMdmHelper;
@Autowired
BatchJobHelper myBatchJobHelper;
@Autowired
MessageHelper myMessageHelper;
@ -55,7 +56,7 @@ public abstract class BaseProviderR4Test extends BaseMdmR4Test {
@BeforeEach
public void before() {
myMdmProvider = new MdmProviderDstu3Plus(myFhirContext, myMdmControllerSvc, myMdmMatchFinderSvc, myMdmSubmitSvc, myMdmSettings);
myMdmProvider = new MdmProviderDstu3Plus(myFhirContext, myMdmControllerSvc, myMdmHelper, myMdmSubmitSvc, myMdmSettings);
defaultScript = myMdmSettings.getScriptText();
}

View File

@ -21,20 +21,36 @@ package ca.uhn.fhir.mdm.provider;
*/
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.mdm.api.IMdmMatchFinderSvc;
import ca.uhn.fhir.mdm.api.IMdmSettings;
import ca.uhn.fhir.mdm.api.MatchedTarget;
import ca.uhn.fhir.mdm.api.MdmConstants;
import ca.uhn.fhir.mdm.util.MdmResourceUtil;
import ca.uhn.fhir.mdm.util.MessageHelper;
import ca.uhn.fhir.model.primitive.IdDt;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.ResourceVersionConflictException;
import ca.uhn.fhir.rest.server.provider.ProviderConstants;
import ca.uhn.fhir.util.BundleBuilder;
import ca.uhn.fhir.validation.IResourceLoader;
import org.hl7.fhir.instance.model.api.IAnyResource;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseBackboneElement;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IBaseDatatype;
import org.hl7.fhir.instance.model.api.IBaseExtension;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Nonnull;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service
public class MdmControllerHelper {
@ -42,12 +58,18 @@ public class MdmControllerHelper {
private final IResourceLoader myResourceLoader;
private final IMdmSettings myMdmSettings;
private final MessageHelper myMessageHelper;
private final IMdmMatchFinderSvc myMdmMatchFinderSvc;
@Autowired
public MdmControllerHelper(FhirContext theFhirContext, IResourceLoader theResourceLoader, IMdmSettings theMdmSettings, MessageHelper theMessageHelper) {
public MdmControllerHelper(FhirContext theFhirContext,
IResourceLoader theResourceLoader,
IMdmMatchFinderSvc theMdmMatchFinderSvc,
IMdmSettings theMdmSettings,
MessageHelper theMessageHelper) {
myFhirContext = theFhirContext;
myResourceLoader = theResourceLoader;
myMdmSettings = theMdmSettings;
myMdmMatchFinderSvc = theMdmMatchFinderSvc;
myMessageHelper = theMessageHelper;
}
@ -104,4 +126,58 @@ public class MdmControllerHelper {
throw new InvalidRequestException(myMessageHelper.getMessageForUnmanagedResource());
}
}
/**
* Helper method which will return a bundle of all Matches and Possible Matches.
*/
public IBaseBundle getMatchesAndPossibleMatchesForResource(IAnyResource theResource, String theResourceType) {
List<MatchedTarget> matches = myMdmMatchFinderSvc.getMatchedTargets(theResourceType, theResource);
matches.sort(Comparator.comparing((MatchedTarget m) -> m.getMatchResult().getNormalizedScore()).reversed());
BundleBuilder builder = new BundleBuilder(myFhirContext);
builder.setBundleField("type", "searchset");
builder.setBundleField("id", UUID.randomUUID().toString());
builder.setMetaField("lastUpdated", builder.newPrimitive("instant", new Date()));
IBaseBundle retVal = builder.getBundle();
for (MatchedTarget next : matches) {
boolean shouldKeepThisEntry = next.isMatch() || next.isPossibleMatch();
if (!shouldKeepThisEntry) {
continue;
}
IBase entry = builder.addEntry();
builder.addToEntry(entry, "resource", next.getTarget());
IBaseBackboneElement search = builder.addSearch(entry);
toBundleEntrySearchComponent(builder, search, next);
}
return retVal;
}
public IBaseBackboneElement toBundleEntrySearchComponent(BundleBuilder theBuilder, IBaseBackboneElement theSearch, MatchedTarget theMatchedTarget) {
theBuilder.setSearchField(theSearch, "mode", "match");
double score = theMatchedTarget.getMatchResult().getNormalizedScore();
theBuilder.setSearchField(theSearch, "score",
theBuilder.newPrimitive("decimal", BigDecimal.valueOf(score)));
String matchGrade = getMatchGrade(theMatchedTarget);
IBaseDatatype codeType = (IBaseDatatype) myFhirContext.getElementDefinition("code").newInstance(matchGrade);
IBaseExtension searchExtension = theSearch.addExtension();
searchExtension.setUrl(MdmConstants.FIHR_STRUCTURE_DEF_MATCH_GRADE_URL_NAMESPACE);
searchExtension.setValue(codeType);
return theSearch;
}
@Nonnull
protected String getMatchGrade(MatchedTarget theTheMatchedTarget) {
String retVal = "probable";
if (theTheMatchedTarget.isMatch()) {
retVal = "certain";
} else if (theTheMatchedTarget.isPossibleMatch()) {
retVal = "possible";
}
return retVal;
}
}

View File

@ -72,9 +72,9 @@ public class MdmProviderDstu3Plus extends BaseMdmProvider {
private static final Logger ourLog = getLogger(MdmProviderDstu3Plus.class);
private final IMdmControllerSvc myMdmControllerSvc;
private final IMdmMatchFinderSvc myMdmMatchFinderSvc;
private final IMdmSubmitSvc myMdmSubmitSvc;
private final IMdmSettings myMdmSettings;
private final MdmControllerHelper myMdmControllerHelper;
public static final int DEFAULT_PAGE_SIZE = 20;
public static final int MAX_PAGE_SIZE = 100;
@ -85,10 +85,14 @@ public class MdmProviderDstu3Plus extends BaseMdmProvider {
* Note that this is not a spring bean. Any necessary injections should
* happen in the constructor
*/
public MdmProviderDstu3Plus(FhirContext theFhirContext, IMdmControllerSvc theMdmControllerSvc, IMdmMatchFinderSvc theMdmMatchFinderSvc, IMdmSubmitSvc theMdmSubmitSvc, IMdmSettings theIMdmSettings) {
public MdmProviderDstu3Plus(FhirContext theFhirContext,
IMdmControllerSvc theMdmControllerSvc,
MdmControllerHelper theMdmHelper,
IMdmSubmitSvc theMdmSubmitSvc,
IMdmSettings theIMdmSettings) {
super(theFhirContext);
myMdmControllerSvc = theMdmControllerSvc;
myMdmMatchFinderSvc = theMdmMatchFinderSvc;
myMdmControllerHelper = theMdmHelper;
myMdmSubmitSvc = theMdmSubmitSvc;
myMdmSettings = theIMdmSettings;
}
@ -98,7 +102,7 @@ public class MdmProviderDstu3Plus extends BaseMdmProvider {
if (thePatient == null) {
throw new InvalidRequestException("resource may not be null");
}
return getMatchesAndPossibleMatchesForResource(thePatient, "Patient");
return myMdmControllerHelper.getMatchesAndPossibleMatchesForResource(thePatient, "Patient");
}
@Operation(name = ProviderConstants.MDM_MATCH)
@ -108,51 +112,7 @@ public class MdmProviderDstu3Plus extends BaseMdmProvider {
if (theResource == null) {
throw new InvalidRequestException("resource may not be null");
}
return getMatchesAndPossibleMatchesForResource(theResource, theResourceType.getValueAsString());
}
/**
* Helper method which will return a bundle of all Matches and Possible Matches.
*/
private IBaseBundle getMatchesAndPossibleMatchesForResource(IAnyResource theResource, String theResourceType) {
List<MatchedTarget> matches = myMdmMatchFinderSvc.getMatchedTargets(theResourceType, theResource);
matches.sort(Comparator.comparing((MatchedTarget m) -> m.getMatchResult().getNormalizedScore()).reversed());
BundleBuilder builder = new BundleBuilder(myFhirContext);
builder.setBundleField("type", "searchset");
builder.setBundleField("id", UUID.randomUUID().toString());
builder.setMetaField("lastUpdated", builder.newPrimitive("instant", new Date()));
IBaseBundle retVal = builder.getBundle();
for (MatchedTarget next : matches) {
boolean shouldKeepThisEntry = next.isMatch() || next.isPossibleMatch();
if (!shouldKeepThisEntry) {
continue;
}
IBase entry = builder.addEntry();
builder.addToEntry(entry, "resource", next.getTarget());
IBaseBackboneElement search = builder.addSearch(entry);
toBundleEntrySearchComponent(builder, search, next);
}
return retVal;
}
public IBaseBackboneElement toBundleEntrySearchComponent(BundleBuilder theBuilder, IBaseBackboneElement theSearch, MatchedTarget theMatchedTarget) {
theBuilder.setSearchField(theSearch, "mode", "match");
double score = theMatchedTarget.getMatchResult().getNormalizedScore();
theBuilder.setSearchField(theSearch, "score",
theBuilder.newPrimitive("decimal", BigDecimal.valueOf(score)));
String matchGrade = getMatchGrade(theMatchedTarget);
IBaseDatatype codeType = (IBaseDatatype) myFhirContext.getElementDefinition("code").newInstance(matchGrade);
IBaseExtension searchExtension = theSearch.addExtension();
searchExtension.setUrl(MdmConstants.FIHR_STRUCTURE_DEF_MATCH_GRADE_URL_NAMESPACE);
searchExtension.setValue(codeType);
return theSearch;
return myMdmControllerHelper.getMatchesAndPossibleMatchesForResource(theResource, theResourceType.getValueAsString());
}
@Operation(name = ProviderConstants.MDM_MERGE_GOLDEN_RESOURCES)
@ -353,17 +313,6 @@ public class MdmProviderDstu3Plus extends BaseMdmProvider {
return retval;
}
@Nonnull
protected String getMatchGrade(MatchedTarget theTheMatchedTarget) {
String retVal = "probable";
if (theTheMatchedTarget.isMatch()) {
retVal = "certain";
} else if (theTheMatchedTarget.isPossibleMatch()) {
retVal = "possible";
}
return retVal;
}
private String getResourceType(String theParamName, IPrimitiveType<String> theResourceId) {
if (theResourceId != null) {
return getResourceType(theParamName, theResourceId.getValueAsString());

View File

@ -39,7 +39,7 @@ public class MdmProviderLoader {
@Autowired
private ResourceProviderFactory myResourceProviderFactory;
@Autowired
private IMdmMatchFinderSvc myMdmMatchFinderSvc;
private MdmControllerHelper myMdmControllerHelper;
@Autowired
private IMdmControllerSvc myMdmControllerSvc;
@Autowired
@ -54,7 +54,11 @@ public class MdmProviderLoader {
case DSTU3:
case R4:
myResourceProviderFactory.addSupplier(() -> {
myMdmProvider = new MdmProviderDstu3Plus(myFhirContext, myMdmControllerSvc, myMdmMatchFinderSvc, myMdmSubmitSvc, myMdmSettings);
myMdmProvider = new MdmProviderDstu3Plus(myFhirContext,
myMdmControllerSvc,
myMdmControllerHelper,
myMdmSubmitSvc,
myMdmSettings);
return myMdmProvider;
});
break;