Search in sources :

Example 1 with IUserLayoutFolderDescription

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

the class FavoritesUtils method getFavoritePortlets.

/**
     * Get the portlets that are in the folder(s) of type "favorites".
     *
     * @param userLayout
     * @return
     */
@SuppressWarnings("unchecked")
public static List<IUserLayoutNodeDescription> getFavoritePortlets(IUserLayout userLayout) {
    logger.trace("Extracting favorite portlets from layout [{}]", userLayout);
    List<IUserLayoutNodeDescription> favorites = new LinkedList<IUserLayoutNodeDescription>();
    Enumeration<String> childrenOfRoot = userLayout.getChildIds(userLayout.getRootId());
    while (childrenOfRoot.hasMoreElements()) {
        //loop over folders that might be the favorites folder
        String nodeId = childrenOfRoot.nextElement();
        try {
            IUserLayoutNodeDescription nodeDescription = userLayout.getNodeDescription(nodeId);
            String parentId = userLayout.getParentId(nodeId);
            String nodeName = nodeDescription.getName();
            IUserLayoutNodeDescription.LayoutNodeType nodeType = nodeDescription.getType();
            if (FOLDER.equals(nodeDescription.getType()) && nodeDescription instanceof IUserLayoutFolderDescription) {
                IUserLayoutFolderDescription folderDescription = (IUserLayoutFolderDescription) nodeDescription;
                if (FAVORITES_TYPE.equalsIgnoreCase(folderDescription.getFolderType())) {
                    // TODO: assumes columns structure, but should traverse tree to collect all portlets regardless
                    Enumeration<String> columns = userLayout.getChildIds(nodeId);
                    //loop through columns to gather beloved portlets
                    while (columns.hasMoreElements()) {
                        String column = (String) columns.nextElement();
                        Enumeration<String> portlets = userLayout.getChildIds(column);
                        while (portlets.hasMoreElements()) {
                            String portlet = (String) portlets.nextElement();
                            IUserLayoutNodeDescription portletDescription = userLayout.getNodeDescription(portlet);
                            favorites.add(portletDescription);
                        }
                    }
                } else {
                    logger.trace("Ignoring non-favorites folder node [{}]", nodeDescription);
                }
            } else {
                logger.trace("Ignoring non-folder node [{}]", nodeDescription);
            }
        } catch (Exception e) {
            logger.error("Ignoring on error a node while examining for favorites: node ID is [{}]", nodeId, e);
        }
    }
    logger.debug("Extracted favorite portlets [{}] from [{}]", favorites, userLayout);
    return favorites;
}
Also used : IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) LinkedList(java.util.LinkedList) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription)

Example 2 with IUserLayoutFolderDescription

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

the class UpdatePreferencesServlet method addTab.

/**
     * Add a new tab to the layout. The new tab will be appended to the end of the list and named
     * with the BLANK_TAB_NAME variable.
     *
     * @param request
     * @throws IOException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=addTab")
public ModelAndView addTab(HttpServletRequest request, HttpServletResponse response, @RequestParam("widths[]") String[] widths) throws IOException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    IPerson per = getPerson(ui, response);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    // Verify that the user has permission to add this tab
    final IAuthorizationPrincipal authPrincipal = this.getUserPrincipal(per.getUserName());
    if (!authPrincipal.hasPermission(IPermission.PORTAL_SYSTEM, IPermission.ADD_TAB_ACTIVITY, IPermission.ALL_TARGET)) {
        logger.warn("Attempt to add a tab through the REST API by unauthorized user '" + per.getUserName() + "'");
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return new ModelAndView("jsonView", Collections.singletonMap("error", "Add tab disabled"));
    }
    // construct a brand new tab
    String id = "tbd";
    String tabName = request.getParameter("tabName");
    if (StringUtils.isBlank(tabName))
        tabName = DEFAULT_TAB_NAME;
    IUserLayoutFolderDescription newTab = new UserLayoutFolderDescription();
    newTab.setName(tabName);
    newTab.setId(id);
    newTab.setFolderType(IUserLayoutFolderDescription.REGULAR_TYPE);
    newTab.setHidden(false);
    newTab.setUnremovable(false);
    newTab.setImmutable(false);
    // add the tab to the layout
    ulm.addNode(newTab, ulm.getRootFolderId(), null);
    try {
        // save the user's layout
        ulm.saveUserLayout();
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
    // get the id of the newly added tab
    String tabId = newTab.getId();
    for (String width : widths) {
        // 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
        ulm.addNode(newColumn, tabId, null);
        this.stylesheetUserPreferencesService.setLayoutAttribute(request, PreferencesScope.STRUCTURE, newColumn.getId(), "width", width + "%");
        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(newColumn.getId());
            UserPrefsHandler.setUserPreference(folder, "width", per);
        } catch (Exception e) {
            logger.error("Error saving new column widths", e);
        }
    }
    // this new tab;  use the currently active tabGroup.
    if (request.getParameter(TAB_GROUP_PARAMETER) != null) {
        String tabGroup = request.getParameter(TAB_GROUP_PARAMETER).trim();
        if (logger.isDebugEnabled()) {
            logger.debug(TAB_GROUP_PARAMETER + "=" + tabGroup);
        }
        if (!TAB_GROUP_DEFAULT.equals(tabGroup) && tabGroup.length() != 0) {
            // Persists SSUP values to the database
            this.stylesheetUserPreferencesService.setLayoutAttribute(request, PreferencesScope.STRUCTURE, tabId, TAB_GROUP_PARAMETER, tabGroup);
        }
    }
    try {
        // save the user's layout
        ulm.saveUserLayout();
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
    return new ModelAndView("jsonView", Collections.singletonMap("tabId", tabId));
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) Element(org.w3c.dom.Element) IAuthorizationPrincipal(org.apereo.portal.security.IAuthorizationPrincipal) ModelAndView(org.springframework.web.servlet.ModelAndView) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription) UserLayoutFolderDescription(org.apereo.portal.layout.node.UserLayoutFolderDescription) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription) XPathExpressionException(javax.xml.xpath.XPathExpressionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PortalException(org.apereo.portal.PortalException) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with IUserLayoutFolderDescription

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

the class UpdatePreferencesServlet method addNodeToTab.

private IUserLayoutNodeDescription addNodeToTab(IUserLayoutManager ulm, IUserLayoutChannelDescription channel, String tabId) {
    IUserLayoutNodeDescription node = null;
    Enumeration<String> columns = ulm.getChildIds(tabId);
    if (columns.hasMoreElements()) {
        while (columns.hasMoreElements()) {
            // attempt to add this channel to the column
            node = ulm.addNode(channel, columns.nextElement(), null);
            // one.  otherwise, we're set.
            if (node != null)
                break;
        }
    } else {
        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 col = ulm.addNode(newColumn, tabId, null);
        // add the channel
        node = ulm.addNode(channel, col.getId(), null);
    }
    return node;
}
Also used : IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription) UserLayoutFolderDescription(org.apereo.portal.layout.node.UserLayoutFolderDescription) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription)

Example 4 with IUserLayoutFolderDescription

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

the class UpdatePreferencesServlet method renameTab.

/**
     * Rename a specified tab.
     *
     * @param request
     * @throws IOException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=renameTab")
public ModelAndView renameTab(HttpServletRequest request, HttpServletResponse response) throws IOException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    // element ID of the tab to be renamed
    String tabId = request.getParameter("tabId");
    IUserLayoutFolderDescription tab = (IUserLayoutFolderDescription) ulm.getNode(tabId);
    // desired new name
    String tabName = request.getParameter("tabName");
    if (!ulm.canUpdateNode(tab)) {
        logger.warn("Attempting to rename an immutable tab");
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return new ModelAndView("jsonView", Collections.singletonMap("error", getMessage("error.element.update", "Unable to update element", RequestContextUtils.getLocale(request))));
    }
    /*
         * Update the tab and save the layout
         */
    tab.setName(StringUtils.isBlank(tabName) ? DEFAULT_TAB_NAME : tabName);
    final boolean updated = ulm.updateNode(tab);
    if (updated) {
        try {
            // save the user's layout
            ulm.saveUserLayout();
        } catch (PortalException e) {
            return handlePersistError(request, response, e);
        }
        //TODO why do we have to do this, shouldn't modifying the layout be enough to trigger a full re-render (layout's cache key changes)
        this.stylesheetUserPreferencesService.setLayoutAttribute(request, PreferencesScope.STRUCTURE, tabId, "name", tabName);
    }
    Map<String, String> model = Collections.singletonMap("message", "saved new tab name");
    return new ModelAndView("jsonView", model);
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) ModelAndView(org.springframework.web.servlet.ModelAndView) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with IUserLayoutFolderDescription

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

the class UpdatePreferencesServlet method moveElementInternal.

/**
     * Moves the source element.
     *
     * <p>- If the destination is a tab, the new element automatically goes to the end of the first
     * column or in a new column. - If the destination is a folder, the element is added to the end
     * of the folder. - Otherwise, the element is inserted before the destination (the destination
     * can't be a tab or folder so it must be a portlet).
     *
     * @return true if the element was moved and saved.
     */
private boolean moveElementInternal(HttpServletRequest request, String sourceId, String destinationId, String method) {
    logger.debug("moveElementInternal invoked for sourceId={}, destinationId={}, method={}", sourceId, destinationId, method);
    if (StringUtils.isEmpty(destinationId)) {
        //shortcut for beginning and end
        return true;
    }
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    boolean success = false;
    if (isTab(ulm, destinationId)) {
        // If the target is a tab type node, move the element to the end of the first column.
        // TODO Try to insert it into the first available column if multiple columns
        Enumeration<String> columns = ulm.getChildIds(destinationId);
        if (columns.hasMoreElements()) {
            success = attemptNodeMove(ulm, sourceId, columns.nextElement(), null);
        } else {
            // Attempt to create a new column
            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 col = ulm.addNode(newColumn, destinationId, null);
            // If column was created (might not if the tab had addChild=false), move the channel.
            if (col != null) {
                success = attemptNodeMove(ulm, sourceId, col.getId(), null);
            } else {
                logger.info("Unable to move item into existing columns on tab {} and unable to create new column", destinationId);
            }
        }
    } else {
        // If destination is a column, attempt to move into end of column
        if (isFolder(ulm, destinationId)) {
            success = attemptNodeMove(ulm, sourceId, destinationId, null);
        } else {
            // If insertBefore move to prior to node else to end of folder containing node
            success = attemptNodeMove(ulm, sourceId, ulm.getParentId(destinationId), "insertBefore".equals(method) ? destinationId : null);
        }
    }
    try {
        if (success) {
            ulm.saveUserLayout();
        }
    } catch (PortalException e) {
        logger.warn("Error saving layout", e);
        return false;
    }
    return success;
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription) UserLayoutFolderDescription(org.apereo.portal.layout.node.UserLayoutFolderDescription) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription)

Aggregations

IUserLayoutFolderDescription (org.apereo.portal.layout.node.IUserLayoutFolderDescription)13 IUserLayoutNodeDescription (org.apereo.portal.layout.node.IUserLayoutNodeDescription)11 PortalException (org.apereo.portal.PortalException)7 UserPreferencesManager (org.apereo.portal.UserPreferencesManager)4 IUserLayoutManager (org.apereo.portal.layout.IUserLayoutManager)4 UserLayoutFolderDescription (org.apereo.portal.layout.node.UserLayoutFolderDescription)4 IUserInstance (org.apereo.portal.user.IUserInstance)4 Element (org.w3c.dom.Element)4 IOException (java.io.IOException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 ArrayList (java.util.ArrayList)2 Enumeration (java.util.Enumeration)2 LinkedList (java.util.LinkedList)2 Vector (java.util.Vector)2 IPerson (org.apereo.portal.security.IPerson)2 Node (org.w3c.dom.Node)2 HashMap (java.util.HashMap)1