From 174ee475496a33963e1682ee9a3bb158f2157619 Mon Sep 17 00:00:00 2001 From: Clement Hennequin Date: Tue, 12 Dec 2023 10:03:02 -0500 Subject: [PATCH] Csv export prototype complete --- .../RepositoryClientFactory.java | 15 +- .../iis/kernal/mapping/MappingHelper.java | 55 ++-- .../mapping/forR4/LocationMapperR4.java | 2 +- .../mapping/forR4/ObservationMapperR4.java | 2 +- .../mapping/forR4/OrganizationMapperR4.java | 4 +- .../kernal/mapping/forR4/PersonMapperR4.java | 2 +- .../mapping/forR5/ImmunizationMapperR5.java | 6 +- .../mapping/forR5/ObservationMapperR5.java | 2 +- .../mapping/forR5/OrganizationMapperR5.java | 4 +- .../kernal/mapping/forR5/PersonMapperR5.java | 4 +- .../mapping/forR5/PractitionerMapperR5.java | 8 +- .../iis/kernal/servlet/LoginServlet.java | 2 + ...ntMatchingDatasetConversionController.java | 299 +++++++++++++++--- src/main/resources/mdm-rules.json | 2 +- 14 files changed, 323 insertions(+), 84 deletions(-) diff --git a/src/main/java/org/immregistries/iis/kernal/InternalClient/RepositoryClientFactory.java b/src/main/java/org/immregistries/iis/kernal/InternalClient/RepositoryClientFactory.java index d5d53fa2..52278e0c 100644 --- a/src/main/java/org/immregistries/iis/kernal/InternalClient/RepositoryClientFactory.java +++ b/src/main/java/org/immregistries/iis/kernal/InternalClient/RepositoryClientFactory.java @@ -18,6 +18,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.env.Environment; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; @@ -27,6 +29,9 @@ import javax.servlet.http.HttpServletRequest; +import java.net.InetAddress; +import java.net.UnknownHostException; + import static org.immregistries.iis.kernal.fhir.interceptors.SessionAuthorizationInterceptor.CONNECTATHON_USER; import static org.immregistries.iis.kernal.fhir.security.ServletHelper.GITHUB_PREFIX; import static org.immregistries.iis.kernal.fhir.security.ServletHelper.SESSION_TENANT; @@ -41,7 +46,9 @@ public class RepositoryClientFactory extends ApacheRestfulClientFactory implemen private IFhirSystemDao fhirSystemDao; private final Logger logger = LoggerFactory.getLogger(RepositoryClientFactory.class); private LoggingInterceptor loggingInterceptor; - private static String serverBase = ""; + + @Autowired + Environment environment; @Autowired public RepositoryClientFactory(){ @@ -55,14 +62,12 @@ private void asynchInit() { loggingInterceptor = new LoggingInterceptor(); loggingInterceptor.setLogger(logger); } - if (serverBase.equals("")) { - serverBase = ServletUriComponentsBuilder.fromCurrentContextPath().build().toUriString() + "/fhir"; - } } public IGenericClient newGenericClient(Tenant tenant) { asynchInit(); - IGenericClient client = newGenericClient(serverBase + "/" + tenant.getOrganizationName()); + IGenericClient client; + client = newGenericClient( "http://" + InetAddress.getLoopbackAddress().getHostAddress() + ":" + environment.getProperty("server.port") + "/iis/fhir/" + tenant.getOrganizationName()); IClientInterceptor authInterceptor; if (tenant.getOrganizationName().equals(CONNECTATHON_USER) && tenant.getUserAccess().getAccessName() == null) { /** diff --git a/src/main/java/org/immregistries/iis/kernal/mapping/MappingHelper.java b/src/main/java/org/immregistries/iis/kernal/mapping/MappingHelper.java index a7210514..135e0ef8 100644 --- a/src/main/java/org/immregistries/iis/kernal/mapping/MappingHelper.java +++ b/src/main/java/org/immregistries/iis/kernal/mapping/MappingHelper.java @@ -1,9 +1,7 @@ package org.immregistries.iis.kernal.mapping; -import ca.uhn.fhir.util.CanonicalIdentifier; import org.apache.commons.lang3.StringUtils; -import org.hl7.fhir.instance.model.api.IBaseParameters; import org.hl7.fhir.r5.model.*; import java.text.SimpleDateFormat; @@ -23,25 +21,31 @@ public class MappingHelper { public static final SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss yyyy"); //TODO choose system id or not - public static Reference getFhirReference(String fhirType, String dbType, String identifier) { + public static Reference getFhirReferenceR5(String fhirType, String dbType, String identifier) { if (StringUtils.isBlank(identifier)) { return null; } else { return new Reference() .setType(fhirType) - .setIdentifier(getFhirIdentifier(dbType, identifier)); + .setIdentifier(getFhirIdentifierR5(dbType, identifier)); } } - public static CodeableReference getFhirCodeableReference(String fhirType, String dbType, String identifier) { + public static Reference getFhirReferenceR5(String fhirType, String dbType, String identifier, String fhirId) { + return new Reference(fhirType + "/" + fhirId) + .setType(fhirType) + .setIdentifier(getFhirIdentifierR5(dbType, identifier)); + } + + public static CodeableReference getFhirCodeableReferenceR5(String fhirType, String dbType, String identifier) { if (StringUtils.isBlank(identifier)) { return null; } else { - return new CodeableReference(getFhirReference(fhirType, dbType, identifier)); + return new CodeableReference(getFhirReferenceR5(fhirType, dbType, identifier)); } } - public static org.hl7.fhir.r4.model.Reference getFhirR4Reference(String fhirType, String dbType, String identifier) { + public static org.hl7.fhir.r4.model.Reference getFhirReferenceR4(String fhirType, String dbType, String identifier) { if (StringUtils.isBlank(identifier)) { return null; } else { @@ -52,25 +56,20 @@ public static org.hl7.fhir.r4.model.Reference getFhirR4Reference(String fhirType .setValue(identifier)); } } - public static Reference getFhirReference(String fhirType, String dbType, String identifier, String fhirId) { - return new Reference(fhirType + "/" + fhirId) - .setType(fhirType) - .setIdentifier(getFhirIdentifier(dbType,identifier)); - } - public static Identifier getFhirIdentifier(String dbType, String identifier) { + public static Identifier getFhirIdentifierR5(String dbType, String identifier) { return new Identifier() - .setSystem(dbType) - .setValue(identifier); + .setSystem(dbType) + .setValue(identifier); } - public static org.hl7.fhir.r4.model.Identifier getFhirR4Identifier(String dbType, String identifier) { + public static org.hl7.fhir.r4.model.Identifier getFhirIdentifierR4(String dbType, String identifier) { return new org.hl7.fhir.r4.model.Identifier() - .setSystem(dbType) - .setValue(identifier); + .setSystem(dbType) + .setValue(identifier); } - public static String identifierToString(List identifiers) { + public static String identifierToString(List identifiers) { if (identifiers.size() > 0) { return identifiers.get(0).getSystem() + "|" + identifiers.get(0).getValue(); } else { @@ -82,19 +81,27 @@ public static String identifierToString(List identifiers) { // return identifiers.stream().filter(identifier -> identifier.getSystemElement().getValue() != null && identifier.getSystemElement().getValue().equals(system)).findFirst().orElse(identifiers.get(0)); // } - public static Identifier filterIdentifier(List identifiers, String system) { + public static Identifier filterIdentifierR5(List identifiers, String system) { return identifiers.stream().filter(identifier -> identifier.getSystem() != null && identifier.getSystem().equals(system)).findFirst().orElse(null); } - public static org.hl7.fhir.r4.model.Identifier filterR4Identifier(List identifiers, String system) { + public static org.hl7.fhir.r4.model.Identifier filterIdentifierR4(List identifiers, String system) { return identifiers.stream().filter(identifier -> identifier.getSystem() != null && identifier.getSystem().equals(system)).findFirst().orElse(null); } - public static Coding filterCodeableConcept(CodeableConcept concept, String system) { - return filterCodingList(concept.getCoding(), system); + public static Identifier filterIdentifierTypeR5(List identifiers, String type) { + return identifiers.stream().filter(identifier -> identifier.hasType() && identifier.getType().hasCoding() && identifier.getType().getCodingFirstRep().getCode().equals(type)).findFirst().orElse(null); + } + + public static org.hl7.fhir.r4.model.Identifier filterIdentifierTypeR4(List identifiers, String type) { + return identifiers.stream().filter(identifier -> identifier.hasType() && identifier.getType().hasCoding() && identifier.getType().getCodingFirstRep().getCode().equals(type)).findFirst().orElse(null); + } + + public static Coding filterCodeableConceptR5(CodeableConcept concept, String system) { + return filterCodingListR5(concept.getCoding(), system); } - public static Coding filterCodingList(List codings, String system) { + public static Coding filterCodingListR5(List codings, String system) { return codings.stream().filter(coding -> coding.getSystem().equals(system)).findFirst().get(); } diff --git a/src/main/java/org/immregistries/iis/kernal/mapping/forR4/LocationMapperR4.java b/src/main/java/org/immregistries/iis/kernal/mapping/forR4/LocationMapperR4.java index 8d8ca3ef..1cefb8d3 100644 --- a/src/main/java/org/immregistries/iis/kernal/mapping/forR4/LocationMapperR4.java +++ b/src/main/java/org/immregistries/iis/kernal/mapping/forR4/LocationMapperR4.java @@ -20,7 +20,7 @@ public org.hl7.fhir.r4.model.Location getFhirResource(OrgLocation ol) { if (ol != null) { org.hl7.fhir.r4.model.Location location = new org.hl7.fhir.r4.model.Location(); location.setId(ol.getOrgLocationId()); - location.addIdentifier(MappingHelper.getFhirR4Identifier("", ol.getOrgFacilityCode())); + location.addIdentifier(MappingHelper.getFhirIdentifierR4("", ol.getOrgFacilityCode())); location.setName(ol.getOrgFacilityName()); if (!ol.getVfcProviderPin().isBlank()) { location.addExtension().setUrl(VFC_PROVIDER_PIN).setValue(new StringType(ol.getVfcProviderPin())); diff --git a/src/main/java/org/immregistries/iis/kernal/mapping/forR4/ObservationMapperR4.java b/src/main/java/org/immregistries/iis/kernal/mapping/forR4/ObservationMapperR4.java index 07545a4d..75f8be5e 100644 --- a/src/main/java/org/immregistries/iis/kernal/mapping/forR4/ObservationMapperR4.java +++ b/src/main/java/org/immregistries/iis/kernal/mapping/forR4/ObservationMapperR4.java @@ -40,7 +40,7 @@ public Observation getFhirResource(ObservationReported observationReported) { .setCode(observationReported.getMethodCode()) .setSystem(observationReported.getMethodTable()) .setDisplay(observationReported.getMethodLabel()); - o.addIdentifier(MappingHelper.getFhirR4Identifier( + o.addIdentifier(MappingHelper.getFhirIdentifierR4( observationReported.getIdentifierTable(),observationReported.getIdentifierCode())); //TODO label o.addComponent().setValue(new org.hl7.fhir.r4.model.DateTimeType(observationReported.getObservationDate())) .setCode(new org.hl7.fhir.r4.model.CodeableConcept().setText(OBSERVATION_DATE)); diff --git a/src/main/java/org/immregistries/iis/kernal/mapping/forR4/OrganizationMapperR4.java b/src/main/java/org/immregistries/iis/kernal/mapping/forR4/OrganizationMapperR4.java index 9fe95c2e..a6e0912d 100644 --- a/src/main/java/org/immregistries/iis/kernal/mapping/forR4/OrganizationMapperR4.java +++ b/src/main/java/org/immregistries/iis/kernal/mapping/forR4/OrganizationMapperR4.java @@ -15,7 +15,7 @@ public class OrganizationMapperR4 implements OrganizationMapper { public Organization getFhirResource(Tenant tenant) { Organization o = new Organization(); - o.addIdentifier(MappingHelper.getFhirR4Identifier("Tenant", Integer.toString(tenant.getOrgId()))); + o.addIdentifier(MappingHelper.getFhirIdentifierR4("Tenant", Integer.toString(tenant.getOrgId()))); o.setName(tenant.getOrganizationName()); return o; } @@ -23,7 +23,7 @@ public Organization getFhirResource(Tenant tenant) { public Tenant getTenant(Organization organization) { Tenant tenant = new Tenant(); tenant.setOrganizationName(organization.getName()); - tenant.setOrgId(Integer.parseInt(MappingHelper.filterR4Identifier(organization.getIdentifier(),"Tenant").getValue())); + tenant.setOrgId(Integer.parseInt(MappingHelper.filterIdentifierR4(organization.getIdentifier(),"Tenant").getValue())); return tenant; } } diff --git a/src/main/java/org/immregistries/iis/kernal/mapping/forR4/PersonMapperR4.java b/src/main/java/org/immregistries/iis/kernal/mapping/forR4/PersonMapperR4.java index 9a939247..d27ea0ab 100644 --- a/src/main/java/org/immregistries/iis/kernal/mapping/forR4/PersonMapperR4.java +++ b/src/main/java/org/immregistries/iis/kernal/mapping/forR4/PersonMapperR4.java @@ -42,7 +42,7 @@ public Person getFhirResource(ModelPerson modelPerson) { if ( modelPerson.getProfessionalSuffix() != null) { name.addSuffix(modelPerson.getProfessionalSuffix()); } - p.setManagingOrganization(MappingHelper.getFhirR4Reference(MappingHelper.ORGANIZATION,ORGANIZATION_ASSIGNING_AUTHORITY, modelPerson.getAssigningAuthority())); + p.setManagingOrganization(MappingHelper.getFhirReferenceR4(MappingHelper.ORGANIZATION,ORGANIZATION_ASSIGNING_AUTHORITY, modelPerson.getAssigningAuthority())); return p; } diff --git a/src/main/java/org/immregistries/iis/kernal/mapping/forR5/ImmunizationMapperR5.java b/src/main/java/org/immregistries/iis/kernal/mapping/forR5/ImmunizationMapperR5.java index 047f42a0..0a6c2d17 100644 --- a/src/main/java/org/immregistries/iis/kernal/mapping/forR5/ImmunizationMapperR5.java +++ b/src/main/java/org/immregistries/iis/kernal/mapping/forR5/ImmunizationMapperR5.java @@ -165,7 +165,7 @@ public VaccinationMaster getMaster(Immunization i){ */ public Immunization getFhirResource(VaccinationMaster vr) { Immunization i = new Immunization(); - i.addIdentifier(MappingHelper.getFhirIdentifier(vr.getExternalLinkSystem(), vr.getExternalLink())); // TODO if system empty ? + i.addIdentifier(MappingHelper.getFhirIdentifierR5(vr.getExternalLinkSystem(), vr.getExternalLink())); // TODO if system empty ? Reference patientReference = new Reference() .setReference("Patient/" + vr.getPatientReported().getPatientId()) // .setIdentifier(new Identifier() @@ -186,7 +186,7 @@ public Immunization getFhirResource(VaccinationMaster vr) { if (StringUtils.isNotBlank(vr.getVaccineNdcCode())) { i.getVaccineCode().addCoding().setCode(vr.getVaccineNdcCode()).setSystem(NDC); } - i.setManufacturer(MappingHelper.getFhirCodeableReference(MappingHelper.ORGANIZATION, MVX, vr.getVaccineMvxCode())); + i.setManufacturer(MappingHelper.getFhirCodeableReferenceR5(MappingHelper.ORGANIZATION, MVX, vr.getVaccineMvxCode())); i.setDoseQuantity(new Quantity().setValue(new BigDecimal(vr.getAdministeredAmount()))); @@ -255,7 +255,7 @@ private Immunization.ImmunizationPerformerComponent performer(ModelPerson person break; } default:{ - actor = MappingHelper.getFhirReference(MappingHelper.PRACTITIONER, person.getIdentifierTypeCode(), person.getPersonExternalLink(), person.getPersonId()); + actor = MappingHelper.getFhirReferenceR5(MappingHelper.PRACTITIONER, person.getIdentifierTypeCode(), person.getPersonExternalLink(), person.getPersonId()); } } performer.setActor(actor); diff --git a/src/main/java/org/immregistries/iis/kernal/mapping/forR5/ObservationMapperR5.java b/src/main/java/org/immregistries/iis/kernal/mapping/forR5/ObservationMapperR5.java index 9d4de630..dc3a5b6b 100644 --- a/src/main/java/org/immregistries/iis/kernal/mapping/forR5/ObservationMapperR5.java +++ b/src/main/java/org/immregistries/iis/kernal/mapping/forR5/ObservationMapperR5.java @@ -40,7 +40,7 @@ public Observation getFhirResource(ObservationReported observationReported) { .setCode(observationReported.getMethodCode()) .setSystem(observationReported.getMethodTable()) .setDisplay(observationReported.getMethodLabel()); - o.addIdentifier(MappingHelper.getFhirIdentifier( + o.addIdentifier(MappingHelper.getFhirIdentifierR5( observationReported.getIdentifierTable(),observationReported.getIdentifierCode())); //TODO label o.addComponent().setValue(new DateTimeType(observationReported.getObservationDate())) .setCode(new CodeableConcept().setText(OBSERVATION_DATE)); diff --git a/src/main/java/org/immregistries/iis/kernal/mapping/forR5/OrganizationMapperR5.java b/src/main/java/org/immregistries/iis/kernal/mapping/forR5/OrganizationMapperR5.java index 7e1aaba2..72182d7a 100644 --- a/src/main/java/org/immregistries/iis/kernal/mapping/forR5/OrganizationMapperR5.java +++ b/src/main/java/org/immregistries/iis/kernal/mapping/forR5/OrganizationMapperR5.java @@ -15,7 +15,7 @@ public class OrganizationMapperR5 implements OrganizationMapper { public Organization getFhirResource(Tenant tenant) { Organization o = new Organization(); - o.addIdentifier(MappingHelper.getFhirIdentifier("Tenant", Integer.toString(tenant.getOrgId()))); + o.addIdentifier(MappingHelper.getFhirIdentifierR5("Tenant", Integer.toString(tenant.getOrgId()))); o.setName(tenant.getOrganizationName()); return o; } @@ -23,7 +23,7 @@ public Organization getFhirResource(Tenant tenant) { public Tenant getTenant(Organization organization) { Tenant tenant = new Tenant(); tenant.setOrganizationName(organization.getName()); - tenant.setOrgId(Integer.parseInt(MappingHelper.filterIdentifier(organization.getIdentifier(),"Tenant").getValue())); + tenant.setOrgId(Integer.parseInt(MappingHelper.filterIdentifierR5(organization.getIdentifier(),"Tenant").getValue())); return tenant; } } diff --git a/src/main/java/org/immregistries/iis/kernal/mapping/forR5/PersonMapperR5.java b/src/main/java/org/immregistries/iis/kernal/mapping/forR5/PersonMapperR5.java index bb0c1850..651fc917 100644 --- a/src/main/java/org/immregistries/iis/kernal/mapping/forR5/PersonMapperR5.java +++ b/src/main/java/org/immregistries/iis/kernal/mapping/forR5/PersonMapperR5.java @@ -33,7 +33,7 @@ public ModelPerson getModelPerson(org.hl7.fhir.r5.model.Person p) { public Person getFhirResource(ModelPerson modelPerson) { org.hl7.fhir.r5.model.Person p = new org.hl7.fhir.r5.model.Person(); p.setId(modelPerson.getPersonId()); - p.addIdentifier(MappingHelper.getFhirIdentifier(MappingHelper.PERSON, modelPerson.getPersonExternalLink())); + p.addIdentifier(MappingHelper.getFhirIdentifierR5(MappingHelper.PERSON, modelPerson.getPersonExternalLink())); HumanName name = p.addName(); name.setFamily(modelPerson.getNameLast()); name.addGiven(modelPerson.getNameFirst()); @@ -41,7 +41,7 @@ public Person getFhirResource(ModelPerson modelPerson) { if ( modelPerson.getProfessionalSuffix() != null) { name.addSuffix(modelPerson.getProfessionalSuffix()); } - p.setManagingOrganization(MappingHelper.getFhirReference(MappingHelper.ORGANIZATION, ORGANIZATION_ASSIGNING_AUTHORITY, modelPerson.getAssigningAuthority())); + p.setManagingOrganization(MappingHelper.getFhirReferenceR5(MappingHelper.ORGANIZATION, ORGANIZATION_ASSIGNING_AUTHORITY, modelPerson.getAssigningAuthority())); return p; } diff --git a/src/main/java/org/immregistries/iis/kernal/mapping/forR5/PractitionerMapperR5.java b/src/main/java/org/immregistries/iis/kernal/mapping/forR5/PractitionerMapperR5.java index 2a9e518d..f0d39b2f 100644 --- a/src/main/java/org/immregistries/iis/kernal/mapping/forR5/PractitionerMapperR5.java +++ b/src/main/java/org/immregistries/iis/kernal/mapping/forR5/PractitionerMapperR5.java @@ -39,19 +39,19 @@ public Practitioner getFhirResource(ModelPerson modelPerson) { try { switch (new Reference(modelPerson.getIdentifierTypeCode()).getType()) { case "Organization": { - practitioner.addIdentifier(MappingHelper.getFhirIdentifier(PRACTITIONER,modelPerson.getPersonExternalLink()).setAssigner(new Reference(modelPerson.getAssigningAuthority()))); + practitioner.addIdentifier(MappingHelper.getFhirIdentifierR5(PRACTITIONER,modelPerson.getPersonExternalLink()).setAssigner(new Reference(modelPerson.getAssigningAuthority()))); break; } case "System" : { - practitioner.addIdentifier(MappingHelper.getFhirIdentifier(modelPerson.getIdentifierTypeCode(),modelPerson.getPersonExternalLink())); + practitioner.addIdentifier(MappingHelper.getFhirIdentifierR5(modelPerson.getIdentifierTypeCode(),modelPerson.getPersonExternalLink())); break; } default: { - practitioner.addIdentifier(MappingHelper.getFhirIdentifier(PRACTITIONER,modelPerson.getPersonExternalLink())); + practitioner.addIdentifier(MappingHelper.getFhirIdentifierR5(PRACTITIONER,modelPerson.getPersonExternalLink())); break; } } } catch (NullPointerException e) { // If typecode is not reference - practitioner.addIdentifier(MappingHelper.getFhirIdentifier(modelPerson.getIdentifierTypeCode(),modelPerson.getPersonExternalLink())); + practitioner.addIdentifier(MappingHelper.getFhirIdentifierR5(modelPerson.getIdentifierTypeCode(),modelPerson.getPersonExternalLink())); } diff --git a/src/main/java/org/immregistries/iis/kernal/servlet/LoginServlet.java b/src/main/java/org/immregistries/iis/kernal/servlet/LoginServlet.java index 20df4202..32bf20b6 100644 --- a/src/main/java/org/immregistries/iis/kernal/servlet/LoginServlet.java +++ b/src/main/java/org/immregistries/iis/kernal/servlet/LoginServlet.java @@ -1,6 +1,8 @@ package org.immregistries.iis.kernal.servlet; +import org.apache.commons.codec.binary.Base64; import org.hibernate.Session; +import org.immregistries.iis.kernal.fhir.security.ServletHelper; import org.immregistries.iis.kernal.model.Tenant; import org.springframework.security.authentication.AnonymousAuthenticationToken; import org.springframework.security.core.Authentication; diff --git a/src/main/java/org/immregistries/iis/kernal/servlet/PatientMatchingDatasetConversionController.java b/src/main/java/org/immregistries/iis/kernal/servlet/PatientMatchingDatasetConversionController.java index f3798475..c884318a 100644 --- a/src/main/java/org/immregistries/iis/kernal/servlet/PatientMatchingDatasetConversionController.java +++ b/src/main/java/org/immregistries/iis/kernal/servlet/PatientMatchingDatasetConversionController.java @@ -1,54 +1,259 @@ package org.immregistries.iis.kernal.servlet; import ca.uhn.fhir.context.FhirContext; - -import ca.uhn.fhir.mdm.util.IdentifierUtil; +import ca.uhn.fhir.context.FhirVersionEnum; +import ca.uhn.fhir.jpa.searchparam.SearchParameterMap; +import ca.uhn.fhir.rest.api.server.IBundleProvider; +import ca.uhn.fhir.rest.param.TokenParam; import ca.uhn.fhir.util.ExtensionUtil; import org.hl7.fhir.instance.model.api.IBaseExtension; +import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.r5.model.*; -//import org.hl7.fhir.r5.model.Patient; +import org.immregistries.iis.kernal.InternalClient.FhirRequester; import org.immregistries.iis.kernal.fhir.security.ServletHelper; import org.immregistries.iis.kernal.mapping.MappingHelper; -import org.immregistries.iis.kernal.model.PatientMaster; import org.immregistries.mismo.match.StringUtils; import org.immregistries.mismo.match.model.Patient; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.Servlet; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.immregistries.iis.kernal.mapping.Interfaces.PatientMapper.*; @RestController -@RequestMapping("patientMatchingDatasetConversion") +@RequestMapping("/patientMatchingDatasetConversion") public class PatientMatchingDatasetConversionController { @Autowired FhirContext fhirContext; + @Autowired + FhirRequester fhirRequester; - - public String post(@RequestBody String stringBundle) { - Bundle bundle = fhirContext.newJsonParser().parseResource(Bundle.class, stringBundle); - for (Bundle.BundleEntryComponent entry: bundle.getEntry()) { - if (entry.getResource() instanceof org.hl7.fhir.r5.model.Patient) { - org.hl7.fhir.r5.model.Patient fhir = (org.hl7.fhir.r5.model.Patient) entry.getResource(); + @PostMapping() + public String post(@RequestBody String stringBundle) throws FileNotFoundException { + List list = new ArrayList<>(20); + if (fhirContext.getVersion().getVersion().equals(FhirVersionEnum.R5)) { + Bundle bundle = fhirContext.newJsonParser().parseResource(Bundle.class, stringBundle); + for (Bundle.BundleEntryComponent entry : bundle.getEntry()) { + if (entry.getResource() instanceof org.hl7.fhir.r5.model.Patient) { + org.hl7.fhir.r5.model.Patient fhirPatient = (org.hl7.fhir.r5.model.Patient) entry.getResource(); + Patient patient = convertFromR5(fhirPatient); + list.add(patient); + } + } + } else if (fhirContext.getVersion().getVersion().equals(FhirVersionEnum.R4)) { + org.hl7.fhir.r4.model.Bundle bundle = fhirContext.newJsonParser().parseResource(org.hl7.fhir.r4.model.Bundle.class, stringBundle); + for (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent entry : bundle.getEntry()) { + if (entry.getResource() instanceof org.hl7.fhir.r4.model.Patient) { + org.hl7.fhir.r4.model.Patient fhirPatient = (org.hl7.fhir.r4.model.Patient) entry.getResource(); + Patient patient = convertFromR4(fhirPatient); + list.add(patient); + } } } + + File csvOutputFile = new File("./target/output.csv"); + try (PrintWriter pw = new PrintWriter(csvOutputFile)) { + csv(list, pw); + pw.flush(); + pw.close(); + } return ""; } + @GetMapping("/all") + public void getFromFacility(HttpServletResponse resp) throws IOException { + List list = new ArrayList<>(20); + if (fhirContext.getVersion().getVersion().equals(FhirVersionEnum.R5)) { + IBundleProvider bundleProvider = fhirRequester.searchRegularRecord(org.hl7.fhir.r5.model.Patient.class, new SearchParameterMap()); + for (IBaseResource iBaseResource : bundleProvider.getAllResources()) { + if (iBaseResource instanceof org.hl7.fhir.r5.model.Patient) { + Patient patient = convertFromR5IncludingLink((org.hl7.fhir.r5.model.Patient) iBaseResource); + list.add(patient); + } + } + } else if (fhirContext.getVersion().getVersion().equals(FhirVersionEnum.R4)) { + IBundleProvider bundleProvider = fhirRequester.searchRegularRecord(org.hl7.fhir.r4.model.Patient.class, new SearchParameterMap()); + for (IBaseResource iBaseResource : bundleProvider.getAllResources()) { + if (iBaseResource instanceof org.hl7.fhir.r4.model.Patient) { + Patient patient = convertFromR4IncludingLink((org.hl7.fhir.r4.model.Patient) iBaseResource); + list.add(patient); + } + } + } + PrintWriter out = new PrintWriter(resp.getOutputStream()); + csv(list, out); + out.flush(); + out.close(); + } + + private void csv(List patientList, PrintWriter printWriter) throws FileNotFoundException { + String csvHeader = + "EnterpriseID," + + "LAST," + + "FIRST," + + "MIDDLE," + + "SUFFIX," + + "DOB," + + "GENDER," + + "SSN," + + "ADDRESS1," + + "ADDRESS2," + + "ZIP," + + "MOTHERS_MAIDEN_NAME," + + "MRN," + + "CITY," + + "STATE," + + "PHONE," + + "PHONE2," + + "EMAIL," + + "ALIAS," + + "LINK_ID"; + printWriter.println(csvHeader); + patientList.stream() + .map(this::csv) + .forEach(printWriter::println); +// return printWriter.; + } + + private String csv(Patient patient) { + String[] line = new String[]{ + patient.getValue("identifier"), + patient.getNameLast(), + patient.getNameFirst(), + patient.getNameMiddle(), + patient.getNameSuffix(), + patient.getBirthDate(), + patient.getGender(), + patient.getSsn(), + patient.getAddress1().getLine1(), + patient.getAddress1().getLine2(), + patient.getAddress1().getZip(), + patient.getMotherMaidenName(), + patient.getMrns(), + patient.getAddress1().getCity(), + patient.getAddress1().getState(), + patient.getPhone(), + "", //phone 2 + patient.getValue("email"), + patient.getNameAlias(), + patient.getLinkWith().getValue("identifier"), // LinkWith + }; + + return Stream.of(line) +// .map(this::escapeSpecialCharacters) + .collect(Collectors.joining(",")); + } - private Patient convert(org.hl7.fhir.r5.model.Patient patient) { + private Patient convertFromR5(org.hl7.fhir.r5.model.Patient patient) { Patient mismo = new Patient(); - mismo.setPatientId(Math.toIntExact(patient.getIdElement().getIdPartAsLong())); + org.hl7.fhir.r5.model.Identifier identifier = MappingHelper.filterIdentifierR5(patient.getIdentifier(), "http://codi.mitre.org"); + if (identifier != null) { + mismo.setValue("identifier",identifier.getValue()); + } + if (patient.hasName()) { + org.hl7.fhir.r5.model.HumanName humanName = patient.getNameFirstRep(); + mismo.setNameFirst(humanName.getGivenAsSingleString()); + String[] family = humanName.getFamily().split("-"); + if (family.length > 1) { + mismo.setNameLast(family[0]); + mismo.setNameLastHyph(family[1]); + + } else { + mismo.setNameLast(humanName.getFamily()); + } + + if (humanName.getGiven().size() > 1) { + mismo.setNameMiddle(humanName.getGiven().get(1).getValue()); + } + mismo.setNameSuffix(humanName.getSuffixAsSingleString()); + mismo.setNameAlias(humanName.getNameAsSingleString()); + } + + mismo.setBirthDate(patient.getBirthDateElement().asStringValue()); + mismo.setGender(patient.getGender().toCode()); + + org.hl7.fhir.r5.model.Identifier ssn = MappingHelper.filterIdentifierTypeR5(patient.getIdentifier(), "SS"); + if (ssn != null) { + mismo.setSsn(ssn.getValue()); + } + + + if (patient.hasAddress()) { + org.hl7.fhir.r5.model.Address address = patient.getAddressFirstRep(); + mismo.setAddressCity(address.getCity()); + mismo.setAddressState(address.getState()); + mismo.setAddressZip(address.getPostalCode()); + if (address.hasLine()) { + mismo.setAddressStreet1(address.getLine().get(0).getValue()); + if (address.getLine().size() > 1) { + mismo.setAddressStreet2(address.getLine().get(1).getValue()); + } + } +// mismo.setAddressStreet1Alt(address.get); + + if (patient.getAddress().size() > 1) { + org.hl7.fhir.r5.model.Address address2 = patient.getAddress().get(1); + mismo.setAddress2City(address2.getCity()); + mismo.setAddress2State(address2.getState()); + mismo.setAddress2Zip(address2.getPostalCode()); + if (address2.hasLine()) { + mismo.setAddress2Street1(address2.getLine().get(0).getValue()); + if (address2.getLine().size() > 1) { + mismo.setAddress2Street2(address2.getLine().get(1).getValue()); + } + } + } + } + IBaseExtension motherMaiden = ExtensionUtil.getExtensionByUrl(patient, MOTHER_MAIDEN_NAME); + if (motherMaiden != null) { + mismo.setMotherMaidenName(motherMaiden.getValue().toString()); + } + + org.hl7.fhir.r5.model.Identifier mrn = MappingHelper.filterIdentifierTypeR5(patient.getIdentifier(), "MR"); + if (mrn != null) { + mismo.setMrns(mrn.getValue()); + } + for (org.hl7.fhir.r5.model.ContactPoint telecom : patient.getTelecom()) { + if (null != telecom.getSystem()) { + if (telecom.getSystem().equals(ContactPoint.ContactPointSystem.PHONE) && StringUtils.isNotEmpty(mismo.getPhone())) { + mismo.setPhone(telecom.getValue()); + } else if (telecom.getSystem().equals(ContactPoint.ContactPointSystem.PHONE) && StringUtils.isNotEmpty(mismo.getPhone())) { + mismo.setValue("phone2", telecom.getValue()); + } else if (telecom.getSystem().equals(ContactPoint.ContactPointSystem.EMAIL)) { + mismo.setValue("email", telecom.getValue()); + } + } + } + + if(patient.hasMultipleBirthBooleanType()) { + mismo.setBirthOrder(patient.getMultipleBirthBooleanType().getValueAsString()); + } else if (patient.hasMultipleBirthIntegerType()) { + mismo.setBirthOrder(patient.getMultipleBirthIntegerType().getValueAsString()); + } + return mismo; + } + private Patient convertFromR4(org.hl7.fhir.r4.model.Patient patient) { + Patient mismo = new Patient(); + org.hl7.fhir.r4.model.Identifier identifier = MappingHelper.filterIdentifierR4(patient.getIdentifier(), "http://codi.mitre.org"); + if (identifier != null) { + mismo.setValue("identifier",identifier.getValue()); + } if (patient.hasName()) { - HumanName humanName = patient.getNameFirstRep(); + org.hl7.fhir.r4.model.HumanName humanName = patient.getNameFirstRep(); mismo.setNameFirst(humanName.getGivenAsSingleString()); String[] family = humanName.getFamily().split("-"); - if (family.length > 1){ + if (family.length > 1) { mismo.setNameLast(family[0]); mismo.setNameLastHyph(family[1]); @@ -66,14 +271,14 @@ private Patient convert(org.hl7.fhir.r5.model.Patient patient) { mismo.setBirthDate(patient.getBirthDateElement().asStringValue()); mismo.setGender(patient.getGender().toCode()); - Identifier ssn = MappingHelper.filterIdentifier(patient.getIdentifier(),SSN); + org.hl7.fhir.r4.model.Identifier ssn = MappingHelper.filterIdentifierTypeR4(patient.getIdentifier(), "SS"); if (ssn != null) { mismo.setSsn(ssn.getValue()); } if (patient.hasAddress()) { - Address address = patient.getAddressFirstRep(); + org.hl7.fhir.r4.model.Address address = patient.getAddressFirstRep(); mismo.setAddressCity(address.getCity()); mismo.setAddressState(address.getState()); mismo.setAddressZip(address.getPostalCode()); @@ -86,7 +291,7 @@ private Patient convert(org.hl7.fhir.r5.model.Patient patient) { // mismo.setAddressStreet1Alt(address.get); if (patient.getAddress().size() > 1) { - Address address2 = patient.getAddress().get(1); + org.hl7.fhir.r4.model.Address address2 = patient.getAddress().get(1); mismo.setAddress2City(address2.getCity()); mismo.setAddress2State(address2.getState()); mismo.setAddress2Zip(address2.getPostalCode()); @@ -98,37 +303,60 @@ private Patient convert(org.hl7.fhir.r5.model.Patient patient) { } } } - IBaseExtension motherMaiden = ExtensionUtil.getExtensionByUrl(patient,MOTHER_MAIDEN_NAME); + IBaseExtension motherMaiden = ExtensionUtil.getExtensionByUrl(patient, MOTHER_MAIDEN_NAME); if (motherMaiden != null) { mismo.setMotherMaidenName(motherMaiden.getValue().toString()); } -// -// IBaseExtension linkedWith = ExtensionUtil.getExtensionByUrl(patient,LINK_ID); -// if (linkedWith != null) { -// mismo.setLinkWith(linkedWith.getValue().toString()); -// } - Identifier mrn = MappingHelper.filterIdentifier(patient.getIdentifier(),""); //TODO change + org.hl7.fhir.r4.model.Identifier mrn = MappingHelper.filterIdentifierTypeR4(patient.getIdentifier(), "MR"); if (mrn != null) { mismo.setMrns(mrn.getValue()); } - for (ContactPoint telecom : patient.getTelecom()) { + for (org.hl7.fhir.r4.model.ContactPoint telecom : patient.getTelecom()) { if (null != telecom.getSystem()) { - if (telecom.getSystem().equals(ContactPoint.ContactPointSystem.PHONE) && StringUtils.isNotEmpty(mismo.getPhone())){ + if (telecom.getSystem().equals(ContactPoint.ContactPointSystem.PHONE) && StringUtils.isNotEmpty(mismo.getPhone())) { mismo.setPhone(telecom.getValue()); - } else if (telecom.getSystem().equals(ContactPoint.ContactPointSystem.PHONE) && StringUtils.isNotEmpty(mismo.getPhone())){ - mismo.setValue("phone2",telecom.getValue()); + } else if (telecom.getSystem().equals(ContactPoint.ContactPointSystem.PHONE) && StringUtils.isNotEmpty(mismo.getPhone())) { + mismo.setValue("phone2", telecom.getValue()); } else if (telecom.getSystem().equals(ContactPoint.ContactPointSystem.EMAIL)) { - mismo.setValue("email",telecom.getValue()); + mismo.setValue("email", telecom.getValue()); } } } - mismo.setBirthOrder(patient.getMultipleBirthIntegerType().getValueAsString()); + if(patient.hasMultipleBirthBooleanType()) { + mismo.setBirthOrder(patient.getMultipleBirthBooleanType().getValueAsString()); + } else if (patient.hasMultipleBirthIntegerType()) { + mismo.setBirthOrder(patient.getMultipleBirthIntegerType().getValueAsString()); + } + return mismo; + } + + private Patient convertFromR4IncludingLink(org.hl7.fhir.r4.model.Patient patient) { + Patient mismo = convertFromR4(patient); + org.hl7.fhir.r4.model.Identifier linkedWith = MappingHelper.filterIdentifierR4(patient.getIdentifier(), LINK_ID); + if (linkedWith != null) { + IBundleProvider bundleProvider = fhirRequester.searchRegularRecord(org.hl7.fhir.r4.model.Patient.class, new SearchParameterMap("identifier", new TokenParam().setSystem(LINK_ID).setValue(linkedWith.getValue()))); + if (!bundleProvider.isEmpty()) { + mismo.setLinkWith(convertFromR4((org.hl7.fhir.r4.model.Patient) bundleProvider.getAllResources().get(0))); + } + } + return mismo; + } + private Patient convertFromR5IncludingLink(org.hl7.fhir.r5.model.Patient patient) { + Patient mismo = convertFromR5(patient); + org.hl7.fhir.r5.model.Identifier linkedWith = MappingHelper.filterIdentifierR5(patient.getIdentifier(), LINK_ID); + if (linkedWith != null) { + IBundleProvider bundleProvider = fhirRequester.searchRegularRecord(org.hl7.fhir.r5.model.Patient.class, new SearchParameterMap("identifier", new TokenParam().setSystem(LINK_ID).setValue(linkedWith.getValue()))); + if (!bundleProvider.isEmpty()) { + mismo.setLinkWith(convertFromR5((org.hl7.fhir.r5.model.Patient) bundleProvider.getAllResources().get(0))); + } + } return mismo; } + // private Patient convertFromMaster(PatientMaster pm) { // Patient m = new Patient(); // m.setNameFirst(pm.getNameFirst()); @@ -137,7 +365,4 @@ private Patient convert(org.hl7.fhir.r5.model.Patient patient) { // m.setNameMiddle(pm.getName()); // // } - - - } diff --git a/src/main/resources/mdm-rules.json b/src/main/resources/mdm-rules.json index ec1bffb6..e4981f06 100644 --- a/src/main/resources/mdm-rules.json +++ b/src/main/resources/mdm-rules.json @@ -105,6 +105,6 @@ "patient-identifier" : "MATCH" }, "eidSystems": { - "Patient": "AIRA-TEST" + "Patient": "http://codi.mitre.org/link_id" } }