Model Mapping with Dozer

Overview

When working with GXT Stores and Binders, your model objects must implement the ModelData interface. You can either implement the interfaces directly, or use one of the default model implementations.

There may be scenarios where you have existing java objects that need to be converted to ModelData instances. For example:

This tutorial will walk through the steps needed to convert any java object to a ModelData instance. This will be done using Dozer. "Dozer is a Java Bean to Java Bean mapper that recursively copies data from one object to another."

Dozer is used on the server. Typically, you would convert your java objects to model instances before sending them to the client with GWT RPC. And then convert the model instances back to the original objects when coming from the client

Setup

First, you will need to download Dozer and its required libraries. Dozer can be downloaded at http://dozer.sourceforge.net/. The following libraries are required with Dozer:

After downloading dozer, and the required libraries, add the jars to your projects classpath.

Source Objects (POJO / DTO)

In this tutorial, there will be 2 dtos that we are going to convert to ModelData instances. Here are the 2 dtos:

There are no restrictions on your data transfer objects. They can extend and implement any class and do not have to have getters and setters. Next, we will create our ModelData instances.

GXT ModelData

We will now create the GXT models by subclassing BaseModelData and adding our domain specific properties. In this example, there is a 1-1 mapping between classes and fields between our dtos and model instances. This is not a requirement.

Mapping

Now all that is needed is to create a "mapping" file that defines the relationship between or dtos and model instances. Essentially, you define each class and what data should be mapped. You have complete control of what data is included and how it gets mapped.

You may be asking why a mapping is required. There are several reasons. First, mappings allow you to configure the relationship when source and destination objects are not alike, including property names and data types. Also, with mappings, you have complete control on what data is mapped. Your object graph might be complex, with many parents and nested children which you do not want sent over GWT RPC.

Here is the mapping file. This is very basic and is meant to illustrate a simple scenario. Consult the Dozer documentation for more information.

The mapping xml file needs to be placed in the project classpath. The recommended location is the same package as your model classes.

Usage

We have defined our DTOs and ModelData classes. Let's see Dozer in action:

Summary

Dozer is a great tool for mapping java objects. Within the GXT context, Dozer is great for converting objects to be used with GWT RPC and the GXT data API. This tutorial provides a simple demonstration on using the Dozer API and barely touches the surface on the features Dozer provides. If you are interested in using Dozer, take a look at the Dozer documentation for more details on the library.