How Do I: Use Script from a Web Service?

You can connect an ECMAScript function to your web service when you want to use the function for translating XML messages to Java or vice versa. When you have created the function in a JSX file, you connect the function to your web service by referencing it within an XML map.

Note: For step-by-step information on creating the script function itself, see How Do I: Create a Script for Use with a Web Service?

The procedures in this topic cover how to use script with parameters, or use script with a return value.

To Use Script with Parameters

1. In Design View, double-click the arrow corresponding to the item (method, callback, or callback handler) with which the script function will be used. The Edit Maps and Interface dialog appears.

2. In the top pane, locate the place in your XML map where you will reference your script function.

This will vary depending on how you are using script.

- For example, your script might translate the parameter values into only part of an outgoing XML message. In that case you may want to include the script reference as one line of a larger XML map — the XML returned from the function will be inserted where the reference is.

- On other other hand, if your script translates from incoming XML to the parameter values, it may be easier to replace the entire map with the function reference. In other words, the reference would be the only text in the root element (the tags containing the name of the Java method or callback).

3. In the top pane, enter a reference to your script function.

Your function reference should have a syntax similar to the following:

The items in parenthesis after the function name must correspond in name and order to the Java parameters that the script will use:

    <placeOrder xmlns="http://www.openuri.org/">
        <order_info>
            {CustomerServices.OrderScripts.convertOrder(currentOrder, currentCustomer)}
        </order_info>
        <customer_id>{currentCustomer.custID}</customer_id>
    </placeOrder>

This example might correspond to the following Java declaration:

    public int placeOrder(Order currentOrder, Customer currentCustomer) {...}

4. Click OK to close the Edit Maps and Interface dialog.

To Use Script with a Return Value

1. In Design View, double-click the arrow corresponding to the item (method, callback, or callback handler) with which the script function will be used. The Edit Maps and Interface dialog appears.

2. In the top pane, locate the place in your XML map where you will reference your script function.

This will vary depending on how you are using script.

- For example, your script might translate the return value into only part of an outgoing XML message. In that case you may want to include the script reference as one line of a larger XML map — the XML returned from the function will be inserted where the reference is.

- On other other hand, if your script translates from incoming XML to the return value, it may be easier to replace the entire map with the function reference. In other words, the reference would be the only text in the root element (the tags containing the name of the Java method or callback).

3. In the top pane, enter a reference to your script function.

Your function reference should have a syntax similar to the following:

The item in parenthesis after the function name must be "return", as in the following example:

    <placeOrderReponse xmlns="http://www.openuri.org/">
        <order_confirmation>
            {CustomerServices.OrderScripts.convertOrderResponse(return)}
        </order_confirmation>
    </placeOrderResponse>

This example might correspond to the following Java declaration:

    public int placeOrder(Order currentOrder, Customer currentCustomer) {...}

4. Click OK to close the Edit Maps and Interface dialog.

# Function References May Not Have Siblings

The function reference (everything between and including the curly braces) must be the only child of its parent element. For example, the following XML map fragment will cause an error:

    <placeOrder xmlns="http://www.openuri.org/">
        {CustomerServices.OrderScripts.convertOrder(currentOrder, currentCustomer)}
        <customer_info>{currentCustomer}</customer_info>
    </placeOrder>

But something like the following would work:

    <placeOrder xmlns="http://www.openuri.org/">
        <order_info>
            {CustomerServices.OrderScripts.convertOrder(currentOrder, currentCustomer)}
        </order_info>
        <customer_id>{currentCustomer.custID}</customer_id>
    </placeOrder>

Related Topics

How Do I: Create a Script for Use with a Web Service?

Using Script Functions From XML Maps