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;
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);
}
use of org.apereo.portal.layout.IUserLayoutManager 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);
}
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);
}
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 {
// Delete the requested element node. This code is the same for
// all node types, so we can just have a generic action.
String elementId = request.getParameter("elementID");
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.emptyMap());
} catch (PortalException e) {
return handlePersistError(request, response, e);
}
}
use of org.apereo.portal.layout.IUserLayoutManager in project uPortal by Jasig.
the class UserLayoutParameterProcessor method processParameters.
@Override
@SuppressWarnings("FallThrough")
public boolean processParameters(HttpServletRequest request, HttpServletResponse response) {
final IPortalRequestInfo portalRequestInfo = this.urlSyntaxProvider.getPortalRequestInfo(request);
final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
final IUserPreferencesManager preferencesManager = userInstance.getPreferencesManager();
final IUserLayoutManager userLayoutManager = preferencesManager.getUserLayoutManager();
final String tabId = portalRequestInfo.getTargetedLayoutNodeId();
if (tabId != null) {
this.stylesheetUserPreferencesService.setStylesheetParameter(request, PreferencesScope.STRUCTURE, "focusedTabID", tabId);
}
// on a portlet, it will get overwritten with the new value below.
if (userLayoutManager instanceof TransientUserLayoutManagerWrapper) {
final TransientUserLayoutManagerWrapper transientUserLayoutManagerWrapper = (TransientUserLayoutManagerWrapper) userLayoutManager;
transientUserLayoutManagerWrapper.setFocusedId(null);
}
final UrlState urlState = portalRequestInfo.getUrlState();
switch(urlState) {
case DETACHED:
this.stylesheetUserPreferencesService.setStylesheetParameter(request, PreferencesScope.STRUCTURE, "detached", Boolean.TRUE.toString());
case MAX:
{
final IPortletRequestInfo portletRequestInfo = portalRequestInfo.getTargetedPortletRequestInfo();
if (portletRequestInfo != null) {
final IPortletWindowId targetWindowId = portletRequestInfo.getPortletWindowId();
final IPortletWindow portletWindow = this.portletWindowRegistry.getPortletWindow(request, targetWindowId);
final IPortletEntity portletEntity = portletWindow.getPortletEntity();
final String channelSubscribeId = portletEntity.getLayoutNodeId();
this.stylesheetUserPreferencesService.setStylesheetParameter(request, PreferencesScope.STRUCTURE, "userLayoutRoot", channelSubscribeId);
if (userLayoutManager instanceof TransientUserLayoutManagerWrapper) {
// get wrapper implementation for focusing
final TransientUserLayoutManagerWrapper transientUserLayoutManagerWrapper = (TransientUserLayoutManagerWrapper) userLayoutManager;
// .. and now set it as the focused id
transientUserLayoutManagerWrapper.setFocusedId(channelSubscribeId);
}
// If portletRequestInfo was null just fall through to NORMAL state
break;
}
}
case NORMAL:
default:
{
this.stylesheetUserPreferencesService.setStylesheetParameter(request, PreferencesScope.STRUCTURE, "userLayoutRoot", IUserLayout.ROOT_NODE_NAME);
break;
}
}
return true;
}
Aggregations