Skip to content

Commit

Permalink
SAK-41141 - Catch and cancel exceptions in FavoritesHandler (sakaipro…
Browse files Browse the repository at this point in the history
  • Loading branch information
jonespm authored and ern committed Jan 3, 2019
1 parent 3dc990e commit c998e74
Showing 1 changed file with 41 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.sakaiproject.site.api.Site;
import org.sakaiproject.tool.api.Session;
import org.sakaiproject.user.cover.PreferencesService;
import lombok.extern.slf4j.Slf4j;
import org.sakaiproject.site.cover.SiteService;
import org.sakaiproject.user.api.Preferences;
import java.util.Collections;
Expand All @@ -45,7 +46,6 @@
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.site.api.SiteService.SelectionType;
import org.sakaiproject.site.api.SiteService.SortType;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
Expand All @@ -56,6 +56,7 @@
* Handles AJAX requests from the "More Sites" drawer.
*
*/
@Slf4j
public class FavoritesHandler extends BasePortalHandler
{
private static final String URL_FRAGMENT = "favorites";
Expand Down Expand Up @@ -188,46 +189,60 @@ private static Set<String> applyAutoFavorites(String userId, ResourceProperties
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());
PreferencesEdit edit = null;
try {
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);
}

PreferencesService.commit(edit);
}
props.removeProperty(FAVORITES_PROPERTY);
for (String siteId : newFavorites) {
props.addPropertyToList(FAVORITES_PROPERTY, siteId);
catch (PermissionException | InUseException | IdUnusedException e) {
log.info("Exception editing user preferences", e);
PreferencesService.cancel(edit);
}

PreferencesService.commit(edit);
}

return newFavorites;
}

private void saveUserFavorites(String userId, UserFavorites favorites) throws PermissionException, InUseException, IdUnusedException, PortalHandlerException {
private void saveUserFavorites(String userId, UserFavorites favorites) throws PortalHandlerException {
if (userId == null) {
return;
}

PreferencesEdit edit = PreferencesService.edit(userId);
ResourcePropertiesEdit props = edit.getPropertiesEdit(org.sakaiproject.user.api.PreferencesService.SITENAV_PREFS_KEY);
PreferencesEdit edit = null;
try {
edit = PreferencesService.edit(userId);
ResourcePropertiesEdit props = edit.getPropertiesEdit(org.sakaiproject.user.api.PreferencesService.SITENAV_PREFS_KEY);

// Replace all existing values
props.removeProperty(FAVORITES_PROPERTY);
// Replace all existing values
props.removeProperty(FAVORITES_PROPERTY);

for (String siteId : favorites.favoriteSiteIds) {
props.addPropertyToList(FAVORITES_PROPERTY, siteId);
}
for (String siteId : favorites.favoriteSiteIds) {
props.addPropertyToList(FAVORITES_PROPERTY, siteId);
}

props.removeProperty(AUTO_FAVORITE_ENABLED_PROPERTY);
props.addProperty(AUTO_FAVORITE_ENABLED_PROPERTY, String.valueOf(favorites.autoFavoritesEnabled));
props.removeProperty(AUTO_FAVORITE_ENABLED_PROPERTY);
props.addProperty(AUTO_FAVORITE_ENABLED_PROPERTY, String.valueOf(favorites.autoFavoritesEnabled));

PreferencesService.commit(edit);
PreferencesService.commit(edit);
}
catch (PermissionException | InUseException | IdUnusedException e) {
log.info("Exception editing user preferences", e);
PreferencesService.cancel(edit);
}
}

public static class UserFavorites {
Expand Down

0 comments on commit c998e74

Please sign in to comment.