Search in sources :

Example 26 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;
    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;
    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<>();
    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 27 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 {
    final IUserInstance ui = userInstanceManager.getUserInstance(request);
    final IPerson person = getPerson(ui, response);
    final UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    final IUserLayoutManager ulm = upm.getUserLayoutManager();
    final int newColumnCount = widths.length;
    // build a list of the current columns for this tab
    final Enumeration<String> columns = ulm.getChildIds(tabId);
    final List<String> columnList = new ArrayList<>();
    while (columns.hasMoreElements()) {
        columnList.add(columns.nextElement());
    }
    final int oldColumnCount = columnList.size();
    final Map<String, Object> model = new HashMap<>();
    // if the new layout has more columns
    if (newColumnCount > oldColumnCount) {
        final List<String> newColumnIds = new ArrayList<>();
        for (int i = columnList.size(); i < newColumnCount; i++) {
            // create new column element
            final 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
            final 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) {
        for (String columnId : deleted) {
            // move all channels in the current column to the last valid column
            final 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);
        }
    }
    /*
         * Now that the number of columns is established, the appearance and behavior of the columns
         * on the page is determined by Structure Stylesheet User Preferences.  "Classic" uPortal
         * columns use a 'width' attribute, while the new(er) columns based on CSS Flex use a
         * 'flexColumns' attribute.
         */
    resetColumnStylesheetUserPreferences(request, person, ulm, // Clear previous selections
    columnList);
    // Choose a column layout strategy...
    // default is "classic"
    boolean useFlexStrategy = false;
    if (columnList.size() == 1) {
        int firstColumnWidthAsInt = Integer.parseInt(widths[0]);
        if (firstColumnWidthAsInt > 100) {
            // A single column with a width parameter creater than 100(%)
            // signals that we want the flexColumns strategy.
            useFlexStrategy = true;
        }
    }
    // Update Structure Stylesheet User Preferences based on the selected strategy
    if (useFlexStrategy) {
        updateColumnStylesheetUserPreferencesFlex(request, person, ulm, columnList, widths);
    } else {
        updateColumnStylesheetUserPreferencesClassic(request, person, ulm, columnList, widths);
    }
    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) 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) 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)

Example 28 with IUserLayoutNodeDescription

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

the class UpdatePreferencesServlet method getTabIdFromName.

private String getTabIdFromName(IUserLayout userLayout, String tabName) {
    @SuppressWarnings("unchecked") 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);
            IUserLayoutNodeDescription.LayoutNodeType nodeType = nodeDescription.getType();
            if (IUserLayoutNodeDescription.LayoutNodeType.FOLDER.equals(nodeType) && nodeDescription instanceof IUserLayoutFolderDescription) {
                IUserLayoutFolderDescription folderDescription = (IUserLayoutFolderDescription) nodeDescription;
                if (tabName.equalsIgnoreCase(folderDescription.getName())) {
                    return folderDescription.getId();
                }
            }
        } catch (Exception e) {
            logger.error("Error getting the nodeID of the tab name " + tabName, e);
        }
    }
    logger.warn("Tab " + tabName + " was searched for but not found");
    // didn't find tab
    return null;
}
Also used : IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) IUserLayoutFolderDescription(org.apereo.portal.layout.node.IUserLayoutFolderDescription) InvocationTargetException(java.lang.reflect.InvocationTargetException) PortalException(org.apereo.portal.PortalException) IOException(java.io.IOException)

Example 29 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 30 with IUserLayoutNodeDescription

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

the class FavoritesEditController method initializeView.

/**
 * Handles all Favorites portlet EDIT mode renders. Populates model with user's favorites and
 * selects a view to display those favorites.
 *
 * <p>View selection:
 *
 * <p>Returns "jsp/Favorites/edit" in the normal case where the user has at least one favorited
 * portlet or favorited collection.
 *
 * <p>Returns "jsp/Favorites/edit_zero" in the edge case where the user has zero favorited
 * portlets AND zero favorited collections.
 *
 * <p>Model: marketPlaceFname --> String functional name of Marketplace portlet, or null if not
 * available. collections --> List of favorited collections (IUserLayoutNodeDescription s)
 * favorites --> List of favorited individual portlets (IUserLayoutNodeDescription s)
 * successMessageCode --> String success message bundle key, or null if none errorMessageCode
 * --> String error message bundle key, or null if none nameOfFavoriteActedUpon --> Name of
 * favorite acted upon, intended as parameter to success or error message
 *
 * @param model . Spring model. This method adds five model attributes.
 * @return jsp/Favorites/edit[_zero]
 */
@RenderMapping
public String initializeView(Model model, RenderRequest renderRequest) {
    IUserInstance ui = userInstanceManager.getUserInstance(portalRequestUtils.getCurrentPortalRequest());
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    IUserLayout userLayout = ulm.getUserLayout();
    // TODO: the portlet could predicate including a non-null marketplace portlet fname
    // on the accessing user having permission to render the portlet referenced by that fname
    // so that portlet would gracefully degrade when configured with bad marketplace portlet
    // fname
    // and also gracefully degrade when the accessing user doesn't have permission to access an
    // otherwise
    // viable configured marketplace.  This complexity may not be worth it.  Anyway it is not
    // yet implemented.
    model.addAttribute("marketplaceFname", this.marketplaceFName);
    List<IUserLayoutNodeDescription> collections = FavoritesUtils.getFavoriteCollections(userLayout);
    model.addAttribute("collections", collections);
    List<IUserLayoutNodeDescription> favorites = FavoritesUtils.getFavoritePortlets(userLayout);
    model.addAttribute("favorites", favorites);
    model.addAttribute("successMessageCode", renderRequest.getParameter("successMessageCode"));
    model.addAttribute("errorMessageCode", renderRequest.getParameter("errorMessageCode"));
    model.addAttribute("nameOfFavoriteActedUpon", renderRequest.getParameter("nameOfFavoriteActedUpon"));
    // default to the regular old edit view
    String viewName = "jsp/Favorites/edit";
    if (collections.isEmpty() && favorites.isEmpty()) {
        // use the special case view
        viewName = "jsp/Favorites/edit_zero";
    }
    logger.trace("Favorites Portlet EDIT mode built model [{}] and selected view {}.", model, viewName);
    return viewName;
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) IUserLayout(org.apereo.portal.layout.IUserLayout) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) IUserPreferencesManager(org.apereo.portal.IUserPreferencesManager) RenderMapping(org.springframework.web.portlet.bind.annotation.RenderMapping)

Aggregations

IUserLayoutNodeDescription (org.apereo.portal.layout.node.IUserLayoutNodeDescription)32 IUserLayoutManager (org.apereo.portal.layout.IUserLayoutManager)15 PortalException (org.apereo.portal.PortalException)14 IUserInstance (org.apereo.portal.user.IUserInstance)13 IUserLayoutFolderDescription (org.apereo.portal.layout.node.IUserLayoutFolderDescription)12 UserPreferencesManager (org.apereo.portal.UserPreferencesManager)9 IUserLayoutChannelDescription (org.apereo.portal.layout.node.IUserLayoutChannelDescription)9 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 Node (org.w3c.dom.Node)6 ArrayList (java.util.ArrayList)5 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)5 Document (org.w3c.dom.Document)5 Enumeration (java.util.Enumeration)4 HashMap (java.util.HashMap)4 Locale (java.util.Locale)4 IUserLayout (org.apereo.portal.layout.IUserLayout)4 UserLayoutChannelDescription (org.apereo.portal.layout.node.UserLayoutChannelDescription)4