Bug 397721 - adding in project ids and handling projects by id
diff --git a/bundles/org.eclipse.orion.client.ui/web/projects/ProjectDataManager.js b/bundles/org.eclipse.orion.client.ui/web/projects/ProjectDataManager.js
index c578803..4b1a7b5 100644
--- a/bundles/org.eclipse.orion.client.ui/web/projects/ProjectDataManager.js
+++ b/bundles/org.eclipse.orion.client.ui/web/projects/ProjectDataManager.js
@@ -18,6 +18,7 @@
 		var PROJECTS_FOLDER = 'projectData';

 		var WORKSPACES_FOLDER = 'workspaces';

 		var PROJECTS_METADATA = 'project.json';

+		var WORKAREA = 'workspace';

 

 		function ProjectDataManager( serviceRegistry, fileClient ){

 			this.serviceRegistry = serviceRegistry;

@@ -28,6 +29,11 @@
 			};

 		}

 		

+		function generateId(){

+			var id = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);

+			return id;

+		}

+		

 		function _findInWorkspace( subtree, name ){

 		

 			var element;

@@ -42,6 +48,26 @@
 			return element;

 		}

 		

+		function projectMaintainance( callback ){

+			

+			/* Moving from using project name as identifier to project id as identifier. 

+			   So - adding in an id for those projects without one. */

+			

+			var project;

+			var self = this;

+			

+			Deferred.when(this.projectsFile, function(file) {

+				self.projectsFile = file;

+				self.fileClient.read( file.Location ).then( function( content ){	

+					var projects = JSON.parse( content );

+					for( var p = 0; p < projects.length; p++ ){

+						projects[p].id = this.generateId();

+						self.save(project);

+					}

+				});

+			});

+		}

+		

 		function getProjectData( callback ){

 			var self = this;

 			Deferred.when(this.projectsFile, function(file) {

@@ -57,7 +83,7 @@
 		

 		}

 		

-		function getProject( projectName, callback ){

+		function getProject( projectId, callback ){

 			var project;

 			var self = this;

 			Deferred.when(this.projectsFile, function(file) {

@@ -65,13 +91,14 @@
 				self.fileClient.read( file.Location ).then( function( content ){	

 					var projects = JSON.parse( content );

 					for( var p = 0; p < projects.length; p++ ){

-						if( projects[p].name === projectName ){

+						if( projects[p].id === projectId ){

 							project = projects[p];

 							break;

 						}

 					}

 					if (!project) {

 						project = { name: "Project " + (projects.length + 1), address: "", description: "", drives: [] };

+						project.id = self.generateId();

 						self.save(project);

 					}

 					if (project.workspace) {

@@ -79,7 +106,7 @@
 					} else {

 						Deferred.when(self.workspacesFolder, function(workspacesFolder) {

 							self.workspacesFolder = workspacesFolder;

-							self.fileClient.createFolder( workspacesFolder.Location, project.name ).then( function( file ){

+							self.fileClient.createFolder( workspacesFolder.Location, WORKAREA ).then( function( file ){

 								project.workspace = file.Location;

 								self.save( project );

 								callback(project, self.loadedWorkspace, self);

@@ -87,7 +114,7 @@
 							function(error) {

 								// handle the case where the workspace folder was there even though the project file had not been saved to indicate so.

 								if (error.status === 412) {

-									project.workspace = self.workspacesFolder.Location + project.name;

+									project.workspace = self.workspacesFolder.Location + WORKAREA;

 									self.save( project );

 									callback(project, self.loadedWorkspace, self);

 								} else {

@@ -113,7 +140,7 @@
 					var existingProject = false;

 					

 					for( var p = 0; p < projects.length; p++ ){

-						if( projects[p].name === projectData.name ){

+						if( projects[p].id === projectData.id ){

 							projects[p] = projectData;

 							existingProject = true;

 							break;

@@ -134,7 +161,7 @@
 						

 		}

 		

-		function removeProject( projectName, callback ){

+		function removeProject( projectId, callback ){

 			var self = this;

 			Deferred.when(this.projectsFile, function(file) {

 				self.projectsFile = file;

@@ -145,7 +172,7 @@
 					

 					for( var p = 0; p < projects.length; p++ ){

 					

-						if( projects[p].name === projectName ){

+						if( projects[p].id === projectId ){

 							index = p;

 							break;

 						}	

@@ -225,6 +252,8 @@
 			});

 		}

 		

+		ProjectDataManager.prototype.generateId = generateId;

+		ProjectDataManager.prototype.projectMaintainance = projectMaintainance;

 		ProjectDataManager.prototype._findInWorkspace = _findInWorkspace;

 		ProjectDataManager.prototype._removeFromArray = _removeFromArray;

 		ProjectDataManager.prototype.startup = startup;

diff --git a/bundles/org.eclipse.orion.client.ui/web/projects/ProjectGrid.js b/bundles/org.eclipse.orion.client.ui/web/projects/ProjectGrid.js
index 88308fd..b38271d 100644
--- a/bundles/org.eclipse.orion.client.ui/web/projects/ProjectGrid.js
+++ b/bundles/org.eclipse.orion.client.ui/web/projects/ProjectGrid.js
@@ -78,11 +78,11 @@
 				

 				var uriTemplate = "{OrionHome}/projects/projectPage.html#?project={project}";

 				

-				var projectname = this.projectData[count].name;

+				var projectId = this.projectData[count].id;

 				

 				var template = new URITemplate(uriTemplate);

 				var url = template.expand({

-					project: projectname

+					project: projectId

 				});

 

 				var listItem = document.createElement('li');

@@ -98,9 +98,9 @@
 				if( this.projectData[count].address ){

 				

 					/* iframe.sandbox = "allow-scripts allow-same-origin" */

-					content = '<div class="tab"><a href="../../projects/projectPage.html#?project=' + projectname + '"><div class="iframeOverlay"></div><iframe src="' + this.projectData[count].address + '"height="' + h + 'px" width="' + w + 'px" scrolling="no"></iframe></a></div>';

+					content = '<div class="tab"><a href="../../projects/projectPage.html#?project=' + projectId + '"><div class="iframeOverlay"></div><iframe src="' + this.projectData[count].address + '"height="' + h + 'px" width="' + w + 'px" scrolling="no"></iframe></a></div>';

 				}else{

-					content = '<div class="tab"><a href="../../projects/projectPage.html#?project=' + projectname + '"><div class="iframeOverlay"></div><img src="../images/placeholder.png"></img></a></div>';

+					content = '<div class="tab"><a href="../../projects/projectPage.html#?project=' + projectId + '"><div class="iframeOverlay"></div><img src="../images/placeholder.png"></img></a></div>';

 				}

 				

 				listItem.innerHTML = content + '<div class="tileTitle">' + this.projectData[count].name + '</div><div class="tileDate">Last modified: ' + date + '</div>';

@@ -125,13 +125,13 @@
 			

 			for( var count = 0; count< this.projectData.length; count++ ){

 			

-				var projectname = this.projectData[count].name;

+				var projectId = this.projectData[count].id;

 				

 				var uriTemplate = "{OrionHome}/projects/projectPage.html#?project={project}";

 				

 				var template = new URITemplate(uriTemplate);

 				var url = template.expand({

-					project: projectname

+					project: projectId

 				});

 			

 				var row = document.createElement('tr');

@@ -140,7 +140,7 @@
 				

 				var date = this.projectData[count].date.getMonth() + 1 + "." + this.projectData[count].date.getDate() + "." + this.projectData[count].date.getFullYear();

 		

-				row.innerHTML = '<td><a href="../../projects/projectPage.html#?project=' + projectname + '">' + this.projectData[count].name + '</a></td><td>' + 

+				row.innerHTML = '<td><a href="../../projects/projectPage.html#?project=' + projectId + '">' + this.projectData[count].name + '</a></td><td>' + 

 				this.projectData[count].description + '</td><td>' + date + '</td>';

 		

 				this.listNode.firstChild.appendChild( row );

diff --git a/bundles/org.eclipse.orion.client.ui/web/projects/SFTPConfiguration.js b/bundles/org.eclipse.orion.client.ui/web/projects/SFTPConfiguration.js
index 3fefc6e..e5c57bf 100644
--- a/bundles/org.eclipse.orion.client.ui/web/projects/SFTPConfiguration.js
+++ b/bundles/org.eclipse.orion.client.ui/web/projects/SFTPConfiguration.js
@@ -78,14 +78,20 @@
 

 				}

 				

-				this.driveWidget.addRows();		

-				

+				this.driveWidget.addRows();					

 			}

 		}

 

 		function saveConfiguration(){

 			

-			var project = new ProjectData();

+			var project;

+			

+			if( this.projectData ){

+				project = this.projectData;

+			}else{

+				project = new ProjectData();

+			}

+			

 			project.name = this.getProjectName();

 			project.address = this.getProjectAddress();

 			project.drives = this.driveWidget.getJSONDrives();

@@ -94,34 +100,23 @@
 		}

 		

 		function deleteConfiguration(){

-		

-			var x;

+

 			var confirmation = window.confirm( "Are you sure you want to delete this project?" );

 			

-			if( confirmation === true ){

-				

-				console.log( 'delete' );		

-				this.projectDataManager.removeProject( this.getProjectName(), this.handleDeletion.bind( this ) );

-			

-			}else{

-

+			if( confirmation === true ){	

+				this.projectDataManager.removeProject( this.projectData.id, this.handleDeletion.bind( this ) );

 			}

 		}

 		

 		function handleDeletion(){

 		

 			var uriTemplate = "{OrionHome}/projects/project.html#";

-			

-			

 			var template = new URITemplate(uriTemplate);

 			var url = template.expand({

 				project: ''

 			});

 		

 			window.location = url;

-		

-			console.log( 'deleted' );

-		

 		}

 		

 		function handleFeedback( result ){

diff --git a/bundles/org.eclipse.orion.client.ui/web/projects/project.js b/bundles/org.eclipse.orion.client.ui/web/projects/project.js
index 356170b..6bf04d3 100644
--- a/bundles/org.eclipse.orion.client.ui/web/projects/project.js
+++ b/bundles/org.eclipse.orion.client.ui/web/projects/project.js
@@ -17,6 +17,7 @@
 	function( mBootstrap, mGlobalCommands, lib, mSelection, mCommands, mFileClient, mSearchClient, mProjectTree, ProjectGrid, ProjectData, ProjectDataManager ){

 	

 		var projectGrid, mainPanel;

+		var projectMetaData;

 	

 		function showProjectGrid( projectData ){

 			

@@ -33,8 +34,6 @@
 			projectGrid.setProjectData( projectList );

 		}

 		

-		var projectMetaData;

-		

 		mBootstrap.startup().then(

 		

 			function(core) {

@@ -65,7 +64,7 @@
 				

 				this.projectDataManager = new ProjectDataManager( serviceRegistry, fileClient );

 				var projectDataManager = this.projectDataManager;

-				mainPanel.appendChild(document.createTextNode("Loading projects..."));

+				mainPanel.appendChild(document.createTextNode("Loading projects..."));	

 				this.projectDataManager.startup(function() { projectDataManager.getProjectData( showProjectGrid ); }); 

 		});

 	}	

diff --git a/bundles/org.eclipse.orion.client.ui/web/projects/projectPage.js b/bundles/org.eclipse.orion.client.ui/web/projects/projectPage.js
index e74290f..62aa498 100644
--- a/bundles/org.eclipse.orion.client.ui/web/projects/projectPage.js
+++ b/bundles/org.eclipse.orion.client.ui/web/projects/projectPage.js
@@ -27,13 +27,11 @@
 		var myproject;

 		var myworkspace;

 		var mainPanel;

+		var projectId;

 		

 		function addSubComponents( ){

-		

 			var sidePanel = document.getElementById( 'projectNavigation' );

-

-			var projectTree = new ProjectNavigation( myproject, myworkspace, sidePanel, serviceRegistry, commandService, progressService, fileClient, contentTypeService, projectDataManager );

-						

+			var projectTree = new ProjectNavigation( myproject, myworkspace, sidePanel, serviceRegistry, commandService, progressService, fileClient, contentTypeService, projectDataManager );		

 			lib.empty(mainPanel);

 			var configuration = new SFTPConfiguration( myproject, projectDataManager, mainPanel, commandService, serviceRegistry, fileClient );	

 		}

@@ -45,7 +43,7 @@
 			if(project){

 				titleArea.innerHTML = '<strong>Project: </strong>' + project.name;

 			}

-			window.location.hash = '?project=' + project.name;

+			window.location.hash = '?project=' + project.id;

 			

 			myproject = project;

 			myworkspace = workspace;

@@ -61,6 +59,10 @@
 			}

 		}

 		

+		function getProject(){	

+			projectDataManager.getProject( projectId, startProjectComponents ); 

+		}

+		

 		mBootstrap.startup().then(

 		

 		function(core) {

@@ -87,7 +89,7 @@
 			

 			mGlobalCommands.generateBanner("orion-projects", serviceRegistry, commandService, preferences, searcher );			

 			

-			var projectName = PageUtil.matchResourceParameters();

+			projectId = PageUtil.matchResourceParameters();

 

 			/* Create the content */

 			

@@ -95,11 +97,11 @@
 			

 			mainPanel.appendChild(document.createTextNode("Loading project data..."));

 

-			projectName = projectName.resource.split('=')[1];

+			projectId = projectId.resource.split('=')[1];

 			

 			projectDataManager = new ProjectDataManager(serviceRegistry, fileClient);

 			

-			projectDataManager.startup(function() {	projectDataManager.getProject( projectName, startProjectComponents ); });

+			projectDataManager.startup( getProject );

 		});

 	}	

 );
\ No newline at end of file