Saturday, November 6, 2010

Automated DTO Generation with Maven and GAS3


Update: this tutorial is now updated, it now uses the recently new Flex Mojos GDS integration

In Recent years Flex has become the ideal client solution for Java server based projects. Flex complements Java in many ways and has many similarities to Java. These include, the syntax, the OOP approach and many other features of the two languages that are similar.

In this tutorial, I will demonstrate one aspect of the Flex Java integration by covering the earliest stage of the development process, which is the build and infrastructure creation. In future posts I will cover later stages of the development.

VO\DTO Generation
The VO generation process converts the existing server side entity classes to client side entity classes. At the end of the process each server side DTO\Entity class will have a client side corresponding class, enabling easy communication between the client and server.

Using Flexmojos and GAS3 to Generate AS3
GAS3 is one the modules of GraniteDS, GDS is a comprehensive Flex remoting framework which will not be covered in this post. GAS3 is responsible for the Java to AS3 VO (or DTO) generation, it reads the domain Java objects and generates the corresponding AS classes. Usually GAS3 is used in conjunction with other GraniteDS modules. However, as I'm about to demonstrate, GAS3 can also be used separately from GDS in conjunction with different remoting frameworks such as BlazeDS or even different integration layers like SOAP and REST services.

GAS3 is created on top of the Groovy templates. It can be customized and used to generate different derivatives of Vos.

MyCRM Sample Application
The sample mini application shown below, is a CRM app (MyCRM). responsible for managing customer information ,it contains three sub projects, as follows:
  • mycrm-core: The Java server, responsible for the server side business logic. In our sample it contains only a number of basic entities.
  • mycrm-parent: Maven's parent project, which serves as a parent POM.
  • mycrm-flex-domain: The Flex side of the domain objects, packaged as an SWC which may be used by any Flex application.
The entire code for the sample application may be downloaded from github

The GAS3 Flexmojos Generate Goal
In the pom.xml under mycrm-flex-domain, the following section is responsible for the GAS3 operation:

<plugin>
<groupId>org.sonatype.flexmojos</groupId>
<artifactId>flexmojos-maven-plugin</artifactId>
<version>3.6</version>
<executions>
    <execution>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <entityTemplate>
                <template>${project.basedir}/../mycrm-flex-domain/gsp/MyCRMEntityBaseTemplate.gsp</template>
                <template>${project.basedir}/../mycrm-flex-domain/gsp/MyCRMEntityTemplate.gsp</template>
            </entityTemplate>
            <includeJavaClasses>
                <includeClass>*net.flexonjava.examples.mycrm.domain*</includeClass>
            </includeJavaClasses>
            <outputDirectory>${project.basedir}/../mycrm-flex-domain/src/main/flex</outputDirectory>
            <baseOutputDirectory>${project.basedir}/../mycrm-flex-domain/src/main/flex-autogenerated</baseOutputDirectory>
        </configuration>
    </execution>
</executions>
</plugin>

Many of the tags above are overrides for the default configurations, you can use them in order to override GAS3 default settings. Lets take a closer look on some of these settings:
<entityTemplate>
<template>${project.basedir}/../mycrm-flex-domain/gsp/MyCRMEntityBaseTemplate.gsp</template>
<template>${project.basedir}/../mycrm-flex-domain/gsp/MyCRMEntityTemplate.gsp</template>
</entityTemplate>

GAS3 generates two types of VOs, the Base class and the class extends Base. Do not touch the Base class, this class is auto-generated every-time you compile, it’s best practice not to include it under your source control management repository. The child class is a different story, this class is simply a wrapper that you are welcome to modify. It can be used for different purposes, such as, validating setter values, annotating, in-line documentation and so on. If you were wondering, The class is also auto-generated like the Base class, Only if a copy of the class is not yet available in your code.

The baseoutputdir tells GAS3 to place the base classes in a place different then my src folder.

<baseOutputDirectory>${project.basedir}/../mycrm-flex-domain/src/main/flex-autogenerated</baseOutputDirectory>

GAS3 uses Groovy templates to convert Java to AS3 classes. GSP (Groovy Server Page) code is used in these templates. GSP is very similar to JSP or any other script language you’ve probably used in the past. By writing your own templates or modifying the existing ones you can modify the generated AS3 classes to suit your needs. You can insert your custom code remarks or apply your own logic on the conversion process. The default templates maybe found under GAS3 distribution (look for *.gsp file name extension). I made some modifications to the basic templates.

Once the build is ready and configured, run mvn clean install. If everything goes well, a new SWC will be created for the mycrm-flex-domain project. This SWC will be used by any Flex project that needs to talk with MyCRM server. The SWC maybe used in conjunction with BlazeDS, SOAP, REST or any other integration layer we would like to use.

7 comments:

velo said...

Flexmojos also integrates to it in maven way, not antrunner needed ;)

If you are interested...
https://docs.sonatype.org/display/FLEXMOJOS/Generator+Mojo


VELO

Lior Boord said...

thanks Velo, looks like this was recently added to Flexmojos, first time I see this. I might need to change my article to include that option

velo said...

Hrmmm, not that new, flexmojos uses GAS3 since 2.0 or something like that.

But it saw great improvements since then.

Lemme know if you need any help

Devaraj said...

Great Post, Thanks. GAS3 have not reached to the mass yet.

Lior Boord said...

I agree with this Devaraj, I think so far people saw it primarily something you use with GDS, hopefully more developers will now start to integrate it to none GDS project making it more popular.

Anonymous said...

I'm not complaining at all but it's "Flex complements Java..." "Complements" instead of "compliments."

Lior Boord said...

Thanks, changed that