Bringing Together Java Ext.Direct and Ext JS Models with DTO2ExtJS

We are close to the beta release of the DTO2ExtJS code generator that automatically converts a Java DTO into Ext JS model. This will be of great help to fellow enterprise developers who use Ext.Direct to remote from Ext JS applications to Java classes in the servlet container. We use DirectJNgine. When Sencha releases an Ext JS implementation of AMF, models generated by our DTO2ExtJS will be used to remote to BlazeDS.

Let’s say you’ve created a Java DTO that looks like this:

package clear.dto;
import com.farata.dto2extjs.annotations.*;
@JSClass
public class UserDTO {
   public String id;
   @JSIgnore      
   public Double salary;
   private Date dob;
   public Date getDob() {
      return dob;
   }
   public void setDob(Date value) {
      dob = value;
   }
   @JSOneToMany(
      foreignKey="userId",
      storeConfig="{"+          
         "paramOrder:['0'],"+   
         "api: {"+
            "create  : Clear.action.TicketAction.getTickets_insertItems,"+
            "read    : Clear.action.TicketAction.getTickets,"+
            "update  : Clear.action.TicketAction.getTickets_updateItems,"+
            "destroy : Clear.action.TicketAction.getTickets_deleteItems"+
         "}"+
      "}")
   public List<TicketDTO> tickets;
}

DTO2ExtJS will generate the corresponding Ext JS code:


// Generated by DTO2EXTJS from AM.model.clear.dto.UserDTO.java on 2012-02-16T20:45:29-05:00
Ext.define('AM.model.clear.dto.generated._UserDTO', {
   extend: 'Ext.data.Model',
   requires: [
      'Ext.data.Types',
      'AM.model.clear.Ticket'
   ],
   fields: [
      { name: 'id', type: Ext.data.Types.STRING, useNull: true },
      { name: 'dob', type: Ext.data.Types.DATE, useNull: true }
   ],
   hasMany: [
      {
         model: 'AM.model.clear.Ticket',
         name: 'getTickets', 
         primaryKey:'id',
         foreignKey:'userId',
         autoLoad: 'true',
         storeType:'AM.store.clear.TicketStore',
         storeConfig:'{
            paramOrder:['0'],
            api: {
               create  : Clear.action.TicketAction.getTickets_insertItems,
               read    : Clear.action.TicketAction.getTickets,
               update  : Clear.action.TicketAction.getTickets_updateItems,
               destroy : Clear.action.TicketAction.getTickets_deleteItems
            }
         }'
     }
   ]
}

and one more class, extending the previous one:


Ext.define('AM.model.clear.dto.UserDTO, {
        extend: 'AM.model.clear.dto.generated._UserDTO'
}); 

As you can see, we keep using the generation gap pattern (the parent class gets regenerates, but the subclass doesn’t) to safely modify the extension and let us keep regenerating the bottom file of the pair, which we do every time the Java file is modified. If your Java class inherits from another one – make sure you annotate the parent as @JSClass and DTO2ExtJs generates models for both, or tell DTO2ExtJs to ignore the parent class entirely, and it’ll obey.

You will be able to fully control the output location and naming of the generated models via APT parameters.

DTO2ExtJS is an annotation processor for Java Annotation Processor Tool and you can use it from the command line and as Eclipse plugin. In the latter case you place com.farata.dto2extjs.asap_4.6.0.jar in
eclipse/plugins folder and com.farata.dto2extjs.annotations.jar in the WebContent/lib of your Eclipse Dynamic Web Project. You can find the current version of the jars at http://www.cleartoolkit.com/downloads/plugins/extjs/dto2extjs

The work-in-progress JavaDoc is available at
http://help.faratasystems.com/en_US/cleartoolkit/reference/java/extjs/

We’ll demo the automatic Ext JS code generation at the end of our 2 day Ext JS workshop in June in New York CIty. Enter Victor as a promo code to get $100 off the price.

Victor

3 thoughts on “Bringing Together Java Ext.Direct and Ext JS Models with DTO2ExtJS

  1. Hi Viktor,
    I have a couple of questions/suggestions if I may.

    Does your clear framework targets Java 5 or 6 platform? In Java 6 there is even better way of generation your code (apt is embedded in javac) and you can just put a jar file with a framework in a class path and code will be generated during normal compilation process – no extra step involved.

    I understand the purpose of storeConfig but that string concatenation doesn’t look natural. Do you think it would be better to have separate annotations or a separate attributes?

    Thank you

  2. Mmmm….

    I didn’t used Direct, I preferred to stay with traditional JSON stores backed up with JAX-RS and JPA.

    Only problem I got was how to persist a complex graph which somebody in the forums resolved overriding Json writer’s getRecordData() & writeRecords.

    Which one is your data access philosophy? I mean:

    Server ExtJS: JSON?
    Server database: Plain SQL, Hibernate, JPA?

  3. Currently we are using JSON and we are planning to use AMF as well. On the server it depends on the customer’s preference: Hibernate, and MyBatis are most popular choices. In fact, if you use our upcoming CDB plugin with Hibernate we generate Java (based on the JPQL annotated interfaces) as well as the client ExtJS code. Thank you for following our blog – this week I am going to publish the reference application that persists a hierarchy (the sample has only two levels but we support unlimited, of course). Yes, the writer is important, but in a different way: we pass to server only the deltas, aka a ChangeObject for each modified/deleted/new record, hence the writer converts a Model into a ChangeObject. There is more to it for automatic JEE transaction management… To be blogged soon.

Comments are closed.