/[xmltv_parser]/trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs
ViewVC logotype

Diff of /trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs	2013/03/16 17:56:55	181
+++ trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs	2013/03/16 18:23:11	182
@@ -11,18 +11,23 @@
 using libxmltv.Core;
 namespace GBPVRProgramDatabaseFixer
 {
+    public interface IOldNewProgram
+    {
+        SQLLITE.IPROGRAMME OldProgram { get; }
+        SQLLITE.IPROGRAMME NewProgram { get; }
+    }
     public interface ISQLLITE
     {
         List<SQLLITE.IPROGRAMME> Programs { get; }
         List<SQLLITE.IRECORDING_SCHEDULE> Recordings { get; }
         List<SQLLITE.ICHANNEL> Channels { get; }
         IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs);
-        List<SQLLITE.IPROGRAMME> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> removed_programs);
+        List<IOldNewProgram> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> removed_programs);
 
     }
     public class SQLLITE : ISQLLITE
     {
-        private class OldNewProgram
+        private class OldNewProgram : IOldNewProgram
         {
             public OldNewProgram() : this(new PROGRAMME(), new PROGRAMME()) { }
             public OldNewProgram(IPROGRAMME _old, IPROGRAMME _new) { OldProgram = _old; NewProgram = _new; }
@@ -197,8 +202,8 @@
             #endregion
         }
 
-        public interface IPROGRAMME
-        {
+        public interface IPROGRAMME : IEquatable<IPROGRAMME>
+            {
             Int64 oid { get; set; }
             String name { get; set; }
             String sub_title { get; set; }
@@ -209,6 +214,7 @@
             String unique_identifier { get; set; }
             String rating { get; set; }
 
+            string ToString();
             IProgramDefinition AsXMLTVProgramDefinition(ISQLLITE sqllite);
         }
         private class PROGRAMME : IPROGRAMME
@@ -249,6 +255,40 @@
                 return definition;
             }
             #endregion
+
+            public bool Equals(IPROGRAMME other)
+            {
+                return this.ToString() == other.ToString();
+            }
+            public override string ToString()
+            {
+                StringBuilder builder = new StringBuilder();
+
+                builder.AppendFormat("oid: '{0}' ", oid);
+                builder.AppendFormat("name: '{0}' ", name);
+                builder.AppendFormat("sub_title: '{0}' ", sub_title);
+                builder.AppendFormat("description: '{0}' ", description);
+                builder.AppendFormat("start_time: '{0}' ", start_time.ToDateTimeString());
+                builder.AppendFormat("end_time: '{0}' ", end_time.ToDateTimeString());
+                builder.AppendFormat("channel_oid: '{0}' ", channel_oid);
+                builder.AppendFormat("unique_identifier: '{0}' ", unique_identifier);
+                builder.AppendFormat("rating: '{0}'", rating);
+
+                return builder.ToString();
+            }
+            public override bool Equals(object obj)
+            {
+                if (obj == null) { throw new ArgumentNullException("obj", "Object to compare cannot be null"); }
+                if (obj.GetType().IsAssignableFrom(typeof(IPROGRAMME)))
+                {
+                    return this.Equals((IPROGRAMME)obj);
+                }
+                return base.Equals(obj);
+            }
+            public override int GetHashCode()
+            {
+                return this.ToString().GetHashCode();
+            }
         }
         #endregion
         private static class TABLES
@@ -304,10 +344,10 @@
             return range;
         }
 
-        public List<SQLLITE.IPROGRAMME> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> source_invalid)
+        public List<IOldNewProgram> FixGBPVRProgramsDatabase(List<SQLLITE.IPROGRAMME> gbpvr_programs, List<libxmltv.Interfaces.IProgramDefinition> xmltv_programs, out List<SQLLITE.IPROGRAMME> source_invalid)
         {
             source_invalid = new List<IPROGRAMME>();
-            List<SQLLITE.IPROGRAMME> source_valid = new List<IPROGRAMME>();
+            List<IOldNewProgram> source_valid = new List<IOldNewProgram>();
             gbpvr_programs = gbpvr_programs.OrderBy(s => s.start_time).ToList();
             xmltv_programs = xmltv_programs.OrderBy(s => DateTime.Parse(s.Start)).ToList();
             gbpvr_programs.TrimExcess();
@@ -334,14 +374,45 @@
                     if(!xmltv_entry.Equals(program_xmltv_entry))
                     {
                         // data is different
-                        if (Debugger.IsAttached)
+                        var updated_program = program;
+
+                        if (program_xmltv_entry.Title != xmltv_entry.Title)
+                        {
+                            gLog.Verbose.Warn.WriteLine("    Program oid: '{0}' - replacing title", updated_program.oid);
+                            updated_program.name = xmltv_entry.Title;
+                        }
+                        if (program_xmltv_entry.SubTitle != xmltv_entry.SubTitle)
+                        {
+                            gLog.Verbose.Warn.WriteLine("    Program oid: '{0}' - replacing sub_title", updated_program.oid);
+                            updated_program.sub_title = xmltv_entry.SubTitle;
+                        }
+                        if (program_xmltv_entry.Stop != xmltv_entry.Stop)
+                        {
+                            gLog.Verbose.Warn.WriteLine("    Program oid: '{0}' - replacing end_time", updated_program.oid);
+                            updated_program.end_time = DateTime.Parse(xmltv_entry.Stop);
+                        }
+                        if (program_xmltv_entry.Start != xmltv_entry.Start)
+                        {
+                            gLog.Verbose.Warn.WriteLine("    Program oid: '{0}' - replacing start_time", updated_program.oid);
+                            updated_program.start_time = DateTime.Parse(xmltv_entry.Start);
+                        }
+                        if (program_xmltv_entry.Description != xmltv_entry.Description)
+                        {
+                            gLog.Verbose.Warn.WriteLine("    Program oid: '{0}' - replacing description", updated_program.oid);
+                            updated_program.description = xmltv_entry.Description;
+                        }
+
+                        var updated_xmltv_entry = updated_program.AsXMLTVProgramDefinition(this);
+                        if (!xmltv_entry.Equals(updated_xmltv_entry))
                         {
-                            Debugger.Break();
+                            throw new Exception(string.Format("Program oid '{0}' was not properly updated.", updated_program.oid));
                         }
+                        source_valid.Add(new OldNewProgram(program, updated_program));
                     }
                     else
                     {
                         // data is the same
+                        source_valid.Add(new OldNewProgram(program, program));
                     }
                 }
                 index++;

 

  ViewVC Help
Powered by ViewVC 1.1.22