private void linkOptionsWithOptionSets(List<OptionSet> optionSets) { // Building option to optionset relationship. if (optionSets != null && !optionSets.isEmpty()) { for (OptionSet optionSet : optionSets) { if (optionSet == null || optionSet.getOptions() == null) { continue; } int sortOrder = 0; for (Option option : optionSet.getOptions()) { option.setUId( optionSet.getUId() + option .getCode()); // options don't have uid, but uid is used in createOperations option.setLastUpdated(new DateTime()); // same with these dates option.setCreated(new DateTime()); option.setOptionSet(optionSet.getUId()); option.setSortOrder(sortOrder); sortOrder++; } } } }
private void getOptionSetDataFromServer() throws ApiException { ResourceType resource = ResourceType.OPTION_SETS; DateTime serverTime = systemInfoApiClient.getSystemInfo().getServerDate(); DateTime lastUpdated = lastUpdatedPreferences.get(resource); List<OptionSet> allOptionSets = optionSetApiClient.getBasicOptionSets(null); List<OptionSet> updatedOptionSets = optionSetApiClient.getFullOptionSets(lastUpdated); linkOptionsWithOptionSets(updatedOptionSets); List<OptionSet> existingPersistedAndUpdatedOptionSets = modelUtils.merge(allOptionSets, updatedOptionSets, mOptionSetStore.queryAll()); List<IDbOperation> operations = new ArrayList<>(); List<OptionSet> persistedOptionSets = mOptionSetStore.queryAll(); if (existingPersistedAndUpdatedOptionSets != null && !existingPersistedAndUpdatedOptionSets.isEmpty()) { for (OptionSet optionSet : existingPersistedAndUpdatedOptionSets) { if (optionSet == null || optionSet.getOptions() == null) { continue; } OptionSet persistedOptionSet = mOptionSetStore.queryByUid(optionSet.getUId()); List<Option> persistedOptions; if (persistedOptionSet != null) { persistedOptions = persistedOptionSet.getOptions(); } else { persistedOptions = new ArrayList<>(); } operations.addAll( transactionManager.createOperations( mOptionStore, persistedOptions, optionSet.getOptions())); } } operations.addAll( transactionManager.createOperations( mOptionSetStore, persistedOptionSets, existingPersistedAndUpdatedOptionSets)); // DbUtils.applyBatch(operations); transactionManager.transact(operations); lastUpdatedPreferences.save(ResourceType.OPTION_SETS, serverTime); }