From acee24af1a39cb96df9d987f6bf965fd9ab0896e Mon Sep 17 00:00:00 2001 From: Syd Logan Date: Sat, 8 May 2021 21:03:34 -0700 Subject: [PATCH] REST API for vaccinations --- .../tscharts_lib/CommonSessionSingleton.java | 42 +- .../tscharts_lib/Vaccination.kt | 370 ++++++++++++++++++ .../tscharts_lib/VaccinationREST.java | 231 +++++++++++ 3 files changed, 641 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/thousandsmiles/tscharts_lib/Vaccination.kt create mode 100644 app/src/main/java/org/thousandsmiles/tscharts_lib/VaccinationREST.java diff --git a/app/src/main/java/org/thousandsmiles/tscharts_lib/CommonSessionSingleton.java b/app/src/main/java/org/thousandsmiles/tscharts_lib/CommonSessionSingleton.java index 61a735f..7402643 100755 --- a/app/src/main/java/org/thousandsmiles/tscharts_lib/CommonSessionSingleton.java +++ b/app/src/main/java/org/thousandsmiles/tscharts_lib/CommonSessionSingleton.java @@ -1,6 +1,6 @@ /* - * (C) Copyright Syd Logan 2018-2020 - * (C) Copyright Thousand Smiles Foundation 2018-2020 + * (C) Copyright Syd Logan 2018-2021 + * (C) Copyright Thousand Smiles Foundation 2018-2021 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -60,6 +60,7 @@ public class CommonSessionSingleton { private ArrayList m_medicationsList = new ArrayList(); private CDTCodesModelList m_cdtCodesList; private MedicalHistory m_patientMedicalHistory = null; + private Vaccination m_patientVaccination = null; private XRay m_patientXRay = null; private ENTHistory m_patientENTHistory = null; private ENTDiagnosis m_patientENTDiagnosis = null; @@ -251,6 +252,43 @@ public int getPatientRoutingSlipId() return m_patientRoutingSlipId; } + /** vaccinations */ + + public void resetPatientVaccination() { + m_patientVaccination = null; + } + + public void setVaccinationId(int id) { + m_patientVaccination.setId(id); + } + + public void setPatientVaccination(JSONObject o) + { + if (m_patientVaccination == null) { + m_patientVaccination = new Vaccination(); + } + m_patientVaccination.fromJSONObject(o); + } + + public void updatePatientVaccination(Vaccination vaccination) { + m_patientVaccination = vaccination; + } + + public Vaccination getPatientVaccination() + { + return m_patientVaccination; + } + + public Vaccination getNewPatientVaccination() + { + if (m_patientVaccination == null) { + m_patientVaccination = new Vaccination(); + } + return m_patientVaccination; + } + + /** end vaccinations */ + public void resetPatientMedicalHistory() { m_patientMedicalHistory = null; } diff --git a/app/src/main/java/org/thousandsmiles/tscharts_lib/Vaccination.kt b/app/src/main/java/org/thousandsmiles/tscharts_lib/Vaccination.kt new file mode 100644 index 0000000..36d9980 --- /dev/null +++ b/app/src/main/java/org/thousandsmiles/tscharts_lib/Vaccination.kt @@ -0,0 +1,370 @@ +package org.thousandsmiles.tscharts_lib +import org.json.JSONException +import org.json.JSONObject + +/* + * (C) Copyright Syd Logan 2021 + * (C) Copyright Thousand Smiles Foundation 2021 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +class Vaccination { + var id = 0 + var clinic = 0 + var patient = 0 + var covid19 = false + var covid19_doses = 0 + var covid19_date: String? = null + var covid19_booster = false + var covid19_booster_date: String? = null + var dtap = false + var dtap_date: String? = null + var dt = false + var dt_date: String? = null + var hib = false + var hib_date: String? = null + var hepa = false + var hepa_date: String? = null + var hepb = false + var hepb_date: String? = null + var hpv = false + var hpv_date: String? = null + var iiv = false + var iiv_date: String? = null + var laiv4 = false + var laiv4_date: String? = null + var mmr = false + var mmr_date: String? = null + var menacwy = false + var menacwy_date: String? = null + var menb = false + var menb_date: String? = null + var pcv13 = false + var pcv13_date: String? = null + var ppsv23 = false + var ppsv23_date: String? = null + var ipv = false + var ipv_date: String? = null + var rv = false + var rv_date: String? = null + var tap = false + var tap_date: String? = null + var td = false + var td_date: String? = null + var vari = false // in the backend, this is "var" + var vari_date: String? = null + var dtap_hepb_ipv = false + var dtap_hepb_ipv_date: String? = null + var dtap_ipv_hib = false + var dtap_ipv_hib_date: String? = null + var dtap_ipv = false + var dtap_ipv_date: String? = null + var dtap_ipv_hib_hepb = false + var dtap_ipv_hib_hepb_date: String? = null + var mmvr = false + var mmvr_date: String? = null + + override fun equals(obj: Any?): Boolean { + var ret = true + if (obj == null) { + ret = false + } + + val other = obj as Vaccination + if (covid19 != other.covid19) { + ret = false + } + if (covid19_doses != other.covid19_doses) { + ret = false + } + if (covid19_date != other.covid19_date) { + ret = false + } + if (covid19_booster != other.covid19_booster) { + ret = false + } + if (covid19_booster_date != other.covid19_booster_date) { + ret = false + } + if (dtap != other.dtap) { + ret = false + } + if (dtap_date != other.dtap_date) { + ret = false + } + if (dt != other.dt) { + ret = false + } + if (dt_date != other.dt_date) { + ret = false + } + if (hib != other.hib) { + ret = false + } + if (hib_date != other.hib_date) { + ret = false + } + if (hepa != other.hepa) { + ret = false + } + if (hepa_date != other.hepa_date) { + ret = false + } + if (hepb != other.hepb) { + ret = false + } + if (hepb_date != other.hepb_date) { + ret = false + } + if (hpv != other.hpv) { + ret = false + } + if (hpv_date != other.hpv_date) { + ret = false + } + if (iiv != other.iiv) { + ret = false + } + if (iiv_date != other.iiv_date) { + ret = false + } + if (laiv4 != other.laiv4) { + ret = false + } + if (laiv4_date != other.laiv4_date) { + ret = false + } + if (mmr != other.mmr) { + ret = false + } + if (mmr_date != other.mmr_date) { + ret = false + } + if (menacwy != other.menacwy) { + ret = false + } + if (menacwy_date != other.menacwy_date) { + ret = false + } + if (menb != other.menb) { + ret = false + } + if (menb_date != other.menb_date) { + ret = false + } + if (pcv13 != other.pcv13) { + ret = false + } + if (pcv13_date != other.pcv13_date) { + ret = false + } + if (ppsv23 != other.ppsv23) { + ret = false + } + if (ppsv23_date != other.ppsv23_date) { + ret = false + } + if (ipv != other.ipv) { + ret = false + } + if (ipv_date != other.ipv_date) { + ret = false + } + if (rv != other.rv) { + ret = false + } + if (rv_date != other.rv_date) { + ret = false + } + if (tap != other.tap) { + ret = false + } + if (tap_date != other.tap_date) { + ret = false + } + if (td != other.td) { + ret = false + } + if (td_date != other.td_date) { + ret = false + } + if (vari != other.vari) { + ret = false + } + if (vari_date != other.vari_date) { + ret = false + } + if (dtap_hepb_ipv != other.dtap_hepb_ipv) { + ret = false + } + if (dtap_hepb_ipv_date != other.dtap_hepb_ipv_date) { + ret = false + } + if (dtap_ipv_hib != other.dtap_ipv_hib) { + ret = false + } + if (dtap_ipv_hib_date != other.dtap_ipv_hib_date) { + ret = false + } + if (dtap_ipv != other.dtap_ipv) { + ret = false + } + if (dtap_ipv_date != other.dtap_ipv_date) { + ret = false + } + if (dtap_ipv_hib_hepb != other.dtap_ipv_hib_hepb) { + ret = false + } + if (dtap_ipv_hib_hepb_date != other.dtap_ipv_hib_hepb_date) { + ret = false + } + if (mmvr != other.mmvr) { + ret = false + } + if (mmvr_date != other.mmvr_date) { + ret = false + } + return ret + } + + fun fromJSONObject(o: JSONObject): Int { + var ret = 0 + try { + id = o.getInt("id") + clinic = o.getInt("clinic") + patient = o.getInt("patient") + + covid19 = o.getBoolean("covid19") + covid19_doses = o.getInt("covid19_doses") + covid19_date = o.getString("covid19_date") + covid19_booster = o.getBoolean("covid19_booster") + covid19_booster_date = o.getString("covid19_booster_date") + dtap = o.getBoolean("dtap") + dtap_date = o.getString("dtap_date") + dt = o.getBoolean("dt") + dt_date = o.getString("dt_date") + hib = o.getBoolean("hib") + hib_date = o.getString("hib_date") + hepa = o.getBoolean("hepa") + hepa_date = o.getString("hepa_date") + hepb = o.getBoolean("hepb") + hepb_date = o.getString("hepb_date") + hpv = o.getBoolean("hpv") + hpv_date = o.getString("hpv_date") + iiv = o.getBoolean("iiv") + iiv_date = o.getString("iiv_date") + laiv4 = o.getBoolean("laiv4") + laiv4_date = o.getString("laiv4_date") + mmr = o.getBoolean("mmr") + mmr_date = o.getString("mmr_date") + menacwy = o.getBoolean("menacwy") + menacwy_date = o.getString("menacwy_date") + menb = o.getBoolean("menb") + menb_date = o.getString("menb_date") + pcv13 = o.getBoolean("pcv13") + pcv13_date = o.getString("pcv13_date") + ppsv23 = o.getBoolean("ppsv23") + ppsv23_date = o.getString("ppsv23_date") + ipv = o.getBoolean("ipv") + ipv_date = o.getString("ipv_date") + rv = o.getBoolean("rv") + rv_date = o.getString("rv_date") + tap = o.getBoolean("tap") + tap_date = o.getString("tap_date") + td = o.getBoolean("td") + td_date = o.getString("td_date") + vari = o.getBoolean("vari") // in the backend, this is "var" + vari_date = o.getString("vari_date") + dtap_hepb_ipv = o.getBoolean("dtap_hepb_ipv") + dtap_hepb_ipv_date = o.getString("dtap_hepb_ipv_date") + dtap_ipv_hib = o.getBoolean("dtap_ipv_hib") + dtap_ipv_hib_date = o.getString("dtap_ipv_hib_date") + dtap_ipv = o.getBoolean("dtap_ipv") + dtap_ipv_date = o.getString("dtap_ipv_date") + dtap_ipv_hib_hepb = o.getBoolean("dtap_ipv_hib_hepb") + dtap_ipv_hib_hepb_date = o.getString("dtap_ipv_hib_hepb_date") + mmvr = o.getBoolean("mmvr") + mmvr_date = o.getString("mmvr_date") + } catch (e: JSONException) { + ret = -1 + } + return ret + } + + fun toJSONObject(includeId: Boolean): JSONObject? { + var data: JSONObject? = JSONObject() + try { + if (includeId == true) { + data!!.put("id", id) + } + data!!.put("covid19", covid19) + data.put("covid19_doses", covid19_doses) + data.put("covid19_date", covid19_date) + data.put("covid19_booster", covid19_booster) + data.put("covid19_booster_date", covid19_booster_date) + data.put("dtap", dtap) + data.put("dtap_date", dtap_date) + data.put("dt", dt) + data.put("dt_date", dt_date) + data.put("hib", hib) + data.put("hib_date", hib_date) + data.put("hepa", hepa) + data.put("hepa_date", hepa_date) + data.put("hepb", hepb) + data.put("hepb_date", hepb_date) + data.put("hpv", hpv) + data.put("hpv_date", hpv_date) + data.put("iiv", iiv) + data.put("iiv_date", iiv_date) + data.put("laiv4", laiv4) + data.put("laiv4_date", laiv4_date) + data.put("mmr", mmr) + data.put("mmr_date", mmr_date) + data.put("menacwy", menacwy) + data.put("menacwy_date", menacwy_date) + data.put("menb", menb) + data.put("menb_date", menb_date) + data.put("pcv13", pcv13) + data.put("pcv13_date", pcv13_date) + data.put("ppsv23", ppsv23) + data.put("ppsv23_date", ppsv23_date) + data.put("ipv", ipv) + data.put("ipv_date", ipv_date) + data.put("rv", rv) + data.put("rv_date", rv_date) + data.put("tap", tap) + data.put("tap_date", tap_date) + data.put("td", td) + data.put("td_date", td_date) + data.put("vari", vari) + data.put("vari_date", vari_date) + data.put("dtap_hepb_ipv", dtap_hepb_ipv) + data.put("dtap_hepb_ipv_date", dtap_hepb_ipv_date) + data.put("dtap_ipv_hib", dtap_ipv_hib) + data.put("dtap_ipv_hib_date", dtap_ipv_hib_date) + data.put("dtap_ipv", dtap_ipv) + data.put("dtap_ipv_date", dtap_ipv_date) + data.put("dtap_ipv_hib_hepb", dtap_ipv_hib_hepb) + data.put("dtap_ipv_hib_hepb_date", dtap_ipv_hib_hepb_date) + data.put("mmvr", mmvr) + data.put("mmvr_date", mmvr_date) + + } catch (e: Exception) { + // not sure this would ever happen, ignore. Continue on with the request with the expectation it fails + // because of the bad JSON sent + data = null + } + return data + } +} \ No newline at end of file diff --git a/app/src/main/java/org/thousandsmiles/tscharts_lib/VaccinationREST.java b/app/src/main/java/org/thousandsmiles/tscharts_lib/VaccinationREST.java new file mode 100644 index 0000000..db835d6 --- /dev/null +++ b/app/src/main/java/org/thousandsmiles/tscharts_lib/VaccinationREST.java @@ -0,0 +1,231 @@ +/* + * (C) Copyright Syd Logan 2021 + * (C) Copyright Thousand Smiles Foundation 2021 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.thousandsmiles.tscharts_lib; + +import android.content.Context; + +import com.android.volley.AuthFailureError; +import com.android.volley.DefaultRetryPolicy; +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.JsonArrayRequest; +import com.android.volley.toolbox.JsonObjectRequest; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashMap; +import java.util.Map; + +public class VaccinationREST extends RESTful { + private final Object m_lock = new Object(); + + private class ResponseListener implements Response.Listener { + + @Override + public void onResponse(JSONObject response) { + + synchronized (m_lock) { + CommonSessionSingleton sess = CommonSessionSingleton.getInstance(); + setStatus(200); + onSuccess(200, ""); + // XXX factor next line to listener + sess.setPatientVaccination(response); + m_lock.notify(); + } + } + } + + private class PostResponseListener implements Response.Listener { + + @Override + public void onResponse(JSONObject response) { + + synchronized (m_lock) { + CommonSessionSingleton sess = CommonSessionSingleton.getInstance(); + try { + // XXX factor next line to listener(s) + sess.setVaccinationId(response.getInt("id")); + setStatus(200); + onSuccess(200, ""); + } catch (JSONException e) { + setStatus(500); + onFail(500, ""); + } + m_lock.notify(); + } + } + } + + private class ErrorListener implements Response.ErrorListener { + @Override + public void onErrorResponse(VolleyError error) { + + synchronized (m_lock) { + if (error.networkResponse == null) { + if (error.getCause() instanceof java.net.ConnectException || error.getCause() instanceof java.net.UnknownHostException) { + setStatus(101); + onFail(101, error.getMessage()); + } else { + setStatus(-1); + onFail(-1, error.getMessage()); + } + } else { + setStatus(error.networkResponse.statusCode); + } + m_lock.notify(); + } + } + } + + private class PutResponseListener implements Response.Listener { + + @Override + public void onResponse(JSONObject response) { + + synchronized (m_lock) { + setStatus(200); + onSuccess(200, ""); + m_lock.notify(); + } + } + } + + public class AuthJSONObjectRequest extends JsonObjectRequest + { + public AuthJSONObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener listener, ErrorListener errorListener) + { + super(method, url, jsonRequest, listener, errorListener); + } + + @Override + public Map getHeaders() throws AuthFailureError { + Map headers = new HashMap(); + headers.put("Authorization", CommonSessionSingleton.getInstance().getToken()); + return headers; + } + } + + public class AuthJSONArrayRequest extends JsonArrayRequest { + + public AuthJSONArrayRequest(String url, JSONArray jsonRequest, + Response.Listener listener, ErrorListener errorListener) { + super(url, listener, errorListener); + } + + public AuthJSONArrayRequest(String url, Response.Listener listener, + Response.ErrorListener errorListener, String username, String password) { + super(url, listener, errorListener); + + } + + private Map headers = new HashMap(); + @Override + public Map getHeaders() throws AuthFailureError { + //return headers; + Map headers = new HashMap(); + headers.put("Authorization", CommonSessionSingleton.getInstance().getToken()); + return headers; + } + + } + + public VaccinationREST(Context context) { + setContext(context); + } + + public Object createVaccination(Vaccination vaccination) { + + VolleySingleton volley = VolleySingleton.getInstance(); + + volley.initQueueIf(getContext()); + + RequestQueue queue = volley.getQueue(); + + JSONObject data = vaccination.toJSONObject(false); + + String url = String.format("%s://%s:%s/tscharts/v1/vaccination/", getProtocol(), getIP(), getPort()); + + VaccinationREST.AuthJSONObjectRequest request = new VaccinationREST.AuthJSONObjectRequest(Request.Method.POST, url, data, new PostResponseListener(), new ErrorListener()); + request.setRetryPolicy(new DefaultRetryPolicy(getTimeoutInMillis(), getRetries(), DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); + + queue.add((JsonObjectRequest) request); + + return m_lock; + } + + public Object getVaccinationData(int vaccinationid) { + + VolleySingleton volley = VolleySingleton.getInstance(); + + volley.initQueueIf(getContext()); + + RequestQueue queue = volley.getQueue(); + + String url = String.format("%s://%s:%s/tscharts/v1/vaccination/%d/", getProtocol(), getIP(), getPort(), vaccinationid); + + AuthJSONObjectRequest request = new AuthJSONObjectRequest(Request.Method.GET, url, null, new ResponseListener(), new ErrorListener()); + request.setRetryPolicy(new DefaultRetryPolicy(getTimeoutInMillis(), getRetries(), DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); + + queue.add((JsonObjectRequest) request); + + return m_lock; + } + + public Object getVaccinationData(int clinicid, int patientid) { + + VolleySingleton volley = VolleySingleton.getInstance(); + + volley.initQueueIf(getContext()); + + RequestQueue queue = volley.getQueue(); + + String url = String.format("%s://%s:%s/tscharts/v1/vaccination/?clinic=%d&patient=%d", + getProtocol(), getIP(), getPort(), clinicid, patientid); + + AuthJSONObjectRequest request = new AuthJSONObjectRequest(Request.Method.GET, url, null, new ResponseListener(), new ErrorListener()); + request.setRetryPolicy(new DefaultRetryPolicy(getTimeoutInMillis(), getRetries(), DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); + + queue.add((JsonObjectRequest) request); + + return m_lock; + } + + public Object updateVaccination(Vaccination vaccination) { + + VolleySingleton volley = VolleySingleton.getInstance(); + + volley.initQueueIf(getContext()); + + RequestQueue queue = volley.getQueue(); + + JSONObject data = vaccination.toJSONObject(true); + + String url = String.format("%s://%s:%s/tscharts/v1/vaccination/%d/", getProtocol(), getIP(), getPort(), vaccination.getId()); + + AuthJSONObjectRequest request = new AuthJSONObjectRequest(Request.Method.PUT, url, data, new PutResponseListener(), new ErrorListener()); + request.setRetryPolicy(new DefaultRetryPolicy(getTimeoutInMillis(), getRetries(), DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); + + queue.add((JsonObjectRequest) request); + + return m_lock; + } +}