Skip to content

Commit

Permalink
Merge pull request #331 from OpenSRP/save-jurisdiction-from-server
Browse files Browse the repository at this point in the history
Save jurisdiction from server and Other Fixes for Namibia Release
  • Loading branch information
githengi authored Sep 16, 2019
2 parents f17b4a8 + ddd4043 commit a6eae15
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -12,4 +14,5 @@ public class LoginResponseData {
public Time time;
public LocationTree locations;
public TeamMember team;
public List<String> jurisdictions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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))) &&
Expand Down Expand Up @@ -497,6 +500,11 @@ public void saveAnmTeam(TeamMember anmTeam) {
saveANMTeamTask.save(anmTeamString);
}

public void saveJurisdictions(List<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,21 +70,30 @@ protected List<Location> syncLocationsStructures(boolean isJurisdiction) {
}
if (serverVersion > 0) serverVersion += 1;
try {
List<String> parentIds = locationRepository.getAllLocationIds();
String featureResponse = fetchLocationsOrStructures(isJurisdiction, serverVersion, TextUtils.join(",", parentIds));
List<Location> locations = locationGson.fromJson(featureResponse, new TypeToken<List<Location>>() {
}.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<String> locationFilter = isJurisdiction ?
Arrays.asList(allSharedPreferences.getPreference(OPERATIONAL_AREAS).split(",")) : locationRepository.getAllLocationIds();
List<Location> 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<List<Location>>() {
}.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)) {
Expand All @@ -99,7 +109,7 @@ protected List<Location> 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 = "/";
Expand All @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public void syncPlans() {
}

List<String> locationIds = locationRepository.getAllLocationIds();
if (locationIds.isEmpty())
return;
Long maxServerVersion = 0l;

int BATCH_SIZE = 10;
Expand Down

0 comments on commit a6eae15

Please sign in to comment.