Search in sources :

Example 11 with IUserLayoutManager

use of org.apereo.portal.layout.IUserLayoutManager 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 12 with IUserLayoutManager

use of org.apereo.portal.layout.IUserLayoutManager 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 13 with IUserLayoutManager

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

Example 14 with IUserLayoutManager

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

the class UpdatePreferencesServlet method removeByFName.

/**
     * Remove the first element with the provided fname from the layout.
     *
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @param fname fname of the portlet to remove from the layout
     * @return json response
     * @throws IOException if the person cannot be retrieved
     */
@RequestMapping(method = RequestMethod.POST, params = "action=removeByFName")
public ModelAndView removeByFName(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "fname", required = true) String fname) throws IOException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    IPerson per = getPerson(ui, response);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    try {
        String elementId = ulm.getUserLayout().findNodeId(new PortletSubscribeIdResolver(fname));
        if (elementId != null && elementId.startsWith(Constants.FRAGMENT_ID_USER_PREFIX) && ulm.getNode(elementId) instanceof org.apereo.portal.layout.node.UserLayoutFolderDescription) {
            removeSubscription(per, elementId, ulm);
        } else if (elementId != null) {
            // all node types, so we can just have a generic action.
            if (!ulm.deleteNode(elementId)) {
                logger.info("Failed to remove element ID {} from layout root folder ID {}, delete node returned false", elementId, ulm.getRootFolderId());
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
                return new ModelAndView("jsonView", Collections.singletonMap("error", getMessage("error.element.update", "Unable to update element", RequestContextUtils.getLocale(request))));
            }
        } else {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return null;
        }
        ulm.saveUserLayout();
        return new ModelAndView("jsonView", Collections.EMPTY_MAP);
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) ModelAndView(org.springframework.web.servlet.ModelAndView) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) PortletSubscribeIdResolver(org.apereo.portal.layout.PortletSubscribeIdResolver) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) UserLayoutFolderDescription(org.apereo.portal.layout.node.UserLayoutFolderDescription) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with IUserLayoutManager

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

the class FavoritesEditController method unFavoriteNode.

/**
     * Un-favorite a favorite node (portlet or collection) identified by node ID. Routed by the
     * action=delete parameter. If no favorites remain after un-favoriting, switches portlet mode to
     * VIEW.
     *
     * <p>Sets render parameters: successMessageCode: message code of success message if applicable
     * errorMessageCode: message code of error message if applicable nameOfFavoriteActedUpon:
     * user-facing name of favorite acted upon. action: will be set to "list" to facilitate not
     * repeatedly attempting delete.
     *
     * <p>Exactly one of [successMessageCode|errorMessageCode] render parameters will be set.
     * nameOfFavoriteActedUpon and action will always be set.
     *
     * @param nodeId identifier of target node
     * @param response ActionResponse onto which render parameters will, mode may, be set
     */
@ActionMapping(params = { "action=delete" })
public void unFavoriteNode(@RequestParam("nodeId") String nodeId, ActionResponse response) {
    try {
        // ferret out the layout manager
        HttpServletRequest servletRequest = this.portalRequestUtils.getCurrentPortalRequest();
        IUserInstance userInstance = this.userInstanceManager.getUserInstance(servletRequest);
        IUserPreferencesManager preferencesManager = userInstance.getPreferencesManager();
        IUserLayoutManager layoutManager = preferencesManager.getUserLayoutManager();
        IUserLayoutNodeDescription nodeDescription = layoutManager.getNode(nodeId);
        String userFacingNodeName = nodeDescription.getName();
        response.setRenderParameter("nameOfFavoriteActedUpon", userFacingNodeName);
        if (nodeDescription.isDeleteAllowed()) {
            boolean nodeSuccessfullyDeleted = layoutManager.deleteNode(nodeId);
            if (nodeSuccessfullyDeleted) {
                layoutManager.saveUserLayout();
                response.setRenderParameter("successMessageCode", "favorites.unfavorite.success.parameterized");
                IUserLayout updatedLayout = layoutManager.getUserLayout();
                // if removed last favorite, return to VIEW mode
                if (!FavoritesUtils.hasAnyFavorites(updatedLayout)) {
                    response.setPortletMode(PortletMode.VIEW);
                }
                logger.debug("Successfully unfavorited [{}]", nodeDescription);
            } else {
                logger.error("Failed to delete node [{}] on unfavorite request, but this should have succeeded?", nodeDescription);
                response.setRenderParameter("errorMessageCode", "favorites.unfavorite.fail.parameterized");
            }
        } else {
            logger.warn("Attempt to unfavorite [{}] failed because user lacks permission to delete that layout node.", nodeDescription);
            response.setRenderParameter("errorMessageCode", "favorites.unfavorite.fail.lack.permission.parameterized");
        }
    } catch (Exception e) {
        // TODO: this log message is kind of useless without the username to put the node in context
        logger.error("Something went wrong unfavoriting nodeId [{}].", nodeId);
        // may have failed to load node description, so fall back on describing by id
        final String fallbackUserFacingNodeName = "node with id " + nodeId;
        response.setRenderParameter("errorMessageCode", "favorites.unfavorite.fail.parameterized");
        response.setRenderParameter("nameOfFavoriteActedUpon", fallbackUserFacingNodeName);
    }
    response.setRenderParameter("action", "list");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) IUserInstance(org.apereo.portal.user.IUserInstance) IUserLayoutNodeDescription(org.apereo.portal.layout.node.IUserLayoutNodeDescription) IUserPreferencesManager(org.apereo.portal.IUserPreferencesManager) IUserLayout(org.apereo.portal.layout.IUserLayout) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) ActionMapping(org.springframework.web.portlet.bind.annotation.ActionMapping)

Aggregations

IUserLayoutManager (org.apereo.portal.layout.IUserLayoutManager)37 IUserInstance (org.apereo.portal.user.IUserInstance)27 IUserPreferencesManager (org.apereo.portal.IUserPreferencesManager)16 PortalException (org.apereo.portal.PortalException)16 UserPreferencesManager (org.apereo.portal.UserPreferencesManager)16 IUserLayoutNodeDescription (org.apereo.portal.layout.node.IUserLayoutNodeDescription)14 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)13 ModelAndView (org.springframework.web.servlet.ModelAndView)13 IPerson (org.apereo.portal.security.IPerson)11 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)9 IPortletWindow (org.apereo.portal.portlet.om.IPortletWindow)7 HashMap (java.util.HashMap)6 Locale (java.util.Locale)6 IUserLayout (org.apereo.portal.layout.IUserLayout)6 IUserLayoutChannelDescription (org.apereo.portal.layout.node.IUserLayoutChannelDescription)6 UserLayoutFolderDescription (org.apereo.portal.layout.node.UserLayoutFolderDescription)6 IUserLayoutFolderDescription (org.apereo.portal.layout.node.IUserLayoutFolderDescription)5 IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)5 IPortletWindowId (org.apereo.portal.portlet.om.IPortletWindowId)4 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3