1010
1111import java .awt .geom .Point2D ;
1212import java .io .IOException ;
13+ import java .util .AbstractMap ;
1314import java .util .ArrayList ;
1415import java .util .Arrays ;
1516import java .util .Calendar ;
139140
140141import org .mitre .synthea .engine .Components ;
141142import org .mitre .synthea .engine .Components .Attachment ;
143+ import org .mitre .synthea .export .rif .CodeMapper ;
142144import org .mitre .synthea .helpers .Config ;
145+ import org .mitre .synthea .helpers .RandomNumberGenerator ;
146+ import org .mitre .synthea .helpers .RandomValueGenerator ;
143147import org .mitre .synthea .helpers .SimpleCSV ;
144148import org .mitre .synthea .helpers .Utilities ;
145149import org .mitre .synthea .identity .Entity ;
@@ -404,7 +408,7 @@ public static Bundle convertToFHIR(Person person, long stopTime) {
404408
405409 if (shouldExport (Condition .class )) {
406410 for (HealthRecord .Entry condition : encounter .conditions ) {
407- condition (personEntry , bundle , encounterEntry , condition );
411+ condition (person , personEntry , bundle , encounterEntry , condition );
408412 }
409413 }
410414
@@ -431,7 +435,7 @@ public static Bundle convertToFHIR(Person person, long stopTime) {
431435
432436 if (shouldExport (org .hl7 .fhir .r4 .model .Procedure .class )) {
433437 for (Procedure procedure : encounter .procedures ) {
434- procedure (personEntry , bundle , encounterEntry , procedure );
438+ procedure (person , personEntry , bundle , encounterEntry , procedure );
435439 }
436440 }
437441
@@ -849,6 +853,27 @@ private static BundleEntryComponent basicInfo(Person person, Bundle bundle, long
849853 return newEntry (bundle , patientResource , (String ) person .attributes .get (Person .ID ));
850854 }
851855
856+ /**
857+ * Add a code translation (if available) of the supplied source code to the
858+ * supplied CodeableConcept.
859+ * @param codeSystem the code system of the translated code
860+ * @param from the source code
861+ * @param to the CodeableConcept to add the translation to
862+ * @param rand a source of randomness
863+ */
864+ private static void addTranslation (String codeSystem , Code from ,
865+ CodeableConcept to , RandomNumberGenerator rand ) {
866+ CodeMapper mapper = Exporter .getCodeMapper (codeSystem );
867+ if (mapper != null && mapper .canMap (from )) {
868+ Coding coding = new Coding ();
869+ Map .Entry <String , String > mappedCode = mapper .mapToCodeAndDescription (from , rand );
870+ coding .setCode (mappedCode .getKey ());
871+ coding .setDisplay (mappedCode .getValue ());
872+ coding .setSystem (ExportHelper .getSystemURI ("ICD10-CM" ));
873+ to .addCoding (coding );
874+ }
875+ }
876+
852877 /**
853878 * Map the given Encounter into a FHIR Encounter resource, and add it to the given Bundle.
854879 *
@@ -894,6 +919,8 @@ private static BundleEntryComponent encounter(Person person, BundleEntryComponen
894919 if (encounter .reason != null ) {
895920 encounterResource .addReasonCode ().addCoding ().setCode (encounter .reason .code )
896921 .setDisplay (encounter .reason .display ).setSystem (SNOMED_URI );
922+ addTranslation ("ICD10-CM" , encounter .reason ,
923+ encounterResource .getReasonCodeFirstRep (), person );
897924 }
898925
899926 Provider provider = encounter .provider ;
@@ -1607,6 +1634,7 @@ private static BundleEntryComponent explanationOfBenefit(BundleEntryComponent pe
16071634 * @return The added Entry
16081635 */
16091636 private static BundleEntryComponent condition (
1637+ RandomNumberGenerator rand ,
16101638 BundleEntryComponent personEntry , Bundle bundle , BundleEntryComponent encounterEntry ,
16111639 HealthRecord .Entry condition ) {
16121640 Condition conditionResource = new Condition ();
@@ -1630,7 +1658,9 @@ private static BundleEntryComponent condition(
16301658 conditionResource .setEncounter (new Reference (encounterEntry .getFullUrl ()));
16311659
16321660 Code code = condition .codes .get (0 );
1633- conditionResource .setCode (mapCodeToCodeableConcept (code , SNOMED_URI ));
1661+ CodeableConcept concept = mapCodeToCodeableConcept (code , SNOMED_URI );
1662+ addTranslation ("ICD10-CM" , code , concept , rand );
1663+ conditionResource .setCode (concept );
16341664
16351665 CodeableConcept verification = new CodeableConcept ();
16361666 verification .getCodingFirstRep ()
@@ -1964,13 +1994,14 @@ static org.hl7.fhir.r4.model.SampledData mapValueToSampledData(
19641994 /**
19651995 * Map the given Procedure into a FHIR Procedure resource, and add it to the given Bundle.
19661996 *
1997+ * @param person The Person
19671998 * @param personEntry The Person entry
19681999 * @param bundle Bundle to add to
19692000 * @param encounterEntry The current Encounter entry
19702001 * @param procedure The Procedure
19712002 * @return The added Entry
19722003 */
1973- private static BundleEntryComponent procedure (
2004+ private static BundleEntryComponent procedure (Person person ,
19742005 BundleEntryComponent personEntry , Bundle bundle , BundleEntryComponent encounterEntry ,
19752006 Procedure procedure ) {
19762007 org .hl7 .fhir .r4 .model .Procedure procedureResource = new org .hl7 .fhir .r4 .model .Procedure ();
@@ -2013,6 +2044,7 @@ private static BundleEntryComponent procedure(
20132044 // we didn't find a matching Condition,
20142045 // fallback to just reason code
20152046 procedureResource .addReasonCode (mapCodeToCodeableConcept (reason , SNOMED_URI ));
2047+ addTranslation ("ICD10-CM" , reason , procedureResource .getReasonCodeFirstRep (), person );
20162048 }
20172049 }
20182050
@@ -2322,6 +2354,8 @@ && shouldExport(org.hl7.fhir.r4.model.Medication.class)) {
23222354 // we didn't find a matching Condition,
23232355 // fallback to just reason code
23242356 medicationResource .addReasonCode (mapCodeToCodeableConcept (reason , SNOMED_URI ));
2357+ addTranslation ("ICD10-CM" , reason , medicationResource .getReasonCodeFirstRep (),
2358+ person );
23252359 }
23262360 }
23272361
@@ -2474,6 +2508,8 @@ private static BundleEntryComponent medicationAdministration(
24742508 // we didn't find a matching Condition,
24752509 // fallback to just reason code
24762510 medicationResource .addReasonCode (mapCodeToCodeableConcept (reason , SNOMED_URI ));
2511+ addTranslation ("ICD10-CM" , reason , medicationResource .getReasonCodeFirstRep (),
2512+ person );
24772513 }
24782514 }
24792515
@@ -2717,6 +2753,8 @@ private static BundleEntryComponent carePlan(Person person,
27172753 activityDetailComponent .addReasonReference ().setReference (reasonCondition .getFullUrl ());
27182754 } else if (reason != null ) {
27192755 activityDetailComponent .addReasonCode (mapCodeToCodeableConcept (reason , SNOMED_URI ));
2756+ addTranslation ("ICD10-CM" , reason , activityDetailComponent .getReasonCodeFirstRep (),
2757+ person );
27202758 }
27212759
27222760 activityComponent .setDetail (activityDetailComponent );
@@ -2863,7 +2901,9 @@ private static BundleEntryComponent careTeam(Person person,
28632901
28642902 if (carePlan .reasons != null && !carePlan .reasons .isEmpty ()) {
28652903 for (Code code : carePlan .reasons ) {
2866- careTeam .addReasonCode (mapCodeToCodeableConcept (code , SNOMED_URI ));
2904+ CodeableConcept concept = mapCodeToCodeableConcept (code , SNOMED_URI );
2905+ addTranslation ("ICD10-CM" , code , concept , person );
2906+ careTeam .addReasonCode (concept );
28672907 }
28682908 }
28692909
0 commit comments