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.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
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.AnnotationDt;
import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt;
import ca.uhn.fhir.model.dstu2.composite.CodingDt;
import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.dstu2.resource.AllergyIntolerance;
import ca.uhn.fhir.model.dstu2.resource.AllergyIntolerance.Reaction;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import gov.va.med.pbm.ampl.constant.FhirExtensionUrls;
import gov.va.med.pbm.ampl.constant.MockDataConstants;
import gov.va.med.pbm.ampl.model.Allergy;
import gov.va.med.pbm.ampl.model.AllergyReaction;
import gov.va.med.pbm.ampl.model.AllergySeverity;
import gov.va.med.pbm.ampl.model.Comment;
import gov.va.med.pbm.ampl.model.Signature;
import gov.va.med.pbm.ampl.utility.DateUtility;
/**
* The UnbundleAllergy class marshals an {@link AllergyIntolerance} resource to a client readable JSON format.
*
* @author Ian Meinert
*/
public class UnbundleAllergy {
private List<Entry> entries;
private String patientId;
/**
* Overloaded constructor accepting a collection of {@link Entry}.
*
* @param patientId the patientId
* @param entries {@link Entry}
*/
public UnbundleAllergy(String patientId, List<Entry> entries) {
this.patientId = patientId;
this.entries = entries;
}
/**
* This method queries the FHIR server for patients.
*
* @return collection {@link Allergy}
*/
public final Collection<Allergy> getEntries() {
Collection<Allergy> allergies = new ArrayList<Allergy>();
for (Entry entry : entries) {
// Only process AllergyIntolerance resources. All other entries will be retrieved automatically by HAPI
if (!entry.getResource().getClass().equals(AllergyIntolerance.class)) {
continue;
}
Allergy allergy = new Allergy();
AllergyIntolerance resource = (AllergyIntolerance) entry.getResource();
CodeableConceptDt substance = resource.getSubstance();
Optional<ResourceReferenceDt> recorder = Optional.ofNullable(resource.getRecorder());
Optional<ResourceReferenceDt> reporter = Optional.ofNullable(resource.getReporter());
UnbundleUndeclaredExtension unbundleUndeclaredExtension = new UnbundleUndeclaredExtension(
resource.getAllUndeclaredExtensions());
allergy.setAllergyAssessment(substance.getCodingFirstRep().getCode());
allergy.setCategory(resource.getCategory());
allergy.setCausativeAgent(substance.getCodingFirstRep().getDisplay());
allergy.setDrugClass(new String[] { MockDataConstants.VERIFY_FHIR_ELEMENT });
allergy.setFacility(unbundleUndeclaredExtension.getFacilityFromCodingDt(FhirExtensionUrls.DSTU2_VA_SITE_CODE));
allergy.setGmrAllergy(substance.getCodingFirstRep().getDisplay());
allergy.setGmrAllergySource(substance.getCodingFirstRep().getCode());
allergy.setIngredients(new String[] { MockDataConstants.VERIFY_FHIR_ELEMENT });
allergy.setObservedHistorical(MockDataConstants.VERIFY_FHIR_ELEMENT);
if (recorder.isPresent() && !recorder.get().getReference().isEmpty()) {
UnbundleSignature unbundleSignature = new UnbundleSignature();
Optional<Date> optDateTimedt = Optional.ofNullable(resource.getOnsetElement().getValue());
Optional<OffsetDateTime> optOffsetDateTime = optDateTimedt.isPresent()
? Optional.of(DateUtility.offsetDateTimeValueOf(optDateTimedt.get()))
: Optional.ofNullable(null);
Signature signature = unbundleSignature.getSignatureFromResourceReference(recorder, optOffsetDateTime);
allergy.setOriginatorSig(signature); // XXX verified GAPv4
}
allergy.setPatientId(patientId);
allergy.setReactions(this.getAllergyReactions(resource.getReaction()));
allergy.setStatus(MockDataConstants.VPR_REMOVED_FIELD);
if("entered-in-error".equalsIgnoreCase(allergy.getStatus())) {
allergy.setEnteredInError(this.getAllergyComments(resource.getNote())[0]);
} else {
allergy.setComments(this.getAllergyComments(resource.getNote()));
}
if (reporter.isPresent() && !reporter.get().getReference().isEmpty()) {
UnbundleSignature unbundleSignature = new UnbundleSignature();
Optional<Date> optDateTimedt = Optional.ofNullable(resource.getRecordedDateElement().getValue());
Optional<OffsetDateTime> optOffsetDateTime = optDateTimedt.isPresent()
? Optional.of(DateUtility.offsetDateTimeValueOf(optDateTimedt.get()))
: Optional.ofNullable(null);
Signature signature = unbundleSignature.getSignatureFromResourceReference(reporter, optOffsetDateTime);
allergy.setVerified(MockDataConstants.NOT_MAPPED); // FIXME FHIR Extension
allergy.setVerifierSig(signature);
}
allergies.add(allergy);
}
return allergies;
}
/**
* This method generates an array of {@link Comment} for a GMR {@link Allergy}.
*
* @param note {@link AnnotationDt}
*
* @return @{link Comment} array
*/
private Comment[] getAllergyComments(AnnotationDt note) {
// TODO add params to method and get allergies
Collection<Comment> comments = new ArrayList<Comment>();
if (note != null) {
Comment comment = new Comment();
UnbundleSignature unbundleSignature = new UnbundleSignature();
Optional<IDatatype> optAuthor = Optional.ofNullable(note.getAuthor());
Optional<Date> optDate = Optional.ofNullable(note.getTime());
Signature signature = unbundleSignature.getSignatureFromIDataType(optAuthor, optDate);
comment.setClassification(MockDataConstants.NOT_MAPPED);
comment.setSignature(signature);
comment.setText(note.getTextElement().getValue());
comments.add(comment);
}
return comments.toArray(new Comment[comments.size()]);
}
/**
* This method retrieves a collection of {@link AllergyReaction} for a given patient.
*
* @param reactions Collection of {@link Reaction}
* @return Collection of AllergyReaction
*/
private Collection<AllergyReaction> getAllergyReactions(List<Reaction> reactions) {
Collection<AllergyReaction> allergyReactions = new ArrayList<AllergyReaction>();
for (Reaction reaction : reactions) {
AllergyReaction allergyReaction = new AllergyReaction();
Optional<Date> optOnset = Optional.ofNullable(reaction.getOnset());
Optional<String> optSeverity = Optional.ofNullable(reaction.getSeverity());
allergyReaction.setCertainty(reaction.getCertainty());
allergyReaction.setDescription(reaction.getDescription());
allergyReaction.setManifestation(getManifestation(reaction.getManifestation()));
allergyReaction.setMechanism(reaction.getExposureRoute().getText());
allergyReaction.setNote(reaction.getNote().getText());
if (optOnset.isPresent()) {
OffsetDateTime onset = DateUtility.offsetDateTimeValueOf(optOnset.get());
// TODO verifiedTime is currently not implemented in HS mapping
// allergyReaction.setVerifiedTime(onset);
allergyReaction.setOnset(onset); // XXX verified GAPv4
}
allergyReaction.setSeverity(this.getAllergySeverityFromString(optSeverity));
allergyReaction.setSubstance(reaction.getSubstance().getText());
allergyReactions.add(allergyReaction);
}
return allergyReactions;
}
/**
* This method retrieves a collection of manifestations for a patient.
* <p>
* It accepts a {@link CodeableConceptDt} and converts the {@link CodingDt} to a collection which JSON can send to the
* client.
*
* @param manifestationsList {@link Collection} of {@link CodeableConceptDt} type
* @return Array of String
*/
private String[] getManifestation(List<CodeableConceptDt> manifestationsList) {
String[] manifestationArray = new String[manifestationsList.size()];
for (int i = 0; i < manifestationsList.size(); i++) {
manifestationArray[i] = manifestationsList.get(i).getCoding().get(0).getDisplay();
}
return manifestationArray;
}
/**
* This method accepts a string based severity and mutates it into an AllergySeverity Enum.
*
* @param severity String which represents the AllergySeverity
* @return AllergySeverity
*/
private AllergySeverity getAllergySeverityFromString(Optional<String> severity) {
if (severity.isPresent()) {
switch (severity.get().toLowerCase(Locale.ENGLISH)) {
case "life threatening":
return AllergySeverity.LIFE_THREATENING;
case "mild":
return AllergySeverity.MILD;
case "moderate":
return AllergySeverity.MODERATE;
case "severe":
return AllergySeverity.SEVERE;
default:
return AllergySeverity.NO_SEVERITY_FOUND;
}
} else {
return AllergySeverity.NO_SEVERITY_FOUND;
}
}
}