/[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 254 - (hide annotations) (download)
Wed Apr 17 21:55:30 2013 UTC (7 years, 6 months ago) by william
File size: 19066 byte(s)
+ update binding source so that we can get an instance of the raw data

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

  ViewVC Help
Powered by ViewVC 1.1.22