/[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 137 - (show annotations) (download)
Wed Mar 13 16:59:40 2013 UTC (6 years, 8 months ago) by william
File size: 14518 byte(s)

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
39 }
40 //static private List<string> known_columns;
41 //static ChannelList()
42 //{
43 // known_columns = new List<string>();
44 // known_columns.Add("Id");
45 // known_columns.Add("Name");
46 //}
47 public ChannelList() { }
48
49 //public object ConvertObjectData(object source) { return this.ConvertData(source); }
50 //public IChannelDefintionList ConvertData(object source)
51 //{
52 // //object t = source;
53 // if (source.GetType() != typeof(ChannelDefintionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(ChannelDefintionList).Name)); }
54 // //if (type != typeof(IChannelDefintionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IChannelDefintionList).Name)); }
55 // IChannelDefintionList t = (source as IChannelDefintionList);
56 // return t;
57 //}
58 /// <summary>
59 ///
60 /// </summary>
61 /// <param name="type"></param>
62 /// <returns></returns>
63 public object CreateBindableDataSource(out Type type)
64 {
65 type = typeof(IChannelDefintionList);
66 object bindable = new object();
67 ChannelDefintionList list = new ChannelDefintionList();
68
69 foreach (var t in this)
70 {
71 try
72 {
73 ChannelDefintion definition = new ChannelDefintion();
74 definition.ChannelId = t.Id;
75 definition.ChannelName = t.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
76 list.Add(definition);
77 }
78 catch (Exception ex) { throw ex; }
79 }
80 list = new ChannelDefintionList(list.OrderBy(s => s.ChannelName).ToList());
81 bindable = list;
82 return bindable;
83 }
84
85 }
86 public class ProgramList : List<IXMLTVProgram>, IDataSourceBindable//, IDataConverter<IProgramDefinitionList>
87 {
88 private class ProgramDefinitionList : List<IProgramDefinition>, IDataSourceSortable, IDataSourceFilterable, IProgramDefinitionList
89 {
90 public ProgramDefinitionList() { }
91 public ProgramDefinitionList(List<IProgramDefinition> collection) { collection.ForEach(s => this.Add(s)); }
92 #region IDataSourceSortable members
93 public void Sort(ref object source, bool descending, params string[] args)
94 {
95 IProgramDefinitionList list = null;
96 IOrderedEnumerable<IProgramDefinition> ordered = null;
97 try
98 {
99 list = (ProgramDefinitionList)source;
100 var col = args.First().ToString().ToLower();
101 switch (col)
102 {
103 case "channelnumber": ordered = descending ? list.OrderByDescending(s => s.ChannelNumber) : list.OrderBy(s => s.ChannelNumber); break;
104 case "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break;
105 case "start": ordered = descending ? list.OrderByDescending(s => s.Start) : list.OrderBy(s => s.Start); break;
106 case "stop": ordered = descending ? list.OrderByDescending(s => s.Stop) : list.OrderBy(s => s.Stop); break;
107 case "description": ordered = descending ? list.OrderByDescending(s => s.Description) : list.OrderBy(s => s.Description); break;
108 case "title": ordered = descending ? list.OrderByDescending(s => s.Title) : list.OrderBy(s => s.Title); break;
109 case "subtitle": ordered = descending ? list.OrderByDescending(s => s.SubTitle) : list.OrderBy(s => s.SubTitle); break;
110 }
111 //bool first = true;
112 foreach (var arg in args.Skip(1))
113 {
114 col = arg.ToLower();
115 switch (col)
116 {
117 case "channelnumber": ordered = descending ? ordered.ThenByDescending(s => s.ChannelNumber) : ordered.ThenBy(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 ? ordered.ThenByDescending(s => s.Start) : ordered.ThenBy(s => s.Start); break;
120 case "stop": ordered = descending ? ordered.ThenByDescending(s => s.Stop) : ordered.ThenBy(s => s.Stop); break;
121 case "description": ordered = descending ? ordered.ThenByDescending(s => s.Description) : ordered.ThenBy(s => s.Description); break;
122 case "title": ordered = descending ? ordered.ThenByDescending(s => s.Title) : ordered.ThenBy(s => s.Title); break;
123 case "subtitle": ordered = descending ? ordered.ThenByDescending(s => s.SubTitle) : ordered.ThenBy(s => s.SubTitle); break;
124 }
125 }
126 list = new ProgramDefinitionList(ordered.ToList());
127 }
128 catch (Exception ex) { throw ex; }
129 if (list != null) { source = list; }
130 }
131 #endregion
132 #region IDataSourceFilterable members
133 public void Filter(ref object source, params string[] args)
134 {
135 IProgramDefinitionList list = null;
136 List<IProgramDefinition> ordered = null;
137 try
138 {
139 list = (ProgramDefinitionList)source;
140 //var col = args.First().ToString().ToLower();
141 //var data = args.().ToString().ToLower();
142
143 if (args.Count() != 2) { throw new ArgumentOutOfRangeException("args", "Excpected 2 aguments: columnname and filtertext"); }
144
145 string columnname = args[0].ToLower();
146 string filtertext = args[1].ToLower();
147
148 switch (columnname)
149 {
150 case "channelnumber": ordered = list.ToList().FindAll(s => s.ChannelNumber.ToString().ToLower().Contains(filtertext)); break;
151 case "channelname": ordered = list.ToList().FindAll(s => s.ChannelName.ToLower().Contains(filtertext)); break;
152 case "start":
153 case "stop":
154 throw new ArgumentException("columnname", string.Format("Filtering for column: '{0}' has not been implemented", columnname));
155 case "description": ordered = list.ToList().FindAll(s => s.Description.ToLower().Contains(filtertext)); break;
156 case "title": ordered = list.ToList().FindAll(s => s.Title.ToLower().Contains(filtertext)); break;
157 case "subtitle": ordered = list.ToList().FindAll(s => s.SubTitle.ToLower().Contains(filtertext)); break;
158 }
159 list = new ProgramDefinitionList(ordered.ToList());
160 if (list.Count == 0)
161 {
162 list = (ProgramDefinitionList)source;
163 gLog.Warn.WriteLine("Filering by: column='{0}' with filer='{1}' returned no data",columnname, filtertext);
164 }
165 }
166 catch (Exception ex) { throw ex; }
167 if (list != null) { source = list; }
168 }
169 #endregion
170 }
171 private class ProgramDefintion : IProgramDefinition
172 {
173 public ProgramDefintion()
174 {
175 //ChannelId = string.Empty;
176 ChannelNumber = 0;
177 ChannelName = string.Empty;
178 Start = new DateTime();
179 Stop = new DateTime();
180 Description = string.Empty;
181 Title = string.Empty;
182 SubTitle = string.Empty;
183 }
184 //public string ChannelId { get; internal set; }
185 public int ChannelNumber { get; internal set; }
186 public string ChannelName { get; internal set; }
187 public DateTime Start { get; internal set; }
188 public DateTime Stop { get; internal set; }
189 public string Title { get; internal set; }
190 public string SubTitle { get; internal set; }
191 public string Description { get; internal set; }
192
193
194 }
195 public ProgramList() { }
196 //public object ConvertObjectData(object source) { return this.ConvertData(source); }
197 //public IProgramDefinitionList ConvertData(object source)
198 //{
199 // if (source.GetType() != typeof(ProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(ProgramDefinitionList).Name)); }
200 // //if (type != typeof(IProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IProgramDefinitionList).Name)); }
201 // IProgramDefinitionList t = (source as IProgramDefinitionList);
202 // return t;
203 //}
204 /// <summary>
205 ///
206 /// </summary>
207 /// <param name="type"></param>
208 /// <returns></returns>
209 public object CreateBindableDataSource(out Type type)
210 {
211 type = typeof(IProgramDefinitionList);
212 object bindable = new object();
213 ProgramDefinitionList list = new ProgramDefinitionList();
214
215 foreach (var t in this)
216 {
217 try
218 {
219 ProgramDefintion definition = new ProgramDefintion();
220 string ChannelId = t.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString();
221 //definition.ChannelId = ChannelId;
222
223 var channels = XMLTV.GetChannels();
224 if (channels != null)
225 {
226 var channel = channels.Find(p => p.Id == ChannelId);
227 if (channel != null)
228 {
229 var channelname = channel.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
230 var split = channelname.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
231 definition.ChannelNumber = Convert.ToInt32(split.First());
232 definition.ChannelName = string.IsNullOrEmpty(split.Last()) ? string.Empty : split.Last().ToString();
233
234 }
235 else
236 {
237 definition.ChannelNumber = 0;
238 }
239 }
240 else
241 {
242 definition.ChannelNumber = 0;
243 }
244 definition.Start = (DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStart];
245 definition.Stop = (DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStop];
246 definition.Description = t.MetaData[XMLTVConstants.Programs.ProgramDescription].ToString();
247 definition.Title = t.MetaData[XMLTVConstants.Programs.ProgramTitle].ToString();
248 definition.SubTitle = t.MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString();
249 list.Add(definition);
250 }
251 catch (Exception ex) { throw ex; }
252 }
253
254 list = new ProgramDefinitionList(list.OrderBy(s => s.Start).ToList());
255
256 bindable = list;
257 return bindable;
258 }
259 }
260 public class ExtraList : List<IExtraMetaData>, IDataSourceBindable
261 {
262
263 public ExtraList() { }
264 /// <summary>
265 ///
266 /// </summary>
267 /// <param name="type"></param>
268 /// <returns></returns>
269 public object CreateBindableDataSource(out Type type)
270 {
271 type = typeof(object);
272 throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
273 //object bindable = new object();
274 //return bindable;
275 }
276 //public object ConvertObjectData(object source) { return source; }
277 //public object Convert(object source)
278 //{
279 // throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
280 //}
281 }
282 }

  ViewVC Help
Powered by ViewVC 1.1.22