[f-spot/cleanup-backend: 17/24] Add database objects and providers for photos



commit a91f45d6b10de9303cda00dc5cf2a3bcccd09964
Author: Mike Gemünde <mike gemuende de>
Date:   Tue Jul 13 10:50:25 2010 +0200

    Add database objects and providers for photos
    
    The classes DatabasePhoto and DatabasePhotoVersion represents and
    obeject stored in database. The classes DatabasePhotoModelProvider
    and DatabasePhotoVersionModelProvider are used to access them.

 src/FSpot.Database/DatabasePhoto.cs             |  119 +++++++++++++++++++++++
 src/FSpot.Database/DatabasePhotoVersion.cs      |   95 ++++++++++++++++++
 src/FSpot.Database/Makefile.am                  |    4 +
 src/FSpot.Database/PhotoModelProvider.cs        |   86 ++++++++++++++++
 src/FSpot.Database/PhotoVersionModelProvider.cs |   33 ++++++
 5 files changed, 337 insertions(+), 0 deletions(-)
---
diff --git a/src/FSpot.Database/DatabasePhoto.cs b/src/FSpot.Database/DatabasePhoto.cs
new file mode 100644
index 0000000..e6d32db
--- /dev/null
+++ b/src/FSpot.Database/DatabasePhoto.cs
@@ -0,0 +1,119 @@
+//
+// DatabasePhoto.cs
+//
+// Author:
+//   Mike Gemuende <mike gemuende de>
+// 
+// Copyright (c) 2010 Mike Gemuende <mike gemuende de>
+//
+// This is free software. See COPYING for details.
+//
+
+using System;
+using System.Collections.Generic;
+
+using Hyena.Data;
+using Hyena.Data.Sqlite;
+
+using FSpot;
+
+
+namespace FSpot.Database
+{
+
+    /// <summary>
+    ///    This class implements a Photo which is stored in the database. It is for using
+    ///    it with a SqliteModelProvider.
+    /// </summary>
+    public class DatabasePhoto : IBrowsableItem, ICacheableItem
+    {
+
+#region Constructor
+
+        public DatabasePhoto ()
+        {
+            /* To be removed */            
+            Tags = new Tag[] {  };
+        }
+
+#endregion
+
+#region Database Columns
+
+        [DatabaseColumn("id", Constraints = DatabaseColumnConstraints.PrimaryKey)]
+        public int Id { get; private set; }
+
+        [DatabaseColumn("time")]
+        public System.DateTime Time { get; private set; }
+
+        [DatabaseColumn("base_uri")]
+        private string BaseUri { get; set; }
+
+        [DatabaseColumn("filename")]
+        private string Filename { get; set; }
+
+        [DatabaseColumn("description")]
+        public string Description { get; private set; }
+
+        [DatabaseColumn("roll_id")]
+        public int RollId { get; private set; }
+
+        [DatabaseColumn("default_version_id")]
+        public int DefaultVersionId { get; private set; }
+
+        [DatabaseColumn("rating")]
+        public int DbRating { get; private set; }
+
+#endregion
+
+#region Remaining IBrowsableItem Implementation
+
+        public Tag[] Tags { get; private set; }
+
+        public IBrowsableItemVersion DefaultVersion {
+            get {
+                foreach (var version in Versions) {
+                    if ((version as DatabasePhotoVersion).VersionId == DefaultVersionId)
+                        return version;
+                }
+                return versions[0];
+                throw new Exception (String.Format ("No default Version, something is horrible wrong (photo id {0}, default version {1} but {2})", Id, (versions[0] as DatabasePhotoVersion).VersionId, DefaultVersionId));
+                //return null;
+            }
+        }
+
+        private List<IBrowsableItemVersion> versions;
+        public IEnumerable<IBrowsableItemVersion> Versions {
+            get { return versions; }
+        }
+
+        public string Name {
+            get { return Filename; }
+        }
+
+        public uint Rating {
+            //get { return (uint) (DbRating ?? 0); }
+            get { return (uint)(DbRating); }
+        }
+
+#endregion
+
+#region Remaining IBrowsableItem Implementation
+
+        internal void SetVersions (List<IBrowsableItemVersion> versions)
+        {
+            this.versions = versions;
+        }
+
+#endregion
+
+#region ICachableItem Implementation
+
+        public object CacheEntryId { get; set; }
+
+        public long CacheModelId { get; set; }
+        
+#endregion
+        
+    }
+}
diff --git a/src/FSpot.Database/DatabasePhotoVersion.cs b/src/FSpot.Database/DatabasePhotoVersion.cs
new file mode 100644
index 0000000..9dde142
--- /dev/null
+++ b/src/FSpot.Database/DatabasePhotoVersion.cs
@@ -0,0 +1,95 @@
+//
+// DatabasePhotoVersion.cs
+//
+// Author:
+//   Mike Gemuende <mike gemuende de>
+// 
+// Copyright (c) 2010 Mike Gemuende <mike gemuende de>
+//
+// This is free software. See COPYING for details.
+//
+
+using System;
+using System.Collections.Generic;
+
+using Hyena;
+using Hyena.Data.Sqlite;
+
+using FSpot;
+
+
+namespace FSpot.Database
+{
+
+    public class DatabasePhotoVersion : IBrowsableItemVersion
+    {
+
+#region Constructor
+
+        public DatabasePhotoVersion ()
+        {
+        }
+
+#endregion
+
+#region Database Columns
+
+        [DatabaseColumn("photo_id", Constraints = DatabaseColumnConstraints.PrimaryKey)]
+        public int PhotoId { get; private set; }
+
+        [DatabaseColumn("version_id")]
+        public int VersionId { get; private set; }
+
+        [DatabaseColumn("time")]
+        public System.DateTime Time { get; private set; }
+
+        [DatabaseColumn("base_uri")]
+        private string BaseUriDb {
+            get {
+                if (BaseUri == null)
+                    return null;
+                return BaseUri.AbsoluteUri;
+            }
+            set {
+                if (value == null) {
+                    BaseUri = null;
+                    return;
+                }
+                
+                BaseUri = new SafeUri (value);
+            }
+        }
+
+        [DatabaseColumn("filename")]
+        public string Filename { get; private set; }
+
+        [DatabaseColumn("import_md5")]
+        public string ImportMD5 { get; private set; }
+
+        [DatabaseColumn("protected")]
+        public bool IsProtected { get; private set; }
+#endregion
+
+#region Remaining IBrowsableItem Implementation
+
+        public string Name {
+            get { return Filename; }
+        }
+
+        public SafeUri BaseUri { get; private set; }
+
+        public SafeUri Uri {
+            get { return BaseUri.Append (Filename); }
+            set {
+                if (value == null)
+                    throw new ArgumentNullException ();
+                
+                BaseUri = value.GetBaseUri ();
+                Filename = value.GetFilename ();
+            }
+        }
+        
+#endregion
+        
+    }
+}
diff --git a/src/FSpot.Database/Makefile.am b/src/FSpot.Database/Makefile.am
index 7f2d0e7..2fc8dc1 100644
--- a/src/FSpot.Database/Makefile.am
+++ b/src/FSpot.Database/Makefile.am
@@ -3,7 +3,11 @@ TARGET = library
 LINK = $(REF_FSPOT_DATABASE)
 
 SOURCES = \
+	DatabasePhoto.cs \
+	DatabasePhotoVersion.cs \
 	FSpotDatabaseConnection.cs \
+	PhotoModelProvider.cs \
+	PhotoVersionModelProvider.cs \
 	SqliteCachedModelProvider.cs
 
 
diff --git a/src/FSpot.Database/PhotoModelProvider.cs b/src/FSpot.Database/PhotoModelProvider.cs
new file mode 100644
index 0000000..e7d6409
--- /dev/null
+++ b/src/FSpot.Database/PhotoModelProvider.cs
@@ -0,0 +1,86 @@
+//
+// PhotoModelProvider.cs
+//
+// Author:
+//   Mike Gemuende <mike gemuende de>
+// 
+// Copyright (c) 2010 Mike Gemuende <mike gemuende de>
+//
+// This is free software. See COPYING for details.
+//
+
+using System;
+using System.Text;
+using System.Collections.Generic;
+
+using Hyena;
+using Hyena.Data.Sqlite;
+
+
+/* obsolete references */
+//using FSpot.Query;
+//using System.Data;
+
+namespace FSpot.Database
+{
+
+
+    public class PhotoModelProvider : SqliteCachedModelProvider<DatabasePhoto>
+    {
+
+#region Private Fields
+
+        private PhotoVersionModelProvider photo_versions;
+
+#endregion
+
+#region Constructor
+
+        public PhotoModelProvider (HyenaSqliteConnection connection) : base(connection, "photos")
+        {
+            photo_versions = new PhotoVersionModelProvider (connection);
+        }
+
+#endregion
+
+#region Override SqliteCachedModelProvider Behavior
+
+        protected override void Populate (DatabasePhoto item)
+        {
+            List<IBrowsableItemVersion> versions = new List<IBrowsableItemVersion> ();
+
+            foreach (DatabasePhotoVersion version in photo_versions.FetchAllMatching ("photo_id = ?", item.Id)) {
+                versions.Add (version);
+            }
+
+            if (versions.Count == 0)
+                Log.DebugFormat ("No versions for photo ({0}) found.", item.Id);
+
+            item.SetVersions (versions);
+        }
+
+        public override void Delete (IEnumerable<DatabasePhoto> photos)
+        {
+            List<DatabasePhotoVersion> versions_to_delete = new List<DatabasePhotoVersion> ();
+
+            foreach (DatabasePhoto photo in photos) {
+                foreach (DatabasePhotoVersion version in photo.Versions)
+                    versions_to_delete.Add (version);
+            }
+
+            base.Delete (photos);
+            photo_versions.Delete (versions_to_delete);
+        }
+
+        public override void Delete (long id)
+        {
+            base.Delete (id);
+
+            // FIXME: this only works, because the the photo id is marked as primary key
+            photo_versions.Delete (id);
+        }
+        
+#endregion
+        
+    }
+}
diff --git a/src/FSpot.Database/PhotoVersionModelProvider.cs b/src/FSpot.Database/PhotoVersionModelProvider.cs
new file mode 100644
index 0000000..916403e
--- /dev/null
+++ b/src/FSpot.Database/PhotoVersionModelProvider.cs
@@ -0,0 +1,33 @@
+//
+// PhotoVersionModelProvider.cs
+//
+// Author:
+//   Mike Gemuende <mike gemuende de>
+// 
+// Copyright (c) 2010 Mike Gemuende <mike gemuende de>
+//
+// This is free software. See COPYING for details.
+//
+
+using System;
+
+using Hyena.Data.Sqlite;
+
+
+namespace FSpot.Database
+{
+
+
+    internal class PhotoVersionModelProvider : SqliteModelProvider<DatabasePhotoVersion>
+    {
+
+#region Constructor
+
+        public PhotoVersionModelProvider (HyenaSqliteConnection connection) : base(connection, "photo_versions")
+        {
+        }
+        
+#endregion
+        
+    }
+}



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