2001-12-01 Gaurav Vaish <gvaish@iitk.ac.in>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / AdRotator.cs
1 /**\r
2  * Namespace: System.Web.UI.WebControls\r
3  * Class:     AdRotator\r
4  * \r
5  * Author:  Gaurav Vaish\r
6  * Maintainer: gvaish@iitk.ac.in\r
7  * Contact: <gvaish@iitk.ac.in>\r
8  * Status:  75%\r
9  * \r
10  * (C) Gaurav Vaish (2001)\r
11  */\r
12 \r
13 using System;\r
14 using System.IO;\r
15 using System.Collections;\r
16 using System.Collections.Specialized;\r
17 using System.Web;\r
18 using System.Web.UI;\r
19 using System.Xml;\r
20 using System.Web.Utils;\r
21 \r
22 namespace System.Web.UI.WebControls\r
23 {\r
24         public class AdRotator: WebControl\r
25         {\r
26 \r
27                 private string advertisementFile;\r
28                 private string keywordFilter;\r
29                 private string target;\r
30                 private static readonly object AdCreatedEvent = new object();\r
31 \r
32                 // Will be set values during (On)PreRender-ing\r
33                 private string alternateText;\r
34                 private string imageUrl;\r
35                 private string navigateUrl;\r
36 \r
37                 private string fileDirctory;\r
38 \r
39                 private static class AdRecord\r
40                 {\r
41                         public IDictionary adProps;\r
42                         public int         hits; // or impressions or clicks\r
43                         public string      keyword;\r
44 \r
45                         public AdRecord()\r
46                         {\r
47                         }\r
48                         \r
49                         public void Initialize(IDictionary adProps)\r
50                         {\r
51                                 this.adProps = adProps;\r
52                         }\r
53                 }\r
54 \r
55 /*\r
56  * Loading / Saving data from/to ad file and all the manipulations wrt to the URL...\r
57  * are incorporated by the following functions.\r
58  * GetData(string)\r
59  * LoadAdFile(string)\r
60  * IsAdMatching(AdRecord)\r
61  * ResolveAdUrl(string)\r
62  * SelectAd()\r
63  * The exact control flow will be detailed. Let me first write the functions\r
64  */\r
65 \r
66                 private AdRecord[] LoadAdFile(string file)\r
67                 {\r
68                         Stream      fSream;\r
69                         ArrayList   list;\r
70                         XmlReader   reader;\r
71                         XmlDocument document;\r
72                         XmlNode     topNode, innerNode;\r
73                         IDictionary hybridDict;\r
74                         AdRecord[]  adsArray = null;\r
75                         try\r
76                         {\r
77                                 fStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);\r
78                         } catch(Exception e)\r
79                         {\r
80                                 throw new HttpException("AdRotator: Unable to open file");\r
81                         }\r
82                         try\r
83                         {\r
84                                 list = new ArrayList();\r
85                                 reader = new XmlTextReader(fStream);\r
86                                 document = new XmlDocument();\r
87                                 document.Load(reader);\r
88                                 if(document.DocumentElement!=null)\r
89                                 {\r
90                                         if(docuent.DocumentElement.LocalName=="Advertisements")\r
91                                         {\r
92                                                 topNode = documentElement.FirstChild;\r
93                                                 while(topNode!=null)\r
94                                                 {\r
95                                                         if(topNode.LocalName=="Ad")\r
96                                                         {\r
97                                                                 innerNode = topNode.FirstChild;\r
98                                                                 while(innerNode!=null)\r
99                                                                 {\r
100                                                                         if(innerNode.NodeType==NodeType.Element)\r
101                                                                         {\r
102                                                                                 if(hybridDic==null)\r
103                                                                                 {\r
104                                                                                         hybirdDict = new HybridDictionary();\r
105                                                                                 }\r
106                                                                                 hybridDic.Add(innerNode.LocalName, innerNode.InnerText);\r
107                                                                         }\r
108                                                                         innerNode = innerNode.NextSibling;\r
109                                                                 }\r
110                                                                 if(hybridDict!=null)\r
111                                                                         list.Add(hybridDict);\r
112                                                         }\r
113                                                         topNode = topNode.NextSibling;\r
114                                                 }\r
115                                         }\r
116                                 }\r
117                                 if(list.Count>0)\r
118                                 {\r
119                                         adsArray = new AdRecord[list.Count];\r
120                                         for(int i=0; i < list.Count; i++)\r
121                                         {\r
122                                                 adsArray[i] = new AdRecord((IDictionary)list.Item[i]);\r
123                                         }\r
124                                 }\r
125                         } catch(Excetion e)\r
126                         {\r
127                                 throw new HttpException("AdRotator: Unable to parse file" + file);\r
128                         } finally\r
129                         {\r
130                                 fStream.close();\r
131                         }\r
132                         if(adsArray == null)\r
133                         {\r
134                                 throw new HttpException("AdRotator: No Advertisements Fount");\r
135                         }\r
136                         return adsArray;\r
137                 }\r
138                 \r
139                 private AdRecord[] GetData(string file)\r
140                 {\r
141                         //TODO: Implement me\r
142                         fileDirectory = UrlUtils.GetDirectory(TemplateSourceDirectory + MapPathSecure(file));\r
143                         throw new NotImplementedException();\r
144                         //TODO: Do I need to check caching?\r
145                         return null;\r
146                 }\r
147                 \r
148                 private IDictionary SelectAd()\r
149                 {\r
150                         //TODO: I have to select a Random Ad from the file\r
151                         AdRecord[] records = GetFileData(AdvertisementFile);\r
152                         if(records!=null)\r
153                         {\r
154                                 if(records.Length > 0)\r
155                                 {\r
156                                         //TODO: Implement Me\r
157                                 }\r
158                         }\r
159                         throw new NotImplementedException();\r
160                         return null;\r
161                 }\r
162                 \r
163                 private bool IsAdMatching(AdRecord currAd)\r
164                 {\r
165                         if(KeywordFilter!=String.Empty)\r
166                         {\r
167                                 /* Perform a case-insensitive keyword filtering\r
168                                  * From the documentation, it looks like there's only one keyword attached.\r
169                                 */\r
170                                 if(currAd.keyword.ToLower() == KeywordFilter.ToLower())\r
171                                         return false;\r
172                         }\r
173                         return true;\r
174                 }\r
175                 \r
176                 private string ResolveAdUrl(string relativeUrl)\r
177                 {\r
178                         throw new NotImplementedException();\r
179                         //TODO: Implement me\r
180                         //TODO: Get the full Url from the relativeUrl\r
181                 }\r
182                 \r
183                 public event AdCreatedEventHandler AdCreated\r
184                 {\r
185                         add\r
186                         {\r
187                                 Events.AddHandler(AdCreatedEvent, value);\r
188                         }\r
189                         remove\r
190                         {\r
191                                 Events.RemoveHandler(AdCreatedEvent, value);\r
192                         }\r
193                 }\r
194                 \r
195                 public AdRotator()\r
196                 {\r
197                         base();\r
198                         advertisementFile = string.Empty;\r
199                         keywordFilter     = string.Empty;\r
200                         target            = string.Empty;\r
201                 }\r
202                 \r
203                 public string AdvertisementFile\r
204                 {\r
205                         get\r
206                         {\r
207                                 return advertisementFile;\r
208                         }\r
209                         set\r
210                         {\r
211                                 advertisementFile = value;\r
212                         }\r
213                 }\r
214                 \r
215                 public string KeywordFilter\r
216                 {\r
217                         get\r
218                         {\r
219                                 return keywordFilter;\r
220                         }\r
221                         set\r
222                         {\r
223                                 keywordFilter = value;\r
224                         }\r
225                 }\r
226                 \r
227                 public string Target\r
228                 {\r
229                         get\r
230                         {\r
231                                 return target;\r
232                         }\r
233                         set\r
234                         {\r
235                                 target = value;\r
236                         }\r
237                 }\r
238                 \r
239                 protected override ControlCollection CreateControlCollection()\r
240                 {\r
241                         return new EmptyControlCollection(this);\r
242                 }\r
243                 \r
244                 protected virtual void OnAdCreated(AdCreatedEventArgs e)\r
245                 {\r
246                         if(Events!=null)\r
247                         {\r
248                                 AdCreatedEventHandler aceh = (AdCreatedEventHandler)(Events[AdCreatedEvent]);\r
249                                 if(aceh!=null)\r
250                                         aceh(this, e);\r
251                         }\r
252                 }\r
253 \r
254                 protected override void OnPreRender(EventArgs e)\r
255                 {\r
256                         if(AdvertisementFile!=String.Empty)\r
257                         {\r
258                                 AdCreatedEventArgs acea = new AdCreatedEventArgs(SelectAd());\r
259                                 imageUrl      = acea.ImageUrl;\r
260                                 navigateUrl   = acea.NavigateUrl;\r
261                                 alternateText = acea.AlternateText;\r
262                         }\r
263                 }\r
264                 \r
265                 protected override void Render(HtmlTextWriter writer)\r
266                 {\r
267                         HyperLink hLink = new HyperLink();\r
268                         Image     image;\r
269 \r
270                         AttributeCollection attributeColl = base.Attributes;\r
271                         ICollection keys = attributeColl.Keys;\r
272                         IEnumerator iterator = keys.GetEnumerator();\r
273                         throw new NotImplementedException();\r
274                 }\r
275         }\r
276 }\r