## ## Document History ## ## Created (peeje01) ## 27-Apr-1999 (peeje01) ## Change include file name from -h.html to _h.html ## 24-Apr-2001 (fanra01) ## Bug 104537 ## Update the code examples. ## 02-Apr-2004 (fanra01) ## Bug 112091 ## Editorial changes from griel02. Globe Shop Home Page

Globe Shop Home Page

Introduction

The aim of this example is to show how, using a few short files, a shopping application can be built rapidly using the rich and extendable feature set of the Web Deployment Option MACROS.

Application Storyboard

The storyboard for our Globe shop tells us that the user will be presented with a list of the available items on the home page. From there, clicking on an item's number will take the user to a description page specifying price for example.

The description page leads to either adding the item to the shopping bag or returning to the product list. If the user adds the item to their bag they can then view the contents of the bag or return to the product page.

From the bag view page the user may either complete their purchase and move to the order confirmation page or return to add more items to their bag. The confirmation page should list all the articles in the bag, the total cost, and the unique order number.

Finally, it should be possible from the bag view page for the user to empty their bag and start afresh.

The Shop Home Page

The home page for the Globe shop has two main jobs. It must list all the items for sale, which are listed in the table play_item. This is a standard type of select statement that we have seen before.

In addition, the home page must allocate an order number for the user (if one does not already exist) and this is achieved by invoking a user defined function extension to the ICE server (file play_neworder.sc). The function invokes a database procedure that increments a count in a table (file: play_newOrder.sql). We will examine each of these files, starting with the procedure. For clarity, error checking code has been left out.

Note: This example assumes that the function is to be built for the windows platform.

The procedure new_order produces a new order number by updating the order number in the counters table. This should be executed in a transaction on its own and the order number used later as needed in another transaction. This is exactly what happens when the Web Deployment Option extension function, newOrder, executes it.

We also include a general shop action page. This activates those links set to Yes in the parameter list.

To begin, we write the database procedure to generate monotonically increasing order numbers.

File: play_NewOrder.sql

/* Procedure new_order */
create procedure new_order as
declare
next integer not null;
begin
    select value into :next from counters where name = 'order';
    next = next + 1 ;
    update counters set value = :next where name = 'order';
    return :next;
end;

The database procedure new_order is invoked on its own in a transaction by the server extension function newOrder, which returns the value as a string in the variable out_OrderNumber.

The file play_NewOrder.sc includes the file play_NewOrder.h, to define return types and the function description table ice_function_table. reproduced here.

File: play_NewOrder.h


# include 

# define ICE_EXT_API __declspec(dllexport)

/*
** Define the systems multi threaded safe memory
** allocater and return type
*/
# define mt_safe_malloc(size) HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY, size)
# define mt_safe_free(ptr)    HeapFree (GetProcessHeap(), 0, ptr)
# define MEMORY_P char*

typedef char*       ICE_STATUS;
typedef ICE_STATUS  (*PFNEXTENSION) (char**, BOOL*, char **);

typedef struct ice_function_table
{
    char*           pszName;
    char**          pszParams;
}SERVER_DLL_FUNCTION, *PSERVER_DLL_FUNCTION;

typedef ICE_STATUS  (*PFNINITIALIZE)(PSERVER_DLL_FUNCTION*);

The ICE server defines an interface to which all extension functions must be written. This includes a defined entry point, which returns a structure describing the function and its parameters. This and the function are to be found in the file play_NewOrder.sc:

File: play_NewOrder.sc

/**
** Name: play_NewOrderNr.sc
**
** Description:
**     Returns a new order number from the counters table
**     Uses a database procedure to get the latest number.
**
*/
# include "play_NewOrder.h"

# define    MAX_SIZE    20  /* size for an int */
/*
** Parameter name list.
** A NULL pointer terminates the list.
*/
static char* pszNewOrderParams[] =
    {"out_orderNumber", NULL};

/*
** Function Description
*/
static SERVER_DLL_FUNCTION FunctionTable[] =
{
	    { "newOrder", { pszNewOrderParams } },
	    { NULL }
};

/*
** Name: InitICEServerExtension
**
** Description:
**     Mandatory function for providing function description to the server.
**
** Inputs:
**     None.
**
** Outputs:
**     ppServerDllFunction: pointer to the function description structure.
**
** Returns:
**     pointer to error text
**     NULL on success
*/
ICE_EXT_API ICE_STATUS
InitICEServerExtension(PSERVER_DLL_FUNCTION* ppServerDllFunction)
{
	ICE_STATUS status = NULL;
	*ppServerDllFunction = FunctionTable;
	return status;
}

/*
** Name: newOrder
**
** Description:
**     Return the next order number
**
** Inputs:
**     None.
**
** Outputs:
**     out_OrderNumber
**
** Return:
**     pointer to error text
**     NULL on success
*/
ICE_EXT_API ICE_STATUS
newOrder(char** out_OrderNumber, BOOL* print, MEMORY_P *context)
{
    ICE_STATUS  status = NULL;

    *print = (char)FALSE;
    /*
    ** if first invocation allocate some memory for the result
    */
    if (*context == NULL)
    {
        exec sql begin declare section;
        long x;
        exec sql end declare section;

        *context = mt_safe_malloc(MAX_SIZE);
        if (*context == NULL)
        {
            return ("Memory error\n");
        }

        exec sql connect 'icetutor' identified by 'icedbuser';
        exec sql execute procedure new_order into :x;

        exec sql commit;
        exec sql disconnect;

        sprintf (*context, "%d", x);

        *out_OrderNumber = *context;
        *print = TRUE;
    }
    else
    {
        mt_safe_free (*context);
        *context = NULL;
        *out_OrderNumber = NULL;
    }

    return (status);
}

Entry Point

You must perform some "housekeeping" when writing an ICE server extension function. There must be a function InitICEServerExtension whose purpose is to return a structure describing the function. This structure contains the name of the function and its parameter list. In our case, the function is newOrder and it exports one value parameter, out_OrderNumber, and a context pointer to the memory location used, context.

Generate a New Order Number

When newOrder is first invoked the context pointer is set to NULL indicating that it must allocate some memory to hold the value to be returned. It returns a pointer to that memory in the context variable. The next order number is returned in the parameter out_OrderNumber.

Clean Up

NewOrder must free context memory when the it is invoked with a non null value for context.

Building the Extension Function

The extension function must be built as a dynamic link library (for the Windows platform) and installed in the files\dynamic directory in the ingres system area (addressed by the environment variable II_SYSTEM).

File: Makefile


CFLAGS=-D_X86_=1 -DWINVER=0x0400 -DWIN32 -D_WIN32

all: play_NewOrder.dll

play_NewOrder.dll: play_NewOrder.obj
    link -debug:mapped,full -debugtype:both /DLL /out:$@ $** ws2_32.lib \
    $(II_SYSTEM)\ingres\lib\ingres.lib

play_NewOrder.obj: play_NewOrder.c play_NewOrder.h
    cl -c -Zi -MD $(CFLAGS) $*.c

play_NewOrder.c: play_NewOrder.sc
    esqlc $*.sc

install: play_NewOrder.dll
    copy play_NewOrder.dll $(II_SYSTEM)\ingres\files\dynamic\play_NewOrder.dll

The 'install' target copies the library to the appropriate place after it has been successfully built (target 'all').

Using the Function play_NewOrder on the Web Page

After the dynamic link library has been successfully built and installed, it can be used in a Web document. We chose to issue a unique order number to every visitor to the shop home page. In addition, we must ensure that an existing visitor who has confirmed an order is issued a new order number in case they wish to come back and order more items.

File: play_shopHome.html


<html>
<head>
<title>Globe Boutique</title>
</head>
<body>
<h1>Globe Boutique</h1>
The Globe Boutique is where you can purchase all your favourite Globe
memorabilia.
<p>
Visit often to find that gift for the person in your life that always seems to have everything
<p>
Genuine quality products and gifts with a unique cultural heritage
<p>
<h2>Instructions</h2>
<!-- #ICE REPEAT IF (DEFINED (e_shopTxn))
        THEN=``
	ELSE=`<!-- #ICE DECLARE=``session.e_shopTxn=NOT-OPEN`` -->`
-->

<!-- #ICE REPEAT IF (DEFINED (e_orderNumber) AND `:e_shopTxn` != `COMPLETE`)
	THEN=``
	ELSE=`<!-- #ICE REPEAT FUNCTION=``play_NewOrder.newOrder``
		HTML=``<!-- #ICE
			DECLARE=````session.e_orderNumber=:out_orderNumber````
		-->``
	-->`
-->
Select an item from the list below to view its description.
<!-- #ICE 
          DATABASE = `icetutor` 
          SQL=`set lockmode session where readlock = nolock;
          select id, name from play_item` 
          TRANSACTION=`Shoppe` 
          CURSOR=`Keeper` 
	  ROWS=`-1`
          TYPE=`TABLE`
          HEADERS=`id,Reference,name,Article` 
          LINKS=`id,plays[play_shopDescribe.html]` 
          -->
<!-- #ICE COMMIT=`Shoppe` -->

<!-- Standard Shop Actions: activate View bag only -->
<!-- #ICE REPEAT INCLUDE=`plays[play_shopAction_h.html]?View=Yes` -->

<!-- Standard Session Control controls -->
<!-- #ICE REPEAT INCLUDE=`plays[play_SessionControl_h.html]` -->
</body>
</html>

First, we check to see if the shop has been entered for the first time by checking for the existence of the variable e_shopTxn:

<!-- #ICE REPEAT IF (DEFINED (e_shopTxn))
        THEN=``
	ELSE=`<!-- #ICE DECLARE=``session.e_shopTxn=NOT-OPEN`` -->`
-->

If the variable does not exist it is created and given the value NOT-OPEN to indicate that a (shopping) transaction has yet to be opened.

Next, we check to see if an order number exists, e_orderNumber, and if the transaction is complete.

<!-- #ICE REPEAT IF (DEFINED (e_orderNumber) AND `:e_shopTxn` != `COMPLETE`)

If no order number exists or the transaction is not complete, we need to issue a new order number using the function newOrder to be found in the library play_NewOrder. (note: Do not add the .dll or .so extension to the library name):

	<!-- #ICE REPEAT FUNCTION=``play_NewOrder.newOrder``

Since the function invocation is part of another Web Deployment Option MACRO statement we must double up the grave quotes.

We then set a session variable to hold the order number and terminate the statement:

		HTML=``<!-- #ICE
			DECLARE=````session.e_orderNumber=:out_orderNumber````
		-->``
	-->`
-->

When we declare the variable e_orderNumber we are nested two levels down, and so need to double the grave quotes again. The number of grave quotes you need is easily calculated as 2 raised to the power "level" where level is the nesting level, here 2 because we start with level = 0 (2 raised to the power 0 is 1).

Having allocated the new order number we perform a select with links to create links on the item identifier to a page providing more detailed description of the item selected. The user can then decide to buy or not to buy.

At any time in the shop application it is possible for the user to move to any of the other pages. This is accomplished by including the file play_shopAction_h.html which takes the parameters in the following table:

Parameter:Meaning:
ViewView contents of shopping bag
ConfirmConfirm order
RemoveEmpty the shopping bag

The file is included as follows:

<!-- Standard Shop Actions: activate View bag only -->
<!-- #ICE REPEAT INCLUDE=`plays[play_shopAction_h.html]?View=Yes` -->

Here we only activate the View option; the return to the product list option is always valid. The action file is as follows:

File: play_shopHome.html

<!-- shopAction_h.html:
    Activate links as appropriate. Link is active if the
    variable of that name is set to Yes, else inactive
    Return to Products is always active.
-->
<hr width="50%">
<table border=0 cellspacing=4>
<tr>
<td>
<!-- #ICE IF (`:View` == `Yes`)
    THEN=`<a href="plays[play_shopView.html]">View Bag Contents</a>`
    ELSE=`View Bag Contents`
-->
<td>
<a href="plays[play_shopHome.html]">Return to Products</a>
<td>
<!-- #ICE IF (`:Confirm` == `Yes`)
    THEN=`<a href="plays[play_shopConfirm.html]">Place Order</a>`
    ELSE=`Place Order`
-->
<td>
<!-- #ICE IF (`:Remove` == `Yes`)
    THEN=`<a href="plays[play_shopRemove.html]">Empty Bag</a>`
    ELSE=`Empty Bag`
-->

This file builds a table and sets the contents of each cell to be either an active link or an inactive link (normal text) depending on the values of the variables passed in. We next examine the item description page.