When the solution matters

Tips...

4D v11 SQL (Current Version)4D 2004 Solution Accelerators Academic Downloads

Solution Accelerators

4D Meetings

Customise 4D Business Kit To Fit Your Needs

4D Business Kit Goes Flash (Part II)
Technical Note 02-48

Introduction

This technical note is the second part of the "4D Business Kit goes Flash" series. It covers extended material for the design aspect of a Flash movie for 4D Business Kit. The topics of discussion will include:
- Maximum number of characters per request
- Flash's interpretation of a space character
- Caching problems in Flash
- Automatic Update
- Keeping the session

Maximum number of characters per request

In 4DBK Goes Flash (Part I), it was shown that 4D Business Kit receives a request only in the form of a URL. All tasks that you want 4D Business Kit to perform must be included in the URL that you submitted. 4D Business Kit will not handle or recognise any variables that are sent with the POST or GET method. With this restriction, it becomes obvious that each request is limited to only 255 characters. Submitting a request such as a new account can easily exceed this limitation.

The solution to this problem is to break your request into several small requests and send them separately. By doing this, you can be sure that all requests will be received by 4D Business Kit.

Suppose you want to submit new account information to 4D Business Kit. The account information includes customer ID, password, first name, last name, street address, ZIP code, city, state and country. If the request is sent as a single URL, then your URL in Flash should be constructed as shown below.

Since the number of characters in this request is obviously longer than 255 characters, you will need to break it up into smaller parts, like the following, and send them individually.
- Submitting customer ID and password
"/4daction/4DBKExecute/4DBKStoreSet/ADDY;4DBKFieldSet/CusCode=" & tCode & ";4DBKFieldSet/CusPassword=" & tPassword & ";4DBKGo/result.html"

-- Submitting firstname and lastname
"/4daction/4DBKExecute/4DBKStoreSet/ADDY;4DBKFieldSet/CusFirstName=" & tFirstName & ";4DBKFieldSet/CusLastName=" & tLastName & ";4DBKGo/result.html"

- Submitting phone number and email address
"/4daction/4DBKExecute/4DBKStoreSet/ADDY;4DBKFieldSet/CusPhone=" & tPhone & ";4DBKFieldSet/CusEmail=" & tEmail & ";4DBKGo/result.html"

- Submitting street address and ZIP code
"/4daction/4DBKExecute/4DBKStoreSet/ADDY;4DBKFieldSet/CusStreetMain=" & tStreetMain & ";4DBKFieldSet/CusZIPMain=" & tZIPMain & ";4DBKGo/result.html"

- Sumitting city and state
"/4daction/4DBKExecute/4DBKStoreSet/ADDY;4DBKFieldSet/CusCityMain=" & tCityMain & ";4DBKFieldSet/CusStateMain=" & tStateMain & ";4DBKGo/result.html"

- Submitting country
"/4daction/4DBKExecute/4DBKStoreSet/ADDY;4DBKFieldSet/CusCountryMain=" & tvCountry & ";4DBKGo/result.html"

Flash's interpretation of a space character

When Flash receives external data (a result that is returned by an external CGI program), the data may not be handled properly if it contains a <space> character in it. All space characters must be sent to Flash as the + sign. Flash will automatically convert the + sign to a <space> character.
The same rule applies to sending data from Flash to an external program (e.g. 4D Business Kit) as well. If the data contains the <space> character, it must be converted to the + sign before it is sent. The most obvious case would be sending a street address to 4D Business Kit.

Suppose you created an input variable named StreetMain. The user enters the street address of "123 Main St." You want to send this value to 4D Business Kit by executing the command 4DBKFieldSet/CusStreetMain. The first thing that you have to do is to convert all <space> characters to the + sign. As shown below, Flash script will read one character at a time from the variable "StreetMain".

Set Variable: "vStrLen" = Length (StreetMain)
Set Variable: "Index" = 1
Set Variable: "tStreetMain" = ""
Loop While (Index <= vStrLen)
Set Variable: "Char" = Substring (StreetMain,Index,1)
If (Ord (Char) = Ord (" "))
Set Variable: "Char" = "+"
End If
Set Variable: "tStreetMain" = tStreetMain & Char
Set Variable: "Index" = Index + 1
End Loop

It checks to see if the current read character is a <space> and replaces it with the + sign. It will then append the character to another variable (tStreetmain).

At the end of the script, tStreetMain will contain "123+Main+St." and is ready to be sent to 4D Business Kit.

"/4daction/4DBKExecute/4DBKStoreSet/ADDY;4DBKFieldSet/CusStreetMain=" & tStreetMain & ";4DBKGo/result.html

Caching problems in Flash

Caching problems are some of the most common issues when designing a Flash movie, especially when dealing with data that changes periodically (non-static data). The problem is that sometimes the data is being retrieved from the cache, thus the Flash movie will receive outdated data.

A workaround to this problem is to use "pragma no-cache," and set the server to prevent caching. But this workaround is not always reliable because some browsers might still ignore this condition. What you need to do is to make the caching mechanism think that the data being requested is new. In Flash, this is usually done by appending a unique value to the URL. This unique value will stop the caching mechanism from taking action. For example, the three "Load Variables" requests below are interpreted as 3 different requests.
Load Variables ("/ADDY_Site/WebPagesUs/CusAccount.html?1,0)
Load Variables ("/ADDY_Site/WebPagesUs/CusAccount.html?2,0)
Load Variables ("/ADDY_Site/WebPagesUs/CusAccount.html?3,0)

These requests are basically the same request. The only thing that separates them is the unique number at the end of the URL.

In Flash, you can use the command Random() to generate a unique value to a variable and then append the variable to the URL. Here is what it should look like in Flash Action-Script:
Set Variable: "Fla_NoCache" = Random (50000)
Load Variables ("/ADDY_Site/WebPagesUs/CusAccount.html?" & Fla_NoCache,0)

Command 4DBKNoCache:

4D Business Kit also has a tag that will return random codes to prevent the requested page from being retrieved from the cache. This tag is called 4DBKNoCache. 4DBKNoCache allows inserting a random code in the page URL, making it unique. The random code that is generated by this tag has a format of NC#####. However, 4DBKNoCache can only be used in an HTML file. It does not work with the Flash movie for the following reason. If 4DBKNoCache is embedded in the HTML file before it is sent out to the browser, 4D Business Kit will process it and replace it with its result. The problem is that the Flash movie is an embedded object in an HTML file. Its Action-Script is part of the Flash movie and it is private to the movie only. There is no way to force an internal constructed URL in Flash to be pre-processed by 4D Business Kit. To get around this problem, we will once again use the command Random() to generate a random code for the URL. Here is what it should look like in Flash Action-Script:

Set Variable: "Fla_NoCache" = Random (50000)
Load Variables ("/4daction/4DBKExecute/4DBKStoreSet/ADDY;4DBKGo/OrderList.html;NC" & Fla_NoCache,0)

Caching problem during design phase

When a Flash movie is loaded into a Web browser, the .swf (Shockwave file) is saved into the Cache. The cached .swf file will be used to speed up movie loading for each request (this avoids a re-downloading, which could be slow on a modem connection). However, this raises a big problem during the design phase of your movie. Each time you make any changes to your Flash movie, the .swf file must be deleted from the cache to avoid the old movie from loading into the browser. If the requested .swf file exists in the cache, the browser will load it from the cache instead of requesting it from the Server (4D Business Kit).

Automatic Update

In the example Flash store, the shopping cart can be displayed as part of the Flash movie (once the user has logged in) and/or a floating HTML window (once the user has entered the store).
Once the user has logged in to his or her account, the shopping order will appear as shown below:

From this page, users can also modify their orders by clicking the "Change Order" button. The "Change Order" option will open the shopping cart as an external window.

* This window can also be opened from other areas by clicking the "View Cart" button.

This floating window is entirely independent from the main browser window in which the Flash movie is loaded. The information that is showing in this window is simply HTML generated.

From this window, users can make any changes to their orders and the result will automatically be updated to the shopping order in the Flash movie.

The automatic order-update in the Flash movie is done by performing a periodic request to 4D Business Kit to obtain the latest data. Each time the order has been modified through the floating shopping cart window, the movie will begin playing a 16-frame cycle (approximately 1 second) between frame 31 and 47.

For each cycle, the following Action-Script will be executed at frame 31 to obtain the most current shopping order.

Set Variable: "Fla_NoCache" = Random (50000)
Load Variables ("/4daction/4DBKExecute/4DBKStoreSet/ADDY;4DBKGo/OrderList.html;NC" & Fla_NoCache, 0)

Keeping the session

All connections to 4D Business Kit are determined by the session ID. 4D Business Kit assigns the session ID for each connection in two different forms:

1) The session is saved as a cookie on the browser. 4D Business Kit saves the session ID in the cookie called 4DBK. Each time the Flash movie makes a request to 4D Business Kit, the 4DBK cookie is sent in a separate header allowing 4D Business Kit to determine current selections, ordered items, selected shipping services, etc.
2) 4D Business Kit generates its internal session ID and attaches it to the URL in the HTML page before sending it out to the browser. This session ID is used in case the Web browser doesn't accept cookies. The attached session ID will be extracted by 4D Business Kit from the requested URL to determine the user session.

Both types of the session ID work well with a store that is HTML based. But both cannot be used by a Flash store. The only way to keep the session between 4D Business Kit and the Flash store is to use cookie session ID (#1). This is one disadvantage with the Flash store because the movie is an embedded object. 4D Business Kit cannot manipulate the Flash URL calling script the same way as an HTML file. Once the index page (e.g. Store.html) is sent to the browser, the movie will be loaded to the client without being processed by 4D Business Kit. Since the page needs to be preprocessed by 4D Business Kit, there is no other way to keep the session except using the cookie session ID.

Summary

This technical note explains several important aspects when designing a Flash movie for 4D Business Kit's store. It gives the reader a solution to a common problem with the Flash movie, especially when the issue involves a request between a Flash movie and 4D Business Kit.

4D UK Newsletter

Please enter your email address to register for the 4D UK newsletter

Minimum System

International | Company | Contact 4D | Site Map | Privacy Policy | © 4D UK 1995-2009 | Change font size: [A] [A] [A] | Print this page