/[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 181 - (hide annotations) (download)
Sat Mar 16 17:56:55 2013 UTC (6 years, 8 months ago) by william
File size: 16572 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22