Unknown macro: {next_previous_links}
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 23 Current »

The request box feature allows you to invoke multiple operations (consecutively) to a datasource using a single operation. Follow the steps given below to define a data service that can invoke request box operations:


Setting up a datasource

Follow the steps given below to set up a MySQL database for this tutorial.

  1. Install the MySQL server.
  2. Download the JDBC driver for MySQL and copy it to your <DSS_HOME>/repository/components/lib/ directory.
  3. Create a database named Company.
  4. Create the following tables:

    • Offices table:

      CREATE TABLE `Offices` (`OfficeCode` int(11) NOT NULL, `AddressLine1` varchar(255) NOT NULL, `AddressLine2` varchar(255) DEFAULT NULL, `City` varchar(255) DEFAULT NULL, `State` varchar(255) DEFAULT NULL, `Country` varchar(255) DEFAULT NULL, `Phone` varchar(255) DEFAULT NULL, PRIMARY KEY (`OfficeCode`));
    • Employees table:

      CREATE TABLE `Employees` (`EmployeeNumber` int(11) NOT NULL, `FirstName` varchar(255) NOT NULL, `LastName` varchar(255) DEFAULT NULL, `Email` varchar(255) DEFAULT NULL, `JobTitle` varchar(255) DEFAULT NULL, `OfficeCode` int(11) NOT NULL, PRIMARY KEY (`EmployeeNumber`,`OfficeCode`)); 
  5. Insert the following data into the Offices table:

    INSERT INTO Offices VALUES (1,"51","Glen Street","Norwich","London","United Kingdom","+441523624");
    INSERT INTO Offices VALUES (2,"72","Rose Street","Pasadena","California","United States","+152346343"); 

Define a data service to invoke request box operations

Let's create a data service using the Create Data Service wizard:

  1. Log into the management console and click Create under Data Service.
  2. Add a name for the data service.

  3. Select the Enable Boxcarring check box. As a result of selecting this check box, the Disable Legacy Boxcarring Mode check box will appear on your screen as shown below. You can select this new check box (Disable Legacy Boxcarring Mode) as well.

    What is Boxcarring and Request Box?

    Boxcarring is a method of grouping a set of service calls so that they can be executed as a group (i.e., the individual service calls will be executed consecutively in the specified order). Note that we have now deprecated the boxcarring method for data services. Instead, we have replaced boxcarring with a new request type called request_box

    Request box is simply a wrapper element (request_box), which wraps the service calls that need to be invoked. When the request_box is invoked, the individual service calls will be executed in the specified order, and the result of the last service call in the list will be returned. In this tutorial, we are using the request box to invoke the following two service calls:

    1. Add a new employee to the database
    2. Get details of the office of the added employee

    When you click the Enable Boxcarring check box for the data service, both of the above functions (Boxcarring and Request box) are enabled. However, since boxcarring is deprecated in the product, it is recommended to disable boxcarring by clicking the Disable Legacy Boxcarring Mode check box.

  4. Click Next and then click Add New Datasource.
  5. Connect to the Company database that you defined above. For instructions, see Exposing an RDBMS as a Data Service.
  6. Click Save, and then click Next to go to the Queries screen.
  7. Click Add New Query to specify the query details:

    1. Enter addEmployeeQuery as the query ID.

    2. Enter the following SQL dialect:

      insert into Employees (EmployeeNumber, FirstName, LastName, Email,OfficeCode) values(:EmployeeNumber,:FirstName, :LastName,:Email,:OfficeCode)
  8. Click Generate Input Mapping and input mappings will be generated automatically:

  9. Click Save.

  10. Click Add New Query to Create another query for the Company datasource:

    1. Enter selectOfficebyID as the query ID.

    2. Enter the following SQL dialect:

      select OfficeCode,AddressLine1,AddressLine2,City,State,Country,Phone from Offices where OfficeCode=:OfficeCode
  11. Generate input and output mappings:

    1. Click Generate Input Mapping and input mappings will be generated automatically for the office code field.

    2. Click Generate Response to automatically generate the output mappings for the fields in the Offices table.

  12. Click Save.

  13. Click Next and then click Add New Operation to create an operation for the addEmployeeQuery query as shown below.

  14. Click Save.

  15. Click Add New Operation to create another operation to invoke the selectOfficebyID query:

  16. Click Save and then click Finish.

Invoking the data service 

The Deployed Services  window allows you to manage data services. You can try the data service you created by using the TryIt tool in this screen, which is in your product by default.  

  1. In the Deployed Services screen, click Try this Service to open the data service from the TryIt tool. You will find that there is an additional request box operation.
  2. Select the request box operation and enter the following values:
    1. Enter values for the addEmployeeQuery query.
    2. Enter the same office code you used for the above query in the selectOfficebyID query.

      <p:request_box xmlns:p="http://ws.wso2.org/dataservice">
            <!--Exactly 1 occurrence-->
            <addEmployeeQueryOp xmlns="http://ws.wso2.org/dataservice">
               <!--Exactly 1 occurrence-->
               <xs:EmployeeNumber xmlns:xs="http://ws.wso2.org/dataservice">1002</xs:EmployeeNumber>
               <!--Exactly 1 occurrence-->
               <xs:FirstName xmlns:xs="http://ws.wso2.org/dataservice">Peter</xs:FirstName>
               <!--Exactly 1 occurrence-->
               <xs:LastName xmlns:xs="http://ws.wso2.org/dataservice">Parker</xs:LastName>
               <!--Exactly 1 occurrence-->
               <xs:Email xmlns:xs="http://ws.wso2.org/dataservice">peter@wso2.com</xs:Email>
               <!--Exactly 1 occurrence-->
               <xs:OfficeCode xmlns:xs="http://ws.wso2.org/dataservice">2</xs:OfficeCode>
            </addEmployeeQueryOp>
            <!--Exactly 1 occurrence-->
            <selectOfficebyIDOP xmlns="http://ws.wso2.org/dataservice">
               <!--Exactly 1 occurrence-->
               <xs:OfficeCode xmlns:xs="http://ws.wso2.org/dataservice">2</xs:OfficeCode>
            </selectOfficebyIDOP>
      </p:request_box>
  3. Execute the request box operation.
    1. First, the addEmployeeQueryOp operation will be executed. The data will be successfully added to the database.

    2. Secondly, the selectOfficebyIDOp operation will be executed and the details of the office attached to the employee will be returned in the result as shown below.

      <Entries xmlns="http://ws.wso2.org/dataservice">
            <Entry>
               <OfficeCode>2</OfficeCode>
               <AddressLine1>72</AddressLine1>
               <AddressLine2>Rose Street</AddressLine2>
               <City>Pasadena</City>
               <State>California</State>
               <Country>United States</Country>
               <Phone>+152346343</Phone>
            </Entry>
      </Entries>
  • No labels