use of org.apereo.portal.layout.node.UserLayoutFolderDescription in project uPortal by Jasig.
the class UpdatePreferencesServlet method addTab.
/**
* Add a new tab to the layout. The new tab will be appended to the end of the list and named
* with the BLANK_TAB_NAME variable.
*
* @param request
* @throws IOException
*/
@RequestMapping(method = RequestMethod.POST, params = "action=addTab")
public ModelAndView addTab(HttpServletRequest request, HttpServletResponse response, @RequestParam("widths[]") String[] widths) throws IOException {
IUserInstance ui = userInstanceManager.getUserInstance(request);
IPerson per = getPerson(ui, response);
UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
IUserLayoutManager ulm = upm.getUserLayoutManager();
// Verify that the user has permission to add this tab
final IAuthorizationPrincipal authPrincipal = this.getUserPrincipal(per.getUserName());
if (!authPrincipal.hasPermission(IPermission.PORTAL_SYSTEM, IPermission.ADD_TAB_ACTIVITY, IPermission.ALL_TARGET)) {
logger.warn("Attempt to add a tab through the REST API by unauthorized user '" + per.getUserName() + "'");
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return new ModelAndView("jsonView", Collections.singletonMap("error", "Add tab disabled"));
}
// construct a brand new tab
String id = "tbd";
String tabName = request.getParameter("tabName");
if (StringUtils.isBlank(tabName))
tabName = DEFAULT_TAB_NAME;
IUserLayoutFolderDescription newTab = new UserLayoutFolderDescription();
newTab.setName(tabName);
newTab.setId(id);
newTab.setFolderType(IUserLayoutFolderDescription.REGULAR_TYPE);
newTab.setHidden(false);
newTab.setUnremovable(false);
newTab.setImmutable(false);
// add the tab to the layout
ulm.addNode(newTab, ulm.getRootFolderId(), null);
try {
// save the user's layout
ulm.saveUserLayout();
} catch (PortalException e) {
return handlePersistError(request, response, e);
}
// get the id of the newly added tab
String tabId = newTab.getId();
for (String width : widths) {
// create new column element
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
ulm.addNode(newColumn, tabId, null);
this.stylesheetUserPreferencesService.setLayoutAttribute(request, PreferencesScope.STRUCTURE, newColumn.getId(), "width", width + "%");
try {
// This sets the column attribute in memory but doesn't persist it. Comment says saves changes "prior to persisting"
Element folder = ulm.getUserLayoutDOM().getElementById(newColumn.getId());
UserPrefsHandler.setUserPreference(folder, "width", per);
} catch (Exception e) {
logger.error("Error saving new column widths", e);
}
}
// this new tab; use the currently active tabGroup.
if (request.getParameter(TAB_GROUP_PARAMETER) != null) {
String tabGroup = request.getParameter(TAB_GROUP_PARAMETER).trim();
if (logger.isDebugEnabled()) {
logger.debug(TAB_GROUP_PARAMETER + "=" + tabGroup);
}
if (!TAB_GROUP_DEFAULT.equals(tabGroup) && tabGroup.length() != 0) {
// Persists SSUP values to the database
this.stylesheetUserPreferencesService.setLayoutAttribute(request, PreferencesScope.STRUCTURE, tabId, TAB_GROUP_PARAMETER, tabGroup);
}
}
try {
// save the user's layout
ulm.saveUserLayout();
} catch (PortalException e) {
return handlePersistError(request, response, e);
}
return new ModelAndView("jsonView", Collections.singletonMap("tabId", tabId));
}
use of org.apereo.portal.layout.node.UserLayoutFolderDescription 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;
}
use of org.apereo.portal.layout.node.UserLayoutFolderDescription 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;
}
use of org.apereo.portal.layout.node.UserLayoutFolderDescription 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);
}
use of org.apereo.portal.layout.node.UserLayoutFolderDescription 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 {
IUserInstance ui = userInstanceManager.getUserInstance(request);
IPerson per = getPerson(ui, response);
UserPreferencesManager upm = (UserPreferencesManager) ui.getPreferencesManager();
IUserLayoutManager ulm = upm.getUserLayoutManager();
int newColumnCount = widths.length;
// build a list of the current columns for this tab
Enumeration<String> columns = ulm.getChildIds(tabId);
List<String> columnList = new ArrayList<String>();
while (columns.hasMoreElements()) {
columnList.add(columns.nextElement());
}
int oldColumnCount = columnList.size();
Map<String, Object> model = new HashMap<String, Object>();
// if the new layout has more columns
if (newColumnCount > oldColumnCount) {
List<String> newColumnIds = new ArrayList<String>();
for (int i = columnList.size(); i < newColumnCount; i++) {
// create new column element
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 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) {
if (columnList.size() != widths.length + deleted.length) {
// TODO: error?
}
for (String columnId : deleted) {
// move all channels in the current column to the last valid column
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);
}
}
int count = 0;
for (String columnId : columnList) {
this.stylesheetUserPreferencesService.setLayoutAttribute(request, PreferencesScope.STRUCTURE, columnId, "width", widths[count] + "%");
try {
// This sets the column attribute in memory but doesn't persist it. Comment says saves changes "prior to persisting"
Element folder = ulm.getUserLayoutDOM().getElementById(columnId);
UserPrefsHandler.setUserPreference(folder, "width", per);
} catch (Exception e) {
logger.error("Error saving new column widths", e);
}
count++;
}
try {
ulm.saveUserLayout();
} catch (PortalException e) {
logger.warn("Error saving layout", e);
}
return new ModelAndView("jsonView", model);
}
Aggregations