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