Saturday, January 14, 2012

Spring Roo 1.1 Cookbook review by Shekhar Gulati


Many thanks to Shekhar for reviewing Spring Roo 1.1 Cookbook :)

You can visit Shekhar's blog and read the complete review here : http://whyjava.wordpress.com/2011/12/21/spring-roo-1-1-cookbook-review/

View and add dynamic finder methods

A dynamic finder method is a finder method for which you don't need to write a JPA query.


Download ch03_persistent_entities.roo script from the source code that accompanies Spring Roo 1.1 Cookbook: http://code.google.com/p/spring-roo-cookbook/downloads/list

The script sets up Hibernate as a persistence provider and creates a Flight entity, which
has FlightKey as its composite primary key class. Additionally, the script adds fields to the
Flight and FlightKey classes. If you are using a different database than MySQL or your
connection settings are different than what is specified in the script, then modify the script
accordingly.



To view dynamic finder methods, follow the given steps:


1. Start Roo shell and set the focus of the subsequent commands on the Flight entity using the focus
command:
roo> focus --class ~.domain.Flight


2. Execute the finder list command to view the list of candidate dynamic finder
methods for the Flight entity, as shown here:
~.domain.Flight roo> finder list
.....
findFlightsByCreatedDateBetween(Date minCreatedDate, Date
maxCreatedDate)
findFlightsByCreatedDateGreaterThan(Date createdDate)
.....



To add dynamic finder methods, follow the given steps:


1. Add the findFlightsByDestinationLikeAndOriginLike dynamic finder
method to the Flight entity using the finder add command:


.. roo> finder add findFlightsByDestinationLikeAndOriginLike
Updated SRC_MAIN_JAVA\sample\roo\flightapp\domain\Flight.java
Created SRC_MAIN_JAVA\sample\roo\flightapp\domain\Flight_Roo_Finder.aj


The following code shows the auto-generated implementation of the
findFlightsByDestinationLikeAndOriginLike finder method in the Flight_Roo_Finder.aj file:

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
...
public static TypedQuery
Flight.findFlightsByDestinationLikeAndOriginLike(String destination, String origin) {
  if (destination == null || destination.length() == 0)
     throw new IllegalArgumentException("The destination argument is required");
  ...
  if (origin == null || origin.length() == 0)
     throw new IllegalArgumentException("The origin argument is required");
  ...
  EntityManager em = Flight.entityManager();
  TypedQuery q = em.createQuery("SELECT Flight FROM Flight AS flight WHERE            
                    LOWER(flight.destination) LIKE
                    LOWER(:destination) AND LOWER(flight.origin) LIKE
                    LOWER(:origin)", Flight.class);
   q.setParameter("destination", destination);
   q.setParameter("origin", origin);
   return q;
}




Monday, December 12, 2011

Adding JSON support to domain objects and controllers

This post shows how you can add JSON support to your applications using Spring Roo. The information in this post has been taken from Spring Roo 1.1 Cookbook.

Step 1: Download Roo scripts and sample code from the following location:http://code.google.com/p/spring-roo-cookbook/downloads/list#columnprefs

Step 2: Execute the ch04_web-app.roo script that creates the flight-app Roo project, sets up
Hibernate as the persistence provider, configures MySQL as the database for the application,
creates the Flight and FlightDescription JPA entities, and defines a many-to-one
relationship between the Flight and FlightDescription entities. If you are using a
different database than MySQL or your connection settings are different than what is specified
in the script, then modify the script accordingly.

Step 3: Execute the controller all command to create controllers and JSPX views corresponding
to JPA entities in the flight-app project, as shown here:
.. roo> controller all --package ~.web
Execute the perform eclipse command to update the project's classpath settings, as
shown here:
.. roo> perform eclipse
Now, import the flight-app project into your Eclipse IDE.

Step 4: To add the json support execute the json add command against the Flight JPA entity:
~.domain.Flight roo> json add --class ~.domain.Flight
Updated SRC_MAIN_JAVA\...\domain\Flight.java
Created SRC_MAIN_JAVA\...\domain\Flight_Roo_Json.aj
Created SRC_MAIN_JAVA\...\web\FlightController_Roo_Controller_Json.aj

Executing the json add command creates
a *_Roo_Json.aj AspectJ ITD, which defines methods for converting objects of the class to
JSON documents and vice versa, as shown here:

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

privileged aspect Flight_Roo_Json {
  public String Flight.toJson() {
    return new JSONSerializer().exclude("*.class").serialize(this);
  }
  public static Flight Flight.fromJsonToFlight(String json) {
    return new JSONDeserializer().use(null, Flight.class).deserialize(json);
  }
  public static String Flight.toJsonArray(
    Collection collection) {
    ...
  }
  public static Collection
    Flight.fromJsonArrayToFlights(String json) {
     ...
  }
}

The following code shows the FlightController_Roo_Controller_Json.aj ITD, which
was generated:
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ResponseBody;

privileged aspect FlightController_Roo_Controller_Json {

   @RequestMapping(value = "/{flightId}", method = RequestMethod.GET, 
     headers =  "Accept=application/json")
  @ResponseBody
  public Object FlightController.showJson(@PathVariable("flightId") Long flightId) {
    Flight flight = Flight.findFlight(flightId);
    if (flight == null) {
         HttpHeaders headers= new HttpHeaders();
         headers.add("Content-Type", "application/text");
         return new ResponseEntity(headers, HttpStatus.NOT_FOUND);
    }
    return flight.toJson();
  }
  ...
}

Monday, November 28, 2011

Spring Roo 1.1 Cookbook review by Cengiz Öner

I just came across review of Spring Roo 1.1 Cookbook by . Many thanks to Cengiz for writing the review :)

You can visit Cengiz's blog and read the complete review here : http://gwtsts.blogspot.com/2011/11/review-spring-roo-11-cookbook-by-ashish.html

Monday, October 31, 2011

Tuesday, October 18, 2011

Spring Roo presentation

Last week I gave a presentation on Spring Roo in Silicon India conference held in Hyderabad. I tried my best to give a complete example that makes use of Spring Roo features to develop a Flight Booking application. Link to PDF: http://www.siliconindia.com/events/siliconindia_events/Java_Hyd_Conf/Ashish_Spring_Roo.pdf

Wednesday, October 5, 2011

Quick introduction to AspectJ ITD

In this post, I'll show a few examples of AspectJ ITDs that are generated by Spring Roo and the declarations contained in those ITDs. You'll find a more detailed discussion in Chapter 1 (which is available for download) of Spring Roo 1.1 Cookbook.

Example 1: FlightService_Roo_ToString.aj
package sample.roo.flightapp.service;

privileged aspect FlightService_Roo_ToString {
   
    public String FlightService.toString() {
        StringBuilder sb = new StringBuilder();
       sb.append("Origin: ")
         .append(getOrigin());
        return sb.toString();
    }
} 

The following figure shows what the above declaration implies:


Example 2FlightService_Roo_Serializable.aj

package sample.roo.flightapp.service;

import java.io.Serializable;

privileged aspect FlightService_Roo_Serializable {
   
  declare parents: FlightService implements Serializable;
   
  private static final long FlightService.serialVersionUID
      = 5059552858884348572L
}


The following figure shows what the above declaration implies: