diff --git a/sql/create_IMMUNIZATION_table.sql b/sql/create_IMMUNIZATION_table.sql new file mode 100644 index 0000000..d49b497 --- /dev/null +++ b/sql/create_IMMUNIZATION_table.sql @@ -0,0 +1,18 @@ +CREATE TABLE `HealthPort`.`IMMUNIZATION` ( + `ID` VARCHAR(45) NOT NULL, + `NAMEURI` TEXT NULL DEFAULT NULL, + `NAMECODING` TEXT NULL DEFAULT NULL, + `NAMEDISPLAY` TEXT NULL DEFAULT NULL, + `SUBJECT` TEXT NULL DEFAULT NULL, + `VACCINATION_DATE` DATETIME NULL DEFAULT NULL, + `SERIES` TEXT NULL DEFAULT NULL, + `MANUFACTURER` TEXT NULL DEFAULT NULL, + `LOT_NUMBER` TEXT NULL DEFAULT NULL, + `DOSE_QUANTITY` TEXT NULL DEFAULT NULL, + `DOSE_UNITS` TEXT NULL DEFAULT NULL, + `SITE` TEXT NULL DEFAULT NULL, + `ROUTE` TEXT NULL DEFAULT NULL, + `PERFORMER_ID` TEXT NULL DEFAULT NULL, + `PERFORMER_NAME` TEXT NULL DEFAULT NULL, + `ENCOUNTER_ID` TEXT NULL DEFAULT NULL, + PRIMARY KEY (`ID`)); \ No newline at end of file diff --git a/src/edu/gatech/i3l/HealthPort/HealthPortInfo.java b/src/edu/gatech/i3l/HealthPort/HealthPortInfo.java index ba7edef..cafa664 100644 --- a/src/edu/gatech/i3l/HealthPort/HealthPortInfo.java +++ b/src/edu/gatech/i3l/HealthPort/HealthPortInfo.java @@ -31,12 +31,15 @@ import ca.uhn.fhir.model.dstu.composite.QuantityDt; import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt; import ca.uhn.fhir.model.dstu.resource.Condition; +import ca.uhn.fhir.model.dstu.resource.Immunization; +import ca.uhn.fhir.model.dstu.resource.Immunization.VaccinationProtocol; import ca.uhn.fhir.model.dstu.resource.Medication; import ca.uhn.fhir.model.dstu.resource.MedicationPrescription; import ca.uhn.fhir.model.dstu.resource.Observation; import ca.uhn.fhir.model.dstu.resource.MedicationPrescription.Dispense; import ca.uhn.fhir.model.dstu.resource.MedicationPrescription.DosageInstruction; import ca.uhn.fhir.model.dstu.valueset.ConditionStatusEnum; +import ca.uhn.fhir.model.dstu.valueset.ImmunizationRouteCodesEnum; import ca.uhn.fhir.model.dstu.valueset.MedicationPrescriptionStatusEnum; import ca.uhn.fhir.model.dstu.valueset.NarrativeStatusEnum; import ca.uhn.fhir.model.dstu.valueset.ObservationReliabilityEnum; @@ -64,6 +67,7 @@ public class HealthPortInfo { public static String OBSERVATION = "OBSERVATION"; public static String CONDITION = "CONDITIONS"; public static String MEDICATIONPRESCRIPTION = "MEDICATIONPRESCRIPTION"; + public static String IMMUNIZATION = "IMMUNIZATION"; private DataSource dataSource; @@ -350,6 +354,40 @@ public static void storeResource(String tableName, Object obj0) pstmt.clearParameters(); pstmt.close(); + } else if (tableName.equals(IMMUNIZATION)) { + ImmunizationSerializable obj = (ImmunizationSerializable) obj0; + SQL_STATEMENT = "REPLACE INTO " + + tableName + + " (ID, NAMEURI, NAMECODING, NAMEDISPLAY, SUBJECT, VACCINATION_DATE, SERIES, MANUFACTURER, LOT_NUMBER, DOSE_QUANTITY, DOSE_UNITS, SITE, ROUTE, PERFORMER_ID, PERFORMER_NAME, ENCOUNTER_ID) VALUES " + + " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + PreparedStatement pstmt = connection.prepareStatement(SQL_STATEMENT); + + // set input parameters + pstmt.setString(1, obj.ID); + pstmt.setString(2, obj.NAMEURI); + pstmt.setString(3, obj.NAMECODING); + pstmt.setString(4, obj.NAMEDISPLAY); + pstmt.setString(5, obj.SUBJECT); + if (obj.VACCINATION_DATE != null) { + Timestamp ts = new Timestamp(obj.VACCINATION_DATE.getTime()); + pstmt.setTimestamp(6, ts); + } else { + pstmt.setTimestamp(6, null); + } + pstmt.setString(7, obj.SERIES); + pstmt.setString(8, obj.MANUFACTURER); + pstmt.setString(9, obj.LOT_NUMBER); + pstmt.setString(10, obj.DOSE_QUANTITY); + pstmt.setString(11, obj.DOSE_UNITS); + pstmt.setString(12, obj.SITE); + pstmt.setString(13, obj.ROUTE); + pstmt.setString(14, obj.PERFORMER_ID); + pstmt.setString(15, obj.PERFORMER_NAME); + pstmt.setString(16, obj.ENCOUNTER_ID); + + pstmt.executeUpdate(); + pstmt.clearParameters(); + pstmt.close(); } } catch (NamingException | SQLException e) { e.printStackTrace(); @@ -703,6 +741,110 @@ else if (textStatus.equalsIgnoreCase("EXTENSIONS")) NarrativeStatusEnum.EXTENSIONS); } retVal.add(medicationPrescript); + } else if (tableName.equals(IMMUNIZATION)) { + String theId = rs.getString("ID"); + String nameUri = rs.getString("NAMEURI"); + String nameCode = rs.getString("NAMECODING"); + String nameDisp = rs.getString("NAMEDISPLAY"); + String subject = rs.getString("SUBJECT"); + Timestamp vdateTS = rs.getTimestamp("VACCINATION_DATE"); + String series = rs.getString("SERIES"); + String manu = rs.getString("MANUFACTURER"); + String lot = rs.getString("LOT_NUMBER"); + String doseQty = rs.getString("DOSE_QUANTITY"); + String doseUnits = rs.getString("DOSE_UNITS"); + String site = rs.getString("SITE"); + String route = rs.getString("ROUTE"); + String provId = rs.getString("PERFORMER_ID"); + String provName = rs.getString("PERFORMER_NAME"); + + Immunization immu = new Immunization(); + + // set ID + immu.setId(theId); + + // set VaccineType + nameDisp = StringEscapeUtils.escapeHtml4(nameDisp); + CodingDt nameCoding = new CodingDt(); + nameCoding.setValueSet(new ResourceReferenceDt("http://hl7.org/fhir/vs/vaccinetype")); + nameCoding.setSystem(nameUri); // typically "http://www2a.cdc.gov/vaccines/iis/iisstandards/vaccines.asp?rpt=cvx" + nameCoding.setCode(nameCode); + nameCoding.setDisplay(nameDisp); + ArrayList codingList = new ArrayList(); + codingList.add(nameCoding); + CodeableConceptDt codeDt = new CodeableConceptDt(); + codeDt.setCoding(codingList); + immu.setVaccineType(codeDt); + + // set Subject as reference to Patient + ResourceReferenceDt subj = new ResourceReferenceDt(subject); + immu.setSubject(subj); + + // set Date + java.util.Date vDate = null; + if (vdateTS != null) + vDate = new Date(vdateTS.getTime()); + if (vDate != null) + immu.setDate(new DateTimeDt(vDate)); + + // set VaccinationProtocol.series + Immunization.VaccinationProtocol ivp = new Immunization.VaccinationProtocol(); + ivp.setSeries(series); + ArrayList ivpList = new ArrayList(); + ivpList.add(ivp); + immu.setVaccinationProtocol(ivpList); + + // set manufacturer = resource reference + if (!manu.equalsIgnoreCase("other")) + immu.setManufacturer(new ResourceReferenceDt(manu)); + + // set lot number + immu.setLotNumber(lot); + + // set DoseQuantity, value and units + if (!doseQty.isEmpty()) { + QuantityDt qty = new QuantityDt(new Double(doseQty)); + qty.setUnits(doseUnits); + immu.setDoseQuantity(qty); + } + + // set site - setSite(CodeableConceptDt theValue) + CodingDt siteCode = new CodingDt(); + siteCode.setValueSet(new ResourceReferenceDt("http://hl7.org/fhir/vs/immunization-site")); + siteCode.setSystem("http://hl7.org/fhir/v3/ActSite"); + siteCode.setDisplay(site); + ArrayList siteCodeList = new ArrayList(); + siteCodeList.add(siteCode); + CodeableConceptDt siteConcept = new CodeableConceptDt(); + siteConcept.setCoding(siteCodeList); + + // set route - setRoute(ImmunizationRouteCodesEnum theValue) + if (route.matches("(?i:im|intramuscular)")) + immu.setRoute(ImmunizationRouteCodesEnum.IM); + else if (route.matches("(?i:nasinhl|nasal|inhalation)")) + immu.setRoute(ImmunizationRouteCodesEnum.NASINHL); + else if (route.matches("(?i:po|oral|by mouth)")) + immu.setRoute(ImmunizationRouteCodesEnum.PO); + + // set provider, id and display name = resource reference + ResourceReferenceDt performer = new ResourceReferenceDt(provId); + performer.setDisplay(provName); + immu.setPerformer(performer); + + /* + * DSTU1 has no option to set encounter_id (resource reference) on the Immunization resource. + * + * However, it is likely to be implemented in DSTU2: + * @see http://hl7.org/fhir/2015May/immunization.html + * + * ...and is alreaday in HAPI-FHIR's DSTU-2: + * @see https://jamesagnew.github.io/hapi-fhir/apidocs-dstu2/ca/uhn/fhir/model/dstu2/resource/Immunization.html + * + * DSTU2 example: + * immu.setEncounter(new ResourceReferenceDt(rs.getString("ENCOUNTER_ID"))); + */ + + retVal.add(immu); } } } diff --git a/src/edu/gatech/i3l/HealthPort/ImmunizationSerializable.java b/src/edu/gatech/i3l/HealthPort/ImmunizationSerializable.java new file mode 100644 index 0000000..59817f9 --- /dev/null +++ b/src/edu/gatech/i3l/HealthPort/ImmunizationSerializable.java @@ -0,0 +1,28 @@ +package edu.gatech.i3l.HealthPort; + +import java.io.Serializable; + +/** + * Bean for Immunization data. + * @author ewall + */ +public class ImmunizationSerializable implements Serializable { + private static final long serialVersionUID = 1L; + + public String ID; + public String NAMEURI; + public String NAMECODING; + public String NAMEDISPLAY; + public String SUBJECT; + public java.util.Date VACCINATION_DATE; + public String SERIES; + public String MANUFACTURER; + public String LOT_NUMBER; + public String DOSE_QUANTITY; + public String DOSE_UNITS; + public String SITE; + public String ROUTE; + public String PERFORMER_ID; + public String PERFORMER_NAME; + public String ENCOUNTER_ID; +} diff --git a/src/edu/gatech/i3l/HealthPort/RestfulServlet.java b/src/edu/gatech/i3l/HealthPort/RestfulServlet.java index b69d1cf..f818df7 100644 --- a/src/edu/gatech/i3l/HealthPort/RestfulServlet.java +++ b/src/edu/gatech/i3l/HealthPort/RestfulServlet.java @@ -10,6 +10,7 @@ import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.RestfulServer; import edu.gatech.i3l.HealthPort.providers.ConditionResourceProvider; +import edu.gatech.i3l.HealthPort.providers.ImmunizationResourceProvider; import edu.gatech.i3l.HealthPort.providers.MedicationPrescrResource; import edu.gatech.i3l.HealthPort.providers.ObservationResourceProvider; import edu.gatech.i3l.HealthPort.providers.PatientResourceProvider; @@ -63,6 +64,7 @@ protected void initialize() throws ServletException { resourceProviders.add(new ObservationResourceProvider()); resourceProviders.add(new ConditionResourceProvider()); resourceProviders.add(new MedicationPrescrResource()); + resourceProviders.add(new ImmunizationResourceProvider()); setResourceProviders(resourceProviders); // /* diff --git a/src/edu/gatech/i3l/HealthPort/UpdateFHIRdB.java b/src/edu/gatech/i3l/HealthPort/UpdateFHIRdB.java index 1cc1a73..f28c5cd 100644 --- a/src/edu/gatech/i3l/HealthPort/UpdateFHIRdB.java +++ b/src/edu/gatech/i3l/HealthPort/UpdateFHIRdB.java @@ -156,7 +156,7 @@ protected void doGet(HttpServletRequest request, syntheticEHRPort.getMedicationPrescriptions(); syntheticCancerPort.getMedicationPrescriptions(); - + } catch (Exception e) { e.printStackTrace(); } finally { @@ -167,6 +167,10 @@ protected void doGet(HttpServletRequest request, e.printStackTrace(); } } + } else if (res.equalsIgnoreCase(HealthPortInfo.IMMUNIZATION)) { + // only ExactDataPort implemented at this time + syntheticEHRPort.getImmunizations(); + syntheticCancerPort.getImmunizations(); } } diff --git a/src/edu/gatech/i3l/HealthPort/ports/ExactDataPort.java b/src/edu/gatech/i3l/HealthPort/ports/ExactDataPort.java index 3beb239..a56526e 100644 --- a/src/edu/gatech/i3l/HealthPort/ports/ExactDataPort.java +++ b/src/edu/gatech/i3l/HealthPort/ports/ExactDataPort.java @@ -25,12 +25,14 @@ import ca.uhn.fhir.model.dstu.composite.QuantityDt; import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt; import ca.uhn.fhir.model.dstu.resource.Condition; +import ca.uhn.fhir.model.dstu.resource.Immunization; import ca.uhn.fhir.model.dstu.resource.Medication; import ca.uhn.fhir.model.dstu.resource.MedicationPrescription; import ca.uhn.fhir.model.dstu.resource.MedicationPrescription.Dispense; import ca.uhn.fhir.model.dstu.resource.MedicationPrescription.DosageInstruction; import ca.uhn.fhir.model.dstu.resource.Observation; import ca.uhn.fhir.model.dstu.valueset.ConditionStatusEnum; +import ca.uhn.fhir.model.dstu.valueset.ImmunizationRouteCodesEnum; import ca.uhn.fhir.model.dstu.valueset.MedicationPrescriptionStatusEnum; import ca.uhn.fhir.model.dstu.valueset.NarrativeStatusEnum; import ca.uhn.fhir.model.dstu.valueset.ObservationReliabilityEnum; @@ -40,6 +42,7 @@ import ca.uhn.fhir.model.primitive.IdDt; import edu.gatech.i3l.HealthPort.ConditionSerializable; import edu.gatech.i3l.HealthPort.HealthPortInfo; +import edu.gatech.i3l.HealthPort.ImmunizationSerializable; import edu.gatech.i3l.HealthPort.MedicationPrescriptionSerializable; import edu.gatech.i3l.HealthPort.ObservationSerializable; import edu.gatech.i3l.HealthPort.PortIf; @@ -1449,6 +1452,270 @@ public List getMedicationPrescriptions() { return retVal; } + + /*******************************************************************/ + + // create HAPI-FHIR Immunization object + private Immunization createImmunization(String id, String nameUri, String nameCode, String nameDisp, + String subjRef, java.util.Date vacDate, String series, String manufacturer, String lot, + String doseQty, String doseUnits, String site, String route, String performerId, String performerName, + String encId) { + Immunization immu = new Immunization(); + + // set ID + immu.setId(id); + + // set VaccineType + nameDisp = StringEscapeUtils.escapeHtml4(nameDisp); + CodingDt nameCoding = new CodingDt(); + nameCoding.setValueSet(new ResourceReferenceDt("http://hl7.org/fhir/vs/vaccinetype")); + nameCoding.setSystem(nameUri); // typically "http://www2a.cdc.gov/vaccines/iis/iisstandards/vaccines.asp?rpt=cvx" + nameCoding.setCode(nameCode); + nameCoding.setDisplay(nameDisp); + ArrayList codingList = new ArrayList(); + codingList.add(nameCoding); + CodeableConceptDt codeDt = new CodeableConceptDt(); + codeDt.setCoding(codingList); + immu.setVaccineType(codeDt); + + // set Subject as reference to Patient + ResourceReferenceDt subj = new ResourceReferenceDt(subjRef); + immu.setSubject(subj); + + // set Date + if (vacDate != null) + immu.setDate(new DateTimeDt(vacDate)); + + // set VaccinationProtocol.series + Immunization.VaccinationProtocol ivp = new Immunization.VaccinationProtocol(); + ivp.setSeries(series); + ArrayList ivpList = new ArrayList(); + ivpList.add(ivp); + immu.setVaccinationProtocol(ivpList); + + // set manufacturer = resource reference + if (!manufacturer.equalsIgnoreCase("other")) + immu.setManufacturer(new ResourceReferenceDt(manufacturer)); + + // set lot number + immu.setLotNumber(lot); + + // set DoseQuantity, value and units + QuantityDt qty = new QuantityDt(new Double(doseQty)); + qty.setUnits(doseUnits); + immu.setDoseQuantity(qty); + + // set site - setSite(CodeableConceptDt theValue) + CodingDt siteCode = new CodingDt(); + siteCode.setValueSet(new ResourceReferenceDt("http://hl7.org/fhir/vs/immunization-site")); + siteCode.setSystem("http://hl7.org/fhir/v3/ActSite"); + siteCode.setDisplay(site); + ArrayList siteCodeList = new ArrayList(); + siteCodeList.add(siteCode); + CodeableConceptDt siteConcept = new CodeableConceptDt(); + siteConcept.setCoding(siteCodeList); + + // set route - setRoute(ImmunizationRouteCodesEnum theValue) + if (route.matches("(?i:im|intramuscular)")) + immu.setRoute(ImmunizationRouteCodesEnum.IM); + else if (route.matches("(?i:nasinhl|nasal|inhalation)")) + immu.setRoute(ImmunizationRouteCodesEnum.NASINHL); + else if (route.matches("(?i:po|oral|by mouth)")) + immu.setRoute(ImmunizationRouteCodesEnum.PO); + + // set provider, id and display name = resource reference + ResourceReferenceDt performer = new ResourceReferenceDt(performerId); + performer.setDisplay(performerName); + immu.setPerformer(performer); + + /* + * DSTU1 has no option to set encounter_id (resource reference) on the Immunization resource. + * + * However, it is likely to be implemented in DSTU2: + * @see http://hl7.org/fhir/2015May/immunization.html + * + * ...and is alreaday in HAPI-FHIR's DSTU-2: + * @see https://jamesagnew.github.io/hapi-fhir/apidocs-dstu2/ca/uhn/fhir/model/dstu2/resource/Immunization.html + * + * DSTU2 example: + * immu.setEncounter(new ResourceReferenceDt(rs.getString("ENCOUNTER_ID"))); + */ + + return immu; + } + + // create immunization object in internal serializable form + private ImmunizationSerializable createImmunizationSz(String id, String nameUri, String nameCode, String nameDisp, + String subjRef, java.util.Date vacDate, String series, String manufacturer, String lot, + String doseQty, String doseUnits, String site, String route, String performerId, String performerName, + String encId) { + ImmunizationSerializable immu = new ImmunizationSerializable(); + + immu.ID = id; + immu.NAMEURI = nameUri; + immu.NAMECODING = nameCode; + immu.NAMEDISPLAY = nameDisp; + immu.SUBJECT = subjRef; + immu.VACCINATION_DATE = vacDate; + immu.MANUFACTURER = manufacturer; + immu.LOT_NUMBER = lot; + immu.DOSE_QUANTITY = doseQty; + immu.DOSE_UNITS = doseUnits; + immu.SITE = site; + immu.ROUTE = route; + immu.PERFORMER_ID = performerId; + immu.PERFORMER_NAME = performerName; + immu.ENCOUNTER_ID = encId; + + return immu; + } + + // get all available immunization ids, stored in HealthPort DB as a side-effect + public List getImmunizations() { + List retVal = new ArrayList(); + + Connection conn = null; + Statement stmt = null; + try { + conn = dataSource.getConnection(); + stmt = conn.createStatement(); + String sql = "SELECT * FROM immunization"; + ResultSet rs = stmt.executeQuery(sql); + List immuList = getImmunizationIds(rs); + if (immuList != null && !immuList.isEmpty()) { + retVal.addAll(immuList); + } + conn.close(); + } catch (SQLException se) { + se.printStackTrace(); + } + return retVal; + } + + // get immunizations ids from a ResultSet, storing them in HealthPort DB as a side-effect + public List getImmunizationIds(ResultSet rs) { + List retVal = new ArrayList(); + ImmunizationSerializable immu = null; + + try { + while (rs.next()) { + String memberID = rs.getString("Member_ID"); + if (memberID.isEmpty()) { + continue; + } + + /* prep id - ExactData's immunization tables do not have ids, so we must create our own + * built from encounter_id + CVX code + * hopefully it's safe to assume you don't get the same vaccine twice on the same visit? + */ + String immuId = rs.getString("Encounter_ID") + "." + rs.getString("Vaccine_CVX"); + + // prep date + java.util.Date vacDate = null; + Timestamp ts = rs.getTimestamp("Vaccination_Date"); + if (ts != null) { + vacDate = new java.util.Date(ts.getTime()); + } + + immu = createImmunizationSz( + immuId, + "http://www2a.cdc.gov/vaccines/iis/iisstandards/vaccines.asp?rpt=cvx", + rs.getString("Vaccine_CVX"), + rs.getString("Vaccine_Name"), + "Patient/" + id + "." + rs.getString("Member_ID"), + vacDate, + rs.getString("Series"), + rs.getString("Manufacturer"), + rs.getString("Lot_Number"), + rs.getString("Dose"), + rs.getString("Units"), + rs.getString("Site"), + rs.getString("Route"), + rs.getString("Provider_ID"), + rs.getString("Provider_Name"), + rs.getString("Encounter_ID") + ); + + HealthPortInfo.storeResource(HealthPortInfo.IMMUNIZATION, immu); + + retVal.add(immuId); + } + } catch (SQLException e) { + e.printStackTrace(); + } + + return retVal; + } + + // get specific HAPI-FHIR Immunization objects from a ResultSet, used by getImmunizationsByPatient() + public ArrayList getImmunizations(ResultSet rs) { + ArrayList retVal = new ArrayList(); + + try { + while (rs.next()) { + String memberID = rs.getString("Member_ID"); + if (memberID.isEmpty()) { + continue; + } + + // prep date + java.util.Date vacDate = null; + Timestamp ts = rs.getTimestamp("Vaccination_Date"); + if (ts != null) { + vacDate = new java.util.Date(ts.getTime()); + } + + String immuId = id + "." + rs.getInt("ID"); + Immunization immu = createImmunization( + immuId, + rs.getString("Vaccine_CVX"), + "http://www2a.cdc.gov/vaccines/iis/iisstandards/vaccines.asp?rpt=cvx", + rs.getString("Vaccine_Name"), + "Patient/" + id + "." + rs.getString("Member_ID"), + vacDate, + rs.getString("Series"), + rs.getString("Manufacturer"), + rs.getString("Lot_Number"), + rs.getString("Dose"), + rs.getString("Units"), + rs.getString("Site"), + rs.getString("Route"), + rs.getString("Provider_ID"), + rs.getString("Provider_Name"), + rs.getString("Encounter_ID") + ); + retVal.add(immu); + } + } catch (SQLException e) { + e.printStackTrace(); + } + return retVal; + } + + // get HAPI-FHIR Immunization objects for a specific patient, not currently called anywhere + public ArrayList getImmunizationsByPatient(String memberID) { + ArrayList retVal = new ArrayList(); + + Connection conn = null; + Statement stmt = null; + try { + conn = dataSource.getConnection(); + stmt = conn.createStatement(); + String sql = "SELECT * FROM immunization WHERE Member_ID='" + memberID + "'"; + ResultSet rs = stmt.executeQuery(sql); + + ArrayList immuList = getImmunizations(rs); + if (immuList != null && !immuList.isEmpty()) { + retVal.addAll(immuList); + } + + conn.close(); + } catch (SQLException se) { + se.printStackTrace(); + } + return retVal; + } + /* * (non-Javadoc) * diff --git a/src/edu/gatech/i3l/HealthPort/providers/ImmunizationResourceProvider.java b/src/edu/gatech/i3l/HealthPort/providers/ImmunizationResourceProvider.java new file mode 100644 index 0000000..787b1c2 --- /dev/null +++ b/src/edu/gatech/i3l/HealthPort/providers/ImmunizationResourceProvider.java @@ -0,0 +1,157 @@ +package edu.gatech.i3l.HealthPort.providers; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import ca.uhn.fhir.model.api.IResource; +import ca.uhn.fhir.model.dstu.resource.Immunization; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.model.primitive.InstantDt; +import ca.uhn.fhir.rest.annotation.IdParam; +import ca.uhn.fhir.rest.annotation.Read; +import ca.uhn.fhir.rest.annotation.RequiredParam; +import ca.uhn.fhir.rest.annotation.Search; +import ca.uhn.fhir.rest.param.ReferenceParam; +import ca.uhn.fhir.rest.param.TokenParam; +import ca.uhn.fhir.rest.server.IBundleProvider; +import ca.uhn.fhir.rest.server.IResourceProvider; +import edu.gatech.i3l.HealthPort.HealthPortInfo; + +/** + * Retrieve Immunization data as HAPI-FHIR resource objects. + * @author Eric Wallace + */ +public class ImmunizationResourceProvider implements IResourceProvider { + private HealthPortInfo healthPortUser; + + public ImmunizationResourceProvider() { + healthPortUser = new HealthPortInfo("jdbc/HealthPort"); + } + + /* (non-Javadoc) + * @see ca.uhn.fhir.rest.server.IResourceProvider#getResourceType() + */ + @Override + public Class getResourceType() { + return Immunization.class; + } + + /** + * Given a single resource, retrieve that Immunization resource. + * @param theId + * @return an Immunization object + */ + @Read() + public Immunization getResourceById(@IdParam IdDt theId) { + ListIds = new ArrayList(); + Ids.add(theId.getIdPart()); + + List resourceList = null; + try { + resourceList = healthPortUser.getResourceList(HealthPortInfo.IMMUNIZATION, Ids); + } catch (SQLException e) { + e.printStackTrace(); // TODO Auto-generated catch block + } + + if (resourceList == null || resourceList.isEmpty()) return null; + return (Immunization) resourceList.get(0); + } + + /** + * Retrieve all available Immunization resources. + * @return a Bundle of Immunization objects + */ + @Search + public IBundleProvider getAllImmunizations() { + final InstantDt searchTime = InstantDt.withCurrentTime(); + final List matchingResourceIds = healthPortUser + .getAllResourceIds(HealthPortInfo.IMMUNIZATION); + + return new IBundleProvider() { + + @Override + public int size() { + return matchingResourceIds.size(); + } + + @Override + public List getResources(int theFromIndex, int theToIndex) { + int end = Math.min(theToIndex, matchingResourceIds.size()); + // System.out.println("From:"+theFromIndex+" To:"+theToIndex+" Total:"+matchingResourceIds.size()); + List idsToReturn = matchingResourceIds.subList(theFromIndex, end); + List retVal = null; + try { + retVal = healthPortUser.getResourceList(HealthPortInfo.IMMUNIZATION, idsToReturn); + } catch (SQLException e) { + e.printStackTrace(); // TODO Auto-generated catch block + } + + return retVal; + } + + @Override + public InstantDt getPublished() { + return searchTime; + } + }; + } + + /** + * Retrieve all available Immunizations associated with a patient. + * @param theSubject a Patient object + * @return a Bundle of Immunization objects + */ + @Search() + public IBundleProvider getImmunizationsByPatient( + @RequiredParam(name = Immunization.SP_SUBJECT) ReferenceParam theSubject) { + + final InstantDt searchTime = InstantDt.withCurrentTime(); + String patientID = theSubject.getIdPart(); + + final List matchingResourceIds = healthPortUser + .getResourceIdsByPatient(HealthPortInfo.IMMUNIZATION, patientID); + + return new IBundleProvider() { + + @Override + public int size() { + return matchingResourceIds.size(); + } + + @Override + public List getResources(int theFromIndex, int theToIndex) { + int end = Math.min(theToIndex, matchingResourceIds.size()); + // System.out.println("From:"+theFromIndex+" To:"+theToIndex+" Total:"+matchingResourceIds.size()); + List idsToReturn = matchingResourceIds.subList(theFromIndex, end); + + List retVal = null; + try { + retVal = healthPortUser.getResourceList(HealthPortInfo.IMMUNIZATION,idsToReturn); + } catch (SQLException e) { + e.printStackTrace(); // TODO Auto-generated catch block + } + + return retVal; + } + + @Override + public InstantDt getPublished() { + return searchTime; + } + }; + + } + + /** + * @param theId search parameter + * @return a Bundle of Immunization objects + */ + @Search() + public IBundleProvider searchByVaccineType( + @RequiredParam(name = Immunization.SP_VACCINE_TYPE) TokenParam theId) { + // TODO implement searchByVaccineType() + throw new UnsupportedOperationException("Not implemented yet"); + } + +}