use of org.mitre.synthea.world.concepts.HealthRecord.Encounter in project synthea by synthetichealth.
the class FhirDstu2 method medication.
/**
* Map the given Medication to a FHIR MedicationRequest resource, and add it to the given Bundle.
*
* @param rand
* Source of randomness to use when generating ids etc
* @param personEntry
* The Entry for the Person
* @param bundle
* Bundle to add the Medication to
* @param encounterEntry
* Current Encounter entry
* @param medication
* The Medication
* @return The added Entry
*/
private static Entry medication(RandomNumberGenerator rand, Entry personEntry, Bundle bundle, Entry encounterEntry, Medication medication) {
MedicationOrder medicationResource = new MedicationOrder();
medicationResource.setPatient(new ResourceReferenceDt(personEntry.getFullUrl()));
medicationResource.setEncounter(new ResourceReferenceDt(encounterEntry.getFullUrl()));
ca.uhn.fhir.model.dstu2.resource.Encounter encounter = (ca.uhn.fhir.model.dstu2.resource.Encounter) encounterEntry.getResource();
medicationResource.setPrescriber(encounter.getParticipantFirstRep().getIndividual());
Code code = medication.codes.get(0);
String system = code.system.equals("SNOMED-CT") ? SNOMED_URI : RXNORM_URI;
medicationResource.setMedication(mapCodeToCodeableConcept(code, system));
medicationResource.setDateWritten(new DateTimeDt(new Date(medication.start)));
if (medication.stop != 0L) {
medicationResource.setStatus(MedicationOrderStatusEnum.STOPPED);
} else {
medicationResource.setStatus(MedicationOrderStatusEnum.ACTIVE);
}
if (!medication.reasons.isEmpty()) {
// Only one element in list
Code reason = medication.reasons.get(0);
for (Entry entry : bundle.getEntry()) {
if (entry.getResource().getResourceName().equals("Condition")) {
Condition condition = (Condition) entry.getResource();
// Only one element in list
CodingDt coding = condition.getCode().getCoding().get(0);
if (reason.code.equals(coding.getCode())) {
medicationResource.setReason(new ResourceReferenceDt(entry.getFullUrl()));
}
}
}
}
if (medication.prescriptionDetails != null) {
JsonObject rxInfo = medication.prescriptionDetails;
DosageInstruction dosage = new DosageInstruction();
// as_needed is true if present
dosage.setAsNeeded(new BooleanDt(rxInfo.has("as_needed")));
// as_needed is true if present
if ((rxInfo.has("dosage")) && (!rxInfo.has("as_needed"))) {
TimingDt timing = new TimingDt();
Repeat timingRepeatComponent = new Repeat();
timingRepeatComponent.setFrequency(rxInfo.get("dosage").getAsJsonObject().get("frequency").getAsInt());
timingRepeatComponent.setPeriod(rxInfo.get("dosage").getAsJsonObject().get("period").getAsDouble());
timingRepeatComponent.setPeriodUnits(convertUcumCode(rxInfo.get("dosage").getAsJsonObject().get("unit").getAsString()));
timing.setRepeat(timingRepeatComponent);
dosage.setTiming(timing);
QuantityDt dose = new SimpleQuantityDt().setValue(rxInfo.get("dosage").getAsJsonObject().get("amount").getAsDouble());
dosage.setDose(dose);
if (rxInfo.has("instructions")) {
for (JsonElement instructionElement : rxInfo.get("instructions").getAsJsonArray()) {
JsonObject instruction = instructionElement.getAsJsonObject();
Code instructionCode = new Code(SNOMED_URI, instruction.get("code").getAsString(), instruction.get("display").getAsString());
dosage.setAdditionalInstructions(mapCodeToCodeableConcept(instructionCode, SNOMED_URI));
}
}
}
List<DosageInstruction> dosageInstruction = new ArrayList<DosageInstruction>();
dosageInstruction.add(dosage);
medicationResource.setDosageInstruction(dosageInstruction);
}
Entry medicationEntry = newEntry(rand, bundle, medicationResource);
// create new claim for medication
medicationClaim(rand, personEntry, bundle, encounterEntry, medication.claim, medicationEntry);
// Create new administration for medication, if needed
if (medication.administration) {
medicationAdministration(rand, personEntry, bundle, encounterEntry, medication);
}
return medicationEntry;
}
use of org.mitre.synthea.world.concepts.HealthRecord.Encounter in project synthea by synthetichealth.
the class FhirDstu2 method encounterClaim.
/**
* Create an entry for the given Claim, associated to an Encounter.
*
* @param rand
* Source of randomness to use when generating ids etc
* @param personEntry
* Entry for the person
* @param bundle
* The Bundle to add to
* @param encounterEntry
* The current Encounter
* @param claim
* the Claim object
* @return the added Entry
*/
private static Entry encounterClaim(RandomNumberGenerator rand, Entry personEntry, Bundle bundle, Entry encounterEntry, Claim claim) {
ca.uhn.fhir.model.dstu2.resource.Claim claimResource = new ca.uhn.fhir.model.dstu2.resource.Claim();
ca.uhn.fhir.model.dstu2.resource.Encounter encounterResource = (ca.uhn.fhir.model.dstu2.resource.Encounter) encounterEntry.getResource();
// assume institutional claim
claimResource.setType(ClaimTypeEnum.INSTITUTIONAL);
claimResource.setUse(UseEnum.COMPLETE);
claimResource.setPatient(new ResourceReferenceDt(personEntry.getFullUrl()));
claimResource.setOrganization(encounterResource.getServiceProvider());
// Add data for the encounter
ca.uhn.fhir.model.dstu2.resource.Claim.Item encounterItem = new ca.uhn.fhir.model.dstu2.resource.Claim.Item();
encounterItem.setSequence(new PositiveIntDt(1));
// assume item type is clinical service invoice
CodingDt itemType = new CodingDt();
itemType.setSystem("http://hl7.org/fhir/v3/ActCode").setCode("CSINV").setDisplay("clinical service invoice");
encounterItem.setType(itemType);
CodingDt itemService = new CodingDt();
ca.uhn.fhir.model.dstu2.resource.Encounter encounter = (ca.uhn.fhir.model.dstu2.resource.Encounter) encounterEntry.getResource();
itemService.setSystem(encounter.getTypeFirstRep().getCodingFirstRep().getSystem()).setCode(encounter.getTypeFirstRep().getCodingFirstRep().getCode()).setDisplay(encounter.getTypeFirstRep().getCodingFirstRep().getDisplay());
encounterItem.setService(itemService);
claimResource.addItem(encounterItem);
int itemSequence = 2;
int conditionSequence = 1;
for (Claim.ClaimEntry claimEntry : claim.items) {
HealthRecord.Entry item = claimEntry.entry;
if (Costs.hasCost(item)) {
// update claimItems list
ca.uhn.fhir.model.dstu2.resource.Claim.Item procedureItem = new ca.uhn.fhir.model.dstu2.resource.Claim.Item();
procedureItem.setSequence(new PositiveIntDt(itemSequence));
// calculate the cost of the procedure
MoneyDt moneyResource = new MoneyDt();
moneyResource.setCode("USD");
moneyResource.setSystem("urn:iso:std:iso:4217");
moneyResource.setValue(item.getCost());
procedureItem.setNet(moneyResource);
// assume item type is clinical service invoice
itemType = new CodingDt();
itemType.setSystem("http://hl7.org/fhir/v3/ActCode").setCode("CSINV").setDisplay("clinical service invoice");
procedureItem.setType(itemType);
// item service should match the entry code
itemService = new CodingDt();
Code code = item.codes.get(0);
String system = ExportHelper.getSystemURI(code.system);
itemService.setSystem(system).setCode(code.code).setDisplay(code.display);
procedureItem.setService(itemService);
claimResource.addItem(procedureItem);
} else {
// assume it's a Condition, we don't have a Condition class specifically
// add diagnosisComponent to claim
ca.uhn.fhir.model.dstu2.resource.Claim.Diagnosis diagnosisComponent = new ca.uhn.fhir.model.dstu2.resource.Claim.Diagnosis();
diagnosisComponent.setSequence(new PositiveIntDt(conditionSequence));
if (item.codes.size() > 0) {
// use first code
Code code = item.codes.get(0);
String system = ExportHelper.getSystemURI(code.system);
diagnosisComponent.setDiagnosis(new CodingDt(system, code.code).setDisplay(code.display));
}
claimResource.addDiagnosis(diagnosisComponent);
conditionSequence++;
}
itemSequence++;
}
return newEntry(rand, bundle, claimResource);
}
use of org.mitre.synthea.world.concepts.HealthRecord.Encounter in project synthea by synthetichealth.
the class CCDAExporter method export.
/**
* Export a CCDA R2.1 document for a Person at a given time.
*
* @param person
* Person to export.
* @param time
* Time the record should be generated. Any content in the record AFTER this time will
* not be included.
* @return String of CCDA R2.1 XML.
*/
public static String export(Person person, long time) {
// create a super encounter... this makes it easier to access
// all the Allergies (for example) in the export templates,
// instead of having to iterate through all the encounters.
Encounter superEncounter = person.record.new Encounter(time, "super");
for (Encounter encounter : person.record.encounters) {
if (encounter.start <= time) {
superEncounter.observations.addAll(encounter.observations);
superEncounter.reports.addAll(encounter.reports);
superEncounter.conditions.addAll(encounter.conditions);
superEncounter.allergies.addAll(encounter.allergies);
superEncounter.procedures.addAll(encounter.procedures);
superEncounter.immunizations.addAll(encounter.immunizations);
superEncounter.medications.addAll(encounter.medications);
superEncounter.careplans.addAll(encounter.careplans);
superEncounter.imagingStudies.addAll(encounter.imagingStudies);
} else {
break;
}
}
// The export templates fill in the record by accessing the attributes
// of the Person, so we add a few attributes just for the purposes of export.
person.attributes.put("UUID", new UUIDGenerator(person));
person.attributes.put("ehr_encounters", person.record.encounters);
person.attributes.put("ehr_observations", superEncounter.observations);
person.attributes.put("ehr_reports", superEncounter.reports);
person.attributes.put("ehr_conditions", superEncounter.conditions);
person.attributes.put("ehr_allergies", superEncounter.allergies);
person.attributes.put("ehr_procedures", superEncounter.procedures);
person.attributes.put("ehr_immunizations", superEncounter.immunizations);
person.attributes.put("ehr_medications", superEncounter.medications);
person.attributes.put("ehr_careplans", superEncounter.careplans);
person.attributes.put("ehr_imaging_studies", superEncounter.imagingStudies);
Observation smokingHistory = person.record.getLatestObservation("72166-2");
if (smokingHistory != null) {
person.attributes.put("ehr_smoking_history", smokingHistory);
}
person.attributes.put("time", time);
person.attributes.put("race_lookup", RaceAndEthnicity.LOOK_UP_CDC_RACE);
person.attributes.put("ethnicity_lookup", RaceAndEthnicity.LOOK_UP_CDC_ETHNICITY_CODE);
person.attributes.put("ethnicity_display_lookup", RaceAndEthnicity.LOOK_UP_CDC_ETHNICITY_DISPLAY);
if (person.attributes.get(Person.PREFERREDYPROVIDER + "wellness") == null) {
// This person does not have a preferred provider. This happens for veterans at age 20 due to
// the provider reset and they don't have a provider until their next wellness visit. There
// may be other cases. This ensures the preferred provider is there for the CCDA template
Encounter encounter = person.record.lastWellnessEncounter();
if (encounter != null) {
person.attributes.put(Person.PREFERREDYPROVIDER + "wellness", encounter.provider);
} else {
throw new IllegalStateException(String.format("Unable to export to CCDA because " + "person %s %s has no preferred provider.", person.attributes.get(Person.FIRST_NAME), person.attributes.get(Person.LAST_NAME)));
}
}
StringWriter writer = new StringWriter();
try {
Template template = TEMPLATES.getTemplate("ccda.ftl");
template.process(person.attributes, writer);
} catch (Exception e) {
throw new RuntimeException(e);
}
return writer.toString();
}
use of org.mitre.synthea.world.concepts.HealthRecord.Encounter in project synthea by synthetichealth.
the class CDWExporter method export.
/**
* Add a single Person's health record info to the CSV records.
* @param person Person to write record data for
* @param time Time the simulation ended
* @throws IOException if any IO error occurs
*/
public void export(Person person, long time) throws IOException {
// Ignore civilians, only consider the veteran population.
if (!person.attributes.containsKey("veteran")) {
return;
}
int primarySta3n = -1;
Provider provider = person.getProvider(EncounterType.WELLNESS, time);
if (provider != null) {
String state = Location.getStateName(provider.state);
String tz = Location.getTimezoneByState(state);
primarySta3n = sta3n.addFact(provider.id, clean(provider.name) + "," + tz);
location.addFact(provider.id, clean(provider.name));
}
int personID = patient(person, primarySta3n, time);
for (Encounter encounter : person.record.encounters) {
int encounterID = encounter(personID, person, encounter, primarySta3n);
for (HealthRecord.Entry condition : encounter.conditions) {
condition(personID, encounterID, encounter, condition, primarySta3n);
}
for (HealthRecord.Entry allergy : encounter.allergies) {
allergy(personID, person, encounterID, encounter, allergy, primarySta3n);
}
for (HealthRecord.Report report : encounter.reports) {
// Ignore death certificates
if (!DeathModule.DEATH_CERTIFICATE.equals(report.codes.get(0))) {
report(personID, encounterID, encounter, primarySta3n, report);
}
}
for (Observation observation : encounter.observations) {
observation(personID, encounterID, encounter, primarySta3n, observation);
}
for (Procedure procedure : encounter.procedures) {
procedure(personID, encounterID, encounter, procedure, primarySta3n);
}
for (Medication medication : encounter.medications) {
medication(personID, encounterID, encounter, medication, primarySta3n);
}
for (Immunization immunization : encounter.immunizations) {
immunization(personID, person, encounterID, encounter, immunization, primarySta3n);
}
}
// Patient Data
lookuppatient.flush();
spatient.flush();
spatientaddress.flush();
spatientphone.flush();
patientrace.flush();
patientethnicity.flush();
// Encounter Data
consult.flush();
visit.flush();
appointment.flush();
inpatient.flush();
// Immunization Data
immunization.flush();
// Allergy Data
allergy.flush();
allergicreaction.flush();
allergycomment.flush();
// Condition Data
problemlist.flush();
vdiagnosis.flush();
// Medication Data
rxoutpatient.flush();
rxoutpatfill.flush();
nonvamed.flush();
cprsorder.flush();
ordereditem.flush();
// Diagnostic Report Data
labchem.flush();
labpanel.flush();
patientlabchem.flush();
vprocedure.flush();
// Procedure Data
surgeryProcedureDiagnosisCode.flush();
surgeryPRE.flush();
// Vital Sign Observation Data
vitalSign.flush();
}
use of org.mitre.synthea.world.concepts.HealthRecord.Encounter in project synthea by synthetichealth.
the class TextExporter method exportEncounter.
/**
* Produce and export a person's record in text format.
*
* @param person Person
* @param time Time the simulation ended
* @throws IOException if any error occurs writing to the standard export location
*/
public static void exportEncounter(Person person, long time) throws IOException {
List<Encounter> encounters = person.record.encounters;
List<Entry> conditions = new ArrayList<>();
List<Entry> allergies = new ArrayList<>();
List<Medication> medications = new ArrayList<>();
List<CarePlan> careplans = new ArrayList<>();
for (Encounter encounter : person.record.encounters) {
conditions.addAll(encounter.conditions);
allergies.addAll(encounter.allergies);
medications.addAll(encounter.medications);
careplans.addAll(encounter.careplans);
}
// reverse these items so they are displayed in reverse chrono order
Collections.reverse(encounters);
Collections.reverse(conditions);
Collections.reverse(allergies);
Collections.reverse(medications);
Collections.reverse(careplans);
// set an integer that will be used as a counter for file naming purposes
int encounterNumber = 0;
for (Encounter encounter : encounters) {
// make a record for each encounter to write information
List<String> textRecord = new LinkedList<>();
basicInfo(textRecord, person, time);
breakline(textRecord);
textRecord.add("ALLERGIES:");
if (allergies.isEmpty()) {
textRecord.add("No Known Allergies");
} else {
for (Entry allergy : allergies) {
condition(textRecord, allergy, false);
}
}
breakline(textRecord);
textRecord.add("ENCOUNTER");
encounterReport(textRecord, person, encounter);
breakline(textRecord);
textRecord.add("CONTINUING");
textRecord.add(" ");
textRecord.add(" CONDITIONS:");
for (Entry condition : conditions) {
conditionpast(textRecord, condition, encounter);
}
textRecord.add(" ");
textRecord.add(" MEDICATIONS:");
for (Medication medication : medications) {
medicationpast(textRecord, medication, encounter);
}
textRecord.add(" ");
textRecord.add(" CAREPLANS:");
for (CarePlan careplan : careplans) {
careplanpast(textRecord, careplan, encounter);
}
textRecord.add(" ");
breakline(textRecord);
encounterNumber++;
// write to the file
File outDirectory2 = Exporter.getOutputFolder("text_encounters", person);
Path outFilePath2 = outDirectory2.toPath().resolve(Exporter.filename(person, Integer.toString(encounterNumber), "txt"));
Files.write(outFilePath2, textRecord, StandardOpenOption.CREATE_NEW);
}
}
Aggregations