* Maintainer: gvaish@iitk.ac.in\r
* Implementation: yes\r
* Contact: <gvaish@iitk.ac.in>\r
- * Status: 75%\r
+ * Status: 100%\r
* \r
* (C) Gaurav Vaish (2001)\r
*/\r
using System.Collections;\r
using System.Collections.Specialized;\r
using System.Web;\r
+using System.Web.Caching;\r
using System.Web.UI;\r
using System.Xml;\r
using System.Web.Utils;\r
{\r
\r
private string advertisementFile;\r
- private string keywordFilter;\r
- private string target;\r
private static readonly object AdCreatedEvent = new object();\r
\r
// Will be set values during (On)PreRender-ing\r
private string imageUrl;\r
private string navigateUrl;\r
\r
- private string fileDirctory;\r
+ private string fileDirectory;\r
\r
- private static class AdRecord\r
+ private class AdRecord\r
{\r
public IDictionary adProps;\r
public int hits; // or impressions or clicks\r
public string keyword;\r
\r
- public AdRecord()\r
- {\r
- }\r
- \r
- public void Initialize(IDictionary adProps)\r
+ public AdRecord(IDictionary adProps)\r
{\r
this.adProps = adProps;\r
+ hits = 0;\r
+ keyword = String.Empty;\r
}\r
}\r
\r
\r
private AdRecord[] LoadAdFile(string file)\r
{\r
- Stream fSream;\r
+ Stream fStream;\r
ArrayList list;\r
XmlReader reader;\r
XmlDocument document;\r
XmlNode topNode, innerNode;\r
- IDictionary hybridDict;\r
+ IDictionary hybridDict = null;\r
AdRecord[] adsArray = null;\r
try\r
{\r
document.Load(reader);\r
if(document.DocumentElement!=null)\r
{\r
- if(docuent.DocumentElement.LocalName=="Advertisements")\r
+ if(document.DocumentElement.LocalName=="Advertisements")\r
{\r
- topNode = documentElement.FirstChild;\r
+ topNode = document.DocumentElement.FirstChild;\r
while(topNode!=null)\r
{\r
if(topNode.LocalName=="Ad")\r
innerNode = topNode.FirstChild;\r
while(innerNode!=null)\r
{\r
- if(innerNode.NodeType==NodeType.Element)\r
+ if(innerNode.NodeType==XmlNodeType.Element)\r
{\r
- if(hybridDic==null)\r
+ if(hybridDict==null)\r
{\r
- hybirdDict = new HybridDictionary();\r
+ hybridDict = new HybridDictionary();\r
}\r
- hybridDic.Add(innerNode.LocalName, innerNode.InnerText);\r
+ hybridDict.Add(innerNode.LocalName, innerNode.InnerText);\r
}\r
innerNode = innerNode.NextSibling;\r
}\r
adsArray = new AdRecord[list.Count];\r
for(int i=0; i < list.Count; i++)\r
{\r
- adsArray[i] = new AdRecord((IDictionary)list.Item[i]);\r
+ adsArray[i] = new AdRecord((IDictionary)list[i]);\r
}\r
}\r
- } catch(Excetion e)\r
+ } catch(Exception e)\r
{\r
- throw new HttpException("AdRotator: Unable to parse file" + file);\r
+ throw new HttpException("AdRotator_Parse_Error" + file);\r
} finally\r
{\r
- fStream.close();\r
+ fStream.Close();\r
}\r
if(adsArray == null)\r
{\r
- throw new HttpException("AdRotator: No Advertisements Fount");\r
+ throw new HttpException("AdRotator_No_Advertisements_Found");\r
}\r
return adsArray;\r
}\r
\r
private AdRecord[] GetData(string file)\r
{\r
- //TODO: Implement me\r
- fileDirectory = UrlUtils.GetDirectory(TemplateSourceDirectory + MapPathSecure(file));\r
- throw new NotImplementedException();\r
- //TODO: Do I need to check caching?\r
- return null;\r
+ string physPath = MapPathSecure(file);\r
+ string AdKey = "AdRotatorCache: " + physPath;\r
+ fileDirectory = UrlUtils.GetDirectory(UrlUtils.Combine(TemplateSourceDirectory, file));\r
+ Cache cache = HttpRuntime.Cache;\r
+ AdRecord[] records = (AdRecord[])cache[AdKey];\r
+ if(records==null)\r
+ {\r
+ records = LoadAdFile(physPath);\r
+ if(records==null)\r
+ {\r
+ return null;\r
+ }\r
+ cache.Insert(AdKey, records, new CacheDependency(physPath));\r
+ }\r
+ return records;\r
}\r
\r
private IDictionary SelectAd()\r
{\r
- //TODO: I have to select a Random Ad from the file\r
- AdRecord[] records = GetFileData(AdvertisementFile);\r
- if(records!=null)\r
+ AdRecord[] records = GetData(AdvertisementFile);\r
+ if(records!=null && records.Length!=0)\r
{\r
- if(records.Length > 0)\r
+ int impressions = 0;\r
+ for(int i=0 ; i < records.Length; i++)\r
{\r
- //TODO: Implement Me\r
+ if(IsAdMatching(records[i]))\r
+ impressions += records[1].hits;\r
+ }\r
+ if(impressions!=0)\r
+ {\r
+ int rnd = (new Random()).Next(impressions) + 1;\r
+ int counter = 0;\r
+ int index = 0;\r
+ for(int i=0; i < records.Length; i++)\r
+ {\r
+ if(IsAdMatching(records[i]))\r
+ {\r
+ if(rnd <= (counter + records[i].hits))\r
+ {\r
+ index = i;\r
+ break;\r
+ }\r
+ counter += records[i].hits;\r
+ }\r
+ }\r
+ return records[index].adProps;\r
}\r
}\r
- throw new NotImplementedException();\r
return null;\r
}\r
\r
{\r
if(KeywordFilter!=String.Empty)\r
{\r
- /* Perform a case-insensitive keyword filtering\r
- * From the documentation, it looks like there's only one keyword attached.\r
- */\r
if(currAd.keyword.ToLower() == KeywordFilter.ToLower())\r
return false;\r
}\r
\r
private string ResolveAdUrl(string relativeUrl)\r
{\r
- throw new NotImplementedException();\r
- //TODO: Implement me\r
- //TODO: Get the full Url from the relativeUrl\r
+ if(relativeUrl.Length==0 || !UrlUtils.IsRelativeUrl(relativeUrl))\r
+ return relativeUrl;\r
+ string fullUrl = String.Empty;\r
+ if(fileDirectory != null)\r
+ fullUrl = fileDirectory;\r
+ if(fullUrl.Length == 0)\r
+ fullUrl = TemplateSourceDirectory;\r
+ if(fullUrl.Length == 0)\r
+ return relativeUrl;\r
+ return (fullUrl + relativeUrl);\r
}\r
\r
public event AdCreatedEventHandler AdCreated\r
}\r
}\r
\r
- public AdRotator()\r
+ public AdRotator(): base()\r
{\r
- base();\r
advertisementFile = string.Empty;\r
- keywordFilter = string.Empty;\r
- target = string.Empty;\r
+ fileDirectory = null;\r
}\r
\r
public string AdvertisementFile\r
{\r
get\r
{\r
- return keywordFilter;\r
+ object o = ViewState["KeywordFilter"];\r
+ if(o!=null)\r
+ return (string)o;\r
+ return String.Empty;\r
}\r
set\r
{\r
- keywordFilter = value;\r
+ if(value!=null)\r
+ ViewState["KeywordFilter"] = value.Trim();\r
}\r
}\r
- \r
+\r
public string Target\r
{\r
get\r
{\r
- return target;\r
+ object o = ViewState["Target"];\r
+ if(o!=null)\r
+ return (string)o;\r
+ return String.Empty;\r
}\r
set\r
{\r
- target = value;\r
+ ViewState["Target"] = value;\r
}\r
}\r
\r
protected override void Render(HtmlTextWriter writer)\r
{\r
HyperLink hLink = new HyperLink();\r
- Image image;\r
-\r
- AttributeCollection attributeColl = base.Attributes;\r
- ICollection keys = attributeColl.Keys;\r
- IEnumerator iterator = keys.GetEnumerator();\r
- throw new NotImplementedException();\r
+ Image adImage = new Image();\r
+ foreach(IEnumerable current in Attributes.Keys)\r
+ {\r
+ hLink.Attributes[(string)current] = Attributes[(string)current];\r
+ }\r
+ if(ID != null && ID.Length > 0)\r
+ hLink.ID = ID;\r
+ hLink.Target = Target;\r
+ hLink.AccessKey = AccessKey;\r
+ hLink.Enabled = Enabled;\r
+ hLink.TabIndex = TabIndex;\r
+ hLink.RenderBeginTag(writer);\r
+ if(ControlStyleCreated)\r
+ {\r
+ adImage.ApplyStyle(ControlStyle);\r
+ }\r
+ if(imageUrl!=null && imageUrl.Length > 0)\r
+ adImage.ImageUrl = ResolveAdUrl(imageUrl);\r
+ adImage.AlternateText = alternateText;\r
+ adImage.ToolTip = ToolTip;\r
+ adImage.RenderControl(writer);\r
+ hLink.RenderEndTag(writer);\r
}\r
}\r
}\r