class LocationServiceHelper extends BaseHelper {

    public void fetchLocationsByLevelAndTags() throws Exception {

        HTTPAgent httpAgent = getHttpAgent();

        if (httpAgent == null) {
            throw new IllegalArgumentException(COMMON_LOCATIONS_SERVICE_URL + " http agent is null");
        }

        String baseUrl = getFormattedBaseUrl();

        SyncConfiguration configs = getSyncConfiguration();

        JSONObject requestPayload = new JSONObject();
        requestPayload.put("locationUUID", allSharedPreferences.fetchDefaultLocalityId(allSharedPreferences.fetchRegisteredANM()));
        requestPayload.put("locationTopLevel", configs.getTopAllowedLocationLevel());
        requestPayload.put("locationTagsQueried", new JSONArray(new Gson().toJson(configs.getSynchronizedLocationTags())));

        Response<String> resp = httpAgent.post(
                MessageFormat.format("{0}{1}",
                        baseUrl,
                        COMMON_LOCATIONS_SERVICE_URL),
                requestPayload.toString());

        if (resp.isFailure()) {
            throw new NoHttpResponseException(COMMON_LOCATIONS_SERVICE_URL + " not returned data");
        }

        List<org.smartregister.domain.jsonmapping.Location> receivedOpenMrsLocations =
                new Gson().fromJson(resp.payload(),
                        new TypeToken<List<org.smartregister.domain.jsonmapping.Location>>() {
                        }.getType());

        for (org.smartregister.domain.jsonmapping.Location openMrsLocation : receivedOpenMrsLocations) {
            Location location = new Location();
            location.setId(openMrsLocation.getLocationId());
            LocationProperty property = new LocationProperty();
            property.setUid(openMrsLocation.getLocationId());
            property.setParentId(openMrsLocation.getParentLocation().getLocationId());
            property.setName(openMrsLocation.getName());
            location.setProperties(property);

            locationRepository.addOrUpdate(location);


            for (String tagName : openMrsLocation.getTags()) {
                LocationTag locationTag = new LocationTag();
                locationTag.setLocationId(openMrsLocation.getLocationId());
                locationTag.setName(tagName);

                locationTagRepository.addOrUpdate(locationTag);
            }
        }
    }

    public  LocationServiceHelper(LocationRepository locationRepository, LocationTagRepository locationTagRepository, StructureRepository structureRepository);

}