[longomatch/redesign3: 151/156] Use project UUID as primery key in the DB instead of the filename



commit 4fa780c804771bd3fa22899cde5db9139b8c7f7f
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Thu Aug 18 00:09:28 2011 +0200

    Use project UUID as primery key in the DB instead of the filename
    
    This will allow among other things having again several projects
    for the same file.

 LongoMatch/Gui/Dialog/ProjectsManager.cs |   23 +-----
 LongoMatch/Gui/MainWindow.cs             |    2 +-
 LongoMatch/Services/DataBase.cs          |  129 ++++++++++++------------------
 LongoMatch/Store/Project.cs              |   10 ++-
 LongoMatch/Store/ProjectDescription.cs   |    9 ++-
 5 files changed, 71 insertions(+), 102 deletions(-)
---
diff --git a/LongoMatch/Gui/Dialog/ProjectsManager.cs b/LongoMatch/Gui/Dialog/ProjectsManager.cs
index 3bbf7c8..ea2d9ae 100644
--- a/LongoMatch/Gui/Dialog/ProjectsManager.cs
+++ b/LongoMatch/Gui/Dialog/ProjectsManager.cs
@@ -33,7 +33,6 @@ namespace LongoMatch.Gui.Dialog
 	public partial class ProjectsManager : Gtk.Dialog
 	{
 
-		private string originalFilePath;
 		private Project openedProject;
 		private List<ProjectDescription> selectedProjects;
 
@@ -52,7 +51,6 @@ namespace LongoMatch.Gui.Dialog
 			projectlistwidget1.ClearSearch();
 			projectlistwidget1.SelectionMode = SelectionMode.Multiple;
 			Clear();
-			originalFilePath=null;
 		}
 
 		private void Clear() {
@@ -80,20 +78,8 @@ namespace LongoMatch.Gui.Dialog
 			if(project == null)
 				return;
 
-			if(project.Description.File.FilePath == originalFilePath) {
-				MainClass.DB.UpdateProject(project);
-				saveButton.Sensitive = false;
-			}
-			else {
-				try {
-					MainClass.DB.UpdateProject(project,originalFilePath);
-					saveButton.Sensitive = false;
-				}
-				catch {
-					MessagePopup.PopupMessage(this, MessageType.Warning,
-					                          Catalog.GetString("A Project is already using this file."));
-				}
-			}
+			MainClass.DB.UpdateProject(project);
+			saveButton.Sensitive = false;
 			projectlistwidget1.QueueDraw();
 		}
 
@@ -119,7 +105,7 @@ namespace LongoMatch.Gui.Dialog
 				                                     Catalog.GetString("Do you really want to delete:")+
 				                                     "\n"+selectedProject.Title);
 				if(md.Run()== (int)ResponseType.Yes) {
-					MainClass.DB.RemoveProject(selectedProject.File.FilePath);
+					MainClass.DB.RemoveProject(selectedProject.UUID);
 					deletedProjects.Add(selectedProject);
 				}
 				md.Destroy();
@@ -179,8 +165,7 @@ namespace LongoMatch.Gui.Dialog
 			}
 			else {
 				projectdetails.Sensitive = true;
-				projectdetails.SetProject(MainClass.DB.GetProject(project.File.FilePath));
-				originalFilePath = project.File.FilePath;
+				projectdetails.SetProject(MainClass.DB.GetProject(project.UUID));
 				saveButton.Sensitive = false;
 				deleteButton.Sensitive = true;
 				exportbutton.Sensitive = true;
diff --git a/LongoMatch/Gui/MainWindow.cs b/LongoMatch/Gui/MainWindow.cs
index 9169925..4b8d634 100644
--- a/LongoMatch/Gui/MainWindow.cs
+++ b/LongoMatch/Gui/MainWindow.cs
@@ -386,7 +386,7 @@ namespace LongoMatch.Gui
 				project = opd.SelectedProject;
 			opd.Destroy();
 			if(project != null)
-				SetProject(MainClass.DB.GetProject(project.File.FilePath), ProjectType.FileProject, new CaptureSettings());
+				SetProject(MainClass.DB.GetProject(project.UUID), ProjectType.FileProject, new CaptureSettings());
 		}
 
 		protected virtual void OnSaveProjectActionActivated(object sender, System.EventArgs e)
diff --git a/LongoMatch/Services/DataBase.cs b/LongoMatch/Services/DataBase.cs
index 9807a62..6c5ab0b 100644
--- a/LongoMatch/Services/DataBase.cs
+++ b/LongoMatch/Services/DataBase.cs
@@ -167,19 +167,21 @@ namespace LongoMatch.DB
 		/// <returns>
 		/// A <see cref="LongoMatch.DB.Project"/>
 		/// </returns>
-		public Project GetProject(String filename) {
-			Project ret;
+		public Project GetProject(Guid id) {
+			Project ret = null;
 			IObjectContainer db = Db4oFactory.OpenFile(file);
+			
 			try	{
-				IQuery query = GetQueryWithContrains(db, file);
+				IQuery query = GetQueryProjectById (db, id);
 				IObjectSet result = query.Execute();
 				ret = (Project) db.Ext().PeekPersisted(result.Next(),10,true);
-				return ret;
-			}
-			finally
-			{
+			} catch (Exception e) {
+				Log.Error("Could not get project with ID: " + id);
+				Log.Exception(e);
+			} finally {
 				CloseDB(db);
 			}
+			return ret;
 		}
 
 		/// <summary>
@@ -190,13 +192,12 @@ namespace LongoMatch.DB
 		/// </param>
 		public void AddProject(Project project) {
 			IObjectContainer db = Db4oFactory.OpenFile(file);
-			try
-			{
-				if(!Exists(project.Description.File.FilePath,db)) {
-					db.Store(project);
-					db.Commit();
-				}
-				else throw new Exception(Catalog.GetString("The Project for this video file already exists.")+"\n"+Catalog.GetString("Try to edit it with the Database Manager"));
+			try {
+				db.Store(project);
+				db.Commit();
+			} catch (Exception e) {
+				Log.Error("Could not add project");
+				Log.Exception(e);
 			}
 			finally {
 				CloseDB(db);
@@ -209,27 +210,28 @@ namespace LongoMatch.DB
 		/// <param name="filePath">
 		/// A <see cref="System.String"/> with the project's video file path
 		/// </param>
-		public void RemoveProject(string filePath) {
+		public void RemoveProject(Guid id) {
 			SetDeleteCascadeOptions();
 			IObjectContainer db = Db4oFactory.OpenFile(file);
 			try	{
-				IQuery query = GetQueryWithContrains(db, file);
+				IQuery query = GetQueryProjectById(db, id);
 				IObjectSet result = query.Execute();
 				Project project = (Project)result.Next();
 				db.Delete(project);
 				db.Commit();
-			}
-			finally
-			{
+			} catch (Exception e) {
+				Log.Error("Could not delete project");
+				Log.Exception(e);
+			} finally {
 				CloseDB(db);
 			}
+			ListObjects();
 		}
 
 		/// <summary>
 		/// Updates a project in the database. Because a <see cref="LongoMatch.DB.Project"/> has
 		/// many objects associated, a simple update would leave in the databse many orphaned objects.
-		/// Therefore we need to delete the old project a replace it with the changed one. We need to
-		/// now the old file path associate to this project in case it has been changed in the update
+		/// Therefore we need to delete the old project a replace it with the changed one.
 		/// </summary>
 		/// <param name="project">
 		/// A <see cref="Project"/> to update
@@ -237,57 +239,27 @@ namespace LongoMatch.DB
 		/// <param name="previousFileName">
 		/// A <see cref="System.String"/> with the old file path
 		/// </param>
-		public void UpdateProject(Project project, string previousFileName) {
-			bool error = false;
-			// Configure db4o to cascade on delete for each one of the objects stored in a Project
-			SetDeleteCascadeOptions();
-			IObjectContainer db = Db4oFactory.OpenFile(file);
-			try	{
-				// We look for a project with the same filename
-				if(!Exists(project.Description.File.FilePath,db)) {
-					IQuery query = GetQueryWithContrains(db, file);
-					IObjectSet result = query.Execute();
-					//Get the stored project and replace it with the new one
-					if(result.Count == 1) {
-						Project fd = (Project)result.Next();
-						db.Delete(fd);
-						// Add the updated project
-						db.Store(project);
-						db.Commit();
-					} else {
-						error = true;
-					}
-				}
-				else
-					error = true;
-			}
-			finally {
-				CloseDB(db);
-				if(error)
-					throw new Exception();
-			}
-		}
-
-		/// <summary>
-		/// Updates a project in the databse whose file path hasn't changed
-		/// </summary>
-		/// <param name="project">
-		/// A <see cref="Project"/> to update
-		/// </param>
 		public void UpdateProject(Project project) {
+			// Configure db4o to cascade on delete for each one of the objects stored in a Project
 			SetDeleteCascadeOptions();
 			IObjectContainer db = Db4oFactory.OpenFile(file);
+			
 			try	{
-				IQuery query = GetQueryWithContrains(db, file);
+				IQuery query = GetQueryProjectById(db, project.UUID);
 				IObjectSet result = query.Execute();
 				//Get the stored project and replace it with the new one
-				Project fd = (Project)result.Next();
-				db.Delete(fd);
-				db.Store(project);
-				db.Commit();
-			}
-			finally
-			{
+				if(result.Count == 1) {
+					Project fd = (Project)result.Next();
+					db.Delete(fd);
+					db.Store(project);
+					db.Commit();
+				} else {
+					Log.Warning("Project with ID " + project.UUID + "not found");
+				}
+			} catch (Exception e) {
+				Log.Error("Could not update project");
+				Log.Exception(e);
+			} finally {
 				CloseDB(db);
 			}
 		}
@@ -302,14 +274,20 @@ namespace LongoMatch.DB
 		/// A <see cref="System.Boolean"/>
 		/// </returns>
 		public bool Exists(Project project) {
+			bool ret;
 			IObjectContainer db = Db4oFactory.OpenFile(file);
+		
 			try {
-				return Exists(project.Description.File.FilePath, db);
+				IQuery query = GetQueryProjectById(db, project.UUID);
+				IObjectSet result = query.Execute();
+				ret = result.HasNext();
 			} catch {
-				return false;
+				ret = false;
 			} finally {
 				CloseDB(db);
 			}
+			
+			return ret;
 		}
 		
 		private void CreateNewDB () {
@@ -385,14 +363,15 @@ namespace LongoMatch.DB
 				return;
 		
 			File.Move(file, Path.Combine(MainClass.DBDir(), backupFilename));
+			Log.Information ("Created backup for database at ", backupFilename);
 			lastBackup = new BackupDate {Date = now};
 			UpdateBackupDate();
 		}
 
-		private IQuery GetQueryWithContrains(IObjectContainer db, string filename) {
+		private IQuery GetQueryProjectById(IObjectContainer db, Guid id) {
 			IQuery query = db.Query();
 			query.Constrain(typeof(Project));
-			query.Descend("description").Descend("file").Descend("filePath").Constrain(filename);
+			query.Descend("_UUID").Constrain(id);
 			return query;
 		}
 
@@ -431,15 +410,7 @@ namespace LongoMatch.DB
 			Db4oFactory.Configure().ObjectClass(typeof(Drawing)).CascadeOnUpdate(true);
 		}
 
-		private bool Exists(string filename, IObjectContainer db) {
-			IQuery query = db.Query();
-			query.Constrain(typeof(Project));
-			query.Descend("file").Descend("filePath").Constrain(filename);
-			IObjectSet result = query.Execute();
-			return (result.HasNext());
-		}
-		
-		
+
 		/* Dummy class to allow having a single instance of BackupDateTime in the DB and make it
 		 * easIer to query */
 		protected class BackupDate 
diff --git a/LongoMatch/Store/Project.cs b/LongoMatch/Store/Project.cs
index 2ddfdd2..05b3a27 100644
--- a/LongoMatch/Store/Project.cs
+++ b/LongoMatch/Store/Project.cs
@@ -49,6 +49,7 @@ namespace LongoMatch.Store
 	{
 
 		private readonly Guid _UUID;
+		private ProjectDescription description;
 		private List<Play> timeline;
 
 		#region Constructors
@@ -73,8 +74,13 @@ namespace LongoMatch.Store
 		}
 		
 		public ProjectDescription Description {
-			get;
-			set;
+			get{
+				return description;
+			}
+			set {
+				value.UUID = UUID;
+				description = value;
+			}
 		}
 
 		/// <value>
diff --git a/LongoMatch/Store/ProjectDescription.cs b/LongoMatch/Store/ProjectDescription.cs
index ab6806e..144aa86 100644
--- a/LongoMatch/Store/ProjectDescription.cs
+++ b/LongoMatch/Store/ProjectDescription.cs
@@ -29,7 +29,14 @@ namespace LongoMatch.Store
 	[Serializable]
 	public class ProjectDescription :  IComparable
 	{
-
+		/// <summary>
+		/// Unique ID of the parent project
+		/// </summary>
+		public Guid UUID {
+			get;
+			set;
+		}
+		
 		/// <summary>
 		/// Title of the project
 		/// </summary>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]