Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LocationTree NullPointerException #839

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=4.3.23-SNAPSHOT
VERSION_NAME=4.3.24-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
@@ -1,5 +1,27 @@
package org.smartregister.sync.helper;

import static org.smartregister.AllConstants.COUNT;
import static org.smartregister.AllConstants.JURISDICTION_IDS;
import static org.smartregister.AllConstants.LocationConstants.DISPLAY;
import static org.smartregister.AllConstants.LocationConstants.LOCATION;
import static org.smartregister.AllConstants.LocationConstants.LOCATIONS;
import static org.smartregister.AllConstants.LocationConstants.SPECIAL_TAG_FOR_OPENMRS_TEAM_MEMBERS;
import static org.smartregister.AllConstants.LocationConstants.TEAM;
import static org.smartregister.AllConstants.LocationConstants.UUID;
import static org.smartregister.AllConstants.OPERATIONAL_AREAS;
import static org.smartregister.AllConstants.PerformanceMonitoring.ACTION;
import static org.smartregister.AllConstants.PerformanceMonitoring.FETCH;
import static org.smartregister.AllConstants.PerformanceMonitoring.LOCATION_SYNC;
import static org.smartregister.AllConstants.PerformanceMonitoring.PUSH;
import static org.smartregister.AllConstants.PerformanceMonitoring.STRUCTURE;
import static org.smartregister.AllConstants.RETURN_COUNT;
import static org.smartregister.AllConstants.TYPE;
import static org.smartregister.util.PerformanceMonitoringUtils.addAttribute;
import static org.smartregister.util.PerformanceMonitoringUtils.clearTraceAttributes;
import static org.smartregister.util.PerformanceMonitoringUtils.initTrace;
import static org.smartregister.util.PerformanceMonitoringUtils.startTrace;
import static org.smartregister.util.PerformanceMonitoringUtils.stopTrace;

import android.content.Context;
import android.text.TextUtils;

Expand Down Expand Up @@ -45,28 +67,6 @@

import timber.log.Timber;

import static org.smartregister.AllConstants.COUNT;
import static org.smartregister.AllConstants.JURISDICTION_IDS;
import static org.smartregister.AllConstants.LocationConstants.DISPLAY;
import static org.smartregister.AllConstants.LocationConstants.LOCATION;
import static org.smartregister.AllConstants.LocationConstants.LOCATIONS;
import static org.smartregister.AllConstants.LocationConstants.SPECIAL_TAG_FOR_OPENMRS_TEAM_MEMBERS;
import static org.smartregister.AllConstants.LocationConstants.TEAM;
import static org.smartregister.AllConstants.LocationConstants.UUID;
import static org.smartregister.AllConstants.OPERATIONAL_AREAS;
import static org.smartregister.AllConstants.PerformanceMonitoring.ACTION;
import static org.smartregister.AllConstants.PerformanceMonitoring.FETCH;
import static org.smartregister.AllConstants.PerformanceMonitoring.LOCATION_SYNC;
import static org.smartregister.AllConstants.PerformanceMonitoring.PUSH;
import static org.smartregister.AllConstants.PerformanceMonitoring.STRUCTURE;
import static org.smartregister.AllConstants.RETURN_COUNT;
import static org.smartregister.AllConstants.TYPE;
import static org.smartregister.util.PerformanceMonitoringUtils.addAttribute;
import static org.smartregister.util.PerformanceMonitoringUtils.clearTraceAttributes;
import static org.smartregister.util.PerformanceMonitoringUtils.initTrace;
import static org.smartregister.util.PerformanceMonitoringUtils.startTrace;
import static org.smartregister.util.PerformanceMonitoringUtils.stopTrace;

public class LocationServiceHelper extends BaseHelper {

public static final String LOCATION_STRUCTURE_URL = "/rest/location/sync";
Expand Down Expand Up @@ -467,22 +467,33 @@ public void fetchAllLocations(int recordCount) {
}.getType()
);

for (Location location : locations) {
try {
location.setSyncStatus(BaseRepository.TYPE_Synced);

locationRepository.addOrUpdate(location);

for (LocationTag tag : location.getLocationTags()) {
LocationTag locationTag = new LocationTag();
locationTag.setLocationId(location.getId());
locationTag.setName(tag.getName());
if (locations != null) {
for (Location location : locations) {
try {
location.setSyncStatus(BaseRepository.TYPE_Synced);

locationTagRepository.addOrUpdate(locationTag);
locationRepository.addOrUpdate(location);
if (location != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this null check necessary? Is there a possibility of any of the returned locations being null?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, removed it

if (location.getLocationTags() != null) {
for (LocationTag tag : location.getLocationTags()) {
LocationTag locationTag = new LocationTag();
locationTag.setLocationId(location.getId());
locationTag.setName(tag.getName());

locationTagRepository.addOrUpdate(locationTag);
}
} else {
Timber.e("Location tag is null");
}
} else {
Timber.e("Location is null");
}
} catch (Exception e) {
Timber.e(e, "EXCEPTION %s", e.toString());
}
} catch (Exception e) {
Timber.e(e, "EXCEPTION %s", e.toString());
}
}else {
Timber.e("Locations are null");
}
} catch (Exception e) {
Timber.e(e, "EXCEPTION %s", e.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,12 @@ protected void removeLocationsFromHierarchy(LocationTree locationTree, Set<Strin
private boolean hasNewAssignments(UserAssignmentDTO currentUserAssignment, Set<Long> existingOrganizations, Set<String> existingJurisdictions) {
if (existingJurisdictions.isEmpty()) {
LocationTree locationTree = gson.fromJson(settingsRepository.fetchANMLocation(), LocationTree.class);
for (String location : currentUserAssignment.getJurisdictions()) {
if (!locationTree.hasLocation(location)) return true;
if (locationTree != null) {
for (String location : currentUserAssignment.getJurisdictions()) {
if (!locationTree.hasLocation(location)) return true;
}
} else {
Timber.e("hasNewAssignments(): The location Tree is Null");
}
return false;
}
Expand Down