OBJECT Table 50000 RSS Feed Demo { OBJECT-PROPERTIES { Date=; Time=; Modified=Yes; Version List=RSS; } PROPERTIES { } FIELDS { { 1 ; ;Id ;Integer ;AutoIncrement=Yes } { 2 ; ;Title ;Text250 } { 3 ; ;URL ;Text250 ;ExtendedDatatype=URL } { 4 ; ;Published Date ;Text80 } { 5 ; ;Description ;Text250 } } KEYS { { ;Id ;Clustered=Yes } } FIELDGROUPS { } CODE { BEGIN // Copyright © Microsoft Corporation. All Rights Reserved. // This code released under the terms of the // Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) END. } } OBJECT Codeunit 50000 Get Curr. Exch. Rates from RSS { OBJECT-PROPERTIES { Date=; Time=; Modified=Yes; Version List=RSS; } PROPERTIES { OnRun=VAR RSSFeed@1000 : TEMPORARY Record 50000; Currency@1002 : Record 4; CurrExchRate@1003 : Record 330; CurrCode@1004 : Code[10]; CurrFactor@1005 : Decimal; ValidFromDate@1006 : Date; NoOfUpdates@1007 : Integer; BEGIN Window.OPEN(Text003); Window.UPDATE(1,Text004); GetRSS('http://pipes.yahoo.com/pipes/pipe.run?_id=_kHVIdZ13hGXgt1VwmH_9A&_render=rss',RSSFeed); // only against USD Window.UPDATE(1,Text005); // -> e.g. Title = 'AED' // -> e.g. Description = '3.6729' // -> e.g. Publ. = 'Mon, 27 Sep 2010 16:00:01 CST'; IF RSSFeed.FIND('-') THEN REPEAT CurrCode := DELCHR(RSSFeed.Title); IF ParseRss( RSSFeed.Description, // curr. exch. rate RSSFeed."Published Date", CurrFactor, ValidFromDate) THEN IF CurrFactor > 0 THEN IF Currency.GET(CurrCode) THEN BEGIN CurrExchRate.SETRANGE("Currency Code",Currency.Code); CurrExchRate.SETFILTER("Starting Date",'<=%1',ValidFromDate); IF NOT CurrExchRate.FIND('+') THEN CurrExchRate."Exchange Rate Amount" := 1; CurrFactor := CurrExchRate."Exchange Rate Amount" / CurrFactor; CurrExchRate."Currency Code" := Currency.Code; CurrExchRate."Starting Date" := ValidFromDate; CurrExchRate."Adjustment Exch. Rate Amount" := CurrExchRate."Exchange Rate Amount"; CurrExchRate."Relational Exch. Rate Amount" := CurrFactor; CurrExchRate."Relational Adjmt Exch Rate Amt" := CurrFactor; IF CurrExchRate.INSERT THEN NoOfUpdates := NoOfUpdates + 1; END; UNTIL RSSFeed.NEXT = 0; Window.CLOSE; MESSAGE(Text001,NoOfUpdates,ValidFromDate); END; } CODE { VAR Text001@1000 : TextConst 'ENU=%1 new exchange rate(s) were inserted with starting date %2.'; Text002@1001 : TextConst 'ENU=Do you want to cheat :-) ?'; Window@1002 : Dialog; Text003@1003 : TextConst 'ENU=#1##########################'; Text004@1004 : TextConst 'ENU=Loading from RSS feed...'; Text005@1005 : TextConst 'ENU=Updating Exchange Rate table...'; LOCAL PROCEDURE ParseRss@10(CurrFactorTxt@1000 : Text[80];PubDate@1004 : Text[80];VAR CurrFactor@1002 : Decimal;VAR ValidFromDate@1003 : Date) : Boolean; VAR i@1005 : Integer; j@1007 : Integer; s@1006 : Text[80]; BEGIN IF PubDate = '' THEN EXIT(FALSE); IF NOT ParseDate(ValidFromDate,PubDate) THEN EXIT(FALSE); IF NOT EVALUATE(CurrFactor,CurrFactorTxt,9) THEN EXIT(FALSE); EXIT(TRUE); END; LOCAL PROCEDURE ParseDate@19(VAR ToDate@1000 : Date;FromString@1001 : Text[80]) : Boolean; VAR i@1002 : Integer; s@1003 : Text[30]; dd@1004 : Integer; mm@1005 : Integer; yy@1006 : Integer; BEGIN // -> e.g. FromString = 'Mon, 27 Sep 2010 16:00:01 CST'; IF STRLEN(FromString) < 16 THEN EXIT(FALSE); i := STRPOS(FromString,' '); IF i < 4 THEN EXIT(FALSE); WHILE FromString[i] = ' ' DO i := i + 1; s := COPYSTR(FromString,i); i := 1; WHILE s[i] IN ['0','1','2','3','4','5','6','7','8','9'] DO i := i + 1; IF NOT EVALUATE(dd,COPYSTR(s,1,i - 1)) THEN EXIT(FALSE); s := COPYSTR(s,i); i := 1; WHILE s[i] = ' ' DO i := i + 1; s := COPYSTR(s,i); i := 1; WHILE s[i] <> ' ' DO i := i + 1; CASE LOWERCASE(COPYSTR(s,1,i - 1)) OF 'jan': mm := 1; 'feb': mm := 2; 'mar': mm := 3; 'apr': mm := 4; 'may': mm := 5; 'jun': mm := 6; 'jul': mm := 7; 'aug': mm := 8; 'sep': mm := 9; 'oct': mm := 10; 'nov': mm := 11; 'dec': mm := 12; ELSE EXIT(FALSE); END; s := COPYSTR(s,i); i := 1; WHILE s[i] = ' ' DO i := i + 1; s := COPYSTR(s,i); IF NOT EVALUATE(yy,COPYSTR(s,1,4)) THEN EXIT(FALSE); ToDate := DMY2DATE(dd,mm,yy); EXIT(TRUE); END; PROCEDURE GetRSS@1(FromURL@1001 : Text[250];VAR RSSFeed@1000 : Record 50000); VAR xml@1008 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{88D96A05-F192-11D4-A65F-0040963251E5}:'Microsoft XML, v6.0'.DOMDocument60"; xmlItems@1007 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{2933BF82-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v6.0'.IXMLDOMNodeList"; xmlNodeTitle@1006 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{2933BF80-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v6.0'.IXMLDOMNode"; xmlNodeLink@1005 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{2933BF80-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v6.0'.IXMLDOMNode"; xmlNodeDescription@1010 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{2933BF80-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v6.0'.IXMLDOMNode"; xmlNodeDate@1009 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{2933BF80-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v6.0'.IXMLDOMNode"; i@1003 : Integer; xmlItems2@1011 : Automation "{F5078F18-C551-11D3-89B9-0000F81FE221} 6.0:{2933BF82-7B36-11D2-B20E-00C04F983E60}:'Microsoft XML, v6.0'.IXMLDOMNodeList"; BEGIN IF FromURL = '' THEN EXIT; RSSFeed.DELETEALL; RSSFeed.Id := 0; CREATE(xml); xml.async := FALSE; xml.load(FromURL); //xml.save('C:\temp\rss.xml'); // debug xmlItems := xml.selectNodes('/rss/channel/item'); FOR i := 0 TO xmlItems.length - 1 DO BEGIN xmlNodeTitle := xmlItems.item(i).selectSingleNode('title/text()'); xmlNodeLink := xmlItems.item(i).selectSingleNode('link/text()'); xmlNodeDescription := xmlItems.item(i).selectSingleNode('description/text()'); xmlNodeDate := xmlItems.item(i).selectSingleNode('pubDate/text()'); RSSFeed.Id := RSSFeed.Id + 1; RSSFeed.Title := COPYSTR( xmlNodeTitle.text, 1,MAXSTRLEN(RSSFeed.Title)); RSSFeed.Description := COPYSTR( xmlNodeDescription.text, 1,MAXSTRLEN(RSSFeed.Description)); xmlItems2 := xmlItems.item(i).selectNodes('link'); IF xmlItems2.length > 0 THEN RSSFeed.URL := COPYSTR( xmlNodeLink.text, 1,MAXSTRLEN(RSSFeed.URL)); xmlItems2 := xmlItems.item(i).selectNodes('pubDate'); IF xmlItems2.length > 0 THEN RSSFeed."Published Date" := COPYSTR( xmlNodeDate.text, 1,MAXSTRLEN(RSSFeed."Published Date")); RSSFeed.INSERT; END; IF RSSFeed.FINDFIRST THEN; END; BEGIN // Copyright © Microsoft Corporation. All Rights Reserved. // This code released under the terms of the // Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) END. } }