/[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/14 19:36:01	157
+++ trunk/GBPVRProgramDatabaseFixer/SQLLITE.cs	2013/03/15 16:57:50	164
@@ -6,12 +6,57 @@
 using System.Data.SQLite;
 using System.Diagnostics;
 using Enterprise.Logging;
-
+using libxmltv;
 namespace GBPVRProgramDatabaseFixer
 {
-    internal class SQLLITE
+    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> FilterProgramsByDateRange(List<SQLLITE.IPROGRAMME> programs, IDateTimeRange range, out List<SQLLITE.IPROGRAMME> removed_programs);
+
+    }
+    public class SQLLITE : ISQLLITE
+    {
+        public static ISQLLITE Create(string database, EventHandler<EventArgs> OnInstanceCreated)
+        {
+            return new SQLLITE(database, OnInstanceCreated);
+        }
+
         #region DATABASE DEFINITIONS
+
+        public interface ICHANNEL
+        {
+            Int64 oid { get; }
+            String name { get; }
+            String channelID { get; }
+            Int64 channel_number { get; }
+            String favourite_channel { get; }
+            String display_name { get; }
+        }
+        private class CHANNEL : ICHANNEL
+        {
+            public CHANNEL() 
+            {
+                BaseDatabaseDefinition<CHANNEL>.CreateDefault(this); 
+            }
+            //public RECORDING_SCHEDULE(SQLiteDataReader r, int index) { BaseDatabaseDefinition<RECORDING_SCHEDULE>.Create(this, r, index); }
+
+            public static void Create(ref CHANNEL instance, SQLiteDataReader r, int index)
+            {
+                BaseDatabaseDefinition<CHANNEL>.Create(ref instance, r, index);
+            }
+            #region ICHANNEL members
+            public Int64 oid { get; set; }
+            public String name { get; set; }
+            public String channelID { get; set; }
+            public Int64 channel_number { get; set; }
+            public String favourite_channel { get; set; }
+            public String display_name { get; set; }
+            #endregion
+        }
         public interface IRECORDING_SCHEDULE
         {
             Int64 oid { get; }
@@ -182,10 +227,11 @@
         {
             public const string RECORDING_SCHEDULE = "RECORDING_SCHEDULE";
             public const string PROGRAMME = "PROGRAMME";
+            public const string CHANNEL = "CHANNEL";
         }
 
         //public SQLLite() { }
-        public SQLLITE(string database, EventHandler<EventArgs> OnInstanceCreated) 
+        protected SQLLITE(string database, EventHandler<EventArgs> OnInstanceCreated) 
         {
             this.OnInstanceCreated = OnInstanceCreated;
             //CreateConnection(database);
@@ -196,6 +242,7 @@
                 return;
             }
             ConnectionTest();
+            ReadChannelData();
             ReadRecodringScheduleData();
             ReadProgrammeData();
             OnCreatedInstance(this, new EventArgs());
@@ -210,10 +257,54 @@
             if (OnInstanceCreated != null) { OnInstanceCreated.Invoke(sender, e); }
         }
 
-        #region SQLLite 
         private string Database;
+        #region ISQLLITE members        
         public List<IPROGRAMME> Programs { get; private set; }
         public List<IRECORDING_SCHEDULE> Recordings { get; private set; }
+        public List<ICHANNEL> Channels { get; private set; }
+
+        public IDateTimeRange GetProgramsDateRange(List<SQLLITE.IPROGRAMME> programs)
+        {
+            var list = new List<IPROGRAMME>(programs.ToArray());
+            DateTime first = new DateTime();
+            DateTime last = new DateTime();
+            first = list.OrderBy(s => s.start_time).ToList().First().start_time;            
+            last = list.OrderBy(s => s.start_time).ToList().Last().start_time;
+            gLog.Info.WriteLine("\tFirst: {0} = ({1})", first.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), first.ToDateTimeString());
+            gLog.Info.WriteLine("\tLast: {0} = ({1})", last.ToString("yyyy/MM/dd HH:mm:ss.fffffff"), last.ToDateTimeString());
+            var range = DateTimeRange.Create(first, last);
+            return range;
+        }
+
+        public List<SQLLITE.IPROGRAMME> FilterProgramsByDateRange(List<SQLLITE.IPROGRAMME> programs, IDateTimeRange range, out List<SQLLITE.IPROGRAMME> removed_programs)
+        {
+            removed_programs = new List<IPROGRAMME>();
+            List<SQLLITE.IPROGRAMME> list = new List<IPROGRAMME>(programs.ToArray());
+            list = list.OrderBy(s => s.start_time).ToList();
+            List<SQLLITE.IPROGRAMME> list2 = new List<IPROGRAMME>();
+            if (range == null)
+            {
+                gLog.Warn.WriteLine("The DateTimeRange passed in is null...returning the original program list");
+                return list;
+            }
+            //gLog.Warn.WriteLine("FilterProgramsByDateRange has not been implemented");
+           
+            foreach (var program in list)
+            {
+                if (program.start_time >= range.Start &&
+                    program.start_time <= range.End)
+                {
+                    list2.Add(program);
+                }
+                else
+                {
+                    removed_programs.Add(program);
+                }
+            }
+            list2 = list2.OrderBy(s => s.channel_oid).ThenBy(s => s.start_time).ToList();
+            removed_programs = removed_programs.OrderBy(s => s.channel_oid).ThenBy(s => s.start_time).ToList();
+            return list2;
+        }
         #endregion
 
 
@@ -233,7 +324,7 @@
                 using (SQLiteConnection con = CreateConnection())
                 {
                     con.Open();
-                    string command_text = string.Format("select * from {0};", TABLES.PROGRAMME);
+                    string command_text = string.Format("select * from {0};", TABLES.CHANNEL);
                     gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
                     using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
                     {
@@ -288,7 +379,47 @@
                 return false;
             }
         }
-
+        private void ReadChannelData()
+        {
+            try
+            {
+                List<ICHANNEL> channels = new List<ICHANNEL>();
+                using (SQLiteConnection con = CreateConnection())
+                {
+                    con.Open();
+                    string command_text = string.Format("select * from {0};", TABLES.CHANNEL);
+                    gLog.Verbose.Debug.WriteLine("Executing Command: '{0}'", command_text);
+                    using (SQLiteCommand cmd = new SQLiteCommand(command_text, con))
+                    {
+                        using (SQLiteDataReader r = cmd.ExecuteReader())
+                        {
+                            if (!r.HasRows)
+                            {
+                                gLog.Warn.WriteLine("Query: '{0}' returned no rows.", cmd.CommandText);
+                            }
+                            else
+                            {
+                                while (r.Read())
+                                {
+                                    CHANNEL channel = new CHANNEL();
+                                    for (int i = 0; i < r.FieldCount; i++)
+                                    {
+                                        CHANNEL.Create(ref channel, r, i);
+                                    }
+                                    channels.Add(channel);
+                                }
+                            }
+                        }
+                    }
+                    con.Clone();
+                }
+                this.Channels = channels;
+            }
+            catch (Exception ex)
+            {
+                gLog.Error.WriteLine(ex.ToString());
+            }
+        }
         private void ReadProgrammeData()
         {
             try
@@ -357,7 +488,6 @@
                                     {
                                         RECORDING_SCHEDULE.Create(ref recording, r, i);
                                     }
-
                                     recordings.Add(recording);
                                 }
                             }
@@ -372,5 +502,6 @@
                 gLog.Error.WriteLine(ex.ToString());
             }
         }
+
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22