Skip to content

Commit

Permalink
Minor refactoring. Introducing hip as variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
angshu committed Jul 29, 2020
1 parent c5d5b5b commit b979a63
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 82 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

### Random Data Generator For Patients.
takes the following parameters
- out - the location directory where the file would be placed. Default in /tmp
- type - PR (Prescription), DR (DiagnosticReport). Must be provided.
- name - should be given and if given, will throw error if not found in *patients.properties* file. Will default to 'navjot' if no name is given. You may add more entries in the patients.properties file. Right now can generate for - hina, navjot, janki, nitesh
- fromDate - provide in format yyyy-MM-dd (e.g 2020-03-21). If not given will default to current date.
- number - how many documents to generate. If not given, only 1 is generated. Documents are generated in 2 days interval.
- **out** - the location directory where the file would be placed. Default in /tmp
- **type** - PR (Prescription), DR (DiagnosticReport). Must be provided.
- **name** - should be given and if given, will throw error if not found in *patients.properties* file. Will default to 'navjot' if no name is given. You may add more entries in the patients.properties file. Right now can generate for - hina, navjot, janki, nitesh
- **fromDate** - provide in format yyyy-MM-dd (e.g 2020-03-21). If not given will default to current date.
- **number** - how many documents to generate. If not given, only 1 is generated. Documents are generated in 2 days interval.
- **hip** - for which HIP to generate data. If not given, max is the default. Possible values are - max, tmh, wbc. You may change by adding another json in the resources/orgs/ directory.

## Build from source

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sourceCompatibility = '11'

jar {
manifest {
attributes('Main-Class': 'in.projecteka.data.Application')
attributes('Main-Class': 'in.projecteka.utils.Application')
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package in.projecteka.data;
package in.projecteka.utils;

import in.projecteka.utils.data.DiagnosticReportGenerator;
import in.projecteka.utils.data.DocumentGenerator;
import in.projecteka.utils.data.PrescriptionGenerator;
import in.projecteka.utils.data.Utils;

import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -27,15 +32,24 @@ public static void main(String[] args) throws Exception {
String patientName = getPatientName(checkRequired("name"));
Date fromDate = getFromDate(checkRequired("fromDate"));
int number = getNumerOfInstances(checkRequired("number"));
String hip = getHip(checkRequired("hip"));
DocumentGenerator documentGenerator = generators.get(type);
documentGenerator.init();
try {
documentGenerator.execute(patientName, fromDate, number, location);
documentGenerator.execute(patientName, fromDate, number, location, hip);
} catch (Exception e) {
e.printStackTrace();
}
}

private static String getHip(String hip) {
if (Utils.isBlank(hip)) {
System.out.println("hip not provided. Defaulting to max");
return "max";
}
return hip;
}

private static String getPatientName(String name) {
if (Utils.isBlank(name)) {
System.out.println("name not provided. Defaulting to navjot");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package in.projecteka.data;
package in.projecteka.utils.data;

public class Constants {
private static final String EKA_SYSTEM = "https://projecteka.in/%s";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package in.projecteka.data;
package in.projecteka.utils.data;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.parser.IParser;
import in.projecteka.data.model.DiagnosticTest;
import in.projecteka.data.model.Doctor;
import in.projecteka.data.model.Obs;
import in.projecteka.utils.data.model.DiagnosticTest;
import in.projecteka.utils.data.model.Doctor;
import in.projecteka.utils.data.model.Obs;
import org.hl7.fhir.r4.model.Attachment;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.CodeableConcept;
Expand Down Expand Up @@ -33,19 +33,8 @@
import java.util.UUID;
import java.util.stream.Collectors;

import static in.projecteka.data.Constants.EKA_LOINC_SYSTEM;
import static in.projecteka.data.FHIRUtils.addToBundleEntry;
import static in.projecteka.data.FHIRUtils.createAuthor;
import static in.projecteka.data.FHIRUtils.createBundle;
import static in.projecteka.data.FHIRUtils.createEncounter;
import static in.projecteka.data.FHIRUtils.getDateTimeType;
import static in.projecteka.data.FHIRUtils.getDiagnosticReportType;
import static in.projecteka.data.FHIRUtils.getIdentifier;
import static in.projecteka.data.FHIRUtils.getPatientResource;
import static in.projecteka.data.FHIRUtils.getReferenceToPatient;
import static in.projecteka.data.FHIRUtils.getReferenceToResource;
import static in.projecteka.data.FHIRUtils.loadOrganization;
import static in.projecteka.data.Utils.randomBool;
import static in.projecteka.utils.data.Constants.EKA_LOINC_SYSTEM;
import static in.projecteka.utils.data.Utils.randomBool;

public class DiagnosticReportGenerator implements DocumentGenerator {
private Properties doctors;
Expand All @@ -58,20 +47,22 @@ public void init() throws Exception {
}

@Override
public void execute(String patientName, Date fromDate, int number, Path location) throws Exception {
public void execute(String patientName, Date fromDate, int number, Path location, String hipPrefix) throws Exception {
FhirContext fhirContext = FhirContext.forR4();
LocalDateTime dateTime = fromDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
for (int i = 0; i < number; i++) {
Date date = Utils.getNextDate(dateTime, i);
Bundle bundle = createDiagnosticReportBundle(date, patientName, "max", fhirContext.newJsonParser());
Bundle bundle = createDiagnosticReportBundle(date, patientName, hipPrefix, fhirContext.newJsonParser());
String encodedString = fhirContext.newJsonParser().encodeResourceToString(bundle);
List<Bundle.BundleEntryComponent> patientEntries =
bundle.getEntry().stream()
.filter(e -> e.getResource().getResourceType().equals(ResourceType.Patient))
.collect(Collectors.toList());
Bundle.BundleEntryComponent patientEntry = patientEntries.get(0);
String fileName = String.format("%sDiagnosticReportDoc%s.json",
patientEntry.getResource().getId(), Utils.formatDate(date, "yyyyMMdd"));
String fileName = String.format("%s%sDiagnosticReportDoc%s.json",
hipPrefix.toUpperCase(),
patientEntry.getResource().getId(),
Utils.formatDate(date, "yyyyMMdd"));
Path path = Paths.get(location.toString(), fileName);
System.out.println("Saving DiagnosticReport to file:" + path.toString());
Utils.saveToFile(path, encodedString);
Expand All @@ -80,61 +71,61 @@ public void execute(String patientName, Date fromDate, int number, Path location
}

private Bundle createDiagnosticReportBundle(Date date, String patientName, String hipPrefix, IParser parser) throws Exception {
Bundle bundle = createBundle(date, hipPrefix);
Patient patientResource = getPatientResource(patientName, patients);
Bundle bundle = FHIRUtils.createBundle(date, hipPrefix);
Patient patientResource = FHIRUtils.getPatientResource(patientName, patients);
Reference patientRef = new Reference();
patientRef.setResource(patientResource);
Composition reportDoc = new Composition();
reportDoc.setId(UUID.randomUUID().toString());
reportDoc.setDate(bundle.getTimestamp());
reportDoc.setIdentifier(getIdentifier(reportDoc.getId(), hipPrefix, "document"));
reportDoc.setIdentifier(FHIRUtils.getIdentifier(reportDoc.getId(), hipPrefix, "document"));
reportDoc.setStatus(Composition.CompositionStatus.FINAL);
reportDoc.setType(getDiagnosticReportType());
reportDoc.setType(FHIRUtils.getDiagnosticReportType());
reportDoc.setTitle("Diagnostic Report Document");
addToBundleEntry(bundle, reportDoc, false);
Practitioner author = createAuthor(hipPrefix, doctors);
addToBundleEntry(bundle, author, false);
FHIRUtils.addToBundleEntry(bundle, reportDoc, false);
Practitioner author = FHIRUtils.createAuthor(hipPrefix, doctors);
FHIRUtils.addToBundleEntry(bundle, author, false);
reportDoc.addAuthor().setResource(author);
if (randomBool()) {
reportDoc.getAuthor().get(0).setDisplay(Doctor.getDisplay(author));
}
addToBundleEntry(bundle, patientResource, false);
reportDoc.setSubject(getReferenceToPatient(patientResource));
FHIRUtils.addToBundleEntry(bundle, patientResource, false);
reportDoc.setSubject(FHIRUtils.getReferenceToPatient(patientResource));
Composition.SectionComponent section = reportDoc.addSection();
section.setTitle("# Diagnostic Report");
section.setCode(getDiagnosticReportType());
section.setCode(FHIRUtils.getDiagnosticReportType());

Encounter encounter = createEncounter("Outpatient visit", "AMB", reportDoc.getDate());
encounter.setSubject(getReferenceToPatient(patientResource));
addToBundleEntry(bundle, encounter, false);
Reference referenceToResource = getReferenceToResource(encounter);
Encounter encounter = FHIRUtils.createEncounter("Outpatient visit", "AMB", reportDoc.getDate());
encounter.setSubject(FHIRUtils.getReferenceToPatient(patientResource));
FHIRUtils.addToBundleEntry(bundle, encounter, false);
Reference referenceToResource = FHIRUtils.getReferenceToResource(encounter);
reportDoc.setEncounter(referenceToResource);

DiagnosticReport report = new DiagnosticReport();
report.setId(UUID.randomUUID().toString());
report.setStatus(DiagnosticReport.DiagnosticReportStatus.FINAL);
section.getEntry().add(getReferenceToResource(report));
addToBundleEntry(bundle, report, false);
report.setSubject(getReferenceToPatient(patientResource));
section.getEntry().add(FHIRUtils.getReferenceToResource(report));
FHIRUtils.addToBundleEntry(bundle, report, false);
report.setSubject(FHIRUtils.getReferenceToPatient(patientResource));
if (randomBool()) {
report.setEncounter(referenceToResource);
}

report.setIssued(date);
if (randomBool()) {
report.setEffective(getDateTimeType(date));
report.setEffective(FHIRUtils.getDateTimeType(date));
}

Organization organization = parser.parseResource(Organization.class, loadOrganization(hipPrefix));
addToBundleEntry(bundle, organization, true);
report.setPerformer(Collections.singletonList(getReferenceToResource(organization)));
Organization organization = parser.parseResource(Organization.class, FHIRUtils.loadOrganization(hipPrefix));
FHIRUtils.addToBundleEntry(bundle, organization, true);
report.setPerformer(Collections.singletonList(FHIRUtils.getReferenceToResource(organization)));

Practitioner interpreter = author;
if (randomBool()) {
interpreter = createAuthor(hipPrefix, doctors);
addToBundleEntry(bundle, interpreter, false);
interpreter = FHIRUtils.createAuthor(hipPrefix, doctors);
FHIRUtils.addToBundleEntry(bundle, interpreter, false);
}
report.setResultsInterpreter(Collections.singletonList(getReferenceToResource(interpreter)));
report.setResultsInterpreter(Collections.singletonList(FHIRUtils.getReferenceToResource(interpreter)));
report.setCode(getDiagnosticTestCode(DiagnosticTest.getRandomTest()));

if (randomBool()) {
Expand All @@ -153,8 +144,8 @@ private Bundle createDiagnosticReportBundle(Date date, String patientName, Strin

if (randomBool()) {
DocumentReference docReference = getReportAsDocReference(author);
addToBundleEntry(bundle, docReference, false);
section.getEntry().add(getReferenceToResource(docReference));
FHIRUtils.addToBundleEntry(bundle, docReference, false);
section.getEntry().add(FHIRUtils.getReferenceToResource(docReference));
}

return bundle;
Expand All @@ -163,22 +154,22 @@ private Bundle createDiagnosticReportBundle(Date date, String patientName, Strin
private void addObservvationsToBundle(IParser parser, Bundle bundle, DiagnosticReport report) {
Observation observation = parser.parseResource(Observation.class, Obs.getObservationResString());
observation.setId(UUID.randomUUID().toString());
addToBundleEntry(bundle, observation, true);
report.addResult(getReferenceToResource(observation));
FHIRUtils.addToBundleEntry(bundle, observation, true);
report.addResult(FHIRUtils.getReferenceToResource(observation));
}

private DocumentReference getReportAsDocReference(Practitioner author) throws IOException {
DocumentReference documentReference = new DocumentReference();
documentReference.setStatus(Enumerations.DocumentReferenceStatus.CURRENT);
documentReference.setId(UUID.randomUUID().toString());
documentReference.setType(getDiagnosticReportType());
documentReference.setType(FHIRUtils.getDiagnosticReportType());
CodeableConcept concept = new CodeableConcept();
Coding coding = concept.addCoding();
coding.setSystem(EKA_LOINC_SYSTEM);
coding.setCode("30954-2");
coding.setDisplay("Surgical Pathology Report");
documentReference.setType(concept);
documentReference.setAuthor(Collections.singletonList(getReferenceToResource(author)));
documentReference.setAuthor(Collections.singletonList(FHIRUtils.getReferenceToResource(author)));
DocumentReference.DocumentReferenceContentComponent content = documentReference.addContent();
content.setAttachment(getSurgicalReportAsAttachment());
return documentReference;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package in.projecteka.data;
package in.projecteka.utils.data;

import java.nio.file.Path;
import java.util.Date;

public interface DocumentGenerator {
void init() throws Exception;
void execute(String patientName, Date fromDate, int number, Path location) throws Exception;
void execute(String patientName, Date fromDate, int number, Path location, String hipPrefix) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package in.projecteka.data;
package in.projecteka.utils.data;

import in.projecteka.data.model.Doctor;
import in.projecteka.utils.data.model.Doctor;
import lombok.SneakyThrows;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.CodeableConcept;
Expand Down Expand Up @@ -65,7 +65,7 @@ static Bundle createBundle(Date forDate, String hipDomain) {
}

static Practitioner createAuthor(String hipPrefix, Properties doctors) {
String details = (String) doctors.get(String.valueOf(Utils.randomInt(1, 3)));
String details = (String) doctors.get(String.valueOf(Utils.randomInt(1, doctors.size())));
Doctor doc = Doctor.parse(details);
Practitioner practitioner = new Practitioner();
practitioner.setId(hipPrefix.toUpperCase() + doc.getDocId());
Expand All @@ -79,7 +79,7 @@ static Patient getPatientResource(String name, Properties patients) throws Excep
if (patientDetail == null) {
throw new Exception("Can not identify patient with name: " + name);
}
in.projecteka.data.model.Patient patient = in.projecteka.data.model.Patient.parse((String) patientDetail);
in.projecteka.utils.data.model.Patient patient = in.projecteka.utils.data.model.Patient.parse((String) patientDetail);
Patient patientResource = new Patient();
patientResource.setId(patient.getHid());
patientResource.setName(Collections.singletonList(getHumanName(patient.getName(), null, null)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package in.projecteka.data;
package in.projecteka.utils.data;

import ca.uhn.fhir.context.FhirContext;
import in.projecteka.data.model.Doctor;
import in.projecteka.data.model.Medicine;
import in.projecteka.utils.data.model.Doctor;
import in.projecteka.utils.data.model.Medicine;
import org.hl7.fhir.r4.model.Binary;
import org.hl7.fhir.r4.model.Bundle;
import org.hl7.fhir.r4.model.CodeableConcept;
Expand Down Expand Up @@ -39,20 +39,22 @@ public void init() throws Exception {
patients = Utils.loadFromFile("/patients.properties");
}

public void execute(String patientName, Date fromDate, int number, Path location) throws Exception {
public void execute(String patientName, Date fromDate, int number, Path location, String hipPrefix) throws Exception {
FhirContext fhirContext = FhirContext.forR4();
LocalDateTime dateTime = fromDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
for (int i = 0; i < number; i++) {
Date date = Utils.getNextDate(dateTime, i);
Bundle bundle = createPrescriptionBundle(date, patientName, "max");
Bundle bundle = createPrescriptionBundle(date, patientName, hipPrefix);
String encodedString = fhirContext.newJsonParser().encodeResourceToString(bundle);
List<Bundle.BundleEntryComponent> patientEntries =
bundle.getEntry().stream()
.filter(e -> e.getResource().getResourceType().equals(ResourceType.Patient))
.collect(Collectors.toList());
Bundle.BundleEntryComponent patientEntry = patientEntries.get(0);
String fileName = String.format("%sPrescriptionDoc%s.json",
patientEntry.getResource().getId(), Utils.formatDate(date, "yyyyMMdd"));
String fileName = String.format("%s%sPrescriptionDoc%s.json",
hipPrefix.toUpperCase(),
patientEntry.getResource().getId(),
Utils.formatDate(date, "yyyyMMdd"));
Path path = Paths.get(location.toString(), fileName);
System.out.println("Saving Prescription to file:" + path.toString());
Utils.saveToFile(path, encodedString);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package in.projecteka.data;
package in.projecteka.utils.data;

import org.hl7.fhir.r4.model.Meta;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package in.projecteka.data.model;
package in.projecteka.utils.data.model;

import in.projecteka.data.Utils;
import in.projecteka.utils.data.Utils;
import lombok.AllArgsConstructor;
import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package in.projecteka.data.model;
package in.projecteka.utils.data.model;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package in.projecteka.data.model;
package in.projecteka.utils.data.model;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package in.projecteka.data.model;
package in.projecteka.utils.data.model;

import ca.uhn.fhir.parser.IParser;
import in.projecteka.data.Utils;
import in.projecteka.utils.data.Utils;
import lombok.AllArgsConstructor;
import lombok.Data;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package in.projecteka.data.model;
package in.projecteka.utils.data.model;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down
Loading

0 comments on commit b979a63

Please sign in to comment.