Search in sources :

Example 1 with IUserLayoutNodeDescription

use of org.apereo.portal.layout.node.IUserLayoutNodeDescription 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<>();
    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);
            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 = columns.nextElement();
                        Enumeration<String> portlets = userLayout.getChildIds(column);
                        while (portlets.hasMoreElements()) {
                            String portlet = 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 IUserLayoutNodeDescription

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

the class SimpleLayout method getChildIds.

@Override
public Enumeration getChildIds(String nodeId) throws PortalException {
    Vector v = new Vector();
    IUserLayoutNodeDescription node = getNodeDescription(nodeId);
    if (node instanceof IUserLayoutFolderDescription) {
        Element element = layout.getElementById(nodeId);
        for (Node n = element.getFirstChild(); n != null; n = n.getNextSibling()) {
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;
                if (e.getAttribute("ID") != null) {
                    v.add(e.getAttribute("ID"));
                }
            }
        }
    }
    return v.elements();
}
Also used : IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Vector(java.util.Vector) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription)

Example 3 with IUserLayoutNodeDescription

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

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

the class UpdatePreferencesServlet method updateAttributes.

/**
 * Update the attributes for the node. Unrecognized attributes will log a warning, but are
 * otherwise ignored.
 *
 * @param request
 * @param response
 * @param targetId - the id of the node whose attributes will be updated.
 * @param attributes - parse the JSON name-value pairs in the body as the attributes of the
 *     folder. e.g. : { "structureAttributes" : {"display" : "row", "other" : "another" },
 *     "attributes" : {"hidden": "true", "type" : "header-top" } }
 */
@RequestMapping(method = RequestMethod.POST, params = "action=updateAttributes")
public ModelAndView updateAttributes(HttpServletRequest request, HttpServletResponse response, @RequestParam("targetId") String targetId, @RequestBody Map<String, Map<String, String>> attributes) {
    IUserLayoutManager ulm = userInstanceManager.getUserInstance(request).getPreferencesManager().getUserLayoutManager();
    if (!ulm.getNode(targetId).isEditAllowed()) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return new ModelAndView("jsonView", Collections.singletonMap("error", getMessage("error.element.update", "Unable to update element", RequestContextUtils.getLocale(request))));
    }
    // Update the attributes based on the supplied JSON (request body name-value pairs)
    IUserLayoutNodeDescription node = ulm.getNode(targetId);
    if (node == null) {
        logger.warn("[updateAttributes()] Unable to locate node with id: " + targetId);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        return new ModelAndView("jsonView", Collections.singletonMap("error", "Unable to locate node with id: " + targetId));
    } else {
        setObjectAttributes(node, request, attributes);
        final Locale locale = RequestContextUtils.getLocale(request);
        try {
            ulm.saveUserLayout();
        } catch (PortalException e) {
            return handlePersistError(request, response, e);
        }
        Map<String, String> model = Collections.singletonMap("success", getMessage("success.element.update", "Updated element attributes", locale));
        return new ModelAndView("jsonView", model);
    }
}
Also used : IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) Locale(java.util.Locale) ModelAndView(org.springframework.web.servlet.ModelAndView) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with IUserLayoutNodeDescription

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

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