/[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 134 - (show annotations) (download)
Wed Mar 13 15:50:07 2013 UTC (6 years, 6 months ago) by william
File size: 13631 byte(s)
when filtering returns no data:
* log a warning message that no data was returned
* include the columnname and filtertext that was supplied
* return the original list, don't empty the datagrid control

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 "channelname": ordered = descending ? list.OrderByDescending(s => s.ChannelName) : list.OrderBy(s => s.ChannelName); break;
104 case "start": ordered = descending ? list.OrderByDescending(s => s.Start) : list.OrderBy(s => s.Start); break;
105 case "stop": ordered = descending ? list.OrderByDescending(s => s.Stop) : list.OrderBy(s => s.Stop); break;
106 case "description": ordered = descending ? list.OrderByDescending(s => s.Description) : list.OrderBy(s => s.Description); break;
107 case "title": ordered = descending ? list.OrderByDescending(s => s.Title) : list.OrderBy(s => s.Title); break;
108 case "subtitle": ordered = descending ? list.OrderByDescending(s => s.SubTitle) : list.OrderBy(s => s.SubTitle); break;
109 }
110 //bool first = true;
111 foreach (var arg in args.Skip(1))
112 {
113 col = arg.ToLower();
114 switch (col)
115 {
116 case "channelname": ordered = descending ? ordered.ThenByDescending(s => s.ChannelName) : ordered.ThenBy(s => s.ChannelName); break;
117 case "start": ordered = descending ? ordered.ThenByDescending(s => s.Start) : ordered.ThenBy(s => s.Start); break;
118 case "stop": ordered = descending ? ordered.ThenByDescending(s => s.Stop) : ordered.ThenBy(s => s.Stop); break;
119 case "description": ordered = descending ? ordered.ThenByDescending(s => s.Description) : ordered.ThenBy(s => s.Description); break;
120 case "title": ordered = descending ? ordered.ThenByDescending(s => s.Title) : ordered.ThenBy(s => s.Title); break;
121 case "subtitle": ordered = descending ? ordered.ThenByDescending(s => s.SubTitle) : ordered.ThenBy(s => s.SubTitle); break;
122 }
123 }
124 list = new ProgramDefinitionList(ordered.ToList());
125 }
126 catch (Exception ex) { throw ex; }
127 if (list != null) { source = list; }
128 }
129 #endregion
130 #region IDataSourceFilterable members
131 public void Filter(ref object source, params string[] args)
132 {
133 IProgramDefinitionList list = null;
134 List<IProgramDefinition> ordered = null;
135 try
136 {
137 list = (ProgramDefinitionList)source;
138 //var col = args.First().ToString().ToLower();
139 //var data = args.().ToString().ToLower();
140
141 if (args.Count() != 2) { throw new ArgumentOutOfRangeException("args", "Excpected 2 aguments: columnname and filtertext"); }
142
143 string columnname = args[0].ToLower();
144 string filtertext = args[1].ToLower();
145
146 switch (columnname)
147 {
148 case "channelname": ordered = list.ToList().FindAll(s => s.ChannelName.ToLower().Contains(filtertext)); break;
149 case "start":
150 case "stop":
151 throw new ArgumentException("columnname", string.Format("Filtering for column: '{0}' has not been implemented", columnname));
152 case "description": ordered = list.ToList().FindAll(s => s.Description.ToLower().Contains(filtertext)); break;
153 case "title": ordered = list.ToList().FindAll(s => s.Title.ToLower().Contains(filtertext)); break;
154 case "subtitle": ordered = list.ToList().FindAll(s => s.SubTitle.ToLower().Contains(filtertext)); break;
155 }
156 list = new ProgramDefinitionList(ordered.ToList());
157 if (list.Count == 0)
158 {
159 list = (ProgramDefinitionList)source;
160 gLog.Warn.WriteLine("Filering by: column='{0}' with filer='{1}' returned no data",columnname, filtertext);
161 }
162 }
163 catch (Exception ex) { throw ex; }
164 if (list != null) { source = list; }
165 }
166 #endregion
167 }
168 private class ProgramDefintion : IProgramDefinition
169 {
170 public ProgramDefintion()
171 {
172 //ChannelId = string.Empty;
173 ChannelName = string.Empty;
174 Start = new DateTime();
175 Stop = new DateTime();
176 Description = string.Empty;
177 Title = string.Empty;
178 SubTitle = string.Empty;
179 }
180 //public string ChannelId { get; internal set; }
181 public string ChannelName { get; internal set; }
182 public DateTime Start { get; internal set; }
183 public DateTime Stop { get; internal set; }
184 public string Title { get; internal set; }
185 public string SubTitle { get; internal set; }
186 public string Description { get; internal set; }
187
188
189 }
190 public ProgramList() { }
191 //public object ConvertObjectData(object source) { return this.ConvertData(source); }
192 //public IProgramDefinitionList ConvertData(object source)
193 //{
194 // if (source.GetType() != typeof(ProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", source.GetType().Name, typeof(ProgramDefinitionList).Name)); }
195 // //if (type != typeof(IProgramDefinitionList)) { throw new InvalidCastException(string.Format("Cannot cast: '{0}' to '{1}'", type.Name, typeof(IProgramDefinitionList).Name)); }
196 // IProgramDefinitionList t = (source as IProgramDefinitionList);
197 // return t;
198 //}
199 /// <summary>
200 ///
201 /// </summary>
202 /// <param name="type"></param>
203 /// <returns></returns>
204 public object CreateBindableDataSource(out Type type)
205 {
206 type = typeof(IProgramDefinitionList);
207 object bindable = new object();
208 ProgramDefinitionList list = new ProgramDefinitionList();
209
210 foreach (var t in this)
211 {
212 try
213 {
214 ProgramDefintion definition = new ProgramDefintion();
215 string ChannelId = t.MetaData[XMLTVConstants.Programs.ProgramChannelId].ToString();
216 //definition.ChannelId = ChannelId;
217
218 var channels = XMLTV.GetChannels();
219 if (channels != null)
220 {
221 var channel = channels.Find(p => p.Id == ChannelId);
222 if (channel != null)
223 {
224 definition.ChannelName = channel.MetaData[XMLTVConstants.Channels.ChannelDisplayName].FirstOrDefault().Value.ToString();
225 }
226 else
227 {
228 definition.ChannelName = string.Empty;
229 }
230 }
231 else
232 {
233 definition.ChannelName = string.Empty;
234 }
235 definition.Start = (DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStart];
236 definition.Stop = (DateTime)t.MetaData[XMLTVConstants.Programs.ProgramStop];
237 definition.Description = t.MetaData[XMLTVConstants.Programs.ProgramDescription].ToString();
238 definition.Title = t.MetaData[XMLTVConstants.Programs.ProgramTitle].ToString();
239 definition.SubTitle = t.MetaData[XMLTVConstants.Programs.ProgramSubTitle].ToString();
240 list.Add(definition);
241 }
242 catch (Exception ex) { throw ex; }
243 }
244
245 list = new ProgramDefinitionList(list.OrderBy(s => s.Start).ToList());
246
247 bindable = list;
248 return bindable;
249 }
250 }
251 public class ExtraList : List<IExtraMetaData>, IDataSourceBindable
252 {
253
254 public ExtraList() { }
255 /// <summary>
256 ///
257 /// </summary>
258 /// <param name="type"></param>
259 /// <returns></returns>
260 public object CreateBindableDataSource(out Type type)
261 {
262 type = typeof(object);
263 throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
264 //object bindable = new object();
265 //return bindable;
266 }
267 //public object ConvertObjectData(object source) { return source; }
268 //public object Convert(object source)
269 //{
270 // throw new NotImplementedException("Conversion of Extra MetaData to a bindable datasource has not been implemented.");
271 //}
272 }
273 }

  ViewVC Help
Powered by ViewVC 1.1.22