Invoking Multiple Operations via Request Box
In this tutorial, you define a data service that can invoke request box operations. The request box feature allows you to invoke multiple operations (consecutively) to a datasource using a single operation.
Setting up a datasource
Follow the steps given below to set up a MySQL database for this tutorial.
Download the product installer from here, and run the installer.
Install the MySQL server.
Download the JDBC driver for MySQL and copy it to your
<EI_HOME>/libdirectory.Create a database named Company.
CREATE DATABASE Company;Create the Employees table:
USE Company; 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`));
Define a data service to invoke request box operations
Let's create a data service using the Create Data Service wizard:
Start the WSO2 ESB profile.
Open the ESB profile's Management Console using https://localhost:9443/carbon, and log in using
adminas the username and the password.Click Create under Data Service.
Add a name for the data service.
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. Select this new check box (Disable Legacy Boxcarring Mode) as well.Click Next and then click Add New Datasource.
Connect to the Company database that you defined above.
Click Save and then click Next to go to the Queries screen.
Click Add New Query to specify the query details:
Enter addEmployeeQuery as the query ID.
Enter the following SQL dialect:
insert into Employees (EmployeeNumber, FirstName, LastName, Email,OfficeCode) values(:EmployeeNumber,:FirstName,:LastName,:Email,:OfficeCode)Click Generate Input Mapping and input mappings are generated automatically.
Click Save.
Click Add New Query to Create another query for the Company datasource:
Enter selectEmployeebyIDQuery as the query ID.
Enter the following SQL dialect:
select EmployeeNumber, FirstName, LastName, Email, OfficeCode from Employees where EmployeeNumber=:EmployeeNumberGenerate input and output mappings:
Click Generate Input Mapping and input mappings are generated automatically for the employee number.
Click Generate Response to automatically generate the output mappings for the fields in the Employees table.
Click Save.
Click Next and then click Add New Operation.
Create two operations to add and select employees.
Enter the following details to create the addEmployeeOp operation that adds new employees to the database.
Save the operation.
Click Add New Operation and enter the details as shown below to create the selectEmployeeOp operation that gets the details of an employee from the database.
Click Save.
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.
In the Deployed Services screen, click Try this Service to open the data service you just created from the TryIt tool.
You will find that there is an additional request_box operation.Select the request_box operation and enter the following values:
Enter values for the addEmployeeQuery query.
Enter the same employee number you used for the above query in the selectEmployeebyID query:
Copy the following, paste it in the TryIt tool, and click Send to try it out:
<body> <p:request_box xmlns:p="http://ws.wso2.org/dataservice"> <!--Exactly 1 occurrence--> <addEmployeeOp xmlns="http://ws.wso2.org/dataservice"> <!--Exactly 1 occurrence--> <xs:EmployeeNumber xmlns:xs="http://ws.wso2.org/dataservice">1003</xs:EmployeeNumber> <!--Exactly 1 occurrence--> <xs:FirstName xmlns:xs="http://ws.wso2.org/dataservice">Chris</xs:FirstName> <!--Exactly 1 occurrence--> <xs:LastName xmlns:xs="http://ws.wso2.org/dataservice">Sam</xs:LastName> <!--Exactly 1 occurrence--> <xs:Email xmlns:xs="http://ws.wso2.org/dataservice">chris@sam.com</xs:Email> <!--Exactly 1 occurrence--> <xs:OfficeCode xmlns:xs="http://ws.wso2.org/dataservice">1</xs:OfficeCode> </addEmployeeOp> <!--Exactly 1 occurrence--> <selectEmployeeOp xmlns="http://ws.wso2.org/dataservice"> <!--Exactly 1 occurrence--> <xs:EmployeeNumber xmlns:xs="http://ws.wso2.org/dataservice">1003</xs:EmployeeNumber> </selectEmployeeOp> </p:request_box> </body>You get the following response:
<axis2ns26:DATA_SERVICE_REQUEST_BOX_RESPONSE xmlns:axis2ns26="http://ws.wso2.org/dataservice"> <Entries xmlns="http://ws.wso2.org/dataservice"> <Entry> <EmployeeNumber>1003</EmployeeNumber> <FirstName>Chris</FirstName> <LastName>Sam</LastName> <Email>chris@sam.com</Email> <OfficeCode>1</OfficeCode> </Entry> </Entries> </axis2ns26:DATA_SERVICE_REQUEST_BOX_RESPONSE>