Documentation

##### WSO2 WSAS 2.2-RC1

- Release Notes
- Contents
- Home

##### Downloads

- Releases

##### Documentation

- User Guide
- FAQs
- Installation Guide
- Source
- Running Behind a Proxy
- Data Services Guide
  - RDBMS Data Source
  - CSV Data Source
  - Excel Data Source
  - JNDI Data Source
- EJB Guide
- Clustering Guide
- JMX Guide
- XKMS Guide
- Changing Keys

##### Samples

- Chad
- Commodity Quote
- Data Services
- Flickr
- Javascript
- Jaxws
- JiBX
- Mtom
- STS
- Trader
- Tiny URL
- Metadata Exchange
- WS-Transfer
- XML Key Managment Specification
- Cluster Management

##### API Documentation

- Java Docs

##### Tools

- Code Generation
- Change Password
- Eclipse IDE Integration
- TCP Monitor
- WSDL2Code
- WSDL View
- Try It
- AAR Validation
- MAR Validation

##### Resources

- News

##### Get Involved

- Mailing Lists
- Integration

##### Project Information

- Project Team
- Issue Tracking
- License

# Data Service Sample Guide

## INTRODUCTION

We will use this sample to demonstrate the data service features of WSO2 Web Services Application Server.

Data service allows you to expose your data in relational databases in to XML as a web service. WSAS uses a data service configuration file to define the SQL and how to map the result in to xml elements. Data configuration is stored as an XML file with the extension .dbs in the WSO2WSAS_HOME/repository/dataservices directory.

In this sample we will expose the data in an Apache Derby embedded database for the examples.

## HOW TO BUILD AND RUN THE SAMPLE

To build the samples you need the Apache Ant build tool.

1. Run WSO2 WSAS. See Installation Guide for installation details.
2. Switch to the sample DataService directory,
   e.g. cd WSO2WSAS_HOME\samples\DataService
   Note: $WSO2WSAS_HOME stands for the installation directory of WSO2 Web Services Application Server.
3. To create the database and install the data service configuration type 'ant deploy'

       wsasuser@wsasuser:~/apps/wso2wsas/samples/DataService$ ant deploy
       Buildfile: build.xml

       init:
           [mkdir] Created dir: /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/database
           [mkdir] Created dir: /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/classes

       create-database:
             [sql] Executing file: /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/sql/CreateTables.sql
             [sql] Executing file: /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/sql/Customers.sql
             [sql] Executing file: /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/sql/Employees.sql
             [sql] Executing file: /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/sql/Offices.sql
             [sql] Executing file: /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/sql/OrderDetails.sql
             [sql] Executing file: /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/sql/Orders.sql
             [sql] Executing file: /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/sql/Payments.sql
             [sql] Executing file: /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/sql/ProductLines.sql
             [sql] Executing file: /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/sql/Products.sql
             [sql] Executing commands
             [sql] 3881 of 3881 SQL statements executed successfully

       deploy:
            [copy] Copying 1 file to /home/wsasuser/apps/wso2wsas-2.0/repository/dataservices
            [copy] Copying 1 file to /home/wsasuser/apps/wso2wsas-2.0/repository/dataservices

       BUILD SUCCESSFUL
       Total time: 16 seconds

4. To run the first web service client to access the data service type 'ant run-client-1'

       wsasuser@wsasuser:~/apps/wso2wsas/samples/DataService$ ant run-client-1
       Buildfile: build.xml

       init:

       compile-all:

       run-client-1:

            [java] Requesting contact details of customers in Boston...

            [java] <?xml version="1.0" encoding="UTF-8"?>
            [java] <data:customers xmlns:data="http://ws.wso2.org/dataservice">
            [java]     <customer>
            [java]         <customer-name>Gifts4AllAges.com</customer-name>
            [java]         <contact-last-name>Yoshido</contact-last-name>
            [java]         <contact-first-name>Juri</contact-first-name>
            [java]         <phone>6175559555</phone>
            [java]         <city>Boston</city>
            [java]         <country>USA</country>
            [java]     </customer>
            [java]     <customer>
            [java]         <customer-name>Diecast Collectables</customer-name>
            [java]         <contact-last-name>Franco</contact-last-name>
            [java]         <contact-first-name>Valarie</contact-first-name>
            [java]         <phone>6175552555</phone>
            [java]         <city>Boston</city>
            [java]         <country>USA</country>
            [java]     </customer>
            [java] </data:customers>

            [java] Requesting employee contact details for employee number 1002...

            [java] <?xml version="1.0" encoding="UTF-8"?>
            [java] <data:employees xmlns:data="http://ws.wso2.org/dataservice">
            [java]     <employee>
            [java]         <last-name>Murphy</last-name>
            [java]         <first-name>Diane</first-name>
            [java]         <email>dmurphy@classicmodelcars.com</email>
            [java]     </employee>
            [java] </data:employees>

            [java] Requestinng customer address for Jean King...

            [java] <?xml version="1.0" encoding="UTF-8"?>
            [java] <data:customer-addresses xmlns:data="http://ws.wso2.org/dataservice">
            [java]     <customer-address>
            [java]         <customernumber>112</customernumber>
            [java]         <contactlastname>King</contactlastname>
            [java]         <contactfirstname>Jean</contactfirstname>
            [java]         <addressline1>8489 Strong St.</addressline1>
            [java]         <addressline2/>
            [java]         <city>Las Vegas</city>
            [java]         <state>NV</state>
            [java]         <postalcode>83030</postalcode>
            [java]         <country>USA</country>
            [java]     </customer-address>
            [java] </data:customer-addresses>

       BUILD SUCCESSFUL
       Total time: 3 seconds

5. To run the second web service client to access the data service type 'ant run-client-2'

       wsasuser@wsasuser:~/apps/wso2wsas/samples/DataService$ ant run-client-2
       Buildfile: build.xml

       init:

       compile-all:
           [javac] Compiling 2 source files to /home/wsasuser/apps/wso2wsas-2.0-beta/samples/DataService/classes

       run-client-2:

            [java] Requesting top customers in California...

            [java] <?xml version="1.0" encoding="UTF-8"?>
            [java] <data:top-customers xmlns:data="http://ws.wso2.org/dataservice">
            [java]     <customer-info>
            [java]         <customer-number>124</customer-number>
            [java]         <last-name>Nelson</last-name>
            [java]         <first-name>Susan</first-name>
            [java]         <telephone>4155551450</telephone>
            [java]     </customer-info>
            [java]     <customer-info>
            [java]         <customer-number>129</customer-number>
            [java]         <last-name>Murphy</last-name>
            [java]         <first-name>Julie</first-name>
            [java]         <telephone>6505555787</telephone>
            [java]     </customer-info>
            [java]     <customer-info>
            [java]         <customer-number>161</customer-number>
            [java]         <last-name>Hashimoto</last-name>
            [java]         <first-name>Juri</first-name>
            [java]         <telephone>6505556809</telephone>
            [java]     </customer-info>
            [java]     <customer-info>
            [java]         <customer-number>205</customer-number>
            [java]         <last-name>Young</last-name>
            [java]         <first-name>Julie</first-name>
            [java]         <telephone>6265557265</telephone>
            [java]     </customer-info>
            [java]     <customer-info>
            [java]         <customer-number>239</customer-number>
            [java]         <last-name>Thompson</last-name>
            [java]         <first-name>Valarie</first-name>
            [java]         <telephone>7605558146</telephone>
            [java]     </customer-info>
            [java]     <customer-info>
            [java]         <customer-number>321</customer-number>
            [java]         <last-name>Brown</last-name>
            [java]         <first-name>Julie</first-name>
            [java]         <telephone>6505551386</telephone>
            [java]     </customer-info>
            [java]     <customer-info>
            [java]         <customer-number>450</customer-number>
            [java]         <last-name>Frick</last-name>
            [java]         <first-name>Sue</first-name>
            [java]         <telephone>4085553659</telephone>
            [java]     </customer-info>
            [java] </data:top-customers>

            [java] Requesting top employees in MA...

            [java] <?xml version="1.0" encoding="UTF-8"?>
            [java] <data:top-employees xmlns:data="http://ws.wso2.org/dataservice">
            [java]     <employee>
            [java]         <emp-no>1188</emp-no>
            [java]         <last-name>Firrelli</last-name>
            [java]         <first-name>Julie</first-name>
            [java]         <job-title>Sales Rep</job-title>
            [java]     </employee>
            [java]     <employee>
            [java]         <emp-no>1216</emp-no>
            [java]         <last-name>Patterson</last-name>
            [java]         <first-name>Steve</first-name>
            [java]         <job-title>Sales Rep</job-title>
            [java]     </employee>
            [java] </data:top-employees>

       BUILD SUCCESSFUL
       Total time: 4 seconds

6. To delete all the generated artifacts, including the database, type 'ant clean'

       wsasuser@wsasuser:~/apps/wso2wsas/samples/DataService$ ant clean
       Buildfile: build.xml

       clean:
          [delete] Deleting directory /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/database
          [delete] Deleting directory /home/wsasuser/apps/wso2wsas-2.0/samples/DataService/classes
          [delete] Deleting: /home/wsasuser/apps/wso2wsas-2.0/repository/dataservices/DataServiceSample1.dbs
          [delete] Deleting: /home/wsasuser/apps/wso2wsas-2.0/repository/dataservices/DataServiceSample2.dbs

       BUILD SUCCESSFUL
       Total time: 0 seconds