How to access data service nested query results using a client stub?

Applies to : WSAS 2.3(and bellow), DS-1.0.1(and bellow)

Background

I will be using the NestQuerySample.dbs created in 'How to use Data Service nested query feature?' for this example.

Step 1 : Generate Client Stub

Click 'Generate Client' link on NestedQuerySample details page. (See Image bellow). A jar file containing Java client stub will be created & you will be prompted to save it.

 

Step 2 : Client Project

Create a project in your IDE. Add the jar file created in previous step as library to your project. Add all jar files in $WSAS_HOME/lib to your project.

Step 3 : Client code

Create a class called 'Client1' inside the package 'org.abc.dataservices'.Content that should go inside Client1.java is as follows.

package org.abc.dataservices;

import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import org.wso2.ws.dataservice.Employee;
import org.wso2.ws.dataservice.EmployeesE;
import org.wso2.ws.dataservice.GetEmployees;
import org.wso2.wsas.client.NestedQuerySampleStub;
import com.abc.salary.Salary;
public class Client1 {
  public static void main(String[] args) {
	try {
		NestedQuerySampleStub stub = 
			new NestedQuerySampleStub("http://localhost:9762/services/NestedQuerySample");
		GetEmployees getEmployees = new GetEmployees();
		EmployeesE result = stub.getEmployees(getEmployees);
		if(result != null){
			Employee[] employee = result.getEmployees().getEmployee();
			for (int a = 0; a < employee.length; a++) {
				//accessing parent 
				Employee emp = employee[a];
				System.out.println(emp.getFirstName() + " : "+emp.getEmail());
				//accessing nested salary
				if(emp.getSalaries() != null){
					Salary[] salaries = emp.getSalaries().getSalary();
					if(salaries != null){
						for (int b = 0; b < salaries.length; b++) {
							Salary salary = salaries[b];
							System.out.println("--> salary = "+salary.getAmount());
						}					
					}
				}
			}
		}else{
			System.out.println("No employee records found!!.");
		}
	} catch (AxisFault e) {
		e.printStackTrace();
	} catch (RemoteException e) {
		e.printStackTrace();
	}
  }
}

Step 4 : Testing the client

Compile & run 'Client1' using your IDE (you might want to write a build file for this later on).
A result (based on sample data populated - Check Step 1 here.) similar to following will be printed to your console.

Step 5 : Analyzing Client1 code

Structure of results returned from NestedQuerySample service is as follows.

You can observe a similar pattern in the client code that reads the response received by the stub.

 

library project main code
Learn Cloud
Learn
Cloud

The WSO2 Application Server is a reliable application server that can host your enterprise web applications. The WSO2 Application Server as a Service is offered in StratosLive, the WSO2 Platform as a Service. This article explains how a simple web application can be developed and deployed from Carbon Studio to the WSO2 Application Server...

Latest Webinar
Different groups within an organization need to monitor different Key Performance Indicators (KPIs) - An operations team will be interested in the response times of business services and loads of each service,..
Thursday, February 9th 2012, 09.00 AM (PST)

Thursday, February 9th 2012, 10.00 AM (GMT)