/[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 16:06:20	175
+++ trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs	2013/03/16 16:22:43	176
@@ -22,6 +22,11 @@
     }
     public class SQLLITE : ISQLLITE
     {
+        private struct OldNewProgram
+        {
+            public IPROGRAMME OldProgram;
+            public IPROGRAMME NewProgram;
+        }
         public static ISQLLITE Create(string database, EventHandler<EventArgs> OnInstanceCreated)
         {
             return new SQLLITE(database, OnInstanceCreated);
@@ -298,6 +303,8 @@
             source_invalid = new List<IPROGRAMME>();
             gbpvr_programs = gbpvr_programs.OrderBy(s => s.start_time).ToList();            
             List<SQLLITE.IPROGRAMME> source_valid = new List<IPROGRAMME>();
+
+            List<OldNewProgram> source_update = new List<OldNewProgram>();
             //if (range == null)
             //{
             //    gLog.Warn.WriteLine("The DateTimeRange passed in is null...returning the original program list");
@@ -347,12 +354,7 @@
                 if (xmltv_entry == null)
                 {
                     gLog.Warn.WriteLine("Invalidating GB-PVR Program: oid='{0}' channel_oid='{1}' title='{2}' start='{3}'", program.oid, program.channel_oid, program.name, program.start_time.ToDateTimeString());
-                    source_invalid.Add(program);
-                    // remove database entry
-                    if (!RemoveProgramDatabaseEntry(program))
-                    {
-                        gLog.Error.WriteLine("Failed to remove entry with oid: '{0}'", program.oid);
-                    }
+                    source_invalid.Add(program);                   
                 }
                 else
                 {
@@ -402,29 +404,65 @@
                     source_valid.Add(updated_program);
                     if (needsupdate)
                     {
-                        // update database entry
-                        if (!UpdateProgramDatabaseEntry(program, updated_program))
-                        {
-                            gLog.Error.WriteLine("Failed to update entry with oid: '{0}'", program.oid);
-                        }
+                        //// update database entry
+                        //if (!UpdateProgramDatabaseEntry(program, updated_program))
+                        //{
+                        //    gLog.Error.WriteLine("Failed to update entry with oid: '{0}'", program.oid);
+                        //}
+                        OldNewProgram p = new OldNewProgram();
+                        p.OldProgram = program;
+                        p.NewProgram = updated_program;
+                        source_update.Add(p);
                     }
+                   
                 }
                 index++;
             }
 
-            //for (int i = 0; i < programs.Count(); i++)
-            //{
-            //    var gbpvr_entry = gbpvr[i];
-            //    var xmltv_entry = gbpvr_entry.AsXMLTVProgramDefinition(sqlite);
-            //    if (!xmltv[i].Equals(xmltv_entry))
-            //    {
-            //        gLog.Warn.WriteLine("Warning GBPVR Program oid: {0} might be invalid", gbpvr_entry.oid);
-            //    }
-            //}
 
             source_valid = source_valid.OrderBy(s => s.channel_oid).ThenBy(s => s.start_time).ToList();
             source_invalid = source_invalid.OrderBy(s => s.channel_oid).ThenBy(s => s.start_time).ToList();
 
+            if (source_invalid.Count > 0)
+            {
+                foreach (var old_program in source_invalid)
+                {
+                    // remove database entry
+                    if (!RemoveProgramDatabaseEntry(old_program))
+                    {
+                        gLog.Error.WriteLine("Failed to remove program with oid: '{0}'", old_program.oid);
+                    }
+                    else
+                    {
+                        gLog.Info.WriteLine("Removed program with oid: '{0}'", old_program.oid);
+                    }
+                }
+            }
+            else
+            {
+                gLog.Info.WriteLine("No old GB-PVR Programs needed to be removed.");
+            }
+
+            if (source_update.Count > 0)
+            {
+                foreach (var p in source_update)
+                {
+                    // remove database entry
+                    if (!UpdateProgramDatabaseEntry(p.OldProgram,p.NewProgram))
+                    {
+                        gLog.Error.WriteLine("Failed to update program with oid: '{0}'", p.OldProgram.oid);
+                    }
+                    else
+                    {
+                        gLog.Info.WriteLine("Upated program with oid: '{0}'", p.OldProgram.oid);
+                    }
+                }
+            }
+            else
+            {
+                gLog.Info.WriteLine("No GB-PVR Programs needed to be updated.");
+            }
+
             gLog.Info.WriteLine("Total XMLTV Programs: 0x{0:x8}", xmltv_programs.Count);
             gLog.Info.WriteLine("Updated: 0x{0:x8} GB-PVR Programs", source_valid.Count);
             gLog.Info.WriteLine("Removed: 0x{0:x8} GB-PVR Programs", source_invalid.Count);
@@ -641,7 +679,7 @@
                 {
                     try
                     {
-                        gLog.Info.WriteLine("Removing old program with oid: '{0}'", old_program.oid);
+                        gLog.Verbose.Info.WriteLine("Removing old program with oid: '{0}'", old_program.oid);
                         con.Open();
                         string command_text = string.Format(@"DELETE FROM [{0}] WHERE [oid] = @oid;", TABLES.PROGRAMME);
                         gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
@@ -649,7 +687,7 @@
                         {
                             cmd.Parameters.Add(new SQLiteParameter("oid", old_program.oid));
                             int rowsupdated = cmd.ExecuteNonQuery();
-                            gLog.Info.WriteLine("Updated '{0}' rows", rowsupdated);
+                            gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
                         }
                         con.Clone();
                         result = true;
@@ -677,7 +715,7 @@
                 {
                     try
                     {
-                        gLog.Info.WriteLine("Updating old program with oid: '{0}'", old_program.oid);
+                        gLog.Verbose.Info.WriteLine("Updating old program with oid: '{0}'", old_program.oid);
                         con.Open();
                         //string command_text = string.Format(@"UPDATE [{0}] SET [name]=@name,[sub_title]=@subtitle, WHERE [OID] = @oid", TABLES.PROGRAMME);
                         string command_text = string.Empty;
@@ -709,7 +747,7 @@
                             cmd.Parameters.Add(new SQLiteParameter("unique_identifier", new_program.unique_identifier));
                             cmd.Parameters.Add(new SQLiteParameter("rating", new_program.rating));
                             int rowsupdated = cmd.ExecuteNonQuery();
-                            gLog.Info.WriteLine("Updated '{0}' rows", rowsupdated);
+                            gLog.Verbose.Info.WriteLine("Updated '{0}' rows", rowsupdated);
                         }
                         con.Clone();
                         result = true;

 

  ViewVC Help
Powered by ViewVC 1.1.22