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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 273 - (hide annotations) (download)
Sat Apr 4 01:29:23 2015 UTC (4 years, 7 months ago) by william
File size: 19435 byte(s)
+ throw an invalidcastexception in equals if obj is not assignable from IProgramDefintion
+ ProgramDefintion ToString() :: skip RawDataSource property

1 william 120 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using libxmltv.Interfaces;
6 william 126 using System.Collections;
7 william 134 using Enterprise.Logging;
8 william 255 using System.ComponentModel;
9 william 120
10     namespace libxmltv.Core
11     {
12 william 128 public class ChannelList : List<IXMLTVChannel>, IDataSourceBindable//, IDataConverter<IChannelDefintionList>
13 william 120 {
14 william 128 private class ChannelDefintionList : List<IChannelDefintion>, IDataSourceSortable, IDataSourceFilterable, IChannelDefintionList
15 william 121 {
16 william 126 public ChannelDefintionList() { }
17 william 128 public ChannelDefintionList(List<IChannelDefintion> collection) { collection.ForEach(s => this.Add(s)); }
18 william 125 #region IDataSourceSortable members
19 william 131 public void Sort(ref object source, bool descending, params string[] args)
20 william 125 {
21     }
22     #endregion
23     #region IDataSourceFilterable members
24 william 126 public void Filter(ref object source, params string[] args)
25 william 125 {
26     }
27     #endregion
28 william 254
29    
30 william 121 }
31 william 128 private class ChannelDefintion : IChannelDefintion
32 william 126 {
33     public ChannelDefintion()
34     {
35     ChannelId = string.Empty;
36     ChannelName = string.Empty;
37     }
38     public string ChannelId { get; internal set; }
39     public string ChannelName { get; internal set; }
40    
41 william 141 public override string ToString()
42     {
43     StringBuilder builder = new StringBuilder();
44     Type t = typeof(ChannelDefintion);
45     var props = t.GetProperties();
46     foreach (var prop in props)
47     {
48     string name = prop.Name;
49     object value = prop.GetValue(this, null);
50     builder.AppendFormat("\t{0}: '{1}'", name, value == null ? "null" : value.ToString());
51     }
52     return builder.ToString();
53     }
54 william 254
55 william 255 private IXMLTVChannel _RawDataSource;
56     [Browsable(false)]
57     public IXMLTVChannel RawDataSource { get { return _RawDataSource; } set { _RawDataSource = value; } }
58 william 254
59    
60     public bool Equals(IChannelDefintion other)
61     {
62     return this.ToString() == other.ToString();
63     }
64     public override bool Equals(object obj)
65     {
66     if (obj == null) { throw new ArgumentNullException("obj", "Object to compare cannot be null"); }
67     if (obj.GetType().IsAssignableFrom(typeof(IChannelDefintion)))
68     {
69     return this.Equals((IChannelDefintion)obj);
70     }
71     return base.Equals(obj);
72     }
73     public override int GetHashCode()
74     {
75     return this.ToString().GetHashCode();
76     }
77    
78 william 126
79     }
80 william 122 //static private List<string> known_columns;
81     //static ChannelList()
82     //{
83     // known_columns = new List<string>();
84     // known_columns.Add("Id");
85     // known_columns.Add("Name");
86     //}
87 william 120 public ChannelList() { }
88    
89 william 128 //public object ConvertObjectData(object source) { return this.ConvertData(source); }
90     //public IChannelDefintionList ConvertData(object source)
91     //{
92     // //object t = source;
93     // if (source.GetType() != typeof(ChannelDefintionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(ChannelDefintionList).Name)); }
94     // //if (type != typeof(IChannelDefintionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IChannelDefintionList).Name)); }
95     // IChannelDefintionList t = (source as IChannelDefintionList);
96     // return t;
97     //}
98     /// <summary>
99     ///
100     /// </summary>
101     /// <param name="type"></param>
102     /// <returns></returns>
103     public object CreateBindableDataSource(out Type type)
104 william 120 {
105 william 128 type = typeof(IChannelDefintionList);
106 william 120 object bindable = new object();
107 william 126 ChannelDefintionList list = new ChannelDefintionList();
108 william 122
109     foreach (var t in this)
110     {
111     try
112     {
113     ChannelDefintion definition = new ChannelDefintion();
114 william 254 definition.RawDataSource = t;
115 william 122 definition.ChannelId = t.Id;
116 william 141 definition.ChannelName = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
117     xmltv_logger.Verbose.Debug.WriteLine(definition.ToString());
118 william 122 list.Add(definition);
119     }
120     catch (Exception ex) { throw ex; }
121     }
122 william 126 list = new ChannelDefintionList(list.OrderBy(s => s.ChannelName).ToList());
123 william 122 bindable = list;
124 william 120 return bindable;
125     }
126 william 128
127 william 120 }
128 william 128 public class ProgramList : List<IXMLTVProgram>, IDataSourceBindable//, IDataConverter<IProgramDefinitionList>
129 william 120 {
130 william 128 private class ProgramDefinitionList : List<IProgramDefinition>, IDataSourceSortable, IDataSourceFilterable, IProgramDefinitionList
131 william 121 {
132 william 126 public ProgramDefinitionList() { }
133 william 128 public ProgramDefinitionList(List<IProgramDefinition> collection) { collection.ForEach(s => this.Add(s)); }
134 william 126 #region IDataSourceSortable members
135 william 131 public void Sort(ref object source, bool descending, params string[] args)
136 william 126 {
137 william 131 IProgramDefinitionList list = null;
138 william 130 IOrderedEnumerable<IProgramDefinition> ordered = null;
139 william 126 try
140     {
141     list = (ProgramDefinitionList)source;
142 william 131 var col = args.First().ToString().ToLower();
143     switch (col)
144 william 126 {
145 william 137 case "channelnumber": ordered = descending ? list.OrderByDescending(s => s.ChannelNumber) : list.OrderBy(s => s.ChannelNumber); break;
146 william 131 case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break;
147 william 252 case "start": ordered = descending ? list.OrderByDescending(s => DateTime.Parse(s.Start)) : list.OrderBy(s => DateTime.Parse(s.Start)); break;
148     case "stop": ordered = descending ? list.OrderByDescending(s => DateTime.Parse(s.Stop)) : list.OrderBy(s => DateTime.Parse(s.Stop)); break;
149 william 131 case "description": ordered = descending ? list.OrderByDescending(s => s.Description) : list.OrderBy(s => s.Description); break;
150     case "title": ordered = descending ? list.OrderByDescending(s => s.Title) : list.OrderBy(s => s.Title); break;
151     case "subtitle": ordered = descending ? list.OrderByDescending(s => s.SubTitle) : list.OrderBy(s => s.SubTitle); break;
152 william 227 case "rating": ordered = descending ? list.OrderByDescending(s => s.Rating) : list.OrderBy(s => s.Rating); break;
153 william 131 }
154     //bool first = true;
155     foreach (var arg in args.Skip(1))
156     {
157     col = arg.ToLower();
158 william 130 switch (col)
159     {
160 william 137 case "channelnumber": ordered = descending ? ordered.ThenByDescending(s => s.ChannelNumber) : ordered.ThenBy(s => s.ChannelNumber); break;
161     case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break;
162 william 252 case "start": ordered = descending ? ordered.ThenByDescending(s => DateTime.Parse(s.Start)) : ordered.ThenBy(s => DateTime.Parse(s.Start)); break;
163     case "stop": ordered = descending ? ordered.ThenByDescending(s => DateTime.Parse(s.Stop)) : ordered.ThenBy(s => DateTime.Parse(s.Stop)); break;
164 william 131 case "description": ordered = descending ? ordered.ThenByDescending(s => s.Description) : ordered.ThenBy(s => s.Description); break;
165     case "title": ordered = descending ? ordered.ThenByDescending(s => s.Title) : ordered.ThenBy(s => s.Title); break;
166     case "subtitle": ordered = descending ? ordered.ThenByDescending(s => s.SubTitle) : ordered.ThenBy(s => s.SubTitle); break;
167 william 227 case "rating": ordered = descending ? ordered.ThenByDescending(s => s.Rating) : ordered.ThenBy(s => s.Rating); break;
168 william 130 }
169 william 126 }
170 william 131 list = new ProgramDefinitionList(ordered.ToList());
171 william 126 }
172     catch (Exception ex) { throw ex; }
173     if (list != null) { source = list; }
174     }
175     #endregion
176     #region IDataSourceFilterable members
177     public void Filter(ref object source, params string[] args)
178     {
179 william 132 IProgramDefinitionList list = null;
180     List<IProgramDefinition> ordered = null;
181     try
182     {
183     list = (ProgramDefinitionList)source;
184     //var col = args.First().ToString().ToLower();
185     //var data = args.().ToString().ToLower();
186    
187     if (args.Count() != 2) { throw new ArgumentOutOfRangeException("args", "Excpected 2 aguments: columnname and filtertext"); }
188    
189     string columnname = args[0].ToLower();
190     string filtertext = args[1].ToLower();
191    
192     switch (columnname)
193     {
194 william 138 case "channelnumber": ordered = list.ToList().FindAll(s => s.ChannelNumber == Convert.ToInt32(filtertext)); break;
195 william 132 case "channelname": ordered = list.ToList().FindAll(s => s.ChannelName.ToLower().Contains(filtertext)); break;
196 william 142 case "start": ordered = list.ToList().FindAll(s => s.Start.ToLower().Contains(filtertext)); break;
197     case "stop": ordered = list.ToList().FindAll(s => s.Stop.ToLower().Contains(filtertext)); break;
198 william 132 case "description": ordered = list.ToList().FindAll(s => s.Description.ToLower().Contains(filtertext)); break;
199     case "title": ordered = list.ToList().FindAll(s => s.Title.ToLower().Contains(filtertext)); break;
200     case "subtitle": ordered = list.ToList().FindAll(s => s.SubTitle.ToLower().Contains(filtertext)); break;
201 william 227 case "rating": ordered = list.ToList().FindAll(s => s.Rating.ToLower().Contains(filtertext)); break;
202 william 132 }
203     list = new ProgramDefinitionList(ordered.ToList());
204 william 134 if (list.Count == 0)
205     {
206     list = (ProgramDefinitionList)source;
207     gLog.Warn.WriteLine("Filering by: column='{0}' with filer='{1}' returned no data",columnname, filtertext);
208     }
209 william 132 }
210     catch (Exception ex) { throw ex; }
211     if (list != null) { source = list; }
212 william 126 }
213     #endregion
214 william 254
215 william 126 }
216 william 181 public class ProgramDefintion : IProgramDefinition, IEquatable<IProgramDefinition>
217 william 126 {
218 william 122 public ProgramDefintion()
219     {
220 william 123 //ChannelId = string.Empty;
221 william 137 ChannelNumber = 0;
222 william 122 ChannelName = string.Empty;
223 william 141 Start = new DateTime().ToDateTimeString();
224     Stop = new DateTime().ToDateTimeString();
225 william 122 Description = string.Empty;
226     Title = string.Empty;
227     SubTitle = string.Empty;
228 william 227 Rating = string.Empty;
229 william 122 }
230 william 123 //public string ChannelId { get; internal set; }
231 william 165 public int ChannelNumber { get; set; }
232     public string ChannelName { get; set; }
233     public string Start { get; set; }
234     public string Stop { get; set; }
235     public string Title { get; set; }
236     public string SubTitle { get; set; }
237     public string Description { get; set; }
238 william 227 public string Rating { get; set; }
239 william 141 public override string ToString()
240     {
241     StringBuilder builder = new StringBuilder();
242     Type t = typeof(ProgramDefintion);
243     var props = t.GetProperties();
244     foreach (var prop in props)
245     {
246 william 273 if (prop.Name.ToLower() == "RawDataSource".ToLower()) { continue; }
247 william 141 string name = prop.Name;
248     object value = prop.GetValue(this, null);
249     builder.AppendFormat("\t{0}: '{1}'", name, value == null ? "null" : value.ToString());
250     }
251     return builder.ToString();
252     }
253 william 181
254 william 255 private IXMLTVProgram _RawDataSource;
255     [Browsable(false)]
256     public IXMLTVProgram RawDataSource { get { return _RawDataSource; } set { _RawDataSource = value; } }
257 william 254
258 william 181 public bool Equals(IProgramDefinition other)
259     {
260     return this.ToString() == other.ToString();
261     }
262     public override bool Equals(object obj)
263     {
264     if (obj == null) { throw new ArgumentNullException("obj", "Object to compare cannot be null"); }
265     if (obj.GetType().IsAssignableFrom(typeof(IProgramDefinition)))
266     {
267     return this.Equals((IProgramDefinition)obj);
268     }
269 william 273 //return base.Equals(obj);
270     throw new InvalidCastException(string.Format("Type: {0} is not assignable from: {1}", obj.GetType().Name, typeof(IProgramDefinition).Name));
271 william 181 }
272     public override int GetHashCode()
273     {
274     return this.ToString().GetHashCode();
275     }
276 william 121 }
277 william 120 public ProgramList() { }
278 william 128 //public object ConvertObjectData(object source) { return this.ConvertData(source); }
279     //public IProgramDefinitionList ConvertData(object source)
280     //{
281     // if (source.GetType() != typeof(ProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(ProgramDefinitionList).Name)); }
282     // //if (type != typeof(IProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IProgramDefinitionList).Name)); }
283     // IProgramDefinitionList t = (source as IProgramDefinitionList);
284     // return t;
285     //}
286     /// <summary>
287     ///
288     /// </summary>
289     /// <param name="type"></param>
290     /// <returns></returns>
291     public object CreateBindableDataSource(out Type type)
292 william 120 {
293 william 128 type = typeof(IProgramDefinitionList);
294 william 120 object bindable = new object();
295 william 126 ProgramDefinitionList list = new ProgramDefinitionList();
296 william 121
297 william 122 foreach (var t in this)
298     {
299     try
300     {
301     ProgramDefintion definition = new ProgramDefintion();
302 william 254 definition.RawDataSource = t;
303 william 123 string ChannelId = t.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString();
304     //definition.ChannelId = ChannelId;
305 william 121
306 william 122 var channels = XMLTV.GetChannels();
307     if (channels != null)
308     {
309 william 123 var channel = channels.Find(p => p.Id == ChannelId);
310 william 122 if (channel != null)
311     {
312 william 137 var channelname = channel.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
313     var split = channelname.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
314     definition.ChannelNumber = Convert.ToInt32(split.First());
315     definition.ChannelName = string.IsNullOrEmpty(split.Last()) ? string.Empty : split.Last().ToString();
316    
317 william 122 }
318     else
319     {
320 william 137 definition.ChannelNumber = 0;
321 william 122 }
322     }
323     else
324     {
325 william 137 definition.ChannelNumber = 0;
326 william 122 }
327 william 141 definition.Start = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStart]).ToDateTimeString();
328     definition.Stop = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStop]).ToDateTimeString();
329 william 122 definition.Description = t.MetaData[XMLTVConstants.Programs.ProgramDescription].ToString();
330     definition.Title = t.MetaData[XMLTVConstants.Programs.ProgramTitle].ToString();
331     definition.SubTitle = t.MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString();
332 william 141
333 william 227
334     var extras = t.GetExtraMetaData();
335     foreach (var extra in extras)
336     {
337     if (extra.Name.ToLower() == "rating")
338     {
339     var root = extra.AsXElement();
340     var child = root.Descendants("value").FirstOrDefault();
341     if (child == null) { throw new NullReferenceException("Expected rating element to have a child elemented named: value"); }
342     string value = child.Value;
343     definition.Rating = value;
344     }
345     }
346 william 141 xmltv_logger.Verbose.Debug.WriteLine(definition.ToString());
347 william 122 list.Add(definition);
348     }
349     catch (Exception ex) { throw ex; }
350     }
351 william 123
352 william 252 list = new ProgramDefinitionList(list.OrderBy(s => DateTime.Parse(s.Start)).ToList());
353 william 123
354 william 122 bindable = list;
355 william 120 return bindable;
356     }
357     }
358     public class ExtraList : List<IExtraMetaData>, IDataSourceBindable
359     {
360 william 121
361 william 120 public ExtraList() { }
362 william 128 /// <summary>
363     ///
364     /// </summary>
365     /// <param name="type"></param>
366     /// <returns></returns>
367     public object CreateBindableDataSource(out Type type)
368 william 120 {
369 william 128 type = typeof(object);
370 william 124 throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
371     //object bindable = new object();
372     //return bindable;
373 william 120 }
374 william 128 //public object ConvertObjectData(object source) { return source; }
375     //public object Convert(object source)
376     //{
377     // throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
378     //}
379 william 120 }
380     }

  ViewVC Help
Powered by ViewVC 1.1.22