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.unbundle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

import ca.uhn.fhir.model.api.IDatatype;
import ca.uhn.fhir.model.dstu2.composite.AddressDt;
import ca.uhn.fhir.model.dstu2.composite.QuantityDt;
import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.dstu2.resource.Observation;
import ca.uhn.fhir.model.dstu2.resource.Organization;
import ca.uhn.fhir.model.dstu2.resource.Practitioner;
import gov.va.med.pbm.ampl.model.Facility;
import gov.va.med.pbm.ampl.model.Measurement;
import gov.va.med.pbm.ampl.model.ValueQuantity;
import gov.va.med.pbm.ampl.model.address.Address;
import gov.va.med.pbm.ampl.utility.NumberUtility;

/**
* The UnbundleMeasurements class marshals the FHIR Observation resource to a client readable JSON format.
*
* @author Ian Meinert
*
*/
public class UnbundleMeasurements {

private Observation observation;

/**
* The overloaded constructor accepting {@link Observation}.
*
* @param observation {@link Observation}
*/
public UnbundleMeasurements(Observation observation) {
this.observation = observation;
}

/**
* Default constructor.
*/
public UnbundleMeasurements() {
// TODO Auto-generated constructor stub
}

/**
* This method returns a filtered {@link Measurement} collection.
*
* @param collection {@link Measurement} collection
*/
public void getMeasuresLast(Collection<Measurement> collection) {
Collection<Measurement> measurements = new ArrayList<Measurement>();

for (Measurement item : collection) {
// if the returning collection doesnt contain a given measurement name, add it
if (measurements.stream().noneMatch(m -> m.getName().equals(item.getName()))) {
measurements.add(item);
} else {
finalCollectionLoop:
// iterate over each in stack. if found, compare the entered date
for (Measurement measurement : measurements) {
if (measurement.getName().equals(item.getName())) {
int dateCompare = measurement.getEntered().compareTo(item.getEntered());

// measurement date entered is older than item date entered
if (dateCompare == -1) {
measurements.remove(measurement);
measurements.add(item);

break finalCollectionLoop;
}
}
}
}
}

collection = measurements;
}

/**
* Updates @link Measurement} details based on type of performer of {@link Observation}.
*
* @param measurement {@link Measurement}
*/
public void getPerformerData(Measurement measurement) {
List<ResourceReferenceDt> performers = observation.getPerformer();

if (!performers.isEmpty()) {
for (ResourceReferenceDt performer : performers) {
String type = performer.getClass().getSimpleName();

switch (type.toLowerCase(Locale.ENGLISH)) {
case "practitioner":
measurement.setEnteredBy(((Practitioner) performer.getResource()).getName().getNameAsSingleString());
break;
case "organization":
Organization organization = (Organization) performer.getResource();
AddressDt orgAddress = organization.getAddressFirstRep();

Address address = new Address();
address.setStreet(orgAddress.getLineFirstRep().getValue());
address.setCity(orgAddress.getCityElement().getValue());
address.setState(orgAddress.getStateElement().getValue());
address.setZip(orgAddress.getPostalCodeElement().getValue());

Facility facility = new Facility();
facility.setLocation(address);
facility.setName(organization.getName());
facility.setSiteName(organization.getName());
facility.setStationNumber(organization.getIdElement().getValue());

measurement.setHospitalLocation(organization.getName());
measurement.setFacility(facility);
break;
case "patient":
measurement.setEnteredBy(((ca.uhn.fhir.model.dstu2.resource.Patient) performer.getResource())
.getNameFirstRep().getNameAsSingleString());
break;
case "relatedperson":
measurement.setEnteredBy(((ca.uhn.fhir.model.dstu2.resource.RelatedPerson) performer.getResource())
.getName().getNameAsSingleString());
break;
default:
break;
}
}
}
}

/**
* This method sets the metric and imperial values for the measurements unit of measure.
*
* @param measurement the Measurement to be updated.
*/
public void getMeasurementUnits(Measurement measurement) {
// Check if the observation exists, if not, set the property to empty.
Optional<IDatatype> optValue = Optional.ofNullable(observation.getValue());

if (!optValue.isPresent()) {
measurement.setImperial(new ValueQuantity());
measurement.setMetric(new ValueQuantity());

return;
}

// The measurement observations
final String[] measurements = { "HEIGHT", "TEMPERATURE", "WEIGHT" };

IDatatype value = optValue.get();
String className = value.getClass().getSimpleName().toLowerCase(Locale.ENGLISH);

switch (className) {
case "quantitydt": // "valueQuantity"
Optional<String> optUnit = Optional.ofNullable(((QuantityDt) value).getValue().toString());

double quantityValue = NumberUtility.doubleValueOf(((QuantityDt) value).getValue().toString());
String quantityUnit = !optUnit.isPresent() ? new String() : String.format(" %s", optUnit.get());
UnbundleValueQuantity unbundler = new UnbundleValueQuantity();

ValueQuantity metricConversion = new ValueQuantity();

// is the displayValue found in the measurements?
if (Arrays.asList(measurements).contains(measurement.getName().toUpperCase())) {
measurement.setImperial(unbundler.getMeasureForObservation(measurement.getName(), quantityValue, false));
measurement.setMetric(unbundler.getMeasureForObservation(measurement.getName(), quantityValue, true));

} else {
// business wants readings stored in Metric if conversion isnt required.
measurement.setMetric(new ValueQuantity(((QuantityDt) value).getValue().toString(), quantityUnit));
measurement.setImperial(metricConversion);
}
break;
case "stringdt": // "valueString"
measurement.setMetric(new ValueQuantity(value.toString(), ""));
default:
break;
}
}
}