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);
}
}
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);
}
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)));
}
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);
}
}
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);
}
Aggregations