Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

package gov.va.med.pbm.ampl.fhir.unmarshall.dstu2;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ca.uhn.fhir.model.dstu2.resource.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import ca.uhn.fhir.model.dstu2.resource.Observation;
import ca.uhn.fhir.model.dstu2.resource.Observation.ReferenceRange;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import gov.va.med.pbm.ampl.constant.FhirExtensionUrls;
import gov.va.med.pbm.ampl.constant.MockDataConstants;
import gov.va.med.pbm.ampl.fhir.client.Dstu2Client;
import gov.va.med.pbm.ampl.fhir.client.IFhirClient;
import gov.va.med.pbm.ampl.fhir.unmarshall.AbstractUnmarshaller;
import gov.va.med.pbm.ampl.fhir.unmarshall.dstu2.unbundle.UnbundleMeasurements;
import gov.va.med.pbm.ampl.fhir.unmarshall.dstu2.unbundle.UnbundleUndeclaredExtension;
import gov.va.med.pbm.ampl.model.Facility;
import gov.va.med.pbm.ampl.model.Measurement;
import gov.va.med.pbm.ampl.model.patientdemographics.Person;
import gov.va.med.pbm.ampl.utility.DateUtility;
import gov.va.med.pbm.ampl.utility.ESAPIValidationType;
import gov.va.med.pbm.ampl.utility.ESAPIValidator;

/**
* The UnmarshallObservation class marshals the FHIR Observation resource to a client readable JSON format.
*
* @author Ian Meinert
*
*/
public class UnmarshallObservation extends AbstractUnmarshaller<Measurement> {

/**
* The application logger.
*/
static final Logger LOGGER = LoggerFactory.getLogger(UnmarshallObservation.class);

/**
* The overloaded constructor accepting the Dstu2Client, patient Id, search term, and sort term.
* <p>
* The constructor also passes these parameters to the super class, along with the {@link Measurement} class definition.
*
* @param client the DSTU2 client
* @param patientId the patient id
* @param searchTerm the search field, operation, and value
* @param sortTerm the field and direction to sort
*/
public UnmarshallObservation(IFhirClient client, String patientId, Optional<String> searchTerm, Optional<String> sortTerm) {
super(client, patientId, searchTerm, sortTerm);
}

/**
* The overloaded constructor accepting the Dstu2Client, patient Id, search term, sort term, size, and page.
* <p>
* The constructor also passes these parameters to the super class, along with the class definition.
*
* @param client the DSTU2 client
* @param patientId the patient id
* @param searchTerm the search field, operation, and value
* @param sortTerm the field and direction to sort
* @param size the limit of the results per page
* @param page the page number for the results
*/
public UnmarshallObservation(Dstu2Client client, String patientId, Optional<String> searchTerm, Optional<String> sortTerm,
Optional<Integer> page, Optional<Integer> size) {
super(client, patientId, searchTerm, sortTerm, page, size);
}

/**
* This method queries the FHIR server for all patients that match the patient Id.
*/
@Override
public void getAllPatientsById() {
Collection<Measurement> measurements = new ArrayList<Measurement>();

Optional<Bundle> optBundle = Optional
.ofNullable(super.getClient().searchMeasurementsByPatientId(ESAPIValidator.validateStringInput(this.getPatientId().toString(),ESAPIValidationType.CROSS_SITE_SCRIPTING_REFLECTED)));

if (optBundle.isPresent()) {
Collection<Entry> entries = optBundle.get().getEntry();

for (Entry entry : entries) {
Measurement measurement = new Measurement();

if (!entry.getResource().getClass().equals(Observation.class)) {
LOGGER.info("disregarding {} in bundle", entry.getResource().getClass().getSimpleName());
continue;
}

Observation observation = (Observation) entry.getResource();
Optional<DateTimeDt> optEffectiveDate = Optional.ofNullable((DateTimeDt) observation.getEffective());
UnbundleUndeclaredExtension unbundleUndeclaredExtension = new UnbundleUndeclaredExtension(
observation.getAllUndeclaredExtensions());
UnbundleMeasurements unbundleMeasurements = new UnbundleMeasurements(observation);
Facility facility = unbundleUndeclaredExtension.getFacilityFromCodingDt(FhirExtensionUrls.DSTU2_VA_SITE_CODE);
Facility hospitalLocation = unbundleUndeclaredExtension
.getFacilityFromCodeableConceptDt(FhirExtensionUrls.DSTU2_PERFORMED_AT);
Person enteredBy = unbundleUndeclaredExtension.getPersonFromHumanName(FhirExtensionUrls.DSTU2_ENTERED_BY);

unbundleMeasurements.getPerformerData(measurement);

if (optEffectiveDate.isPresent()) {
OffsetDateTime effectiveDate = DateUtility.offsetDateTimeValueOf(optEffectiveDate.get().getValueAsString());

measurement.setEntered(effectiveDate);
measurement.setTimeTaken(effectiveDate);
}

measurement.setEnteredBy(enteredBy.getName());
measurement.setFacility(facility);
measurement.setFlag(MockDataConstants.VERIFY_FHIR_ELEMENT);
measurement.setHospitalLocation(hospitalLocation.getSiteName());
measurement.setName(observation.getCode().getCodingFirstRep().getDisplay());
measurement.setPatientId(ESAPIValidator.validateStringInput(this.getPatientId(),ESAPIValidationType.CROSS_SITE_SCRIPTING_REFLECTED));
measurement.setQualifier(MockDataConstants.VERIFY_FHIR_ELEMENT);
Optional<ReferenceRange> refRange = Optional.of(observation.getReferenceRangeFirstRep());
measurement.setRange(refRange.isPresent() ? refRange.get().getText() : new String());
measurement.setRate(MockDataConstants.VERIFY_FHIR_ELEMENT);
measurement.setSupplementalOxygen(MockDataConstants.VERIFY_FHIR_ELEMENT);

unbundleMeasurements.getMeasurementUnits(measurement);

measurements.add(measurement);
}
}

LOGGER.info("Storing {} record(s)", measurements.size());
super.setCollection(measurements);
}

/**
* This method returns a filtered {@link Measurement} collection.
*
*/
public void getMeasuresLast() {
UnbundleMeasurements unbundler = new UnbundleMeasurements();
unbundler.getMeasuresLast(super.getCollection());
}
}