Skip to content

Commit

Permalink
SAK-40723 - Unnecessary updating database on applyAutofavorites (saka…
Browse files Browse the repository at this point in the history
  • Loading branch information
SedueRey authored and Miguel Pellicer committed Oct 3, 2018
1 parent d0f70c5 commit 2612023
Showing 1 changed file with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,36 +174,38 @@ private static Set<String> applyAutoFavorites(String userId, ResourceProperties
// Take the default
}

// Update our list of seen sites
PreferencesEdit edit = PreferencesService.edit(userId);
ResourcePropertiesEdit props = edit.getPropertiesEdit(org.sakaiproject.user.api.PreferencesService.SITENAV_PREFS_KEY);

// This should not call getUserSites(boolean, boolean) because the property is variable, while the call is cacheable otherwise
List<Site> userSites = SiteService.getSites(SelectionType.MEMBER, null, null, null, SortType.TITLE_ASC, null, false);
Set<String> newFavorites = new LinkedHashSet<String>();

props.removeProperty(SEEN_SITES_PROPERTY);
for (Site userSite : userSites) {
if (!oldSiteSet.contains(userSite.getId()) && !existingFavorites.contains(userSite.getId()) && !firstTimeFavs) {
newFavorites.add(userSite.getId());
}
props.addPropertyToList(SEEN_SITES_PROPERTY, userSite.getId());
}
newFavorites.addAll(existingFavorites);

if (firstTimeFavs) {
props.removeProperty(FIRST_TIME_PROPERTY);
props.addProperty(FIRST_TIME_PROPERTY, String.valueOf(false));
}
if( !newFavorites.equals(existingFavorites) || firstTimeFavs ) {
// There are new favourites and need to update database.
// We will not lock database if it's not neccessary
PreferencesEdit edit = PreferencesService.edit(userId);
ResourcePropertiesEdit props = edit.getPropertiesEdit(org.sakaiproject.user.api.PreferencesService.SITENAV_PREFS_KEY);
if (firstTimeFavs) {
props.removeProperty(FIRST_TIME_PROPERTY);
props.addProperty(FIRST_TIME_PROPERTY, String.valueOf(false));
}
props.removeProperty(SEEN_SITES_PROPERTY);
for (Site userSite : userSites) {
props.addPropertyToList(SEEN_SITES_PROPERTY, userSite.getId());
}
props.removeProperty(FAVORITES_PROPERTY);
for (String siteId : newFavorites) {
props.addPropertyToList(FAVORITES_PROPERTY, siteId);
}

// Add our new properties and any existing favorite sites after them
props.removeProperty(FAVORITES_PROPERTY);
for (String siteId : newFavorites) {
props.addPropertyToList(FAVORITES_PROPERTY, siteId);
PreferencesService.commit(edit);
}

PreferencesService.commit(edit);

return newFavorites;
}

Expand Down

0 comments on commit 2612023

Please sign in to comment.