class PlanIntentServiceHelper extends BaseHelper {

    private int batchFetchPlansFromServer(boolean returnCount) {
        int batchFetchCount = 0;
        try {
            long serverVersion = 0;
            try {
                serverVersion = Long.parseLong(allSharedPreferences.getPreference(PLAN_LAST_SYNC_DATE));
            } catch (NumberFormatException e) {
                Timber.e(e, "EXCEPTION %s", e.toString());
            }
            if (serverVersion > 0) {
                serverVersion += 1;
            }
            // fetch and save plans
            Long maxServerVersion = 0l;

            String organizationIds = allSharedPreferences.getPreference(AllConstants.ORGANIZATION_IDS);
            String plansResponse = fetchPlans(Arrays.asList(organizationIds.split(",")), serverVersion, returnCount);
            List<PlanDefinition> plans = gson.fromJson(plansResponse, new TypeToken<List<PlanDefinition>>() {
            }.getType());
            for (PlanDefinition plan : plans) {
                try {
                    planDefinitionRepository.addOrUpdate(plan);
                } catch (Exception e) {
                    Timber.e(e, "EXCEPTION %s", e.toString());
                }
            }
            // update most recent server version
            if (!Utils.isEmptyCollection(plans)) {
                batchFetchCount = plans.size();
                allSharedPreferences.savePreference(PLAN_LAST_SYNC_DATE, String.valueOf(getPlanDefinitionMaxServerVersion(plans, maxServerVersion)));

                syncProgress.setPercentageSynced(Utils.calculatePercentage(totalRecords, batchFetchCount));
                sendSyncProgressBroadcast(syncProgress, context);

                // retry fetch since there were items synced from the server
                batchFetchPlansFromServer(false);
            }
        } catch (Exception e) {
            Timber.e(e, "EXCEPTION %s", e.toString());
        }

        return batchFetchCount;
    }

    private  PlanIntentServiceHelper(PlanDefinitionRepository planRepository, LocationRepository locationRepository);

}