Search in sources :

Example 1 with EqualPredicate

use of org.apache.commons.collections.functors.EqualPredicate in project uPortal by Jasig.

the class UpdatePreferencesServlet method removeFavorite.

/**
     * This method removes the channelId specified from favorites. Note that even if you pass in the
     * layout channel id, it will always remove from the favorites.
     *
     * @param channelId The long channel ID that is used to determine which fname to remove from
     *     favorites
     * @param request
     * @param response
     * @return returns a mav object with a response attribute for noty
     * @throws IOException if it has problem reading the layout file.
     */
@RequestMapping(method = RequestMethod.POST, params = "action=removeFavorite")
public ModelAndView removeFavorite(@RequestParam String channelId, HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserPreferencesManager upm = (UserPreferencesManager) userInstanceManager.getUserInstance(request).getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    final Locale locale = RequestContextUtils.getLocale(request);
    IPortletDefinition portletDefinition = portletDefinitionRegistry.getPortletDefinition(channelId);
    if (portletDefinition != null && StringUtils.isNotBlank(portletDefinition.getFName())) {
        String functionalName = portletDefinition.getFName();
        List<IUserLayoutNodeDescription> favoritePortlets = FavoritesUtils.getFavoritePortlets(ulm.getUserLayout());
        //search for the favorite to delete
        EqualPredicate nameEqlPredicate = new EqualPredicate(functionalName);
        Object result = CollectionUtils.find(favoritePortlets, new BeanPredicate("functionalName", nameEqlPredicate));
        if (result != null && result instanceof UserLayoutChannelDescription) {
            UserLayoutChannelDescription channelDescription = (UserLayoutChannelDescription) result;
            try {
                if (!ulm.deleteNode(channelDescription.getChannelSubscribeId())) {
                    logger.warn("Error deleting the node" + channelId + "from favorites for user " + (upm.getPerson() == null ? "unknown" : upm.getPerson().getID()));
                    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                    return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("error.remove.favorite", "Can''t remove favorite", locale)));
                }
                // save the user's layout
                ulm.saveUserLayout();
            } catch (PortalException e) {
                return handlePersistError(request, response, e);
            }
            //document success for notifications
            Map<String, String> model = new HashMap<String, String>();
            model.put("response", getMessage("success.remove.portlet", "Removed from Favorites successfully", locale));
            return new ModelAndView("jsonView", model);
        }
    }
    // save the user's layout
    ulm.saveUserLayout();
    return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("error.finding.favorite", "Can''t find favorite", locale)));
}
Also used : Locale(java.util.Locale) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) EqualPredicate(org.apache.commons.collections.functors.EqualPredicate) BeanPredicate(org.apache.commons.beanutils.BeanPredicate) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) PortalException(org.apereo.portal.PortalException) IUserLayoutChannelDescription(org.apereo.portal.layout.node.IUserLayoutChannelDescription) UserLayoutChannelDescription(org.apereo.portal.layout.node.UserLayoutChannelDescription) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) IPortletDefinition(org.apereo.portal.portlet.om.IPortletDefinition) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 BeanPredicate (org.apache.commons.beanutils.BeanPredicate)1 EqualPredicate (org.apache.commons.collections.functors.EqualPredicate)1 PortalException (org.apereo.portal.PortalException)1 UserPreferencesManager (org.apereo.portal.UserPreferencesManager)1 IUserLayoutManager (org.apereo.portal.layout.IUserLayoutManager)1 IUserLayoutChannelDescription (org.apereo.portal.layout.node.IUserLayoutChannelDescription)1 IUserLayoutNodeDescription (org.apereo.portal.layout.node.IUserLayoutNodeDescription)1 UserLayoutChannelDescription (org.apereo.portal.layout.node.UserLayoutChannelDescription)1 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1