From 22911dc3d2c068d56035d6917f4aaa509f11bd6f Mon Sep 17 00:00:00 2001 From: Samuel Githengi Date: Fri, 13 Sep 2019 02:52:49 +0300 Subject: [PATCH 1/6] Save jurisdiction names received after login --- gradle.properties | 2 +- .../domain/jsonmapping/LoginResponseData.java | 3 +++ .../main/java/org/smartregister/service/UserService.java | 8 ++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index f2a3f05f3..409428616 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=1.7.19-SNAPSHOT +VERSION_NAME=1.7.20-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Core Application diff --git a/opensrp-app/src/main/java/org/smartregister/domain/jsonmapping/LoginResponseData.java b/opensrp-app/src/main/java/org/smartregister/domain/jsonmapping/LoginResponseData.java index 06618e790..8a3ee874f 100644 --- a/opensrp-app/src/main/java/org/smartregister/domain/jsonmapping/LoginResponseData.java +++ b/opensrp-app/src/main/java/org/smartregister/domain/jsonmapping/LoginResponseData.java @@ -3,6 +3,8 @@ import org.smartregister.domain.jsonmapping.util.LocationTree; import org.smartregister.domain.jsonmapping.util.TeamMember; +import java.util.List; + /** * Created by keyman on 2/28/2018. */ @@ -12,4 +14,5 @@ public class LoginResponseData { public Time time; public LocationTree locations; public TeamMember team; + public List jurisdictions; } diff --git a/opensrp-app/src/main/java/org/smartregister/service/UserService.java b/opensrp-app/src/main/java/org/smartregister/service/UserService.java index 531e8c1c6..4edcc66cc 100644 --- a/opensrp-app/src/main/java/org/smartregister/service/UserService.java +++ b/opensrp-app/src/main/java/org/smartregister/service/UserService.java @@ -45,6 +45,7 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.Date; +import java.util.List; import java.util.TimeZone; import javax.crypto.Cipher; @@ -58,6 +59,7 @@ import static org.smartregister.AllConstants.KANNADA_LOCALE; import static org.smartregister.AllConstants.OPENSRP_AUTH_USER_URL_PATH; import static org.smartregister.AllConstants.OPENSRP_LOCATION_URL_PATH; +import static org.smartregister.AllConstants.OPERATIONAL_AREAS; import static org.smartregister.event.Event.ON_LOGOUT; public class UserService { @@ -367,6 +369,7 @@ public void remoteLogin(String userName, String password, LoginResponseData user saveDefaultTeam(userName, getUserDefaultTeam(userInfo)); saveDefaultTeamId(userName, getUserDefaultTeamId(userInfo)); saveServerTimeZone(userInfo); + saveJurisdictions(userInfo.jurisdictions); if (loginSuccessful && (StringUtils.isBlank(getUserDefaultLocationId(userInfo)) || StringUtils.isNotBlank(allSharedPreferences.fetchDefaultLocalityId(userName))) && @@ -497,6 +500,11 @@ public void saveAnmTeam(TeamMember anmTeam) { saveANMTeamTask.save(anmTeamString); } + public void saveJurisdictions(List jurisdictions) { + if (jurisdictions != null) + allSharedPreferences.savePreference(OPERATIONAL_AREAS, android.text.TextUtils.join(",", jurisdictions)); + } + public void saveUserInfo(User user) { try { if (user != null && user.getPreferredName() != null) { From 73cfd096abd8512fecfd3aee7653fce2bb740177 Mon Sep 17 00:00:00 2001 From: Samuel Githengi Date: Fri, 13 Sep 2019 02:53:56 +0300 Subject: [PATCH 2/6] Disable sync without location ids --- .../org/smartregister/sync/helper/PlanIntentServiceHelper.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/opensrp-app/src/main/java/org/smartregister/sync/helper/PlanIntentServiceHelper.java b/opensrp-app/src/main/java/org/smartregister/sync/helper/PlanIntentServiceHelper.java index ca7dd20fe..e16160e38 100644 --- a/opensrp-app/src/main/java/org/smartregister/sync/helper/PlanIntentServiceHelper.java +++ b/opensrp-app/src/main/java/org/smartregister/sync/helper/PlanIntentServiceHelper.java @@ -67,6 +67,8 @@ public void syncPlans() { } List locationIds = locationRepository.getAllLocationIds(); + if (locationIds.isEmpty()) + return; Long maxServerVersion = 0l; int BATCH_SIZE = 10; From a86dec83a293b25fb4d3c79104d8bc15de6bf34b Mon Sep 17 00:00:00 2001 From: Samuel Githengi Date: Fri, 13 Sep 2019 03:09:40 +0300 Subject: [PATCH 3/6] Dont save operational areas preference if blank --- .../src/main/java/org/smartregister/service/UserService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opensrp-app/src/main/java/org/smartregister/service/UserService.java b/opensrp-app/src/main/java/org/smartregister/service/UserService.java index 4edcc66cc..e176910df 100644 --- a/opensrp-app/src/main/java/org/smartregister/service/UserService.java +++ b/opensrp-app/src/main/java/org/smartregister/service/UserService.java @@ -501,7 +501,7 @@ public void saveAnmTeam(TeamMember anmTeam) { } public void saveJurisdictions(List jurisdictions) { - if (jurisdictions != null) + if (jurisdictions != null && !jurisdictions.isEmpty()) allSharedPreferences.savePreference(OPERATIONAL_AREAS, android.text.TextUtils.join(",", jurisdictions)); } From b6c475c42e022221015ae5cbf83e44d925b08ca7 Mon Sep 17 00:00:00 2001 From: Samuel Githengi Date: Fri, 13 Sep 2019 11:37:05 +0300 Subject: [PATCH 4/6] Use default trust manager --- .../src/main/java/org/smartregister/ssl/OpensrpSSLHelper.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/opensrp-app/src/main/java/org/smartregister/ssl/OpensrpSSLHelper.java b/opensrp-app/src/main/java/org/smartregister/ssl/OpensrpSSLHelper.java index f6107f7e8..378b88e8e 100644 --- a/opensrp-app/src/main/java/org/smartregister/ssl/OpensrpSSLHelper.java +++ b/opensrp-app/src/main/java/org/smartregister/ssl/OpensrpSSLHelper.java @@ -82,7 +82,8 @@ public javax.net.ssl.SSLSocketFactory getSSLSocketFactory() { tmf.init(trustedKeystore); //Create new SSLContext using our new TrustManagerFactory SSLContext context = SSLContext.getInstance(BuildConfig.SSL_CONTEXT_PROTOCOL); - context.init(null, tmf.getTrustManagers(), null); + context.init(null, null, null); + // context.init(null, tmf.getTrustManagers(), null); //Get a SSLSocketFactory from our SSLContext return context.getSocketFactory(); } catch (Exception e) { From c77eea05d522ed8d9dc7cc1c79f2d1a650514e78 Mon Sep 17 00:00:00 2001 From: Samuel Githengi Date: Fri, 13 Sep 2019 11:56:49 +0300 Subject: [PATCH 5/6] Batch jurisdictions and structures download --- .../sync/helper/LocationServiceHelper.java | 48 +++++++++++-------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/opensrp-app/src/main/java/org/smartregister/sync/helper/LocationServiceHelper.java b/opensrp-app/src/main/java/org/smartregister/sync/helper/LocationServiceHelper.java index 7cf5dda2c..c935b367e 100644 --- a/opensrp-app/src/main/java/org/smartregister/sync/helper/LocationServiceHelper.java +++ b/opensrp-app/src/main/java/org/smartregister/sync/helper/LocationServiceHelper.java @@ -25,6 +25,7 @@ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.MessageFormat; +import java.util.Arrays; import java.util.List; import static org.smartregister.AllConstants.OPERATIONAL_AREAS; @@ -69,21 +70,30 @@ protected List syncLocationsStructures(boolean isJurisdiction) { } if (serverVersion > 0) serverVersion += 1; try { - List parentIds = locationRepository.getAllLocationIds(); - String featureResponse = fetchLocationsOrStructures(isJurisdiction, serverVersion, TextUtils.join(",", parentIds)); - List locations = locationGson.fromJson(featureResponse, new TypeToken>() { - }.getType()); - - for (Location location : locations) { - try { - location.setSyncStatus(BaseRepository.TYPE_Synced); - if (isJurisdiction) - locationRepository.addOrUpdate(location); - else { - structureRepository.addOrUpdate(location); + int BATCH_SIZE = 10; + + List locationFilter = isJurisdiction ? + Arrays.asList(allSharedPreferences.getPreference(OPERATIONAL_AREAS).split(",")) : locationRepository.getAllLocationIds(); + List locations = null; + + for (int i = 0; i < locationFilter.size(); i = i + BATCH_SIZE) { + int lastIndex = i + BATCH_SIZE < locationFilter.size() ? i + BATCH_SIZE : locationFilter.size(); + String locationFilterValue = TextUtils.join(",", locationFilter.subList(i, lastIndex)); + String featureResponse = fetchLocationsOrStructures(isJurisdiction, serverVersion, locationFilterValue); + locations = locationGson.fromJson(featureResponse, new TypeToken>() { + }.getType()); + + for (Location location : locations) { + try { + location.setSyncStatus(BaseRepository.TYPE_Synced); + if (isJurisdiction) + locationRepository.addOrUpdate(location); + else { + structureRepository.addOrUpdate(location); + } + } catch (Exception e) { + e.printStackTrace(); } - } catch (Exception e) { - e.printStackTrace(); } } if (!Utils.isEmptyCollection(locations)) { @@ -99,7 +109,7 @@ protected List syncLocationsStructures(boolean isJurisdiction) { return null; } - private String makeURL(boolean isJurisdiction, long serverVersion, String parentId) { + private String makeURL(boolean isJurisdiction, long serverVersion, String locationFilterValue) { String baseUrl = CoreLibrary.getInstance().context(). configuration().dristhiBaseURL(); String endString = "/"; @@ -109,24 +119,24 @@ private String makeURL(boolean isJurisdiction, long serverVersion, String parent if (isJurisdiction) { String preferenceLocationNames = null; try { - preferenceLocationNames = URLEncoder.encode(allSharedPreferences.getPreference(OPERATIONAL_AREAS), "UTF-8"); + preferenceLocationNames = URLEncoder.encode(locationFilterValue, "UTF-8"); } catch (UnsupportedEncodingException e) { Log.e(getClass().getName(), e.getMessage(), e); } return baseUrl + LOCATION_STRUCTURE_URL + "?is_jurisdiction=" + isJurisdiction + "&location_names=" + preferenceLocationNames + "&serverVersion=" + serverVersion; } - return baseUrl + LOCATION_STRUCTURE_URL + "?parent_id=" + parentId + "&isJurisdiction=" + isJurisdiction + "&serverVersion=" + serverVersion; + return baseUrl + LOCATION_STRUCTURE_URL + "?parent_id=" + locationFilterValue + "&isJurisdiction=" + isJurisdiction + "&serverVersion=" + serverVersion; } - private String fetchLocationsOrStructures(boolean isJurisdiction, Long serverVersion, String parentId) throws Exception { + private String fetchLocationsOrStructures(boolean isJurisdiction, Long serverVersion, String locationFilterValue) throws Exception { HTTPAgent httpAgent = CoreLibrary.getInstance().context().getHttpAgent(); if (httpAgent == null) { throw new IllegalArgumentException(LOCATION_STRUCTURE_URL + " http agent is null"); } - Response resp = httpAgent.fetch(makeURL(isJurisdiction, serverVersion, parentId)); + Response resp = httpAgent.fetch(makeURL(isJurisdiction, serverVersion, locationFilterValue)); if (resp.isFailure()) { throw new NoHttpResponseException(LOCATION_STRUCTURE_URL + " not returned data"); } From ddd40433613c5d4577e1836020a9218565d0dab3 Mon Sep 17 00:00:00 2001 From: Samuel Githengi Date: Mon, 16 Sep 2019 09:46:03 +0300 Subject: [PATCH 6/6] Upgrade library version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 409428616..9d85732e4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=1.7.20-SNAPSHOT +VERSION_NAME=1.7.21-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Core Application