// TODO: better validation
String spaceName = name.replace(' ', '_');
String templateFolderPath = actionRequest.getPreferences().getValue(SPACE_TEMPLATE, DEFAULT_SPACE_TEMPLATE);
if (!pageManager.folderExists(templateFolderPath))
{
throw new PortletException("Space template folder does not exist: " + templateFolderPath);
}
Folder templateFolder = pageManager.getFolder(templateFolderPath);
String owner = actionRequest.getUserPrincipal().getName();
try
{
userManager.getUser(owner);
}
catch (SecurityException notFoundEx)
{
throw new PortletException("Space owner is not found: " + owner);
}
Space space = spacesService.createSpace(spaceName, owner, templateFolder, title, title, description, theme, constraint);
// redirect
String path = admin.getPortalURL(actionRequest, actionResponse, space.getPath());
actionResponse.sendRedirect(path);
}
else
{
String owner = scrapeParameter(actionRequest, "spaceOwner");
// FIXME: lookupSpace() can find system spaces only, not user space.
// So, what if a system space name is as same as a user space name?
Space space = spacesService.lookupSpace(name);
if (space == null)
{
space = spacesService.lookupUserSpace(name);
}
if (space != null)
{
space.setDescription(description);
space.setTitle(title);
space.setShortTitle(title);
space.setTheme(theme);
if (constraint.equals(""))
{
String old = space.getSecurityConstraint();
if (old != null)
{
if (!"".equals(old))
space.setSecurityConstraint(constraint);
}
}
else
{
space.setSecurityConstraint(constraint);
}
if (owner != null && !owner.equals(space.getOwner()))
{
try
{
userManager.getUser(owner);
}
catch (SecurityException notFoundEx)
{
throw new PortletException("Space owner is not found: " + owner);
}
space.setOwner(owner);
}
spacesService.storeSpace(space);