/[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 273 - (show 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 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 using System.ComponentModel;
9
10 namespace libxmltv.Core
11 {
12 public class ChannelList : List<IXMLTVChannel>, IDataSourceBindable//, IDataConverter<IChannelDefintionList>
13 {
14 private class ChannelDefintionList : List<IChannelDefintion>, IDataSourceSortable, IDataSourceFilterable, IChannelDefintionList
15 {
16 public ChannelDefintionList() { }
17 public ChannelDefintionList(List<IChannelDefintion> collection) { collection.ForEach(s => this.Add(s)); }
18 #region IDataSourceSortable members
19 public void Sort(ref object source, bool descending, params string[] args)
20 {
21 }
22 #endregion
23 #region IDataSourceFilterable members
24 public void Filter(ref object source, params string[] args)
25 {
26 }
27 #endregion
28
29
30 }
31 private class ChannelDefintion : IChannelDefintion
32 {
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 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
55 private IXMLTVChannel _RawDataSource;
56 [Browsable(false)]
57 public IXMLTVChannel RawDataSource { get { return _RawDataSource; } set { _RawDataSource = value; } }
58
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
79 }
80 //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 public ChannelList() { }
88
89 //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 {
105 type = typeof(IChannelDefintionList);
106 object bindable = new object();
107 ChannelDefintionList list = new ChannelDefintionList();
108
109 foreach (var t in this)
110 {
111 try
112 {
113 ChannelDefintion definition = new ChannelDefintion();
114 definition.RawDataSource = t;
115 definition.ChannelId = t.Id;
116 definition.ChannelName = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
117 xmltv_logger.Verbose.Debug.WriteLine(definition.ToString());
118 list.Add(definition);
119 }
120 catch (Exception ex) { throw ex; }
121 }
122 list = new ChannelDefintionList(list.OrderBy(s => s.ChannelName).ToList());
123 bindable = list;
124 return bindable;
125 }
126
127 }
128 public class ProgramList : List<IXMLTVProgram>, IDataSourceBindable//, IDataConverter<IProgramDefinitionList>
129 {
130 private class ProgramDefinitionList : List<IProgramDefinition>, IDataSourceSortable, IDataSourceFilterable, IProgramDefinitionList
131 {
132 public ProgramDefinitionList() { }
133 public ProgramDefinitionList(List<IProgramDefinition> collection) { collection.ForEach(s => this.Add(s)); }
134 #region IDataSourceSortable members
135 public void Sort(ref object source, bool descending, params string[] args)
136 {
137 IProgramDefinitionList list = null;
138 IOrderedEnumerable<IProgramDefinition> ordered = null;
139 try
140 {
141 list = (ProgramDefinitionList)source;
142 var col = args.First().ToString().ToLower();
143 switch (col)
144 {
145 case "channelnumber": ordered = descending ? list.OrderByDescending(s => s.ChannelNumber) : list.OrderBy(s => s.ChannelNumber); break;
146 case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break;
147 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 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 case "rating": ordered = descending ? list.OrderByDescending(s => s.Rating) : list.OrderBy(s => s.Rating); break;
153 }
154 //bool first = true;
155 foreach (var arg in args.Skip(1))
156 {
157 col = arg.ToLower();
158 switch (col)
159 {
160 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 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 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 case "rating": ordered = descending ? ordered.ThenByDescending(s => s.Rating) : ordered.ThenBy(s => s.Rating); break;
168 }
169 }
170 list = new ProgramDefinitionList(ordered.ToList());
171 }
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 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 case "channelnumber": ordered = list.ToList().FindAll(s => s.ChannelNumber == Convert.ToInt32(filtertext)); break;
195 case "channelname": ordered = list.ToList().FindAll(s => s.ChannelName.ToLower().Contains(filtertext)); break;
196 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 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 case "rating": ordered = list.ToList().FindAll(s => s.Rating.ToLower().Contains(filtertext)); break;
202 }
203 list = new ProgramDefinitionList(ordered.ToList());
204 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 }
210 catch (Exception ex) { throw ex; }
211 if (list != null) { source = list; }
212 }
213 #endregion
214
215 }
216 public class ProgramDefintion : IProgramDefinition, IEquatable<IProgramDefinition>
217 {
218 public ProgramDefintion()
219 {
220 //ChannelId = string.Empty;
221 ChannelNumber = 0;
222 ChannelName = string.Empty;
223 Start = new DateTime().ToDateTimeString();
224 Stop = new DateTime().ToDateTimeString();
225 Description = string.Empty;
226 Title = string.Empty;
227 SubTitle = string.Empty;
228 Rating = string.Empty;
229 }
230 //public string ChannelId { get; internal set; }
231 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 public string Rating { get; set; }
239 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 if (prop.Name.ToLower() == "RawDataSource".ToLower()) { continue; }
247 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
254 private IXMLTVProgram _RawDataSource;
255 [Browsable(false)]
256 public IXMLTVProgram RawDataSource { get { return _RawDataSource; } set { _RawDataSource = value; } }
257
258 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 //return base.Equals(obj);
270 throw new InvalidCastException(string.Format("Type: {0} is not assignable from: {1}", obj.GetType().Name, typeof(IProgramDefinition).Name));
271 }
272 public override int GetHashCode()
273 {
274 return this.ToString().GetHashCode();
275 }
276 }
277 public ProgramList() { }
278 //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 {
293 type = typeof(IProgramDefinitionList);
294 object bindable = new object();
295 ProgramDefinitionList list = new ProgramDefinitionList();
296
297 foreach (var t in this)
298 {
299 try
300 {
301 ProgramDefintion definition = new ProgramDefintion();
302 definition.RawDataSource = t;
303 string ChannelId = t.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString();
304 //definition.ChannelId = ChannelId;
305
306 var channels = XMLTV.GetChannels();
307 if (channels != null)
308 {
309 var channel = channels.Find(p => p.Id == ChannelId);
310 if (channel != null)
311 {
312 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 }
318 else
319 {
320 definition.ChannelNumber = 0;
321 }
322 }
323 else
324 {
325 definition.ChannelNumber = 0;
326 }
327 definition.Start = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStart]).ToDateTimeString();
328 definition.Stop = ((DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStop]).ToDateTimeString();
329 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
333
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 xmltv_logger.Verbose.Debug.WriteLine(definition.ToString());
347 list.Add(definition);
348 }
349 catch (Exception ex) { throw ex; }
350 }
351
352 list = new ProgramDefinitionList(list.OrderBy(s => DateTime.Parse(s.Start)).ToList());
353
354 bindable = list;
355 return bindable;
356 }
357 }
358 public class ExtraList : List<IExtraMetaData>, IDataSourceBindable
359 {
360
361 public ExtraList() { }
362 /// <summary>
363 ///
364 /// </summary>
365 /// <param name="type"></param>
366 /// <returns></returns>
367 public object CreateBindableDataSource(out Type type)
368 {
369 type = typeof(object);
370 throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
371 //object bindable = new object();
372 //return bindable;
373 }
374 //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 }
380 }

  ViewVC Help
Powered by ViewVC 1.1.22