// The workspace name you create must be Orion Content. See Bug 439735
Logger logger = LoggerFactory.getLogger("org.eclipse.orion.server.config"); //$NON-NLS-1$
logger.info("SimpleMetaStore.createWorkspace: workspace name conflict: name will be \"Orion Content\": user " + workspaceInfo.getUserId() + " provided " + workspaceInfo.getFullName() + " instead.");
workspaceInfo.setFullName(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);
}
UserInfo userInfo;
try {
userInfo = readUser(workspaceInfo.getUserId());
} catch (CoreException exception) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1, "SimpleMetaStore.createWorkspace: could not find user with id: " + workspaceInfo.getUserId() + ", user does not exist.", null));
}
if (userInfo == null) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1, "SimpleMetaStore.createWorkspace: could not find user with id: " + workspaceInfo.getUserId() + ", user does not exist.", null));
}
if (!userInfo.getWorkspaceIds().isEmpty()) {
// We have an existing workspace already, you cannot create a second workspace. See Bug 439735
String existingWorkspaceIds = userInfo.getWorkspaceIds().get(0);
Logger logger = LoggerFactory.getLogger("org.eclipse.orion.server.config"); //$NON-NLS-1$
logger.info("SimpleMetaStore.createWorkspace: workspace conflict: cannot create a second workspace for user id: " + userInfo.getUniqueId() + ", existing workspace is being used: " + existingWorkspaceIds);
workspaceInfo.setUniqueId(existingWorkspaceIds);
return;
}
// We create a meta folder for the workspace using the encoded workspace name
String workspaceId = SimpleMetaStoreUtil.encodeWorkspaceId(workspaceInfo.getUserId(), workspaceInfo.getFullName());
String encodedWorkspaceName = SimpleMetaStoreUtil.decodeWorkspaceNameFromWorkspaceId(workspaceId);
// It is possible to have two workspaces with the same name, so append an integer if this is a duplicate name.
File userMetaFolder = SimpleMetaStoreUtil.readMetaUserFolder(getRootLocation(), userInfo.getUniqueId());
workspaceInfo.setUniqueId(workspaceId);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put(SimpleMetaStore.ORION_VERSION, VERSION);
jsonObject.put(MetadataInfo.UNIQUE_ID, workspaceInfo.getUniqueId());
jsonObject.put("UserId", workspaceInfo.getUserId());
jsonObject.put(UserConstants2.FULL_NAME, workspaceInfo.getFullName());
jsonObject.put("ProjectNames", new JSONArray());
JSONObject properties = updateProperties(jsonObject, workspaceInfo);
jsonObject.put("Properties", properties);
} catch (JSONException e) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1, "SimpleMetaStore.createWorkspace: could not create workspace: " + encodedWorkspaceName + " for user " + userInfo.getUserName(), e));
}
if (!SimpleMetaStoreUtil.createMetaFolder(userMetaFolder, encodedWorkspaceName)) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1, "SimpleMetaStore.createWorkspace: could not create workspace: " + encodedWorkspaceName + " for user " + userInfo.getUserName(), null));
}
if (!SimpleMetaStoreUtil.createMetaFile(userMetaFolder, workspaceId, jsonObject)) {
throw new CoreException(new Status(IStatus.ERROR, ServerConstants.PI_SERVER_CORE, 1, "SimpleMetaStore.createWorkspace: could not create workspace: " + encodedWorkspaceName + " for user " + userInfo.getUserName(), null));
}
// Update the user with the new workspaceId
List<String> newWorkspaceIds = new ArrayList<String>();
newWorkspaceIds.addAll(userInfo.getWorkspaceIds());
newWorkspaceIds.add(workspaceId);
userInfo.setWorkspaceIds(newWorkspaceIds);
updateUser(userInfo);
}