* @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())) {
log.warn("Error deleting the node" + channelId + "from favorites for user " + (upm.getPerson() == null ? "unknown" : upm.getPerson().getID()));
response.setStatus(HttpServletResponse.SC_ACCEPTED);
return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("error.remove.favorite", "Can''t remove favorite", locale)));
}
// save the user's layout
ulm.saveUserLayout();