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.List;
import java.util.Optional;

import ca.uhn.fhir.model.dstu2.resource.DocumentReference;
import ca.uhn.fhir.model.dstu2.resource.DocumentReference.RelatesTo;
import ca.uhn.fhir.model.dstu2.resource.Encounter;
import ca.uhn.fhir.model.api.ExtensionDt;
import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt;
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.ProgressNote;
import gov.va.med.pbm.ampl.model.Signature;

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

private List<Entry> entries;
private String patientId;

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

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

// Iterate over each entry to filter out the desired progress notes
for (Entry entry : entries) {

// get the DocumentReference
if (DocumentReference.class.equals(entry.getResource().getClass())) {
DocumentReference document = (DocumentReference) entry.getResource();

progressNotes.add(this.createNote(document));
}
}

return progressNotes;
}

/**
* This creates a {@link ProgressNote} from a given {@link DocumentReference}.
*
* @param document {@link DocumentReference}
* @return {@link ProgressNote
*/
private ProgressNote createNote(DocumentReference document) {

ProgressNote progressNote = new ProgressNote();

// some references may not be available. Set them as Optional.
Optional<Encounter> optEncounter = Optional.ofNullable((Encounter) document.getContext().getEncounter().getResource());
Optional<ResourceReferenceDt> optAuthor = !document.getAuthor().isEmpty()
? Optional.ofNullable(document.getAuthor().get(0))
: Optional.ofNullable(null);
// get the extension methods
List<ExtensionDt> extensions = document.getAllUndeclaredExtensions();
UnbundleUndeclaredExtension uExtensions = new UnbundleUndeclaredExtension(extensions);
UnbundleSignature unbundleSignature = new UnbundleSignature();
UnbundleFacility unbundleFacility = new UnbundleFacility(document.getCustodian());

// unbundle the extensions
String clinician = uExtensions.getCodingDisplayValue(FhirExtensionUrls.DSTU2_DOCREF_CLINICIAN);
String enteredBy = uExtensions.getCodingDisplayValue(FhirExtensionUrls.DSTU2_ENTERED_BY);

OffsetDateTime fromTime = uExtensions.getDateTimeValue(FhirExtensionUrls.DSTU2_DOC_FROM_TIME);
OffsetDateTime toTime = uExtensions.getDateTimeValue(FhirExtensionUrls.DSTU2_TO_TIME);
OffsetDateTime authTime = uExtensions.getDateTimeValue(FhirExtensionUrls.DSTU2_AUTHORIZATION_TIME);

Signature signature = unbundleSignature.getSignatureFromResourceReference(optAuthor, Optional.ofNullable(authTime));

progressNote.setAuthor(enteredBy);
progressNote.setAddendums(this.getAddendums(document.getRelatesTo()));

if (!progressNote.getAddendums().isEmpty()) {
Signature ammendmentSignature = progressNote.getAddendums().stream().findFirst().get().getSignature();
progressNote.setAmendmentSignature(ammendmentSignature);
}

progressNote.setCosignature(signature);
progressNote.setDocType(document.getType().getElementSpecificId());
progressNote.setEntryDate(toTime);
progressNote.setExpCosigner(clinician);
progressNote.setExpSigner(signature.getBlockName());
progressNote.setFacility(unbundleFacility.getResourceAsOrganization());
progressNote.setHospitalLocation(document.getContext().getPracticeSetting().getText());
progressNote.setLocalTitle(document.getDescription());
progressNote.setNoteDate(fromTime);
progressNote.setPatientId(patientId);
progressNote.setReportText(optEncounter.isPresent() ? optEncounter.get().getText().getDivAsString() : new String());
progressNote.setSignature(signature);
progressNote.setStandardTitle(document.getDescription());
progressNote.setStatus(document.getStatus());
progressNote.setUrgency(MockDataConstants.NOT_MAPPED);

return progressNote;

}

/**
* This method builds the addendums.
*
* @param list list of {@link RelatesTo}
*
* @return collection of {@link ProgressNote}
*/
private Collection<ProgressNote> getAddendums(List<RelatesTo> list) {
Collection<ProgressNote> notes = new ArrayList<ProgressNote>();

for (RelatesTo item : list) {
DocumentReference document = (DocumentReference) item.getTarget().getResource();
notes.add(this.createNote(document));
}

return notes;
}
}