public ClientApiWorkspace toClientApi(Workspace workspace, User user, boolean includeVertices) {
checkNotNull(workspace, "workspace cannot be null");
checkNotNull(user, "user cannot be null");
try {
ClientApiWorkspace workspaceClientApi = new ClientApiWorkspace();
workspaceClientApi.setWorkspaceId(workspace.getWorkspaceId());
workspaceClientApi.setTitle(workspace.getDisplayTitle());
String creatorUserId = getCreatorUserId(workspace, user);
if (creatorUserId != null) {
workspaceClientApi.setCreatedBy(creatorUserId);
workspaceClientApi.setSharedToUser(!creatorUserId.equals(user.getUserId()));
}
workspaceClientApi.setEditable(hasWritePermissions(workspace.getWorkspaceId(), user));
for (WorkspaceUser u : findUsersWithAccess(workspace.getWorkspaceId(), user)) {
String userId = u.getUserId();
ClientApiWorkspace.User workspaceUser = new ClientApiWorkspace.User();
workspaceUser.setUserId(userId);
workspaceUser.setAccess(u.getWorkspaceAccess());
workspaceClientApi.addUser(workspaceUser);
}
if (includeVertices) {
for (WorkspaceEntity workspaceEntity : findEntities(workspace, user)) {
if (!workspaceEntity.isVisible()) {
continue;
}
ClientApiWorkspace.Vertex v = new ClientApiWorkspace.Vertex();
v.setVertexId(workspaceEntity.getEntityVertexId());
Integer graphPositionX = workspaceEntity.getGraphPositionX();
Integer graphPositionY = workspaceEntity.getGraphPositionY();
if (graphPositionX != null && graphPositionY != null) {
GraphPosition graphPosition = new GraphPosition(graphPositionX, graphPositionY);
v.setGraphPosition(graphPosition);
}
workspaceClientApi.addVertex(v);
}
} else {
workspaceClientApi.removeVertices();
}
return workspaceClientApi;
} catch (JSONException e) {
throw new RuntimeException(e);