|
Newsletters
|
|
|
|
|
|
Writing A Multi-Currency Converter Using WMLScript (cont.)
by Wei Meng LEE
|
The WMLScript Program
The listing below shows the ASP document that generates the WMLScript program (Calculate.asp):
<%
Response.ContentType="text/vnd.wap.wmlscript"
if hour(time())>=0 AND minute(time())>=00 AND hour(time())<8 AND minute(time())<=59 then
Response.ExpiresAbsolute = MonthName( month(date()) , true) & " " & day(date()) & " 8:00:00"
else
Response.ExpiresAbsolute = MonthName( month(date()+1) , true) & " " & day(date() + 1) & " 8:00:00"
end if
%>
extern function Convert(amount){
var origAmt=amount;
var fromCurrency = WMLBrowser.getVar("fromCurrency");
var toCurrency = WMLBrowser.getVar("toCurrency");
if (String.compare(fromCurrency,toCurrency)==0) {
WMLBrowser.go("currency.wml#card2");
return;
}
<%
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
connStr = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("currency.mdb") & ";"
rs.Open "Conversion", connStr, adOpenKeySet, adLockOptimistic
%>
var USD =<% rs.Find "Currency = 'USD'"
response.write rs("SinEquiv")
%>;
var SIN =<% rs.MoveFirst
rs.Find "Currency = 'SIN'"
response.write rs("SinEquiv")
%>;
var RM = <% rs.MoveFirst
rs.Find "Currency = 'RM'"
response.write rs("SinEquiv")
%>;
if (fromCurrency=="USD") {
amount = USD * amount;
} else if (fromCurrency=="RM") {
amount = RM * amount;
}
if (toCurrency=="USD") {
amount = amount / USD;
} else if (toCurrency=="RM") {
amount = amount / RM;
}
amount *=1.0;
var str = origAmt + " " + fromCurrency + " is " ;
Dialogs.alert(str + String.format("%.2f",amount) + " " + toCurrency);
}
I use ASP to dynamically generate the WMLScript program. The reason for this is that I want to load the currency exchange rate
from a database so that it is always current. By doing that, the user is always using the latest exchange rate for conversion.
However, there is a little problem in doing this. As database access is required, it could be a time consuming affair if we generate
the WMLScript program everytime someone needs to use the application. In a web environment where concurrency is an important factor in
determining the success of your site, this problem is going to drastically slow down your server.
Fortunately, the nature of this application does not require that the user access up-to-the-minute exchange rates. It would be reasonable
if the exchange rates were updated once a day (at least for this application).
To solve this problem, I make use of the caching property of WMLScript on the device. What I do is to set the expiration date of the WMLScript
to every morning at 8 AM. When a user accesses the application after 8am, or loads the application for the first time, the WMLScript is
fetched from the server and cached for later use. Subsequent usage would then be loaded from the cache.
Let's now take a closer look at our WMLScript file.
Since I am using ActiveX Data Objects (ADO) for database access, I need to include the Adovbs.inc file containing
all the ADO constants used by VBScript.
Response.ContentType="text/vnd.wap.wmlscript"
Remember that since I did not save the WMLScript file with a .wmls extension, I need to explicitly set the MIME type in
the ASP document using the Response.ContentType property. Note that a pair of "(% %)" tags encloses the ASP codes (VBScript in this case).
if hour(time())>=0 AND minute(time())>=00 AND hour(time())<8 AND minute(time())<=59 then
Response.ExpiresAbsolute = MonthName( month(date()) , true) & " " & day(date()) & " 8:00:00"
else
Response.ExpiresAbsolute = MonthName( month(date()+1) , true) & " " & day(date() + 1) & " 8:00:00"
end if
The next portion of the code determines the expiration date of the WMLScript. The checking is simple: if the user loads the
ASP document after 12 midnight, the expiration date would be set to be 8am the same day. If the ASP document is loaded after 8am,
the expiration date would then be set to 8am the next day. To set the expiration date, use the Response.ExpiresAbsolute property.
The date format looks like this: "Oct 21 8:00:00".
extern function Convert(amount){
var origAmt=amount;
var fromCurrency = WMLBrowser.getVar("fromCurrency");
var toCurrency = WMLBrowser.getVar("toCurrency");
Next we have the Convert() function. For a WMLScript function to be callable from WML, it needs to have the extern
keyword. Within the function we defined three variables using the var keyword. The first variable is used to store the original
amount to be converted while the next two variables retrieve the two currencies involved in the conversion. In order for WMLScript
to interact with WML, you can use the WMLBrowser library. There are a number of functions within the library, which allows
you to communicate with the WML deck. The getVar() function retrieves the values of WML variables. For more information
about libraries, please refer to Beginning WAP, WML and WMLScript.
Before we start the conversion, we want to make sure that the two currencies involved are not identical. For string comparisons,
use the compare() function from the String library. If they are identical, load the second card in the WML deck and exit
the WMLScript function using the return keyword.
if (String.compare(fromCurrency,toCurrency)==0) {
WMLBrowser.go("currency.wml#card2");
return;
}
As we are getting the conversion rates from a database, we need to use ADO for data access.
<%
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
connStr = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("currency.mdb") & ";"
rs.Open "Conversion", connStr, adOpenKeySet, adLockOptimistic
%>
Once the records in the database are retrieved, I proceed to assign the individual rate to the respective variables:
var USD =<% rs.Find "Currency = 'USD'"
response.write rs("SinEquiv")
%>;
var SIN =<% rs.MoveFirst
rs.Find "Currency = 'SIN'"
response.write rs("SinEquiv")
%>;
var RM = <% rs.MoveFirst
rs.Find "Currency = 'RM'"
response.write rs("SinEquiv")
%>;
The Find() method of the Recordset object is used to locate the correct record to assign to the variables.
Note that after the first variable is assigned, you need to perform a MoveFirst() operation so as to ensure that the
search always begin from the first record. Also, recall earlier that we opened the recordset using the adOpenKeyset cursor:
rs.Open "Conversion", connStr, adOpenKeySet, adLockOptimistic
This is important, as using the default cursor (adOpenForwardOnly) will cause the MoveFirst() method to fail.
Finally, we perform the conversion:
if (fromCurrency=="USD") {
amount = USD * amount;
} else if (fromCurrency=="RM") {
amount = RM * amount;
}
if (toCurrency=="USD") {
amount = amount / USD;
} else if (toCurrency=="RM") {
amount = amount / RM;
}
The result is then displayed using the alert() function from the Dialogs library.
amount *= 1.0;
var str = origAmt + " " + fromCurrency + " is " ;
Dialogs.alert(str + String.format("%.2f",amount) + " " + toCurrency);
}
The format() function from the String library formats the result to two decimal places.
Next: Debugging The WMLScript
|
|
|