Once you have defined the structure of an XML document, you might want to let users see and edit the data in a Windows application or over the Web. This second case is interesting because Delphi provides specific support for it. Delphi 5 introduced an architecture called Internet Express, which is now available as part of the WebSnap platform. WebSnap also offers support for XSL, which I'll discuss later in this chapter.
In Chapter 16, "Multitier DataSnap Applications," I discussed the development of DataSnap applications. Internet Express provides a client component called XMLBroker for this architecture, which can be used in place of a client dataset to retrieve data from a middle-tier DataSnap program and make it available to a specific type of page producer called InetXPageProducer. You can use these components in a standard WebBroker application or in a WebSnap program. The idea behind Internet Express is that you write a web server extension (as discussed in Chapter 20, "Web Programming with WebBroker and WebSnap"), which in turn produces web pages hooked to your DataSnap server. Your custom application acts as a DataSnap client and produces pages for a browser client. Internet Express offers the services required to build this custom application easily.
I know this sounds confusing, but Internet Express is a four-tier architecture: SQL server, application server (the DataSnap server), web server with a custom application, and web browser. Of course, you can place the database access components within the same application handling the HTTP request and generating the resulting HTML, as in a client/ server solution. You can even access a local database or an XML file, in a two-tier structure (the server program and the browser).
The InetXPageProducer allows you to generate HTML forms from datasets in a visual way similar to the development of an AdapterPageProducer user interface. The Internet Express architecture, the interfaces it uses internally, and some of its IDE editor can together be considered the parent of the WebSnap architecture. With the notable difference of generating scripts to be executed on the server side and on the client side, they both provide an editor for placing visual components and generating such scripts. Personally, I'm not terribly happy that the older Internet Express is more XML-oriented than the newer WebSnap.
To deploy this architecture you don't need anything special on the client side, because any browser up to the HTML 4 standard can be used, on any operating system. The web server, however, must be a Win32 server (this technology is not available in Kylix), and you must deploy the DataSnap libraries on it.
To better understand what I'm talking about, and as a way to cover more technical details, let's try a demo called IeFirst. To avoid configuration issues, this is a CGI application accessing a dataset directly—in this case, a local table retrieved via a ClientDataSet component. Later, I'll show you how to turn an existing DataSnap Windows client into a browser-based interface. To build IeFirst, I created a new CGI application and added to its data module a ClientDataSet component hooked to a local .CDS file and a DataSetProvider component connected with the dataset. The next step is to add an XMLBroker component and connect it to the provider:
object ClientDataSet1: TClientDataSet FileName = 'C:\Program Files\Common Files\Borland Shared\Data\employee.cds' end object DataSetProvider1: TDataSetProvider DataSet = ClientDataSet1 end object XMLBroker1: TXMLBroker ProviderName = 'DataSetProvider1' WebDispatch.MethodType = mtAny WebDispatch.PathInfo = 'XMLBroker1' ReconcileProducer = PageProducer1 OnGetResponse = XMLBroker1GetResponse end
The ReconcileProducer property is required to show a proper error message in case of an update conflict. As you'll see later, one of the Delphi demos includes some custom code, but in this example I've connected a traditional PageProducer component with a generic HTML error message. After setting up the XML broker, you can add an InetXPageProducer to the web data module. This component has a standard HTML skeleton; I've customized it to add a title, without touching the special tags:
<HTML><HEAD> <title>IeFirst</title> </HEAD><BODY> <h1>Internet Express First Demo (IeFirst.exe)</h1> <#INCLUDES><#STYLES><#WARNINGS><#FORMS><#SCRIPT> </BODY>
To customize the resulting HTML of the InetXPageProducer, you can use its editor, which again is similar to the one for WebSnap server-side scripting. Double-click the InetXPageProducer component, and Delphi opens a window like the one shown in Figure 22.10 (with the example's final settings). In this editor, you can create complex structures starting with a query form, data form, or generic layout group. In the example data form, I added a DataGrid and a DataNavigator component without customizing them any further (an operation you do by adding child buttons, columns, and other objects, which fully replace the defaults).
Figure 22.10: The InetXPage-Producer editor allows you to build complex HTML forms visually, similarly to the AdapterPageProducer.
The DFM code for the InetXPageProducer and its internal components in my example is as follows. You can see the core settings plus some limited graphical customizations:
object InetXPageProducer1: TInetXPageProducer IncludePathURL = '/jssource/' HTMLDoc.Strings = (...) object DataForm1: TDataForm object DataNavigator1: TDataNavigator XMLComponent = DataGrid1 Custom = 'align="center"' end object DataGrid1: TDataGrid XMLBroker = XMLBroker1 DisplayRows = 5 TableAttributes.BgColor = 'Silver' TableAttributes.CellSpacing = 0 TableAttributes.CellPadding = 2 HeadingAttributes.BgColor = 'Aqua' object EmpNo: TTextColumn... object LastName: TTextColumn... object FirstName: TTextColumn... object PhoneExt: TTextColumn... object HireDate: TTextColumn... object Salary: TTextColumn... object StatusColumn1: TStatusColumn... end end end
// buttons <table align="center"> <tr><td colspan="2"> <input type="button" value="|<" onclick='if (xml_ready) DataGrid1_Disp.first();'> <input type="button" value="<<" onclick='if (xml_ready) DataGrid1_Disp.pgup();'> ... // data grid heading <table cellspacing="0" cellpadding="2" border="1" bgcolor="silver"> <tr bgcolor="aqua"> <th>EmpNo</th> <th>LastName</th> ... </tr> <tr> // a data cell <td><div> <input type="text" name="EmpNo" size="10" onfocus='if(xml_ready)DataGrid1_Disp.xfocus(this);' onkeydown='if(xml_ready)DataGrid1_Disp.keys(this);'> </div></td>...
When the HTML generator is set up, you can go back to the web data module, add an action to it, and connect the action with the InetXPageProducer via the Producer property. This should be enough to make the program work through a browser, as you can see in Figure 22.11.
The XMLBroker applies the changes to the server, returning the content of the provider connected to the ReconcileProvider property (or raising an exception if this property is not defined). When everything works fine, the XMLBroker redirects the control to the main page that contains the data. However, I've experienced some problems with this technique, so the IeFirst example handles the OnGetResponse, indicating this is an update view:
procedure TWebModule1.XMLBroker1GetResponse(Sender: TObject; Request: TWebRequest; Response: TWebResponse; var Handled: Boolean); begin Response.Content := '<h1>Updated</h1><p>' + InetXPageProducer1.Content; Handled := True; end;
|Copyright © 2004-2021 "Delphi Sources" by BrokenByte Software. Delphi Programming Guide||