/[xmltv_parser]/trunk/libxmltv/Core/DataSourceBindable.cs
ViewVC logotype

Diff of /trunk/libxmltv/Core/DataSourceBindable.cs

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

--- trunk/libxmltv/Core/DataSourceBindable.cs	2013/03/13 15:16:59	131
+++ trunk/libxmltv/Core/DataSourceBindable.cs	2013/03/16 17:56:55	181
@@ -4,6 +4,7 @@
 using System.Text;
 using libxmltv.Interfaces;
 using System.Collections;
+using Enterprise.Logging;
 
 namespace libxmltv.Core
 {
@@ -34,6 +35,19 @@
             public string ChannelId { get; internal set; }
             public string ChannelName { get; internal set; }
 
+            public override string ToString()
+            {
+                StringBuilder builder = new StringBuilder();
+                Type t = typeof(ChannelDefintion);
+                var props = t.GetProperties();
+                foreach (var prop in props)
+                {
+                    string name = prop.Name;
+                    object value = prop.GetValue(this, null);
+                    builder.AppendFormat("\t{0}: '{1}'", name, value == null ? "null" : value.ToString());
+                }
+                return builder.ToString();
+            }
             
         }
         //static private List<string> known_columns;
@@ -71,7 +85,8 @@
                 {
                     ChannelDefintion definition = new ChannelDefintion();
                     definition.ChannelId = t.Id;
-                    definition.ChannelName = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();                    
+                    definition.ChannelName = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
+                    xmltv_logger.Verbose.Debug.WriteLine(definition.ToString());
                     list.Add(definition);
                 }
                 catch (Exception ex) { throw ex; }
@@ -99,6 +114,7 @@
                     var col = args.First().ToString().ToLower();
                     switch (col)
                     {
+                        case "channelnumber": ordered = descending ? list.OrderByDescending(s => s.ChannelNumber) : list.OrderBy(s => s.ChannelNumber); break;
                         case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break;
                         case "start": ordered = descending ? list.OrderByDescending(s => s.Start) : list.OrderBy(s => s.Start); break;
                         case "stop": ordered = descending ? list.OrderByDescending(s => s.Stop) : list.OrderBy(s => s.Stop); break;
@@ -112,7 +128,8 @@
                         col = arg.ToLower();
                         switch (col)
                         {
-                            case "channelname": ordered = descending ? ordered.ThenByDescending(s => s.ChannelName) : ordered.ThenBy(s => s.ChannelName); break;
+                            case "channelnumber": ordered = descending ? ordered.ThenByDescending(s => s.ChannelNumber) : ordered.ThenBy(s => s.ChannelNumber); break;
+                            case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break;
                             case "start": ordered = descending ? ordered.ThenByDescending(s => s.Start) : ordered.ThenBy(s => s.Start); break;
                             case "stop": ordered = descending ? ordered.ThenByDescending(s => s.Stop) : ordered.ThenBy(s => s.Stop); break;
                             case "description": ordered = descending ? ordered.ThenByDescending(s => s.Description) : ordered.ThenBy(s => s.Description); break;
@@ -129,30 +146,94 @@
             #region IDataSourceFilterable members
             public void Filter(ref object source, params string[] args)
             {
+                IProgramDefinitionList list = null;
+                List<IProgramDefinition> ordered = null;
+                try
+                {
+                    list = (ProgramDefinitionList)source;
+                    //var col = args.First().ToString().ToLower();
+                    //var data = args.().ToString().ToLower();
+
+                    if (args.Count() != 2) { throw new ArgumentOutOfRangeException("args", "Excpected 2 aguments: columnname and filtertext"); }
+
+                    string columnname = args[0].ToLower();
+                    string filtertext = args[1].ToLower();
+
+                    switch (columnname)
+                    {
+                        case "channelnumber": ordered = list.ToList().FindAll(s => s.ChannelNumber == Convert.ToInt32(filtertext)); break;
+                        case "channelname": ordered = list.ToList().FindAll(s => s.ChannelName.ToLower().Contains(filtertext)); break;
+                        case "start": ordered = list.ToList().FindAll(s => s.Start.ToLower().Contains(filtertext)); break;
+                        case "stop": ordered = list.ToList().FindAll(s => s.Stop.ToLower().Contains(filtertext)); break;
+                        case "description": ordered = list.ToList().FindAll(s => s.Description.ToLower().Contains(filtertext)); break;
+                        case "title": ordered = list.ToList().FindAll(s => s.Title.ToLower().Contains(filtertext)); break;
+                        case "subtitle": ordered = list.ToList().FindAll(s => s.SubTitle.ToLower().Contains(filtertext)); break;
+                    }                   
+                    list = new ProgramDefinitionList(ordered.ToList());
+                    if (list.Count == 0)
+                    {
+                        list = (ProgramDefinitionList)source;
+                        gLog.Warn.WriteLine("Filering by: column='{0}' with filer='{1}' returned no data",columnname, filtertext);
+                    }
+                }
+                catch (Exception ex) { throw ex; }
+                if (list != null) { source = list; }
             }
             #endregion
         }
-        private class ProgramDefintion : IProgramDefinition
+        public class ProgramDefintion : IProgramDefinition, IEquatable<IProgramDefinition>
         {
             public ProgramDefintion()
             {
                 //ChannelId = string.Empty;
+                ChannelNumber = 0;
                 ChannelName = string.Empty;
-                Start = new DateTime();
-                Stop = new DateTime();
+                Start = new DateTime().ToDateTimeString();
+                Stop = new DateTime().ToDateTimeString();
                 Description = string.Empty;
                 Title = string.Empty;
                 SubTitle = string.Empty;
             }
             //public string ChannelId { get; internal set; }
-            public string ChannelName { get; internal set; }
-            public DateTime Start { get; internal set; }
-            public DateTime Stop { get; internal set; }
-            public string Title { get; internal set; }
-            public string SubTitle { get; internal set; }
-            public string Description { get; internal set; }
+            public int ChannelNumber { get; set; }
+            public string ChannelName { get; set; }
+            public string Start { get; set; }
+            public string Stop { get; set; }
+            public string Title { get; set; }
+            public string SubTitle { get; set; }
+            public string Description { get; set; }
 
-          
+            public override string ToString()
+            {
+                StringBuilder builder = new StringBuilder();
+                Type t = typeof(ProgramDefintion);
+                var props = t.GetProperties();
+                foreach (var prop in props)
+                {
+                    string name = prop.Name;
+                    object value = prop.GetValue(this, null);
+                    builder.AppendFormat("\t{0}: '{1}'", name, value == null ? "null" : value.ToString());
+                }
+                return builder.ToString();
+            }
+
+            public bool Equals(IProgramDefinition other)
+            {
+                return this.ToString() == other.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(IProgramDefinition)))
+                {
+                    return this.Equals((IProgramDefinition)obj);
+                }
+                return base.Equals(obj);
+            }
+            public override int GetHashCode()
+            {
+                return this.ToString().GetHashCode();
+            }
         }
         public ProgramList() { }
         //public object ConvertObjectData(object source) { return this.ConvertData(source); }
@@ -188,22 +269,28 @@
                         var channel = channels.Find(p => p.Id == ChannelId);
                         if (channel != null)
                         {
-                            definition.ChannelName = channel.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
+                            var channelname = channel.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
+                            var split = channelname.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
+                            definition.ChannelNumber = Convert.ToInt32(split.First());
+                            definition.ChannelName = string.IsNullOrEmpty(split.Last()) ? string.Empty : split.Last().ToString();
+
                         }
                         else
                         {
-                            definition.ChannelName = string.Empty;
+                            definition.ChannelNumber = 0;
                         }
                     }
                     else
                     {
-                        definition.ChannelName = string.Empty;
+                        definition.ChannelNumber = 0;
                     }
-                    definition.Start = (DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStart];
-                    definition.Stop = (DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStop];
+                    definition.Start = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStart]).ToDateTimeString();
+                    definition.Stop = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStop]).ToDateTimeString();
                     definition.Description = t.MetaData[XMLTVConstants.Programs.ProgramDescription].ToString();
                     definition.Title = t.MetaData[XMLTVConstants.Programs.ProgramTitle].ToString();
                     definition.SubTitle = t.MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString();
+
+                    xmltv_logger.Verbose.Debug.WriteLine(definition.ToString());
                     list.Add(definition);
                 }
                 catch (Exception ex) { throw ex; }

 

  ViewVC Help
Powered by ViewVC 1.1.22