terça-feira, 10 de abril de 2012

Fetching REST Data Sources with DataFX

DataFX is a project that helps you to show data in a JavaFX application from different sources of data in a few formats. You may read local data using the FileSource and this datasource may contain data in XML or CSV formats. I demonstrated how to use it in a previous post, today I want to quickly show you show to fetch data using the new RestRequestBuilder class.

RestRequestBuilder is very easy to use and with a few lines of code you will be able to fetch a JavaFX TableView from a remote XML, JSON or CSV source. As the name says, this class uses the builder way to create REST requests, which means that in a simple line of code you will have your request ready to fetch data in a TableView. Then you will simply have to inform to your datasource(as datasource you can understand a XMLDataSource or CSVDatasource).

Well, this post is very abstract so far, is time to use what we talked about in a program. To do that I want to present you a WEB Service I created a few years ago that allows to access information of our Brazilian Election Candidates using REST. The advantages of it is a centralized, "machine ready" and aggregated base of data, so will have one place to access using programatic ways and it will allow you to create Mashups and other applications (we need to be careful with politics in Brazil, we have a lot of corrupt politcs here :-(). The API is very simple to use and it was published in ProgrammableWeb, which is a site that has as objective put together all APIs(SOAP, REST or Javascript) and Mashups made with these APIs.

The main method of the API is the search. With three query parameters we can query all the base of data and bring the data in XML or JSON format. For example, the URI:

Will bring all the candidates that has "ANT" in the name. The default format of the data offered by the API is XML. Additionally you can inform the page you are reading by using the parameter "pagina", but it's out of scope of this post. We will consume this source of data in JavaFX.

Now we know the target service to consume, we need to know how DataFX will help us to show these data. First, we can use RestRequestBuilder as follow:

NetworkSource ns = new RestRequestBuilder(
    .queryParam("nome", "ANT").build();
As the data is served in XML format, we need to chose which columns we will show in the TableView(I chose "nome", "nomeUrna", "estadoCivil", "resultadoEleicao", and "sexo") and the root element that contain the content to this columns(in our case it is "candidato"). The following code shows how can we retrieve this information.
XMLDataSource candidatosDataSource = new XMLDataSource(
    ns, "candidato", "nome", "nomeUrna",  "estadoCivil",
    "resultadoEleicao", "sexo");
By last we need to fill our TableView. It's pretty straightforward and described in my previous post. To finish here is our entire code and an image of the resulting JavaFX app.

package view;

import javafx.application.Application;
import javafx.scene.SceneBuilder;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

import org.javafxdata.datasources.io.NetworkSource;
import org.javafxdata.datasources.io.RestRequestBuilder;
import org.javafxdata.datasources.protocol.XMLDataSource;

 * @author jesuino
public class Main extends Application {

 public void start(Stage stage) throws Exception {
  stage.setTitle("DataFX Test");
  // Just loading the URL and creating a Networkdatasource for it
  NetworkSource ns = new RestRequestBuilder(
    .queryParam("nome", "ANT").build();

  XMLDataSource candidatosDataSource = new XMLDataSource(
    ns, "candidato", "nome", "nomeUrna", "estadoCivil",
    "resultadoEleicao", "sexo");
  TableView candidatos = new TableView();


 public static void main(String[] args) {

AS you can see it's very easy to read a remote REST Datasource with DataFX and it will get better since it's an open source project. Of course you can use it in a more complex application, it's a simple demonstration.

Um comentário:

  1. Muito bom! Só falta você colocar em português p/ galera que tem um pouco de dificuldade com o idioma...