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

import ca.uhn.fhir.model.dstu2.resource.Appointment.Participant;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import ca.uhn.fhir.model.dstu2.resource.Practitioner;
import gov.va.med.pbm.ampl.model.Location;
import gov.va.med.pbm.ampl.model.Appointment;
import gov.va.med.pbm.ampl.utility.DateUtility;

/**
* The UnbundleAppointment class marshals an {@link Appointment} resource to a client readable JSON format.
*
* @author Pavani Mukthipudi
*/
public class UnbundleAppointment {

private List<Entry> entries;
private String patientId;

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

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

// Iterate over each entry to filter out the desired appointments
for (Entry entry : entries) {
Appointment appointment = new Appointment();

// Only process AllergyIntolerance resources. All other entries will be retrieved automatically by HAPI
if (!entry.getResource().getClass().equals(ca.uhn.fhir.model.dstu2.resource.Appointment.class)) {
continue;
}

ca.uhn.fhir.model.dstu2.resource.Appointment fhirAppointment = (ca.uhn.fhir.model.dstu2.resource.Appointment) entry
.getResource();

Location location = (Location) this.getParticipantByResourceType(fhirAppointment,
ca.uhn.fhir.model.dstu2.resource.Location.class);
Optional<Practitioner> practitioner = Optional
.ofNullable((Practitioner) this.getParticipantByResourceType(fhirAppointment, Practitioner.class));

appointment.setPatientId(patientId);
appointment.setAppointmentId(fhirAppointment.getId().getIdPart());
appointment.setStatus(fhirAppointment.getStatus());
appointment.setAppointmentDate(DateUtility.offsetDateTimeValueOf(fhirAppointment.getStart()));
appointment.setLocation(location);
appointment
.setProvider(practitioner.isPresent() ? practitioner.get().getName().getNameAsSingleString() : new String());

appointments.add(appointment);
}

return appointments;
}

/**
* This method will return an object from {@link Appointment}'s {@link Participant} list which matches the given type class.
*
* @param fhirAppointment {@link Appointment}
* @param type class type to cast
* @return Object
*/
private Object getParticipantByResourceType(ca.uhn.fhir.model.dstu2.resource.Appointment fhirAppointment, Class<?> type) {
for (Participant fhirParticipant : fhirAppointment.getParticipant()) {
Class<?> clazz = fhirParticipant.getActor().getResource().getClass();

if (type.equals(clazz)) {
if (fhirParticipant.getActor().getResource() instanceof ca.uhn.fhir.model.dstu2.resource.Location) {
// Unbundle the location
UnbundleLocation unbundler = new UnbundleLocation(fhirParticipant.getActor());
return unbundler.getResourceAsLocation();
}
if (fhirParticipant.getActor().getResource() instanceof Practitioner) {
return (Practitioner) fhirParticipant.getActor().getResource();
}
}
}

return null;
}
}