Search in sources :

Example 21 with IUserLayoutManager

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

the class UpdatePreferencesServlet method removeElement.

/**
     * Remove an element from the layout.
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=removeElement")
public ModelAndView removeElement(HttpServletRequest request, HttpServletResponse response) throws IOException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    IPerson per = getPerson(ui, response);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    try {
        // if the element ID starts with the fragment prefix and is a folder,
        // attempt first to treat it as a pulled fragment subscription
        String elementId = request.getParameter("elementID");
        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 {
            // 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.sendError(HttpServletResponse.SC_FORBIDDEN);
                return new ModelAndView("jsonView", Collections.singletonMap("error", getMessage("error.element.update", "Unable to update element", RequestContextUtils.getLocale(request))));
            }
        }
        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) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) UserLayoutFolderDescription(org.apereo.portal.layout.node.UserLayoutFolderDescription) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 22 with IUserLayoutManager

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

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

the class UpdatePreferencesServlet method moveTab.

/**
     * Move a tab left or right.
     *
     * @param sourceId node ID of tab to move
     * @param method insertBefore or appendAfter. If appendAfter, tab is added as last tab (parent
     *     of destinationId).
     * @param destinationId insertBefore: node ID of tab to move sourceId before. insertAfter: node
     *     ID of another tab
     * @param request
     * @param response
     * @throws PortalException
     * @throws IOException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=moveTab")
public ModelAndView moveTab(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "sourceID") String sourceId, @RequestParam String method, @RequestParam(value = "elementID") String destinationId) throws IOException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    final Locale locale = RequestContextUtils.getLocale(request);
    // If we're moving this element before another one, we need
    // to know what the target is. If there's no target, just
    // assume we're moving it to the very end of the list.
    String siblingId = null;
    if ("insertBefore".equals(method))
        siblingId = destinationId;
    try {
        // move the node as requested and save the layout
        if (!ulm.moveNode(sourceId, ulm.getParentId(destinationId), siblingId)) {
            logger.warn("Failed to move tab in user layout. moveNode returned false");
            response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("error.move.tab", "There was an issue moving the tab, please refresh the page and try again.", locale)));
        }
        ulm.saveUserLayout();
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
    return new ModelAndView("jsonView", Collections.singletonMap("response", getMessage("success.move.tab", "Tab moved successfully", locale)));
}
Also used : IUserInstance(org.apereo.portal.user.IUserInstance) Locale(java.util.Locale) 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 24 with IUserLayoutManager

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

the class UpdatePreferencesServlet method subscribeToTab.

/**
     * Subscribe a user to a pre-formatted tab (pulled DLM fragment).
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
@RequestMapping(method = RequestMethod.POST, params = "action=subscribeToTab")
public ModelAndView subscribeToTab(HttpServletRequest request, HttpServletResponse response) throws IOException {
    IUserInstance ui = userInstanceManager.getUserInstance(request);
    IPerson per = getPerson(ui, response);
    UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
    IUserLayoutManager ulm = upm.getUserLayoutManager();
    // Get the fragment owner's name from the request and construct
    // an IPerson object representing that user
    String fragmentOwnerName = request.getParameter("sourceID");
    if (StringUtils.isBlank(fragmentOwnerName)) {
        logger.warn("Attempted to subscribe to tab with null owner ID");
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return new ModelAndView("jsonView", Collections.singletonMap("error", "Attempted to subscribe to tab with null owner ID"));
    }
    RestrictedPerson fragmentOwner = PersonFactory.createRestrictedPerson();
    fragmentOwner.setUserName(fragmentOwnerName);
    // Mark the currently-authenticated user as subscribed to this fragment.
    // If an inactivated fragment registration already exists, update it
    // as an active subscription.  Otherwise, create a new fragment
    // subscription.
    IUserFragmentSubscription userFragmentInfo = userFragmentInfoDao.getUserFragmentInfo(per, fragmentOwner);
    if (userFragmentInfo == null) {
        userFragmentInfo = userFragmentInfoDao.createUserFragmentInfo(per, fragmentOwner);
    } else {
        userFragmentInfo.setActive(true);
        userFragmentInfoDao.updateUserFragmentInfo(userFragmentInfo);
    }
    try {
        // reload user layout and stylesheet to incorporate new DLM fragment
        ulm.loadUserLayout(true);
        // get the target node this new tab should be moved after
        String destinationId = request.getParameter("elementID");
        // get the user layout for the currently-authenticated user
        int uid = userIdentityStore.getPortalUID(fragmentOwner, false);
        final DistributedUserLayout userLayout = userLayoutStore.getUserLayout(per, upm.getUserProfile());
        Document layoutDocument = userLayout.getLayout();
        // attempt to find the new subscribed tab in the layout so we can
        // move it
        StringBuilder expression = new StringBuilder("//folder[@type='root']/folder[starts-with(@ID,'").append(Constants.FRAGMENT_ID_USER_PREFIX).append(uid).append("')]");
        XPathFactory fac = XPathFactory.newInstance();
        XPath xpath = fac.newXPath();
        NodeList nodes = (NodeList) xpath.evaluate(expression.toString(), layoutDocument, XPathConstants.NODESET);
        String sourceId = nodes.item(0).getAttributes().getNamedItem("ID").getTextContent();
        ulm.moveNode(sourceId, ulm.getParentId(destinationId), destinationId);
        ulm.saveUserLayout();
        return new ModelAndView("jsonView", Collections.singletonMap("tabId", sourceId));
    } catch (XPathExpressionException e) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return new ModelAndView("jsonView", Collections.singletonMap("error", "Xpath error"));
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) ModelAndView(org.springframework.web.servlet.ModelAndView) IUserFragmentSubscription(org.apereo.portal.fragment.subscribe.IUserFragmentSubscription) Document(org.w3c.dom.Document) UserPreferencesManager(org.apereo.portal.UserPreferencesManager) IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) XPathFactory(javax.xml.xpath.XPathFactory) RestrictedPerson(org.apereo.portal.security.provider.RestrictedPerson) DistributedUserLayout(org.apereo.portal.layout.dlm.DistributedUserLayout) PortalException(org.apereo.portal.PortalException) IUserLayoutManager(org.apereo.portal.layout.IUserLayoutManager) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with IUserLayoutManager

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

the class UpdatePreferencesServlet method addFolder.

/**
     * Add a new folder to the layout.
     *
     * @param request
     * @param response
     * @param targetId - id of the folder node to add the new folder to. By default, the folder will
     *     be inserted after other existing items in the node unless a siblingId is provided.
     * @param siblingId - if set, insert new folder prior to the node with this id, otherwise simple
     *     insert at the end of the list.
     * @param attributes - if included, parse the JSON name-value pairs in the body as the
     *     attributes of the folder. These will override the defaults. e.g. : {
     *     "structureAttributes" : {"display" : "row", "other" : "another" }, "attributes" :
     *     {"hidden": "true", "type" : "header-top" } }
     */
@RequestMapping(method = RequestMethod.POST, params = "action=addFolder")
public ModelAndView addFolder(HttpServletRequest request, HttpServletResponse response, @RequestParam("targetId") String targetId, @RequestParam(value = "siblingId", required = false) String siblingId, @RequestBody(required = false) Map<String, Map<String, String>> attributes) {
    IUserLayoutManager ulm = userInstanceManager.getUserInstance(request).getPreferencesManager().getUserLayoutManager();
    final Locale locale = RequestContextUtils.getLocale(request);
    if (!ulm.getNode(targetId).isAddChildAllowed()) {
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        return new ModelAndView("jsonView", Collections.singletonMap("error", getMessage("error.add.element", "Unable to add element", locale)));
    }
    UserLayoutFolderDescription newFolder = new UserLayoutFolderDescription();
    newFolder.setHidden(false);
    newFolder.setImmutable(false);
    newFolder.setAddChildAllowed(true);
    newFolder.setFolderType(IUserLayoutFolderDescription.REGULAR_TYPE);
    // Update the attributes based on the supplied JSON (optional request body name-value pairs)
    if (attributes != null && !attributes.isEmpty()) {
        setObjectAttributes(newFolder, request, attributes);
    }
    ulm.addNode(newFolder, targetId, siblingId);
    try {
        ulm.saveUserLayout();
    } catch (PortalException e) {
        return handlePersistError(request, response, e);
    }
    Map<String, Object> model = new HashMap<>();
    model.put("response", getMessage("success.add.folder", "Added a new folder", locale));
    model.put("folderId", newFolder.getId());
    model.put("immutable", newFolder.isImmutable());
    return new ModelAndView("jsonView", model);
}
Also used : Locale(java.util.Locale) HashMap(java.util.HashMap) 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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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