/[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 252 - (show annotations) (download)
Wed Apr 3 05:28:26 2013 UTC (6 years, 7 months ago) by william
File size: 17916 byte(s)
Ensure when filtering Start/Stop times for Programs, don't sort by the string value; instead sort it using DateTime.Parse(string)

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

  ViewVC Help
Powered by ViewVC 1.1.22