Search in sources :

Example 16 with IUserLayoutNodeDescription

use of org.apereo.portal.layout.node.IUserLayoutNodeDescription 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)

Example 17 with IUserLayoutNodeDescription

use of org.apereo.portal.layout.node.IUserLayoutNodeDescription in project uPortal by Jasig.

the class UpdatePreferencesServlet method updatePermissions.

@RequestMapping(method = RequestMethod.POST, params = "action=updatePermissions")
public ModelAndView updatePermissions(HttpServletRequest request, HttpServletResponse response) throws IOException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    String elementId = request.getParameter("elementID");
    IUserLayoutNodeDescription node = ulm.getNode(elementId);
    if (node == null) {
        logger.warn("Failed to locate node for permissions update");
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return new ModelAndView("jsonView", Collections.singletonMap("error", "Invalid node id " + elementId));
    }
    String deletable = request.getParameter("deletable");
    if (!StringUtils.isBlank(deletable)) {
        node.setDeleteAllowed(Boolean.valueOf(deletable));
    }
    String movable = request.getParameter("movable");
    if (!StringUtils.isBlank(movable)) {
        node.setMoveAllowed(Boolean.valueOf(movable));
    }
    String editable = request.getParameter("editable");
    if (!StringUtils.isBlank(editable)) {
        node.setEditAllowed(Boolean.valueOf(editable));
    }
    String canAddChildren = request.getParameter("addChildAllowed");
    if (!StringUtils.isBlank(canAddChildren)) {
        node.setAddChildAllowed(Boolean.valueOf(canAddChildren));
    }
    ulm.updateNode(node);
    try {
        // save the user's layout
        ulm.saveUserLayout();
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
    return new ModelAndView("jsonView", Collections.EMPTY_MAP);
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) ModelAndView(org.springframework.web.servlet.ModelAndView) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 18 with IUserLayoutNodeDescription

use of org.apereo.portal.layout.node.IUserLayoutNodeDescription in project uPortal by Jasig.

the class UpdatePreferencesServlet method addPortlet.

/**
     * Add a new channel.
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws PortalException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=addPortlet")
public ModelAndView addPortlet(HttpServletRequest request, HttpServletResponse response) throws IOException, PortalException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    final Locale locale = RequestContextUtils.getLocale(request);
    // gather the parameters we need to move a channel
    String destinationId = request.getParameter("elementID");
    String sourceId = request.getParameter("channelID");
    String method = request.getParameter("position");
    String fname = request.getParameter("fname");
    if (destinationId == null) {
        String tabName = request.getParameter("tabName");
        if (tabName != null) {
            destinationId = getTabIdFromName(ulm.getUserLayout(), tabName);
        }
    }
    IPortletDefinition definition = null;
    if (sourceId != null)
        definition = portletDefinitionRegistry.getPortletDefinition(sourceId);
    else if (fname != null)
        definition = portletDefinitionRegistry.getPortletDefinitionByFname(fname);
    else {
        logger.error("SourceId or fname invalid when adding a portlet");
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return new ModelAndView("jsonView", Collections.singletonMap("error", "SourceId or fname invalid"));
    }
    IUserLayoutChannelDescription channel = new UserLayoutChannelDescription(definition);
    IUserLayoutNodeDescription node = null;
    if (isTab(ulm, destinationId)) {
        node = addNodeToTab(ulm, channel, destinationId);
    } else {
        boolean isInsert = method != null && method.equals("insertBefore");
        //If neither an insert or type folder - Can't "insert into" non-folder
        if (!(isInsert || isFolder(ulm, destinationId))) {
            logger.error("Cannot insert into portlet element");
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return new ModelAndView("jsonView", Collections.singletonMap("error", "Cannot insert into portlet element"));
        }
        String siblingId = isInsert ? destinationId : null;
        String target = isInsert ? ulm.getParentId(destinationId) : destinationId;
        // move the channel into the column
        node = ulm.addNode(channel, target, siblingId);
    }
    if (node == null) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return new ModelAndView("jsonView", Collections.singletonMap("error", getMessage("error.add.element", "Unable to add element", locale)));
    }
    String nodeId = node.getId();
    try {
        // save the user's layout
        ulm.saveUserLayout();
        if (addedWindowState != null) {
            IPortletWindow portletWindow = this.portletWindowRegistry.getOrCreateDefaultPortletWindowByFname(request, channel.getFunctionalName());
            portletWindow.setWindowState(addedWindowState);
            this.portletWindowRegistry.storePortletWindow(request, portletWindow);
        }
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
    Map<String, String> model = new HashMap<String, String>();
    model.put("response", getMessage("success.add.portlet", "Added a new channel", locale));
    model.put("newNodeId", nodeId);
    return new ModelAndView("jsonView", model);
}
Also used : Locale(java.util.Locale) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) IUserLayoutChannelDescription(org.apereo.portal.layout.node.IUserLayoutChannelDescription) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) IPortletWindow(org.apereo.portal.portlet.om.IPortletWindow) IUserInstance(org.apereo.portal.user.IUserInstance) 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)

Example 19 with IUserLayoutNodeDescription

use of org.apereo.portal.layout.node.IUserLayoutNodeDescription in project uPortal by Jasig.

the class UpdatePreferencesServlet method addFavorite.

@RequestMapping(method = RequestMethod.POST, params = "action=addFavorite")
public ModelAndView addFavorite(@RequestParam String channelId, HttpServletRequest request, HttpServletResponse response) throws IOException {
    final IUserInstance ui = userInstanceManager.getUserInstance(request);
    final IPerson person = getPerson(ui, response);
    final IPortletDefinition pdef = portletDefinitionRegistry.getPortletDefinition(channelId);
    final Locale locale = RequestContextUtils.getLocale(request);
    final IAuthorizationPrincipal authPrincipal = this.getUserPrincipal(person.getUserName());
    final String targetString = PermissionHelper.permissionTargetIdForPortletDefinition(pdef);
    if (!authPrincipal.hasPermission(IPermission.PORTAL_SYSTEM, IPermission.PORTLET_FAVORITE_ACTIVITY, targetString)) {
        logger.warn("Unauthorized attempt to favorite portlet '{}' through the REST API by user '{}'", pdef.getFName(), person.getUserName());
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("error.favorite.not.permitted", "Favorite not permitted", locale)));
    }
    final UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    final IUserLayoutManager ulm = upm.getUserLayoutManager();
    final IUserLayoutChannelDescription channel = new UserLayoutChannelDescription(pdef);
    //get favorite tab
    final String favoriteTabNodeId = FavoritesUtils.getFavoriteTabNodeId(ulm.getUserLayout());
    if (favoriteTabNodeId != null) {
        //add portlet to favorite tab
        final IUserLayoutNodeDescription node = addNodeToTab(ulm, channel, favoriteTabNodeId);
        if (node == null) {
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("error.add.portlet.in.tab", "Can''t add a new favorite", locale)));
        }
        try {
            // save the user's layout
            ulm.saveUserLayout();
        } catch (PortalException e) {
            return handlePersistError(request, response, e);
        }
        //document success for notifications
        final Map<String, String> model = new HashMap<String, String>();
        final String channelTitle = channel.getTitle();
        model.put("response", getMessage("favorites.added.favorite", channelTitle, "Added " + channelTitle + " as a favorite.", locale));
        model.put("newNodeId", node.getId());
        return new ModelAndView("jsonView", model);
    } else {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("error.finding.favorite.tab", "Can''t find favorite tab", locale)));
    }
}
Also used : Locale(java.util.Locale) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) IUserLayoutChannelDescription(org.apereo.portal.layout.node.IUserLayoutChannelDescription) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) IAuthorizationPrincipal(org.apereo.portal.security.IAuthorizationPrincipal) 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)

Example 20 with IUserLayoutNodeDescription

use of org.apereo.portal.layout.node.IUserLayoutNodeDescription in project uPortal by Jasig.

the class UpdatePreferencesServlet method changeColumns.

/**
     * Change the number of columns on a specified tab. In the event that the user is decreasing the
     * number of columns, extra columns will be stripped from the right-hand side. Any channels in
     * these columns will be moved to the bottom of the last preserved column.
     *
     * @param widths array of column widths
     * @param deleted array of deleted column IDs
     * @param acceptor not sure what this is
     * @param request HttpRequest
     * @param response HttpResponse
     * @throws IOException
     * @throws PortalException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=changeColumns")
public ModelAndView changeColumns(HttpServletRequest request, HttpServletResponse response, @RequestParam("tabId") String tabId, @RequestParam("widths[]") String[] widths, @RequestParam(value = "deleted[]", required = false) String[] deleted, @RequestParam(value = "acceptor", required = false) String acceptor) throws IOException, PortalException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    IPerson per = getPerson(ui, response);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    int newColumnCount = widths.length;
    // build a list of the current columns for this tab
    Enumeration<String> columns = ulm.getChildIds(tabId);
    List<String> columnList = new ArrayList<String>();
    while (columns.hasMoreElements()) {
        columnList.add(columns.nextElement());
    }
    int oldColumnCount = columnList.size();
    Map<String, Object> model = new HashMap<String, Object>();
    // if the new layout has more columns
    if (newColumnCount > oldColumnCount) {
        List<String> newColumnIds = new ArrayList<String>();
        for (int i = columnList.size(); i < newColumnCount; i++) {
            // create new column element
            IUserLayoutFolderDescription newColumn = new UserLayoutFolderDescription();
            newColumn.setName("Column");
            newColumn.setId("tbd");
            newColumn.setFolderType(IUserLayoutFolderDescription.REGULAR_TYPE);
            newColumn.setHidden(false);
            newColumn.setUnremovable(false);
            newColumn.setImmutable(false);
            // add the column to our layout
            IUserLayoutNodeDescription node = ulm.addNode(newColumn, tabId, null);
            newColumnIds.add(node.getId());
            model.put("newColumnIds", newColumnIds);
            columnList.add(node.getId());
        }
    } else // if the new layout has fewer columns
    if (deleted != null && deleted.length > 0) {
        if (columnList.size() != widths.length + deleted.length) {
        // TODO: error?
        }
        for (String columnId : deleted) {
            // move all channels in the current column to the last valid column
            Enumeration channels = ulm.getChildIds(columnId);
            while (channels.hasMoreElements()) {
                ulm.addNode(ulm.getNode((String) channels.nextElement()), acceptor, null);
            }
            // delete the column from the user's layout
            ulm.deleteNode(columnId);
            columnList.remove(columnId);
        }
    }
    int count = 0;
    for (String columnId : columnList) {
        this.stylesheetUserPreferencesService.setLayoutAttribute(request, PreferencesScope.STRUCTURE, columnId, "width", widths[count] + "%");
        try {
            // This sets the column attribute in memory but doesn't persist it.  Comment says saves changes "prior to persisting"
            Element folder = ulm.getUserLayoutDOM().getElementById(columnId);
            UserPrefsHandler.setUserPreference(folder, "width", per);
        } catch (Exception e) {
            logger.error("Error saving new column widths", e);
        }
        count++;
    }
    try {
        ulm.saveUserLayout();
    } catch (PortalException e) {
        logger.warn("Error saving layout", e);
    }
    return new ModelAndView("jsonView", model);
}
Also used : IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ModelAndView(org.springframework.web.servlet.ModelAndView) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription) UserLayoutFolderDescription(org.apereo.portal.layout.node.UserLayoutFolderDescription) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) XPathExpressionException(javax.xml.xpath.XPathExpressionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PortalException(org.apereo.portal.PortalException) IOException(java.io.IOException) IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

IUserLayoutNodeDescription (org.apereo.portal.layout.node.IUserLayoutNodeDescription)30 PortalException (org.apereo.portal.PortalException)14 IUserLayoutManager (org.apereo.portal.layout.IUserLayoutManager)14 IUserInstance (org.apereo.portal.user.IUserInstance)12 IUserLayoutFolderDescription (org.apereo.portal.layout.node.IUserLayoutFolderDescription)11 UserPreferencesManager (org.apereo.portal.UserPreferencesManager)8 IUserLayoutChannelDescription (org.apereo.portal.layout.node.IUserLayoutChannelDescription)8 Element (org.w3c.dom.Element)7 IUserPreferencesManager (org.apereo.portal.IUserPreferencesManager)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 ModelAndView (org.springframework.web.servlet.ModelAndView)6 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)5 Document (org.w3c.dom.Document)5 Node (org.w3c.dom.Node)5 ArrayList (java.util.ArrayList)4 Enumeration (java.util.Enumeration)4 HashMap (java.util.HashMap)4 Locale (java.util.Locale)4 UserLayoutChannelDescription (org.apereo.portal.layout.node.UserLayoutChannelDescription)4 List (java.util.List)3