Using XMLBroker with IntraWeb

By: Guinther Pauli

Abstract: This article will explain how to use the XMLBroker in a IntraWeb application, caching data and updates in browser. And finally, how to resolve updates to database server.

Delphi 5 brought the Internet Express technology that allows the creation of Thin-Clients to MultiTier applications, based on a Web Browser. By using the XMLBroker we can connect it to a DataSetProvider and extract information from an application server in a XML format, which is sent to the browser. The data is then updated (creating a local cache called "delta") and sent back to the XMLBroker, which repasses the updating to the o DataSetProvider and finally to database server.

The page’s interface (the HTML code) is produced by the InetXPageProducer (former MidasPageProducer). The javaScript codes carry out the workability of the page, such as the manipulation of the XML data packet (this is basically done by xmldb.js and xmldisp.js files).

The most interesting aspect of this architecture is that the user can work locally in the records, doing updates, inserts and deletes, without the need of a new requisition to the server in each operation. All the updates are sent to the server only when the user asks for the Apply command. The IntraWeb offers a similar option, available through the components of the IW Client Side palette. The problem is that the data sent to the browser are currently read-only.

In this article we will create an IntraWeb application combining XMLBroker features. What we will basically do is to adapt the code generated by the InetXPageProducer to the IntraWeb.

To build this example you need IntraWeb 5.1 or higher and Delphi Enterprise. Some features used here are not available in the IntraWeb 5.0 and Delphi Professional. The Upgrade to IntraWeb 5.1 is free for Delphi 7 users.

    The XMLBroker architecture with IntraWeb

The following picture shows the XMLBroker architecture, using IntraWeb as Web server application.

Hide image

    Creating the application and the database connection

Start a new IntraWeb Stand Alone application with a DataModule (File|New|Other|IntraWeb). Put a SQLConnection (from dbExpress) component in the DataModule and configure a connection to the Interbase EMPLOYEE.GDB database. After that, set its LoginPrompt property to False.

Put a SQLDataSet (from dbExpress) and set its SQLConnection property. Type “select CUST_NO, CUSTOMER, CONTACT_FIRST, CONTACT_LAST from CUSTOMER” in its CommandText property.

Put a DataSetProvider (from Data Access) component in the DataModule and set its DataSet property to SQLDataSet1. Put also a XMLBroker (from InternetExpress) and set its ProviderName property to DataSetProvider1, and Connected to True.

Hide image

In this example we will not create an application server. So the DataSetProvider and XMLBroker will remain in the same DataModule. If you want to use a application server the steps are basically the same.

    Creating the main form

Go to the main form and using IWLabels, IWEdits and IWButtons (from IW Standard) build your user interface, some like the picture below. Here we will use only the main table fields. Define the Name property of the IWEdits components to CUSTNO, CUSTOMER, CONTACTFIRST and CONTACTLAST respectively.

Hide image

Here is the code that you must put in the ScriptEvents property (OnClick) of each one of the buttons:

Button ScriptEvents (OnClick)
First (|<) If(xml_ready)FieldGroup1_Disp.first();
Prior (<) If(xml_ready)FieldGroup1_Disp.up();
Next (>) If(xml_ready)FieldGroup1_Disp.down();
Last (>|) If(xml_ready)FieldGroup1_Disp.last();
Insert If(xml_ready)FieldGroup1_Disp.newRow();
Delete if(xml_ready)FieldGroup1_Disp.removeRow();
Undo if(xml_ready)FieldGroup1_Disp.undo();
Post if(xml_ready)FieldGroup1_Disp.post();
Apply document.SubmitForm.elements.IW_Action.value='IWBUTTON9'; if(xml_ready)XMLBroker1_RS.Apply(Submit_XMLBroker1, Submit_XMLBroker1.postdelta);

IWBUTTON9 in this case is the name of Apply Button

Remember that JavaScript is case-sensitive!

    Setting the WebMidas JavaScript files

Create a directory called files in the directory where you saved the project files (or where the stand alone application will be run). Copy to this directory the xmldb.js and xmldisp.js files that are in the directory $(DELPHI)/Source/WebMidas.

We need to do a little modification in the xmldb.js file so that there is not a conflict with the functions defined by the IntraWeb. Open this file and on line 313 change the declaration "Validate" for "DoValidate". Do the same on line 409.

Attention: Note that even using IntraWeb you are making use of MIDAS/DataSnap technologies. The use of WebMidas technology requires a license from Delphi Enterprise. See the Borland documentation for further information about the developing with MIDAS/DataSnap.

The next step is to create a JavaScript file which defines the global variables used by WebMidas. Create a file called defvars.js in the same directory ( files ). The content of the file is the following:
var XMLBroker1_RS;
var DataForm1;
var FieldGroup1_Names;
var FieldGroup1_IDs;
var FieldGroup1_Disp;
var Submit_XMLBroker1;

Note that here the global variables are just defined. We initialize the variables in a second file that you must create in the same directory files, with the name initvars.js. Here is the content of the file:

XMLBroker1_RS = new xmlRowSet(XMLPACKET, null, null);
DataForm1 = document.forms['SubmitForm'];
FieldGroup1_Names = new Array("CUST_NO", "CUSTOMER", "CONTACT_FIRST","CONTACT_LAST");
FieldGroup1_IDs = new Array(DataForm1.CUSTNO, DataForm1.CUSTOMER, DataForm1.CONTACTFIRST,
DataForm1.CONTACTLAST);
FieldGroup1_Disp = new xmlDisplay(XMLBroker1_RS, FieldGroup1_IDs, FieldGroup1_Names, null);
Submit_XMLBroker1 = document.forms['SubmitForm'];
xml_ready=true;

    Adding the JavaScript files

The next step is to include the WebMidas script files to document and the file that defines the global variables. We can do this using AddScriptFile method of TIWAppForm. In the OnCreate event of main form type this:

procedure TIWForm1.IWAppFormCreate(Sender: TObject);
begin
AddScriptFile('/files/xmldb.js');
AddScriptFile('/files/xmldisp.js');
AddScriptFile('/files/defvars.js');
end;

Note that we do not include the initialization file of the JavaScript (initvars.js) variables. This code must be added to the initialization of the HTML document, as we will do further on using AddToInitProc.

    Initializing the XML packet the and WebMidas variables

Now we will add the the XML data packet to the document and include in the Initialize function the JavaScript code that initializes the WebMidas variables. In the OnRender event of the main form write the following:

procedure TIWForm1.IWAppFormRender(Sender: TObject);
var
SL : TStringList;
V : OleVariant;
I : integer;
XML : TIWHTMLTag; //declare IWHTMLTag in uses
begin
XML:=TIWHTMLTag.CreateTag('XML');
try
XML.AddStringParam('ID','XMLPACKET');
XML.Contents.AddText(DataModule1.XMLBroker1.GetXMLRecords(I,V,[]));
ExtraHeader.Clear; // clear ExtraHeaders
ExtraHeader.Add(XML.Render); // add the XML Data Packet
finally
XML.Free;
end;
// add initialization variables to the Initialize function of IW document
SL:=TStringList.create;
try
SL.LoadFromFile('files/initvars.js');
AddToInitProc(SL.Text);
finally
SL.Free;
end;
end;

Remember to declare IWHTMLTag in uses.

    Getting the Delta and sending it to the AppServer

Open the HiddenFields property of the IntraWeb main form and add the following line:

postdelta=

This is the field that will keep the Delta which will be sent back to the Web server application. The next step is to get the delta in the server and repass it to the XMLBroker.

When we work with InternetExpress applications, the XMLBroker is automatically registered as the main component of dispatch. XMLBroker get the Delta and repassing it to the server application (DataSetProvider), without the need of requisition handle. We can also create a event handler for its OnGetResponse and apply the cache manually using the method ApplyXMLUpdates. Using XMLBroker with IntraWeb we can't to use this auto dispacth mechanism

To manually get and apply the delta in the IntraWeb Application, in the OnClick event of IWButton9 type the following:

delta:=DataModule1.XMLBroker1.GetDelta(WebApplication.Request); // get delta
DataModule1.XMLBroker1.ApplyXMLUpdates(delta,I); // apply delta

Declare a local string variable named delta, and an Integer named I.

In the start of this article we place a Javascript code in the OnClick event of this button, to send the delta to the IntraWeb server. This function submit the form internally, without the IntraWeb handling. As we manually submit the form, then we have a problem, the OnClick event in the IntraWeb server never fire. To do this, we need to set manually a HiddenField named IW_Action (already defined by IW) to point to IWBUTTON9. We made this in the beginning of the article, in OnClick ScriptEvent of IWButton9, see:
document.SubmitForm.elements.IW_Action.value='IWBUTTON9';
Note that, in a normal IntraWeb application, this is made internally in the SubmitClick function. Then, when the form is submited, IntraWeb read this hidden field to map the right event in the server, in this case, OnClick of IWButton9.

    Testing the application

Run the application. Include, update and delete some records, and note that there is no communication with the Web server. Click then on the button Apply to send the delta (XML) to the server that will repass the updated data to the XMLBroker.

The following pictures shows an update in the two first records, and after the Apply we can see that the o Delta was applied correctly in the Interbase database.

Hide image

Hide image

    Download

    About the Author

Guinther de Bitencourt Pauli is Borland Delphi 7 Advanced Product Certified, Borland Delphi 6 Web Development Certified, Delphi 6 Product Certified and Kylix Product Certified, author of more than 50 articles for the major Delphi magazine in Latin America (ClubeDelphi Magazine), where he also participates as editor. Was speaker in 2: Borland Conference Brazil, and today is developer of SIG, in Santa Maria - RS - Brazil. E-Mail: guintherns@clubedelphi.com.br - remove "ns" when mail-me

Copyright ) 2003 Guinther Pauli
ALL RIGHTS RESERVED. NO PART OF THIS DOCUMENT CAN BE COPIED IN ANY FORM WITHOUT THE EXPRESS, WRITTEN CONSENT OF THE AUTHOR.


Server Response from: BDN9A

 
© Copyright 2008 Embarcadero Technologies, Inc. All Rights Reserved. Contact Us   Site Map   Legal Notices   Privacy Policy   Report Software Piracy