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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 254 - (show 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 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using libxmltv.Interfaces;
6 using System.Collections;
7 using Enterprise.Logging;
8
9 namespace libxmltv.Core
10 {
11 public class ChannelList : List<IXMLTVChannel>, IDataSourceBindable//, IDataConverter<IChannelDefintionList>
12 {
13 private class ChannelDefintionList : List<IChannelDefintion>, IDataSourceSortable, IDataSourceFilterable, IChannelDefintionList
14 {
15 public ChannelDefintionList() { }
16 public ChannelDefintionList(List<IChannelDefintion> collection) { collection.ForEach(s => this.Add(s)); }
17 #region IDataSourceSortable members
18 public void Sort(ref object source, bool descending, params string[] args)
19 {
20 }
21 #endregion
22 #region IDataSourceFilterable members
23 public void Filter(ref object source, params string[] args)
24 {
25 }
26 #endregion
27
28
29 }
30 private class ChannelDefintion : IChannelDefintion
31 {
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 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
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
76 }
77 //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 public ChannelList() { }
85
86 //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 {
102 type = typeof(IChannelDefintionList);
103 object bindable = new object();
104 ChannelDefintionList list = new ChannelDefintionList();
105
106 foreach (var t in this)
107 {
108 try
109 {
110 ChannelDefintion definition = new ChannelDefintion();
111 definition.RawDataSource = t;
112 definition.ChannelId = t.Id;
113 definition.ChannelName = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
114 xmltv_logger.Verbose.Debug.WriteLine(definition.ToString());
115 list.Add(definition);
116 }
117 catch (Exception ex) { throw ex; }
118 }
119 list = new ChannelDefintionList(list.OrderBy(s => s.ChannelName).ToList());
120 bindable = list;
121 return bindable;
122 }
123
124 }
125 public class ProgramList : List<IXMLTVProgram>, IDataSourceBindable//, IDataConverter<IProgramDefinitionList>
126 {
127 private class ProgramDefinitionList : List<IProgramDefinition>, IDataSourceSortable, IDataSourceFilterable, IProgramDefinitionList
128 {
129 public ProgramDefinitionList() { }
130 public ProgramDefinitionList(List<IProgramDefinition> collection) { collection.ForEach(s => this.Add(s)); }
131 #region IDataSourceSortable members
132 public void Sort(ref object source, bool descending, params string[] args)
133 {
134 IProgramDefinitionList list = null;
135 IOrderedEnumerable<IProgramDefinition> ordered = null;
136 try
137 {
138 list = (ProgramDefinitionList)source;
139 var col = args.First().ToString().ToLower();
140 switch (col)
141 {
142 case "channelnumber": ordered = descending ? list.OrderByDescending(s => s.ChannelNumber) : list.OrderBy(s => s.ChannelNumber); break;
143 case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break;
144 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 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 case "rating": ordered = descending ? list.OrderByDescending(s => s.Rating) : list.OrderBy(s => s.Rating); break;
150 }
151 //bool first = true;
152 foreach (var arg in args.Skip(1))
153 {
154 col = arg.ToLower();
155 switch (col)
156 {
157 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 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 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 case "rating": ordered = descending ? ordered.ThenByDescending(s => s.Rating) : ordered.ThenBy(s => s.Rating); break;
165 }
166 }
167 list = new ProgramDefinitionList(ordered.ToList());
168 }
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 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 case "channelnumber": ordered = list.ToList().FindAll(s => s.ChannelNumber == Convert.ToInt32(filtertext)); break;
192 case "channelname": ordered = list.ToList().FindAll(s => s.ChannelName.ToLower().Contains(filtertext)); break;
193 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 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 case "rating": ordered = list.ToList().FindAll(s => s.Rating.ToLower().Contains(filtertext)); break;
199 }
200 list = new ProgramDefinitionList(ordered.ToList());
201 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 }
207 catch (Exception ex) { throw ex; }
208 if (list != null) { source = list; }
209 }
210 #endregion
211
212 }
213 public class ProgramDefintion : IProgramDefinition, IEquatable<IProgramDefinition>
214 {
215 public ProgramDefintion()
216 {
217 //ChannelId = string.Empty;
218 ChannelNumber = 0;
219 ChannelName = string.Empty;
220 Start = new DateTime().ToDateTimeString();
221 Stop = new DateTime().ToDateTimeString();
222 Description = string.Empty;
223 Title = string.Empty;
224 SubTitle = string.Empty;
225 Rating = string.Empty;
226 }
227 //public string ChannelId { get; internal set; }
228 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 public string Rating { get; set; }
236 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
250 private IXMLTVProgram _RawDataSource; public IXMLTVProgram RawDataSource { get { return _RawDataSource; } set { _RawDataSource = value; } }
251
252 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 }
270 public ProgramList() { }
271 //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 {
286 type = typeof(IProgramDefinitionList);
287 object bindable = new object();
288 ProgramDefinitionList list = new ProgramDefinitionList();
289
290 foreach (var t in this)
291 {
292 try
293 {
294 ProgramDefintion definition = new ProgramDefintion();
295 definition.RawDataSource = t;
296 string ChannelId = t.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString();
297 //definition.ChannelId = ChannelId;
298
299 var channels = XMLTV.GetChannels();
300 if (channels != null)
301 {
302 var channel = channels.Find(p => p.Id == ChannelId);
303 if (channel != null)
304 {
305 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 }
311 else
312 {
313 definition.ChannelNumber = 0;
314 }
315 }
316 else
317 {
318 definition.ChannelNumber = 0;
319 }
320 definition.Start = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStart]).ToDateTimeString();
321 definition.Stop = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStop]).ToDateTimeString();
322 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
326
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 xmltv_logger.Verbose.Debug.WriteLine(definition.ToString());
340 list.Add(definition);
341 }
342 catch (Exception ex) { throw ex; }
343 }
344
345 list = new ProgramDefinitionList(list.OrderBy(s => DateTime.Parse(s.Start)).ToList());
346
347 bindable = list;
348 return bindable;
349 }
350 }
351 public class ExtraList : List<IExtraMetaData>, IDataSourceBindable
352 {
353
354 public ExtraList() { }
355 /// <summary>
356 ///
357 /// </summary>
358 /// <param name="type"></param>
359 /// <returns></returns>
360 public object CreateBindableDataSource(out Type type)
361 {
362 type = typeof(object);
363 throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
364 //object bindable = new object();
365 //return bindable;
366 }
367 //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 }
373 }

  ViewVC Help
Powered by ViewVC 1.1.22