use of ca.uhn.fhir.model.dstu2.resource.Patient in project eCRNow by drajer-health.
the class Dstu2CdaHeaderGenerator method getPatientDetails.
public static String getPatientDetails(Dstu2FhirData data, Patient p, LaunchDetails details) {
StringBuilder patientDetails = new StringBuilder();
patientDetails.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.RECORD_TARGET_EL_NAME));
patientDetails.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.PATIENT_ROLE_EL_NAME));
IdentifierDt id = Dstu2CdaFhirUtilities.getIdentifierForType(p.getIdentifier(), IdentifierTypeCodesEnum.MR);
if (id != null) {
if (!StringUtils.isEmpty(id.getSystem()) && !StringUtils.isEmpty(id.getValue())) {
logger.info(" Found Identifier with Type MR ");
String system = CdaGeneratorUtils.getRootOid(id.getSystem(), details.getAssigningAuthorityId());
patientDetails.append(CdaGeneratorUtils.getXmlForII(system, id.getValue()));
} else {
logger.info(" Using Resource Identifier as id ");
;
patientDetails.append(CdaGeneratorUtils.getXmlForII(details.getAssigningAuthorityId(), p.getId().toString()));
}
} else {
logger.info(" Using Resource Identifier as id ");
;
patientDetails.append(CdaGeneratorUtils.getXmlForII(details.getAssigningAuthorityId(), p.getId().toString()));
}
// Add Address.
patientDetails.append(Dstu2CdaFhirUtilities.getAddressXml(p.getAddress()));
// Add Telecom
patientDetails.append(Dstu2CdaFhirUtilities.getTelecomXml(p.getTelecom()));
// Add patient
patientDetails.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.PATIENT_EL_NAME));
patientDetails.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.NAME_EL_NAME));
patientDetails.append(Dstu2CdaFhirUtilities.getNameXml(p.getName()));
patientDetails.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.NAME_EL_NAME));
patientDetails.append(Dstu2CdaFhirUtilities.getGenderXml(p.getGenderElement()));
patientDetails.append(Dstu2CdaFhirUtilities.getDateTypeXml(p.getBirthDateElement(), CdaGeneratorConstants.BIRTH_TIME_EL_NAME));
if (p.getDeceased() == null || (p.getDeceased() != null && p.getDeceased().isEmpty())) {
patientDetails.append(CdaGeneratorUtils.getXmlForValue(CdaGeneratorConstants.SDTC_DECEASED_IND, CdaGeneratorConstants.CCDA_FALSE));
} else {
patientDetails.append(CdaGeneratorUtils.getXmlForValue(CdaGeneratorConstants.SDTC_DECEASED_IND, CdaGeneratorConstants.CCDA_TRUE));
if (p.getDeceased() instanceof DateTimeDt) {
DateTimeDt d = (DateTimeDt) p.getDeceased();
patientDetails.append(CdaGeneratorUtils.getXmlForEffectiveTime(CdaGeneratorConstants.SDTC_DECEASED_TIME, d.getValue().toString()));
} else {
patientDetails.append(CdaGeneratorUtils.getXmlForNullEffectiveTime(CdaGeneratorConstants.SDTC_DECEASED_TIME, CdaGeneratorConstants.NF_NI));
}
}
CodingDt race = Dstu2CdaFhirUtilities.getCodingExtension(p.getUndeclaredExtensions(), CdaGeneratorConstants.FHIR_ARGO_RACE_EXT_URL, CdaGeneratorConstants.OMB_RACE_CATEGORY_URL);
if (race != null && race.getCode() != null) {
patientDetails.append(CdaGeneratorUtils.getXmlForCD(CdaGeneratorConstants.RACE_CODE_EL_NAME, race.getCode(), CdaGeneratorConstants.RACE_CODE_SYSTEM, CdaGeneratorConstants.RACE_CODE_SYSTEM_NAME, race.getDisplay()));
} else {
patientDetails.append(CdaGeneratorUtils.getXmlForNullCD(CdaGeneratorConstants.RACE_CODE_EL_NAME, CdaGeneratorConstants.NF_NI));
}
CodingDt ethnicity = Dstu2CdaFhirUtilities.getCodingExtension(p.getUndeclaredExtensions(), CdaGeneratorConstants.FHIR_ARGO_ETHNICITY_EXT_URL, CdaGeneratorConstants.OMB_RACE_CATEGORY_URL);
if (ethnicity != null && ethnicity.getCode() != null) {
patientDetails.append(CdaGeneratorUtils.getXmlForCD(CdaGeneratorConstants.ETHNIC_CODE_EL_NAME, ethnicity.getCode(), CdaGeneratorConstants.RACE_CODE_SYSTEM, CdaGeneratorConstants.RACE_CODE_SYSTEM_NAME, ethnicity.getDisplay()));
} else {
patientDetails.append(CdaGeneratorUtils.getXmlForNullCD(CdaGeneratorConstants.ETHNIC_CODE_EL_NAME, CdaGeneratorConstants.NF_NI));
}
// Adding Guardian details for patient
if (p.getContact() != null && p.getContact().size() > 0) {
// Add Guardian element
Contact guardianContact = Dstu2CdaFhirUtilities.getGuardianContact(p.getContact());
if (guardianContact != null) {
patientDetails.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.GUARDIAN_EL_NAME));
// Add Telecom
patientDetails.append(Dstu2CdaFhirUtilities.getTelecomXml(guardianContact.getTelecom()));
patientDetails.append(Dstu2CdaFhirUtilities.getEmailXml(guardianContact.getTelecom()));
// Add Name
patientDetails.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.GUARDIAN_PERSON_EL_NAME));
patientDetails.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.NAME_EL_NAME));
patientDetails.append(Dstu2CdaFhirUtilities.getNameXml(guardianContact.getName()));
patientDetails.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.NAME_EL_NAME));
patientDetails.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.GUARDIAN_PERSON_EL_NAME));
patientDetails.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.GUARDIAN_EL_NAME));
}
}
// Add language communication
patientDetails.append(CdaGeneratorUtils.getXmlForStartElement(CdaGeneratorConstants.LANGUAGE_COMM_EL_NAME));
CodingDt language = Dstu2CdaFhirUtilities.getLanguage(p.getCommunication());
if (language != null && language.getCode() != null) {
patientDetails.append(CdaGeneratorUtils.getXmlForCD(CdaGeneratorConstants.LANGUAGE_CODE_EL_NAME, language.getCode()));
} else {
patientDetails.append(CdaGeneratorUtils.getXmlForNullCD(CdaGeneratorConstants.LANGUAGE_CODE_EL_NAME, CdaGeneratorConstants.NF_NI));
}
patientDetails.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.LANGUAGE_COMM_EL_NAME));
patientDetails.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.PATIENT_EL_NAME));
patientDetails.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.PATIENT_ROLE_EL_NAME));
patientDetails.append(CdaGeneratorUtils.getXmlForEndElement(CdaGeneratorConstants.RECORD_TARGET_EL_NAME));
return patientDetails.toString();
}
use of ca.uhn.fhir.model.dstu2.resource.Patient in project eCRNow by drajer-health.
the class Dstu2CdaEicrGenerator method convertDstu2FhirBundletoCdaEicr.
public static String convertDstu2FhirBundletoCdaEicr(Dstu2FhirData data, LaunchDetails details, Eicr ecr) {
StringBuilder eICR = new StringBuilder();
if (data != null) {
Bundle bundle = data.getData();
if (bundle != null) {
List<Entry> entries = bundle.getEntry();
for (Entry ent : entries) {
// Populate data ..this can be moved to the APIs where the bundle is getting created.
if (ent.getResource() instanceof Patient) {
logger.info(" Bundle contains Patient ");
data.setPatient((Patient) ent.getResource());
} else if (ent.getResource() instanceof Practitioner) {
logger.info(" Bundle contains Practitioner ");
data.setPractitioner((Practitioner) ent.getResource());
} else if (ent.getResource() instanceof Encounter) {
logger.info(" Bundle contains Encounter ");
data.setEncounter((Encounter) ent.getResource());
} else if (ent.getResource() instanceof Location) {
logger.info(" Bundle contains Location ");
data.setLocation((Location) ent.getResource());
} else if (ent.getResource() instanceof Organization) {
logger.info(" Bundle contains Organization ");
data.setOrganization((Organization) ent.getResource());
} else if (ent.getResource() instanceof Condition) {
logger.info(" Bundle contains Condition ");
data.getConditions().add((Condition) ent.getResource());
} else if (ent.getResource() instanceof Observation) {
Observation obs = (Observation) ent.getResource();
if (obs.getCategory() != null && obs.getCategory().getCodingFirstRep() != null && obs.getCategory().getCodingFirstRep().getCode() != null && obs.getCategory().getCodingFirstRep().getCode().contentEquals(CdaGeneratorConstants.FHIR_LAB_RESULT_CATEGORY)) {
logger.info(" Bundle contains Lab Results ");
data.getLabResults().add((Observation) ent.getResource());
} else if (obs.getCategory() != null && obs.getCategory().getCodingFirstRep() != null && obs.getCategory().getCodingFirstRep().getCode() != null) {
logger.info("Code for Observation Category = " + obs.getCategory().getCodingFirstRep().getCode());
}
// Compare Code for Travel Obs
// Compare Codes for Pregnancy Obs and sort it out.
} else if (ent.getResource() instanceof DiagnosticReport) {
logger.info(" Bundle contains Diagnostic Report ");
data.getDiagReports().add((DiagnosticReport) ent.getResource());
} else if (ent.getResource() instanceof DiagnosticOrder) {
logger.info(" Bundle contains Diagnostic Order ");
data.getDiagOrders().add((DiagnosticOrder) ent.getResource());
} else if (ent.getResource() instanceof MedicationStatement) {
logger.info(" Bundle contains MedicationStatement ");
data.getMedications().add((MedicationStatement) ent.getResource());
} else if (ent.getResource() instanceof Immunization) {
logger.info(" Bundle contains Immunization ");
data.getImmunizations().add((Immunization) ent.getResource());
}
}
eICR.append(Dstu2CdaHeaderGenerator.createCdaHeader(data, details));
eICR.append(Dstu2CdaBodyGenerator.generateCdaBody(data, details));
eICR.append(CdaGeneratorUtils.getEndXMLHeaderForCdaDocument());
}
} else {
logger.error(" No Fhir Bundle Available to create CDA Documents ");
}
return eICR.toString();
}
use of ca.uhn.fhir.model.dstu2.resource.Patient in project eCRNow by drajer-health.
the class LoadingQueryDstu2Bundle method createDSTU2Bundle.
public Bundle createDSTU2Bundle(LaunchDetails launchDetails, Dstu2FhirData dstu2FhirData, Date start, Date end) {
Bundle bundle = new Bundle();
logger.info("Initializing FHIR Context for Version:::: {}", launchDetails.getFhirVersion());
FhirContext context = fhirContextInitializer.getFhirContext(launchDetails.getFhirVersion());
logger.info("Initializing Client");
IGenericClient client = fhirContextInitializer.createClient(context, launchDetails);
// GET Patient Details and Add to Bundle
try {
logger.info("Get Patient Data");
Patient patient = (Patient) fhirContextInitializer.getResouceById(launchDetails, client, context, "Patient", launchDetails.getLaunchPatientId());
Entry patientEntry = new Entry();
patientEntry.setResource(patient);
bundle.addEntry(patientEntry);
} catch (Exception e) {
logger.error("Error in getting Patient Data", e);
}
// Step 1: Get Encounters for Patient based on encId. (Create a method to get
// encounters)
// If encId is null, find encounters for patient within the start and end time
// provided.
// Add to the bundle.
// As you are adding to the bundle within Fhir Data, add the codeable concept
// also to the list of encounterCodes.
Encounter encounter = null;
try {
logger.info("Get Encounter Data");
encounter = dstu2ResourcesData.getEncounterData(context, client, launchDetails, dstu2FhirData, start, end);
if (encounter.getParticipant() != null) {
List<Participant> participants = encounter.getParticipant();
for (Participant participant : participants) {
if (participant.getIndividual() != null) {
ResourceReferenceDt practitionerReference = participant.getIndividual();
Practitioner practitioner = (Practitioner) fhirContextInitializer.getResouceById(launchDetails, client, context, "Practitioner", practitionerReference.getReference().getIdPart());
Entry practitionerEntry = new Entry().setResource(practitioner);
bundle.addEntry(practitionerEntry);
}
}
}
if (encounter.getServiceProvider() != null) {
ResourceReferenceDt organizationReference = encounter.getServiceProvider();
Organization organization = (Organization) fhirContextInitializer.getResouceById(launchDetails, client, context, "Organization", organizationReference.getReference().getIdPart());
Entry organizationEntry = new Entry().setResource(organization);
bundle.addEntry(organizationEntry);
}
if (encounter.getLocation() != null) {
List<Location> enocunterLocations = encounter.getLocation();
for (Location location : enocunterLocations) {
if (location.getLocation() != null) {
ResourceReferenceDt locationReference = location.getLocation();
ca.uhn.fhir.model.dstu2.resource.Location locationResource = (ca.uhn.fhir.model.dstu2.resource.Location) fhirContextInitializer.getResouceById(launchDetails, client, context, "Location", locationReference.getReference().getIdPart());
Entry locationEntry = new Entry().setResource(locationResource);
bundle.addEntry(locationEntry);
}
}
}
Entry encounterEntry = new Entry().setResource(encounter);
bundle.addEntry(encounterEntry);
} catch (Exception e) {
logger.error("Error in getting Encounter Data", e);
}
// also to the list of ConditionCodes.
try {
logger.info("Get Condition Data");
List<Condition> conditionsList = dstu2ResourcesData.getConditionData(context, client, launchDetails, dstu2FhirData, encounter, start, end);
logger.info("Filtered ConditionsList----> {}", conditionsList.size());
dstu2FhirData.setConditions(conditionsList);
for (Condition condition : conditionsList) {
Entry conditionsEntry = new Entry().setResource(condition);
bundle.addEntry(conditionsEntry);
}
} catch (Exception e) {
logger.error("Error in getting Condition Data", e);
}
// also to the list of labResultCodes.
try {
logger.info("Get Observation Data");
List<Observation> observationList = dstu2ResourcesData.getObservationData(context, client, launchDetails, dstu2FhirData, encounter, start, end);
logger.info("Filtered Observations----> {}", observationList.size());
dstu2FhirData.setLabResults(observationList);
for (Observation observation : observationList) {
Entry observationsEntry = new Entry().setResource(observation);
bundle.addEntry(observationsEntry);
}
} catch (Exception e) {
logger.error("Error in getting Observation Data", e);
}
// Get Pregnancy Observations
try {
logger.info("Get Pregnancy Observation Data");
List<Observation> observationList = dstu2ResourcesData.getPregnancyObservationData(context, client, launchDetails, dstu2FhirData, encounter, start, end);
logger.info("Filtered Observations----> {}", observationList.size());
dstu2FhirData.setPregnancyObs(observationList);
for (Observation observation : observationList) {
Entry observationsEntry = new Entry().setResource(observation);
bundle.addEntry(observationsEntry);
}
} catch (Exception e) {
logger.error("Error in getting Pregnancy Observation Data", e);
}
// Get Travel Observations
try {
logger.info("Get Travel Observation Data");
List<Observation> observationList = dstu2ResourcesData.getTravelObservationData(context, client, launchDetails, dstu2FhirData, encounter, start, end);
logger.info("Filtered Observations----> {}", observationList.size());
dstu2FhirData.setTravelObs(observationList);
for (Observation observation : observationList) {
Entry observationsEntry = new Entry().setResource(observation);
bundle.addEntry(observationsEntry);
}
} catch (Exception e) {
logger.error("Error in getting Travel Observation Data", e);
}
// also to the list of medicationCodes.
try {
logger.info("Get MedicationAdministration Data");
List<MedicationAdministration> medAdministrationsList = dstu2ResourcesData.getMedicationAdministrationData(context, client, launchDetails, dstu2FhirData, encounter, start, end);
logger.info("Filtered MedicationAdministration-----------> {}", medAdministrationsList.size());
dstu2FhirData.setMedicationAdministrations(medAdministrationsList);
for (MedicationAdministration medAdministration : medAdministrationsList) {
if (medAdministration.getMedication() != null && !medAdministration.getMedication().isEmpty() && medAdministration.getMedication() instanceof ResourceReferenceDt) {
BaseResourceReferenceDt medRef = (BaseResourceReferenceDt) medAdministration.getMedication();
String medReference = medRef.getReference().getValue();
if (medReference.startsWith("#")) {
BaseContainedDt medAdministrationContained = medAdministration.getContained();
List<Medication> containedResources = (List<Medication>) medAdministrationContained.getContainedResources();
if (containedResources.stream().anyMatch(resource -> resource.getIdElement().getValue().equals(medReference))) {
logger.info("Medication Resource exists in MedicationAdministration.contained. So no need to add again in Bundle.");
}
} else {
logger.info("Medication Reference Found=============>");
Medication medication = dstu2ResourcesData.getMedicationData(context, client, launchDetails, dstu2FhirData, medReference);
Entry medicationEntry = new Entry().setResource(medication);
bundle.addEntry(medicationEntry);
if (medication != null) {
List<Medication> medicationList = new ArrayList<>();
medicationList.add(medication);
dstu2FhirData.setMedicationList(medicationList);
}
}
}
Entry medAdministrationEntry = new Entry().setResource(medAdministration);
bundle.addEntry(medAdministrationEntry);
}
} catch (Exception e) {
logger.error("Error in getting the MedicationAdministration Data", e);
}
try {
logger.info("Get MedicationStatement Data");
List<MedicationStatement> medStatementsList = dstu2ResourcesData.getMedicationStatementData(context, client, launchDetails, dstu2FhirData, encounter, start, end);
logger.info("Filtered MedicationStatement-----------> {}", medStatementsList.size());
for (MedicationStatement medStatement : medStatementsList) {
Entry medStatementEntry = new Entry().setResource(medStatement);
bundle.addEntry(medStatementEntry);
}
dstu2FhirData.setMedications(medStatementsList);
} catch (Exception e) {
logger.error("Error in getting the MedicationStatement Data", e);
}
try {
logger.info("Get DiagnosticOrder Data");
List<DiagnosticOrder> diagnosticOrdersList = dstu2ResourcesData.getDiagnosticOrderData(context, client, launchDetails, dstu2FhirData, encounter, start, end);
logger.info("Filtered DiagnosticOrders-----------> {}", diagnosticOrdersList.size());
dstu2FhirData.setDiagOrders(diagnosticOrdersList);
for (DiagnosticOrder diagnosticOrder : diagnosticOrdersList) {
Entry diagnosticOrderEntry = new Entry().setResource(diagnosticOrder);
bundle.addEntry(diagnosticOrderEntry);
}
} catch (Exception e) {
logger.error("Error in getting the DiagnosticOrder Data", e);
}
// Add to the bundle
try {
List<Immunization> immunizationsList = dstu2ResourcesData.getImmunizationData(context, client, launchDetails, dstu2FhirData, encounter, start, end);
logger.info("Filtered Immunizations-----------> {}", immunizationsList.size());
dstu2FhirData.setImmunizations(immunizationsList);
for (Immunization immunization : immunizationsList) {
Entry immunizationEntry = new Entry().setResource(immunization);
bundle.addEntry(immunizationEntry);
}
} catch (Exception e) {
logger.error("Error in getting the Immunization Data", e);
}
// Add to the bundle
try {
List<DiagnosticReport> diagnosticReportList = dstu2ResourcesData.getDiagnosticReportData(context, client, launchDetails, dstu2FhirData, encounter, start, end);
logger.info("Filtered DiagnosticReports-----------> {}", diagnosticReportList.size());
dstu2FhirData.setDiagReports(diagnosticReportList);
for (DiagnosticReport diagnosticReport : diagnosticReportList) {
Entry diagnosticReportEntry = new Entry().setResource(diagnosticReport);
bundle.addEntry(diagnosticReportEntry);
}
} catch (Exception e) {
logger.error("Error in getting the DiagnosticReport Data", e);
}
// Setting bundle to FHIR Data
logger.info("------------------------------CodeableConcept Codes------------------------------");
logger.info("Encounter Codes Size=====> {}", dstu2FhirData.getEncounterCodes().size());
logger.info("Conditions Codes Size=====> {}", dstu2FhirData.getConditionCodes().size());
logger.info("Observation Codes Size=====> {}", dstu2FhirData.getLabResultCodes().size());
logger.info("Medication Codes Size=====> {}", dstu2FhirData.getMedicationCodes().size());
logger.info("Immunization Codes Size=====> {}", dstu2FhirData.getImmuniationCodes().size());
logger.info("DiagnosticReport Codes Size=====> {}", dstu2FhirData.getDiagnosticReportCodes().size());
String fileName = ActionRepo.getInstance().getLogFileDirectory() + "/LoadingQueryDSTU2Bundle-" + launchDetails.getLaunchPatientId() + ".json";
ApplicationUtils.saveDataToFile(context.newJsonParser().encodeResourceToString(bundle), fileName);
return bundle;
}
use of ca.uhn.fhir.model.dstu2.resource.Patient in project eCRNow by drajer-health.
the class Dstu2ResourcesData method getEncounterData.
public Encounter getEncounterData(FhirContext context, IGenericClient client, LaunchDetails launchDetails, Dstu2FhirData dstu2FhirData, Date start, Date end) {
Encounter encounter;
// If Encounter Id is present in Launch Details
if (launchDetails.getEncounterId() != null) {
encounter = (Encounter) resourceData.getResouceById(launchDetails, client, context, "Encounter", launchDetails.getEncounterId());
dstu2FhirData.setEncounterCodes(findEncounterCodes(encounter));
} else {
// If Encounter Id is not Present in Launch Details Get Encounters by Patient Id
// and Find the latest Encounter
Bundle bundle = (Bundle) resourceData.getResourceByPatientId(launchDetails, client, context, "Encounter");
Map<Encounter, Date> encounterMap = new HashMap<>();
for (Entry entry : bundle.getEntry()) {
Encounter encounterEntry = (Encounter) entry.getResource();
// falling in between Start and End Date
if (!encounterEntry.getPeriod().isEmpty()) {
PeriodDt period = encounterEntry.getPeriod();
if (period.getStart().after(start) || period.getEnd().before(end)) {
encounterMap.put(encounterEntry, encounterEntry.getMeta().getLastUpdated());
}
// If period is not present using LastUpdatedDate in meta information to filter
// the Encounter
} else {
Date lastUpdatedDateTime = encounterEntry.getMeta().getLastUpdated();
if (lastUpdatedDateTime.after(start) && lastUpdatedDateTime.before(end)) {
encounterMap.put(encounterEntry, encounterEntry.getMeta().getLastUpdated());
}
}
}
encounter = Collections.max(encounterMap.entrySet(), Map.Entry.comparingByValue()).getKey();
dstu2FhirData.setEncounterCodes(findEncounterCodes(encounter));
}
return encounter;
}
use of ca.uhn.fhir.model.dstu2.resource.Patient in project hapi-fhir-jpaserver-starter by hapifhir.
the class ExampleServerDstu2IT method testCreateAndRead.
@Test
void testCreateAndRead() {
String methodName = "testCreateResourceConditional";
Patient pt = new Patient();
pt.addName().addFamily(methodName);
IIdType id = ourClient.create().resource(pt).execute().getId();
Patient pt2 = ourClient.read().resource(Patient.class).withId(id).execute();
assertEquals(methodName, pt2.getName().get(0).getFamily().get(0).getValue());
}
Aggregations