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.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import ca.uhn.fhir.model.dstu2.composite.AnnotationDt;
import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.dstu2.composite.SimpleQuantityDt;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import ca.uhn.fhir.model.dstu2.resource.Immunization.Reaction;
import ca.uhn.fhir.model.dstu2.resource.Organization;
import ca.uhn.fhir.model.primitive.StringDt;
import gov.va.med.pbm.ampl.constant.MockDataConstants;
import gov.va.med.pbm.ampl.model.Facility;
import gov.va.med.pbm.ampl.model.Immunization;
import gov.va.med.pbm.ampl.model.VaccineInfoStatement;
import gov.va.med.pbm.ampl.model.ValueQuantity;
import gov.va.med.pbm.ampl.utility.DateUtility;

/**
* The UnbundleImmunization class marshals an {@link Immunization} resource to a client readable JSON format.
*
* @author Ian Meinert
*/
public class UnbundleImmunization {

private List<Entry> entries;
private String patientId;

/**
* Overloaded constructor accepting a collection of {@link Entry}.
*
* @param patientId the patientId
* @param entries {@link Entry}
*/
public UnbundleImmunization(String patientId, List<Entry> entries) {
this.patientId = patientId;
this.entries = entries;
}

/**
* This method queries the FHIR server for patients.
*
* @return collection {@link Immunization}
*/
public final Collection<Immunization> getEntries() {
Collection<Immunization> immunizations = new ArrayList<Immunization>();

// Iterate over each immunization
for (Entry entry : entries) {
ca.uhn.fhir.model.dstu2.resource.Immunization resource = (ca.uhn.fhir.model.dstu2.resource.Immunization) entry
.getResource();

Immunization immunization = new Immunization();
immunization.setPatientId(patientId);
immunization.setName(resource.getVaccineCode().getText());
immunization.setFullName(MockDataConstants.NOT_MAPPED);
immunization.setVaccineInfoStatement(getVaccineInfo());
immunization.setDose(convertSimpleQuantityDt(resource.getDoseQuantity()));
immunization.setSeries(resource.getVaccinationProtocolFirstRep().getDoseSequence());
immunization.setMaxInSeries(resource.getVaccinationProtocolFirstRep().getSeriesDoses());
immunization.setAdminRoute(resource.getRoute().getText());
immunization.setAdminBodySite(resource.getSite().getText());
immunization.setContraindicated(MockDataConstants.NOT_MAPPED);
immunization.setManufacturer(getResRefStr(resource.getManufacturer()));
immunization.setLotNumber(resource.getLotNumber());
immunization.setExpirationDate(DateUtility.offsetDateTimeValueOf(resource.getExpirationDateElement().getValue()));
immunization.setNdc(resource.getVaccineCode().getText());
immunization.setLocation(getResRefStr(resource.getLocation()));
immunization.setAdministeredBy(getResRefStr(resource.getPerformer()));
immunization.setOrderedBy(getResRefStr(resource.getRequester()));
immunization.setComments(convertAnnotationDt(resource.getNoteFirstRep()));
immunization.setAdministered(DateUtility.offsetDateTimeValueOf(resource.getDateElement().getValue()));
immunization.setReaction(convertReactionDt(resource.getReactionFirstRep()));
immunization.setFacility(this.getFacility(resource.getLocation()));

immunizations.add(immunization);
}

return immunizations;
}

/**
* Marshals FHIR {@link Organization} into a {@link Facility}.
* @param resource
*
* @return {@link Facility}
*/
private Facility getFacility(ResourceReferenceDt resource) {
UnbundleFacility unbundler = new UnbundleFacility(resource);

return unbundler.getResourceAsLocationForFacility();
}

/**
* This method gets the location name from a {@link ResourceReferenceDt}
*
* @param location {@link ResourceReferenceDt}
* @return ResourceReferenceDt
*/
private String getResRefStr(ResourceReferenceDt location) {
Optional<ResourceReferenceDt> optLocation = Optional.of(location);

if (!optLocation.isPresent()) {
return new String();
}

Optional<StringDt> displayElement = Optional.ofNullable(location.getDisplayElement());

return displayElement.isPresent() ? displayElement.get().getValueAsString() : new String();
}

/**
* This method needs params, so FIXME .
*
* @return collection of {@link VaccineInfoStatement}
*/
private Collection<VaccineInfoStatement> getVaccineInfo() {
Collection<VaccineInfoStatement> rtn = new ArrayList<VaccineInfoStatement>();
VaccineInfoStatement vis = new VaccineInfoStatement();

vis.setName(MockDataConstants.NOT_MAPPED);
vis.setEditionLang(MockDataConstants.NOT_MAPPED);

rtn.add(vis);

return rtn;
}

/**
* This method marshals a list of {@link Reaction} to the string value of the reaction.
*
* @param reaction {@link Reaction}
* @return the reaction.
*/
private String convertReactionDt(Reaction reaction) {
return reaction.getDetail().getDisplay().toString();
}

/**
* This medhod marshals the {@link AnnotationDt} to the string value of the comment.
*
* @param note the note {@link AnnotationDt}
* @return the comment
*/
private String convertAnnotationDt(AnnotationDt note) {
String rtn = "";

if (note != null) {
rtn = note.getText();
}

return rtn;
}

/**
* This method marshals a {@link SimpleQuantityDt} to a {@link ValueQuantity}.
*
* @param simpleQuantityDt {@link SimpleQuantityDt}
* @return {@link ValueQuantity}
*/
private ValueQuantity convertSimpleQuantityDt(SimpleQuantityDt simpleQuantityDt) {

Optional<SimpleQuantityDt> optSimpleQuantityDt = Optional.of(simpleQuantityDt);
Optional<BigDecimal> optSimpleQuantityDtValue = Optional.of(optSimpleQuantityDt.get().getValue());

if (!optSimpleQuantityDt.isPresent()) {
return null;
}

ValueQuantity quantity = new ValueQuantity();

quantity.setUnit(simpleQuantityDt.getUnit());

if (optSimpleQuantityDtValue.isPresent()) {
quantity.setValue(simpleQuantityDt.getValue().toPlainString());
}

return quantity;
}
}