Composition on Grails - Another Sample Application

This scenario was chosen to allow direct comparison to Vladimir Pavlov's excellent CE102 JEE5 tutorial scenario. Unfortunately, I don't think anyone besides SAP employees has access to the materials. I'll have to contact Vladimir and see if I can persuade him to publish the CE102 handouts so readers can compare the two.

The CE102 sample scenario involves creating a sales order application. In that tutorial, you spend quite a bit of time manually creating and configuring your database tables and EJBs. Developing the application is therefore much faster to do using Composition on Grails than it is using standard JEE5, but standard JEE would win in terms of raw performance and scalability. This is just a tradeoff; it's not to say that Composition on Grails is unusable, or that JEE5 development is impossibly slow and difficult.

To allow your data to persist in your database even after applications have been reloaded, install your preferred database driver to C:\workspace\grails-1.0\lib. Run generateDeployable.bat again, so that an updated grails.sda (including the new libraries) is generated. This newly generated grails.sda then needs to be deployed with NWDS before running any CoGs application.
Edit the DevelopmentDataSource.groovy file (located in C:\workspace\grails-1.0\cog-apps\SalesTables\grails-app\conf) with your database specific information and tell grails to update the database. If you're using MySQL, the file might look like this (include quotes):
boolean pooling = true
String dbCreate = "update" // one of 'create', 'create-drop', 'update'
String "jdbc:mysql://localhost/salestables_dev" //database created for SalesTables app
String driverClassName = "org.mysql.jdbc.Driver"
String username = "db user name"
String password = "db password"
  1. Go to the Grails folder C:\workspace\grails1.0.
  2. Run RunMeFirst.bat. This should open a command prompt in C:\workspace\grails1.0\cog-apps folder. Alternatively, you can use "External Tools" in Eclipse for running grails commands instead of using the command prompt.
    Don't close the command prompt as you have to use it for entering commands throughout the tutorial.
  3. Create a Composition on Grails application.
    grails create-app SalesTables
    
  4. Create a domain class. Each class will correspond to a table in your database.
    cd SalesTables
    grails create-domain-class Customer
    grails create-domain-class SalesOrder
    
  5. With the text editor of your choice, open the domain class Customer that you created. You'll find it in grails workspace\cog-apps\SalesTables\grails-app\domain. The constraint "nullable" is set to true to indicate that an attribute may be left blank when creating a new Customer in the table.
    class Customer {
    	static constraints = {
            name(nullable: true)
            street(nullable: true)
            zip(nullable: true)
            city(nullable: true)
        }
    
    	String name
    	String street
    	String zip
    	String city
    
    	static hasMany = [salesOrders : SalesOrder]
    
    
    	String toString(){
    		if(name!=null)
    			return name;
    		else
    			return "Customer: "+id;
    	  }
    }
    

    Since we're using grails, an ID is automatically created for every object.

  6. Now edit the SalesOrder class that you created.
    class SalesOrder {
    
    	static constraints = {
    		orderDate(nullable: true)
    		shipTo(nullable: true)
    	}
    	Date orderDate
    	String shipTo
    	Customer customer
    }
    
  7. Go back to command prompt and generate the webdynpro controllers and views.
    grails cog-generate-all Customer
    
    grails cog-generate-all SalesOrder
    
  8. Generate dev.html.
    grails cog-generate-html
    
  9. Click on dev.html (in workspace\cog-apps\SalesTables) to see your application.
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.