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 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..e176910df 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 && !jurisdictions.isEmpty()) + allSharedPreferences.savePreference(OPERATIONAL_AREAS, android.text.TextUtils.join(",", jurisdictions)); + } + public void saveUserInfo(User user) { try { if (user != null && user.getPreferredName() != null) { 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) { 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"); } 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;