The Source for Java Technology Collaboration
User: Password:



Srikanth Shenoy

Srikanth Shenoy's Blog

Copying data from Struts ActionForm to ValueObject: Easing the pain

Posted by srikanth on March 10, 2005 at 01:04 PM | Comments (6)

Enterprise applications are all about data manipulation. Data flows through the system from one tier to another.

Consider a typical J2EE application built using Struts. The user fills out the HTML Form. The data makes its way into a Action Form. The they are copied into Value Objects using Value Object Assemblers (or DTO Assemblers). Then the data is copied into Entity EJBs (if used) and are persisted. The whole process is reversed when user retrieves data.

Traditionally value object assemblers are hand coded. More recently Commons BeanUtils has been used to simplify this tedious and errorprone task of copying data from one object to another. Remember BeanUtils.copyProperties()... However, BeanUtils have some important limitations.

BeanUtils can automatically copy (and convert) properties between objects, when the names of the properties are same between source and target objects. In large projects, this happens a lot as the web tier objects do not match persistence objects in both name and structure simply because they were developed by different groups.

BeanUtils package provides PropertyUtils class that can copy any property between source and target. However you have to explicitly invoke PropertyUtils.getProperty() and PropertyUtils.setProperty(). This works very well in many scenarios (like generic frameworks for example.), but doesn't work when you have to copy data between objects in the absence of mapping metadata. This would be almost like coding target.setXXX(source.getXXX()).

BeanUtils (actually ConvertUtils class) can only convert from String to primitives (such as int, boolean) and basic java.lang objects (such as Integer, String) and vice versa. and nothing else. If you want to convert from java.sql.Timestamp to long (say) or foo.bar.Blah to java.util.Date, you simply cannot. The only extensibility in BeanUtils is to write custom converter from String to foo.bar.Blah and vice versa.

If you are facing a scanarios like one of the following a new framework called OTOM (stands for Object To Object Maping - pronounced as Autumn :-D ) can be a big help.

  • Source and target objects have different names and structures
  • You have to convert data from and to arbitrary data types
  • You want to map data conditionally

Like all of us, I have faced this problem. Being the lazy person that I am, I didn't do anything about it until recently. The truth is that such a need at one of my client scratched my itch.

And so, last week, I rolled out OTOM 0.5. As is evident, it is not complete yet but has most of the features for helping in the above mentioned scenarios. So, how does OTOM work? It is pretty simple actually.

OTOM has a Swing based GUI that allows you to graphically map properties from a source class to target class. Variety of mappings are allowed - Direct Mapping, Type Conversion Mapping, Collection Mapping (to be added) and even Conditional Mapping. The GUI generates a XML mapping file that stores the conversion metadata. The GUI looks as follows:

The mapping metadata is stored in xml format. OTOM uses the concept of repositories to store the metadata. A repository consists of multiple Class Mappings. A Class Mapping stores the mapping information from a source class to a target class. Each ClassMapping has a collection of PropertyMapping correpsonding to mapping of JavaBean properties in the source class to those in the target class. The mapping can be conditional too. All thise can be done through the GUI itself. Writing the GUI as an Eclipse Plugin is still in my radar, but given that my immediate priority was to provide IDE independent GUI, I chose Swing.

The source code for the mapping can then be generated using an Ant task provided. The actual generation is done via Velocity templates. One of the nice features is that the generated code has absolutely no dependency on the OTOM framework. The generated code also does not use reflection as the mapping metadata helps me avoid it.

So, the next time you have to copy data from one object to another, think if you can use OTOM. Typical usages for OTOM include data mapping from Struts ActionForm to ValueObject (or POJO), JAXB generated objects to POJOs and so on. If you have suggestions for improvements, please post it in the project forums.

Spring break might be just a week away, but it still feels like OTOM to me ;-D. But then hey, I am from Austin, TX and there is no such weather as winter down here.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • This is really cool. I liked the GUI - It has got some touch of SWT to it. Excellent job.

    Posted by: tchangu on March 11, 2005 at 01:50 PM

  • It has some SWT feel because, I used the images for the toolbar and the tree from Eclipse ;-D

    Posted by: srikanth on March 11, 2005 at 04:25 PM

  • A question: OTOM is designed to be used with Struts? or it is a generic independent tool ?

    best regards,

    Felipe Gaúcho

    Posted by: felipegaucho on March 14, 2005 at 04:54 AM


  • Very nice indeed.


    Another feature that I would like in a tool like this would be to be able to create an adapter implementing a pre-defined interface.


    You would do the mapping between the interface methods and the bean to be adapted and then generate an adapter implementation.


    Posted by: bob_boothby on March 15, 2005 at 06:41 AM

  • Felip,

    OTOM is not a Struts specific tool. In fact it is not even a J2EE specific tool. It can be used to map any Java Object to any other.

    It is most useful when data from large objects have to be copied into other Java Objects with a different structure altogether.
    One of the seemingly everyday place where this happens is copying data from Struts ActionForm to Value Object. Hence the example of Struts.

    Posted by: srikanth on March 15, 2005 at 08:41 AM

  • Bob,

    Nice idea. I agree.
    Adapter Generation seems like an area where this tool can be used very easily. I

    Posted by: srikanth on March 15, 2005 at 08:53 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds