Constructing the JB Book Seller's Application - XML

As discussed in the case study, the individual bookstores use the Office XP-based solution to maintain their daily transactions. The following steps discuss the transactions that take place at the bookstores of JB Book Sellers.

  1. When a user places an order for a book, the bookstore receives the order and saves the details of the order in the database.
  2. The bookstores also accept orders through mail, e-mail messages, or phone calls. Therefore, the details of the customer, such as the name and address, are stored in the Microsoft Access database by using the Microsoft Access forms-based interface.
  3. The details of the order, such as order number and credit card details, are also stored in the Microsoft Access database.
  4. To validate the data that the customer entered, certain validation tests are done. These tests include validating the ZIP Code against the given city and state, verifying the e-mail address, and checking the credit card number for Mod 10 compatibility. You learned about the Mod 10 formula in Chapter, "Design and Creation of a Web Service Using the IBM Toolkit."

Calling Web Services from an Office XP Application

Before you call Web services from an Office XP application, you need to create a Microsoft Access application. Therefore, it's necessary to add the support for the Web service. The Microsoft Access application contains an OrderDetails form. The form is shown in Figure.

The OrderDetails Form

The OrderDetails Form

As you can see, the OrderDetails form contains text boxes that accept user input for ZIP Code, E-mail, Credit Card, and Card Type. The ZIP Code needs to be validated against the state and the city, as specified by the user. The e-mail address needs to be verified and, finally, the credit card number needs to be checked for Mod 10 compliance. When the credit card number is validated, the card type is detected and filled in the text box.

You can use the existing Web services to perform the preceding tasks. You can search for information about these Web services in the UDDI directory. As you know, however, UDDI is too cluttered with false and junk data.

NoteWeb sites such as SalCentral and XMethods are commonly used sites for searching information about Web services and their features.

Adding Reference to a Web Service

To call a Web service from an Office XP application, perform the following steps:

  1. Open the OrderDetails form in the Design view and add a button next to the Credit Card text box.
  2. Label the button Validate Credit Card.
  3. Right-click on the button and select Build Event.
  4. In the resulting menu, select Code Builder. The internal VBA editor is displayed, as shown in Figure.
  5. The Office's Internal VBA Editor

    The Office's Internal VBA Editor

    Notice that installing the Web services toolkit has resulted in a change in the Add Web Services References option in the Tools menu.

  6. Click on the Add Web Services References option. A window, as shown in Figure, is displayed.
  7. The Add Web Services References Option

    The Add Web Services References Option

    This window is similar to the Add Web Reference dialog box in Visual Studio .NET that is used to add a Web reference to a Web service from a .NET application.

  8. Browse the site to locate a Web service that can perform the required validations.
  9. In Office XP, click on Web Services Reference in the Tools menu.
  10. In the resulting dialog box, click on the Web Service URL option button. A text box is activated.
  11. Enter the path to the .wsdl file of the Web service you have chosen.
  12. Note:This dialog box allows you to search for various UDDIs of a service by using business or service types or keywords.

  13. Click on the Search button. The Web service and the Web methods are displayed in the dialog box.
  14. Note :If the Web service that is found is grayed out, it implies that a Web method of the Web service returns a complex type variable, which Office XP doesn't support.

  15. Select one or more XML Web services for which you want the tool to create proxy classes, and click on the Add to Project button. Selecting the Add to Project button enables the Web Service References Tool to perform several activities, as discussed in the following list:
    • Create a class module file, which is a .cls file, for each Web service. The name of the class module file contains a prefix clsws_ followed by the name of the Web service. For example, if the name of the Web service is CreditCardValidator, the name of the .cls file would be clsws_CreditCardValidator.
    • Create a private constant, c_WSDL_URL, in the .cls file that corresponds to the .wsdl file of the Web service.
    • Create public functions that correspond to the Web methods in the Web service. The name of the function is prefixed with wsm_ followed by the name of the Web method.

The Web Service References Tool in this case creates a class module with the name clsws_CreditCardValidator. The code for the clsws_ CreditCardValidator file contains two sections. The first part contains comments that describe the details of the class module. The next section discusses the instructions to declare and instantiate this class in the code. The code for the class module is shown in the following example:

Next, two internal class variables are initialized to the SoapClient proxy object and the URL of the .wsdl file, respectively. This SoapClient proxy object is used to exchange XML Web service calls between the client application and the Web server.

Note :The SoapClient proxy class variable is prefixed with sc_, whereas the name of the variable initialized to the URL is always c_WSDL_URL.

The code for the class variables is as follows:

The preceding code creates a SoapClient object and initializes it with the URL of the Web service represented by the proxy class. However, if your application accesses the Web through a proxy server, you need to add the following code that provides the credentials for authentication by the proxy server or the corporate firewall:

Next, the Class_Terminate event is used to deallocate memory to resources that are no longer used. For example, when the SoapClient proxy object is no longer used, its memory is deallocated by using the Class_Terminate event, as shown in the following code:

In the preceding code, the CreditCardValidatorErrorHandler subroutine is used to handle the errors generated by the Web server hosting the Web service. As you can see, the name of the subroutine contains the keyword ErrorHandler preceded by the name of the Web service, CreditCardValidator.

Finally, declare the methods in the Web service. It is important to note that Web methods created by the Web Service References Tool are always public, prefixed with wsm_ followed by the Web method name, and list input parameters with a data type prefix followed by the Web method parameter.

The code that the Web Service References Tool generates for the Web methods is as follows:

Adding Code to the Validate Credit Card Button

After you add a reference to the Credit Card validation Web service, add the following code for the Validate Credit Card button:

The preceding code creates an instance of the proxy class. First, the code checks whether the credit card number is a valid number and retrieves the card type. Then it displays the card type in the Card Type text box.

Adding a Web Service to Validate the E-Mail

Similarly, you can add references to the Web services that validate the e-mail address.

After you have created the proxy for the mxchecker Web service, you can call its methods, as shown in the following code:

Adding a Web Service to Validate the ZIP Code

To enable validation of the ZIP Code, a Web service at http:// services. pagedownweb. com/ZipCodes.asmx?WSDL offers methods that accept a ZIP Code and state or a ZIP Code and city as parameters. This Web service returns True or False after validation. The code for invoking the Web service at the click of a button is shown in the following sample:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

XML Topics