ObjectStore Active Toolkit Tutorial

Chapter 2

Accessing ATK ActiveX Server from ASP Applications

Introduction
Active Server Page (ASP) allows you to write scripts that run on a server and provide standard HTML output. Although ASP is available only in Microsoft Internet Information Server or Microsoft Peer Web Services, any web browser can display the HTML output.

You can write ASP scripts using VBScript or JavaScript. Because these scripts interact with the ATK ActiveX server, they provide true object-oriented access to the data by means of the ATK object model. (In contrast, the ObjectStore Active Toolkit OLE DB provider uses a standard relational interface, rather than an object-oriented interface.) Thus, you can create and interact with ActiveX objects that implement behavior customized for your application.

Software requirements
To complete this tutorial, you need these software resources:
ResourceWhere to Find It
Database

\odi\ATK6.0\Examples\demodbs\
carsdemo.db


Visual Basic projects

\odi\ATK6.0\Examples\Tutorial2

Active Server Pages

Microsoft Internet Information Server 3.0 or later, or Microsoft Peer Web Services 3.0 or later

These sample applications were developed with Microsoft Visual InterDev 1.0. Although these sample applications were developed and tested with Microsoft Visual InterDev 1.0, you do not need it to run, build, or modify the source code.

In this chapter
In this chapter, you create ASP applications that use the ATK ActiveX server to retrieve tabular information from an ObjectStore database. This chapter contains these exercises:
ExerciseDescription
Build an Application with ATK and ASP

Query an ObjectStore database, build a table, and display the table in HTML.

Use Inspector Instance Formats

Displays the same data view in HTML using different instance formats.

Display Multimedia Object Managers

Displays ObjectStore multimedia Object Managers using the ATK ActiveX server.

Display Tables of Tables

Display the data at many levels of abstraction, and allow the user to navigate to additional data.

Build an Application with ATK and ASP

Overview

This application uses ATK and ASP to query an ObjectStore database to retrieve ActiveX objects, build a table for display, and publish the output in HTML for display in any web browser.

Process

To build an application with ATK and ASP, follow these steps:

  1. Create a data view to use in the application.

  2. Customize the instance format.

  3. Create the Active Server Page.

  4. Create and access ATK ActiveX objects.

  5. Test the application.

1 Create a data view to use in the application.

  • Start Inspector.
  • Open the sample database \odi\ATK6.0\Examples\demodbs\carsdemo.db.
  • Double-click on the part root name in the Database Roots pane of Inspector.
  • Select Data View | Create from the menu bar.

    2 Customize the instance format.

  • Right-click anywhere in the data view grid.
  • Select Set Format of Class from the shortcut menu. The Instance Format dialog box appears.

  • Modify the existing instance format so that it includes all data members in the Part class - partId, price, stock, and description. Double-click on each data member to add it to the Data Members You Want to Show list box.

    The data view is refreshed based on these changes to the instance format.

  • Select Grid | Template | Save As to save these changes as a grid template you can use again. Name the grid template my_tutorial2_1.

    Tip: In Inspector, instance format information is saved in grid templates. Instance format information is just one aspect of grid templates - grid templates include other information (fonts, formulas, and annotation, for example) that is not used by ATK.

  • Select File | Save All.
  • Name the new data view my_tut2_table1.

    The metaknowledge for the cardsdemo.db database now contains the definition of the new table.

    3 Create the Active Server Page.

    You can create an Active Server Page using Visual InterDev or a simple text editor. Procedures for using Visual InterDev are shown here.

    Using Visual InterDev

    To create an Active Server Page using Visual InterDev:

  • Start InterDev and select File | New.
  • On the Files sheet, select Active Server Page and click OK.

    A new window appears, containing the Active Server Page.

    Tip: If you are using a text editor to create an Active Server Page, create a file with an .asp extension that contains the text and format shown in the preceding window.

    4 Create and access ATK ActiveX objects.

    You can write VBScript or JavaScript code to create and access ATK ActiveX objects.

    In VBScript

    This VBScript code is very similar to the Visual Basic example in Chapter 1, A Visual Basic Application with ATK ActiveX Server:

    <%
    
          On Error Resume Next
    
    'Create the ATKKernel object
    
          Set theKernel=Server.CreateObject("ATKKernel.Document")
    
    'Try opening the database
    
          Set theDatabase=theKernel.OpenDatabase("c:\odi\ATK6.0\Examples\demodbs\carsdemo.db")
    
          If Err.Number = 0 Then 'If no errors occurred...
    
    'Open "my_tut2_table1"
    
          Set theDataView=theDatabase.GetDataView("my_tut2_table1")
    
          If Err.Number = 0 Then 'If no errors occurred...
    
                Set theTable= theDataView.GetATKTable 'Get the ATKTable object...
    
                WriteTable(theTable)                  '...and print it
    
          Else  'Error condition
    
                Response.Write("Data view not found: " & Err.Description & "<BR>")
    
          End If
    
          Else   'Error condition
    
                Response.Write("Error opening database: " & Err.Description & "<BR>")
    
    End If
    
    %>
    
    The WriteTable procedure can display any ATKTable object in HTML:

    <SCRIPT LANGUAGE="VBScript" RUNAT=Server>
    
    Sub WriteTable(theTable)
    
    'Initialize the HTML table
    
          Response.Write("<TABLE BORDER=1>" & CHR(13) & CHR(10))
    
    'Fill the column headings
    
          For Each colHeading in theTable.GetHeaders
    
                Response.Write("<TH><B><PRE>" & colHeading & "</PRE></B></TH>")
    
          Next
    
    'Scan all the objects in the table
    
          while not theTable.IsEOT
    
    'Get the tabular representation of each object
    
          For Each aRow in theTable.GetTabularRepresentation
    
    'Each row in the representation is a row in the HTML table
    
                Response.Write("<TR>")
    
    'Scan all the cells in the row
    
          For Each aCell in aRow
    
                Response.Write("<TD>" & aCell & "</TD>")
    
                Next
    
                Response.Write("</TR>" & CHR(13) & CHR(10))
    
          Next
    
    'Jump to the next object in the table
    
          theTable.MoveNext
    
          wend
    
          Response.Write("</TABLE>" & CHR(13) & CHR(10))
    
    End Sub
    
    </SCRIPT>
    

    In JavaScript

    You can also access an ATK ActiveX object through ASP using JavaScript. Refer to the odi\ATK6.0\Examples\Tutorial3\tut2_2.asp for an example of JavaScript code using the same data view.

    5 Test the application.

  • Save the generated Active Server Page in the INetPub\ASPSamp\ATK\Tutorial2 directory as tut2_1.asp.
  • In a web browser, open the URL http://localhost/aspsamp/atk/tutorial2/tut2_1.asp. Your browser window should display information similar to the following:

    Note that the four data members you defined in the instance format are included in this display.

    Use Inspector Instance Formats

    Overview

    Instead of defining new data views every time you want to display different data, you can use different instance formats for the same data view. Instance formats describe the columns of data to display, the column order, and the column heading text.

    Tip: Remember that instance formats are saved in grid templates in Inspector.

    Process

    To display data using a new instance format, follow these steps:

    1. Define a new instance format.

    2. Save the modified metaknowledge.

    3. Modify the ASP code to refresh the metaknowledge.

    4. Check the modified ASP code.

    5. Change the ASP code to select the instance format.

    1 Define a new instance format.

    Using Inspector, define a new instance format for the class Part, and save the instance format in a collection grid.

  • Start Inspector.
  • Open the \odi\ATK6.0\demodbs\carsdemo.db database.
  • Select Data View | Open.
  • Open the data view you saved in the previous exercise, my_tut2_table1. The Data View window displays the name of the grid template currently applied to the data view. In this case, it is my_tutorial2_1, which you saved in the previous example.

  • Right-click anywhere in the data view and select Set Format of Grid Template from the shortcut menu.
  • Remove the stock and description data members from the instance format. The data view is refreshed based on your changes.

  • Select Grid | Template | Save As and save the instance format in the grid template my_tutorial_2_2.

    2 Save the modified metaknowledge.

    Select File | Save All to save the database. The metaknowledge for the database now associates the data view my_tut2_table1 with the my_tutorial2_2 grid template by default, which includes the new instance format.

    However, if you reopen tut2_1.asp, it will behave as if the new instance format was never created or applied (returning the table with four columns). This is because ATK caches the metaknowledge of each database. In this case, ATK caches the metaknowledge of \odi\ATK6.0\Examples\demodbs\carsdemo.db. You need to modify the ASP code to refresh the metaknowledge.

    3 Modify the ASP code to refresh the metaknowledge.

    Modify tut2_1.asp so it forces ATK to reload the metaknowledge for all of the databases it has opened. In the first lines of the ASP code, call ATKKernel::ReloadMetaKnowledge.

    <%
    
          On Error Resume Next
    
    'Create the ATKKernel object
    
          Set theKernel=Server.CreateObject("ATKKernel.Document")
    
    'Force ATKKernel to reload its metaknowledge cache
    
          theKernel.ReloadMetaknowledge
    
    'Try opening the database
    
          Set theDatabase=theKernel.OpenDatabase("c:\odi\ATK6.0\Examples\demodbs\carsdemo.db")
    
    %>
    

    4 Check the modified ASP code.

    In a web browser, open the URL http://localhost/aspsamp/atk/tutorial2/tut2_1.asp to display the result, which uses the instance format saved in the my_tutorial2_2 grid template. Your browser window should display information similar to the following:

    Note that the display now includes only two data members: partId and price.

    5 Change the ASP code to select the instance format.

    You can modify the application to choose the instance format from among those you have saved with any grid template in the database. GetATKTable accepts an optional argument in which you can specify the name of the instance format you want to retrieve. Refer to ATKDataView::GetATKTable in the ObjectStore Active Toolkit Reference for details.

  • In tut2_1.asp, modify the GetATKTable call using the format <ClassName>::<InstanceFormatName>:
    Set theTable = theDataView.GetATKTable("Part::my_tutorial2_1")
    
    WriteTable(theTable)
    
    This call forces ATK to format the selected data view using the Part::my_tutorial2_1 instance format saved with the my_tutorial2_1 grid format. (You can specify any valid Part instance format that you have saved with an Inspector grid template.)

  • Remove the ATKKernel::ReloadMetaknowledge call. Because you have not modified the database metaknowledge, refreshing it is unnecessary.
  • Reload the web browser page. The table now displays four columns, including the stock and description data members.

    Display Multimedia Object Managers

    Overview

    Although you can access multimedia data through the ObjectStore Active Toolkit OLE DB provider, you must do so through a standard OLE DB or ADO (Active Data Objects) interface. However, an ASP script that queries the ATK ActiveX server can directly access the multimedia Object Manager instances, retrieve the data, and display it in a web browser.

    Process

    To display multimedia object managers, follow these steps:

    1. Determine how to access the image Object Manager instances in the database.

    2. Write the ASP code to display multimedia Object Managers.

    1 Determine how to access the image Object Manager instances in the database.

    The way in which you access image Object Manager instances depends on whether or not they are connected to a root.

  • If the images are contained in a collection that is attached to a root, you can access the images through the ATK ActiveX server by specifying the root name.

  • If the images are not connected to a root, you must define a data view to access them.

    Use Inspector
    You can use Inspector to browse multimedia Object Manager instances to determine whether or not they are attached to a root.

  • Start Inspector.
  • Open the \odi\ATK6.0\Examples\demodbs\extrademo.db database. The IMAGES root contains an ObjectStore collection of
    osmmVirageImage instances.

  • Double-click on the IMAGES root to populate the instance pane.
  • Double-click on an element in the collection to see the stored image.

    Because the images are contained in a collection that is attached to a root, there is no need to define a data view to access them. You can access the images through the ATK ActiveX server by specifying the root name.

    2 Write the ASP code to display multimedia Object Managers.

    The structure of an HTML page requires two Active Server Pages:

  • The first page opens the database, retrieves the root, and builds a table containing IMG SRC tags that point to the second page.

  • The second page retrieves the reference to the image and dumps the actual data in the image to the HTML page.

    Using Microsoft Visual InterDev or any text editor:

  • Create the first Active Server Page with this code:
    <%
    
          On Error Resume Next
    
          Set theKernel=Server.CreateObject("ATKKernel.Document")
    
    'Open the extrademo.db database
    
          Set theDatabase=theKernel.OpenDatabase("c:\odi\ATK6.0\Examples\demodbs\extrademo.db")
    
          If Err.Number = 0 Then 'If no errors occurred...
    
    'Retrieve the "exhibit_root" root
    
          Set theRoot=theDatabase.GetRoot("IMAGES")
    
    'Get the ATKReference of the object associated with the root
    
          Set rootValue=theRoot.GetATKReference
    
    'Get the ATKReferences representing the collection associated with the root
    
          Set rootValues=rootValue.GetCollectionItems
    
    'Initialize the HTML table
    
                Response.Write("<TABLE BORDER=1>")
    
                Response.Write("<TH><B>Images</B></TH>")
    
    'Loop on all the Object Managers in the collection
    
          For Each anOM in rootValues
    
                Response.Write("<TR><TD>")
    
    'Build an URL pointing to tut2_om2.asp, containing the code that dumps an OM
    
                Response.Write("<IMG SRC=""tut2_om2.asp?dumpedReference=" & _
    
                      Server.URLEncode(anOM.GetReference) & """>")
    
                Response.Write("</TD></TR>")
    
          Next
    
                Response.Write("</TABLE>" & CHR(13) & CHR(10))
    
          Else
    
                Response.Write("Error opening database: " & Err.Description & "<BR>")
    
          End If
    
    %>
    
  • Save this page in the INetPub\ASPSamp\ATK\Tutorial2 directory as tut2_om1.asp.
  • Create the second Active Server Page to retrieve the image. If you are using Visual InterDev, remove the default InterDev code. The output of this page must contain the type and the actual data from the multimedia Object Manager. This is the code for the second page:

    <%@ LANGUAGE="VBSCRIPT" %>
    
    <%
    
          On Error Resume Next
    
          Set theKernel=Server.CreateObject("ATKKernel.Document")
    
    'Resolve the reference passed in the "dumpedReference" HTML variable
    
          Set theObject=theKernel.ResolveReference( _
    
                Request.QueryString("dumpedReference"))
    
          If theObject.IsObjectManager Then  'If the object is an Object Manager...
    
    'Get the ATKObjectManager object
    
          Set theOM=theObject.GetATKObjectManager
    
    'Store its mime type in the Response.ContentType field
    
                Response.ContentType=theOM.GetOMMimeType
    
    'Write the OM bits
    
                Response.BinaryWrite(theOM.GetDataArray)
    
          End If
    
    %>
    
  • Save the Active Server Page in the INetPub\ASPSamp\ATK\Tutorial2 directory as tut2_om2.asp.
  • In a web browser, open the URL http://localhost/aspsamp/atk/tutorial2/tut2_om1.asp. The information should be similar to that shown here:

    Display Tables of Tables

    Overview

    Typically, when you extract HTML tables from a database, you want to display the data at many levels of abstraction, and allow the user to navigate to additional data. The application you create in this section demonstrates how you can use ATK ActiveX server to create a table that displays all work orders in a database and, upon the user's request, detail all the service items for a particular work order.

    Process

    To display tables of tables, follow these steps:

    1. Check the data view in Inspector.

    2. Check the data view format.

    3. Build the ASP code to display a table of tables.

    4. Test the application.

    1 Check the data view in Inspector.

    This example reuses a prototype of the my_tut1_table2 data view you defined in Chapter 1, A Visual Basic Application with ATK ActiveX Server.

    2 Check the data view format.

  • Start Inspector.
  • Open the \odi\ATK6.0\Examples\demodbs\carsdemo.db database.
  • Select Data View | Open.
  • Select the my_tut1_table2 data view.

    Use this data view to build the main table for display in HTML. You want to design the application so a user accessing this information in a web browser can click on the orderNumber column to get the details of the relation, called items, which links the WorkOrder class to the ServiceItem class.

  • Click on the service root in Inspector to preview the instance format ATK uses to display ServiceItem instances.

    3 Build the ASP code to display a table of tables.

    In the example Display Multimedia Object Managers, the Active Server Page simply displays a collection of images in a web browser. However, this example contains additional functionality that allows the user to click on an item in the first column of the table and navigate that item's relationship to more detailed data.

  • Add a link to the first column so the user can navigate. In
    tut2_1.asp, change the WriteTable procedure so it contains this code:
    For Each aRow in theTable.GetTabularRepresentation
    
          Response.Write("<TR>")
    
          isFirstColumn=true 'used to modify the first column
    
          For Each aCell in aRow
    
                If isFirstColumn Then  'is this the first column?
    
    'if it is the first column, navigate the "items" relation
    
          Set theRelatedItems=theTable.GetObject.GetSlotValue("items")
    
    'then use the returned ATKReference to build the URL of the link;
    
    'we send the dumped reference to the tut2_nav2 page
    
          Response.Write("<TD><A HREF=""tut2_nav2.asp?dumpedReference=" _
    
                & Server.URLEncode(theRelatedItems.GetReference) & """>" _
    
                & aCell & "</A></TD>")
    
          Else  'this isn't the first column
    
          Response.Write("<TD>" & aCell & "</TD>")
    
          End If
    
          isFirstColumn=false  'no more the first column
    
                Next
    
                Response.Write("</TR>" & CHR(13) & CHR(10))
    
          Next
    
  • Save this page in the INetPub\ASPSamp\ATK\Tutorial2 directory as tut2_nav1.asp.
  • The second Active Server Page must decode the dumpedReference value and build the ATKTable object. Use this code for the second page:

    <%
    
          On Error Resume Next
    
          Set theKernel=Server.CreateObject("ATKKernel.Document")
    
    'Resolve the reference passed in the "dumpedReference" HTML variable
    
          Set theObject=theKernel.ResolveReference( _
    
                Request.QueryString("dumpedReference"))
    
    'If the object is a collection
    
          If theObject.IsCollection Then
    
    'write the table gotten through an ATKReferences object
    
                WriteTable(theObject.GetCollectionItems.GetATKTable("ServiceItem"))
    
          End If
    
    %>
    
    The WriteTable procedure is the same one used in tut2_1.asp. Note that the GetATKTable call specifies the name of the class whose instances are contained in the ObjectStore collection that is being displayed. Thus, ATK formats the ATKTable object using the default ServiceItem instance format.

  • Save the second Active Server Page in the INetPub\ASPSamp\ATK\Tutorial2 directory as tut2_nav2.asp.

    4 Test the application.

  • In a web browser, open the URL http://localhost/aspsamp/atk/tutorial2/tut2_nav1.asp. The information displayed should be similar to that shown here:

  • Click orderNumber 1 to display its related item:

  • Click orderNumber 13 to display its details:

    Summary

    In this chapter, you built simple Active Server Page applications using data retrieved by the ATK ActiveX server. By using more of the classes and methods in the ATK object model, you can build much more sophisticated applications.

    You can also implement these examples in a context other than the web, such as Visual Basic.



    [previous] [next]

    Copyright © 1998 Object Design, Inc. All rights reserved.

    Updated: 05/11/99 11:58:34