Johan Vos started to work with Java in 1995. He worked on the Java Linux port with the Blackdown team. He has been doing Java consulting and development for a number of customers in completely different areas. Over the years, he has been active in a number of Java-based community projects, e.g. OSGi, the Glassfish project and JFXtras.org. Johan is a DZone MVB and is not an employee of DZone and has posted 22 posts at DZone. You can read more from them at their website. View Full User Profile

Writeback Support in DataFX

09.08.2013
| 2943 views |
  • submit to reddit

Over the last months, we have been working very hard on DataFX 2.0. We will talk about DataFX during JavaOne 2013, but I didn't want to wait to briefly mention one of the new cool features of DataFX 2.0, which is the write-back support.

With DataFX 1, you can easily obtain (remote) data and populate JavaFX controls with that data. Data can come from a variety of sources, in different format. In DataFX 2.0, it is also possible to modify data locally, and request that this data and then sent back to the original source. This is achieved by using a WriteBackHandler.

Both ObjectDataProvider and ListDataProvider now implement WriteBackProvider. As a consequence, when the data is changed locally after it has been retrieved (from a database, REST server, file,...) a callback will be executed in order to change the data on the source of origin. The developer is still responsible for doing this, by registering a callback handler with the DataProvider implementation.

You can checkout the code at https://bitbucket.org/datafx/datafx or you can download DataFX from maven central (groupId: org.javafxdata, artifactId: datafx-core and datafx-samples).

The code snippet below is taken form the JDBCSample.java file in datafx-samples, and it shows how to enable write-back support for a simple case where the data is stored in a relational database

DataReader dr = new JdbcSource(dbURL, converter, "PERSON", "firstName", "lastName", "country");
ListDataProvider lodp = new ListDataProvider(dr);
ObservableList myList = FXCollections.observableArrayList();
lodp.setResultObservableList(myList);
lodp.setWriteBackHandler(new WriteBackHandler() {
@Override
  public WritableDataReader createDataSource(Person me) {
  String statement = "UPDATE PERSON SET lastName=\'" + me.getLastName() + "\' WHERE firstName=\'" + me.getFirstName() + "\'";
  JdbcSource dr = new JdbcSource(dbURL, statement, null);
  dr.setUpdateQuery(true);
  System.out.println("Writeback called with statement "+statement);
  return dr;
  }
});
lodp.retrieve();

DataFX will keep track of changes to properties in the retrieved data, and it will call the WriteBackHandler in order to make that change persistent.

speakingWe will provide more samples and tutorials later, and you are very welcome to join us at JavaOne (CON3202, Wednesday Sep 25, 08:30 am, Hilton - Plaza B)

Published at DZone with permission of Johan Vos, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)