From 6b4a1c068034bf67cf9432827ac852424c8434d4 Mon Sep 17 00:00:00 2001 From: Rizki Sunaryo Date: Tue, 19 Feb 2019 03:28:10 +0700 Subject: [PATCH] fix (sync): wrong delivery status of Ibu **The Cause** 1. Client (the application) does: - ANC: event date: 15 Feb 2019 - Dokumentasi Persalinan: event date: 15 Feb 2019 2. Sync (upload): ANC and Dokumentasi Persalinan (delivery document) will be sent, and the server will add the same time for dateCreated. Because it's a bulky transaction. 3. Sync (download), sometimes like this: Dokumentasi Persalinan data is above ANC. Because the date is the same, so no order (sorted by), and the data can be flipped. While in the client (application), Dokumentasi Persalinan is processed first, then ANC. That's why the status is still ANC (EDD Passed). **The Fix** When there are both ANC and Dokumentasi Persalinan, with actually it's a sequence, so the status should be Dokumentasi Persalinan (delivered). https://github.com/OpenSRP/opensrp-client-sid/issues/32 --- .../bidan/sync/ClientProcessor.java | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/opensrp-bidan/src/main/java/org/smartregister/bidan/sync/ClientProcessor.java b/opensrp-bidan/src/main/java/org/smartregister/bidan/sync/ClientProcessor.java index 2d7f03a..fa97f09 100644 --- a/opensrp-bidan/src/main/java/org/smartregister/bidan/sync/ClientProcessor.java +++ b/opensrp-bidan/src/main/java/org/smartregister/bidan/sync/ClientProcessor.java @@ -119,8 +119,50 @@ public synchronized void processClient(List events) throws Exception if (!events.isEmpty()) { for (JSONObject event : events) { - if (event.getString("eventType").equals(ANC_REGISTRATION)){ + // START: find existing anc_id in ec_pnc (Dokumentasi Persalinan) + // get obs of event + JSONArray obs = event.has("obs") ? event.getJSONArray("obs") : null; + if (obs != null) { + // ANC ID is possible to be more than 1, + // so we use List + List ancList = null; + // get ANC ID list + for (int i = 0; i < obs.length(); i++) { + JSONObject ob = obs.getJSONObject(i); + String fieldCode = ob.has("fieldCode") ? ob.getString("fieldCode") : null; + // check whether the fieldCode is ancId + if (fieldCode != null && fieldCode.equalsIgnoreCase("ancId")) { + JSONArray values = ob.has("values") ? ob.getJSONArray("values") : null; + // store values to ancList + ancList = getValues(values); + break; + } + } + + if (ancList != null) { + // building question marks for query + String inQuestionMarks = "?"; + for (int i = 2; i < ancList.size(); i++) { + inQuestionMarks += ", ?"; + } + + // query, to get whether there is existing data in ec_pnc (Dokumentasi Persalinan) + // that matches ANC ID list + net.sqlcipher.Cursor temp = BidanApplication + .getInstance() + .getContext() + .initRepository() + .getWritableDatabase() + .rawQuery("SELECT * FROM ec_pnc WHERE anc_id IN ("+ inQuestionMarks +")", ancList.toArray(new String[ancList.size()])); + // if yes, then skip both removePncData and removeAncData + if (temp.getCount() > 0) { + continue; + } + } + } + // END: find existing anc_id in ec_pnc (Dokumentasi Persalinan) + removePncData(event.getString("baseEntityId")); removeAncData(event.getString("baseEntityId")); }