June 02, 2009

Trails Framework Example

« Python Plasmoid Example | Main | Vnstat - Netzwerk analysieren »

Recently I looked for an API which is possible to create automatically user interfaces from standard domain objects in order to edit and manipulate this objects. The Naked Objects Pattern describes this principle.

Trails is a domain driven framework which is based on the naked objects pattern. It is very easy to create a trails project with maven.

mvn archetype:create -DarchetypeGroupId=org.trailsframework 
 -DarchetypeArtifactId=trails-archetype -DarchetypeVersion=1.2.1 
 -DgroupId=org.developers.blog.examples -DartifactId=trails-example

The Trails Maven Archetype creates all necessary files how Spring application contexts, resource bundles, logging configuration, a HSQLDB based session factory. After that you have only to create the domain objects. I created a rudimentary Customer and Account class.

package org.developers.blog.examples;

import java.util.List;
import org.hibernate.validator.NotNull;
import org.trails.descriptor.annotation.PropertyDescriptor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Customer {

    private Long id;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @PropertyDescriptor(index = 0)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    private String firstName;
    private String secondName;
    private String street;
    private String city;
    private String cityCode;


    @NotNull
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @NotNull
    public String getCityCode() {
        return cityCode;
    }

    public void setCityCode(String cityCode) {
        this.cityCode = cityCode;
    }

    @NotNull
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
    @NotNull
    public String getSecondName() {
        return secondName;
    }

    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }
    
    @NotNull
    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    private List accounts;

    @OneToMany
    public List getAccounts() {
        return accounts;
    }

    public void setAccounts(List accounts) {
        this.accounts = accounts;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Customer that = (Customer) o;

        return getId() != null ? getId().equals(that.getId()) : that.getId() == null;
    }

    public int hashCode() {
        return (getId() != null ? getId().hashCode() : 0);
    }

    public String toString() {
        return this.getFirstName() + " " + getSecondName();
    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.developers.blog.examples;


import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.validator.NotNull;
import org.trails.descriptor.annotation.PropertyDescriptor;

/**
 *
 * @author rafsob
 */

@Entity
public class Account {
    private Long id;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @PropertyDescriptor(index = 0)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    private String service;

    @NotNull
    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @NotNull
    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }
    private Double price;
}

Then you can test the project with the following jetty:run command at http://localhost:8080 .

mvn jetty:run

Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 10:56 PM in Uncategorized

 

[Trackback URL for this entry]

Comment: Kalle Korhonen at Mi, 13 Jan 7:21 AM

Just to let you you know that the next version of Trails is called Tynamo - come visit us at http://tynamo.org

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
 
test