|
SYS-CON Magazines
|
Top Linux Links You Must Click On
Feature Java Kicks Ruby on Rails in the Butt
This article demonstrates that Java is more productive than Ruby
By: Javier Paniza
May. 16, 2009 10:00 PM
This article tries to demonstrate that Java can be more productive than Ruby. We are going to develop the same application of the article Rolling with Ruby on Rails Revisited (part 1 [1] and part 2 [2]) but using POJO [3]s annotated with JPA [4] and a Model Driven Framework, OpenXava [5] in this case. The result is that with less code, and less time you obtain a more powerful application. Ruby and rails: The regressive frameworkRuby on rails [6] is so elegant, so easy, so productive. I cannot avoid read and heard continuously these comments. For example, the article Rolling with Ruby on Rails Revisited of Bill Walton says: “What would you think if I told you that you can develop a web application at least ten times faster with Rails than you can with a typical Java framework?” Oops! Ten times faster! Well, after these comments I decided to learn Ruby on Rails. I need to know the true key of the productivity and programmer happiness. After have a taste of RnR I found it a very classic framework, with old techniques:
The Java problem: Java developersThe productivity in Java world is a cultural problem, not a technical one. That is this is not a Java fault, it's our fault, we, the Java developers, need to design very beautiful architectures, to apply everywhere the GoF [15] patterns, to do everything reusable, to put 3 tiers in all our systems and to use web services [16] for all. We are not looking for simplicity, therefore we have not found it. But, Java is a very elegant language that allows simpler approach to software development. Java productivity: The other wayA way for productivity is to use a Model Driven approach. That is, develop the model part, and only the model part, of our application, and to use a framework to produce all the application from it. MDA [17], OpenXava [5], Trails [18], NakedObjects [19], RomaFramework [20] and JMatter [21] are examples of this approach. The goalThis is the main screen of the wanted application:
Basically, the app's supposed to do three things:
The Ruby on Rails first sprintThe first step using RnR is creating the new project, from command line you have to write: $ rails cookbook2 Now you must create and configure your database. Then it's the time for writing your first code, in this case SQL code: drop table if exists recipes; Obviously you have to execute these sentences against your database. And the final step is generate the Ruby code, you only need execute the next command in the shell of your O.S: $ ruby script\generate scaffold recipe recipe $ ruby script\generate scaffold category category Yes. You have the very first version of your RnR application ready to run. Yes, very little work, a simple “create table”, and executing a wizard. Let's see the result. The Rails resultThis is the resulting application:
Little work. Little result. The JPA on OX first sprintGo on using OpenXava [5]. The first step using OpenXava is creating the new project: $ ant CreateNewProject.xml -Dproject=CookBook Now you must create and configure your database. Then it's the time for writing your first code, in this case Java code: Recipe.java: package org.openxava.cookbook.model; Category.java: package org.openxava.cookbook.model; And the final step is to generate the dababase schema, you only need to execute the next ant target from your project: $ ant updateSchema Yes. You have the very first version of your OpenXava application ready to run. Yes, very little work, a simple POJOs, and executing 'updateSchema'. Let's see the result. The OpenXava resultThis is the resulting application:
Note that the user can create,update, delete, generate PDF from list, export the list to excel, order by each column, paging with support for large resultsets and filter data. Moreover you can deploy directly, with no code, only executing an ant target, your application in a JSR-168 [22] portal, and the look & feel of the OpenXava portlet adapts to the look & feel of the portal. This is, from first time, an application ready for production. Little work, polished result. From a philosophical point of view the difference here between RnR and OX is that in RnR you write first the tables and in OpenXava you write first the classes. The controllersRails has generated for you the controller logic for the basic CRUD, you can see it here:
In the other hand OX has not generated any code for CRUD, OpenXava just have a generic code for doing CRUD and Printing, and it is assigned automatically to all entities. You can write your own generic CRUD logic, or you can write a specific logic for a particular entity, but you haven't a controller code for each entity. In this way, you have less code to maintain, and you can change the logic of all CRUD modules touching in a single place. That is for controllers Rails uses generated code while OX uses generic code. You can learn more about OX controllers in OpenXava wiki [23]. Adding a relationshipFor adding a relationship from category to recipe in Ruby you have to write the next code in category.rb:
and this one in recipe.rb:
Yes, pretty simple. But, you have more work to do. You must edit cookbook2\app\views\recipe\_form.rhtml and add the next code: <p><label The result is:
For its part, in OpenXava you have to define the relationship using JPA in Category.java: @ManyToOne(optional=false) @DescriptionsList and in Recipe.java: @OneToMany(mappedBy="category") And you do not need to touch any HTML-like code. You application will show just this:
You have a link for modifying or creating new categories from here. Without adding any additional code if the user go to the Category module he will obtain a collection of Recipes in each Category, as following:
In this point the RnR application still does not have this features, you need to write some Ruby and HTML to code to obtain the same effect. The main difference here between RnR and OX here is that in OX you do not write any HTML-like code, indeed you do not write User Interface code at all. Calculating a initial valueThe next step in the Ruby on Rails tutorial is to generated a initial value for a property. In RnR you have to edit the controller code in order to achieve it. Let's see it: You modify the new and update method adding the line: @recipe.date = Time.now The equivalent in OX is adding the @DefaultValueCalculator annotation in model: @DefaultValueCalculator(CurrentDateCalculator.class) You obtain the same effect in a more declarative way. That, while in RnR you put the code on controller, in OX the code for calculating initial values, for validations and for business logic in general is on the model. OX promotes moving business logic from controller to model. As curiosity, in the RnR article says: “I modified the model files so I need to restart our web server.” While using Eclipse WTP I only need to press Ctrl – B, and click on refresh in my browser in order to see the change of my model in my OpenXava application. ConclusionThe main difference between Ruby on Rails and OpenXava is that RnR is a MVC framework, you have to write the model, the view and the controllers, and OX is a model-driven framework, you only need to write the model. The result is less code for a better application. Another big difference is that RnR uses passive code generation; that is, it generates the code for you, but after it if you want to extend or refine the code you have to edit the generated code. OpenXava does not use code generation, the only code you have is the code you write. You can find productivity inside the Java universe. References
Links[1] http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-r...
Subscribe to our RSS feeds now and receive the next article instantly!
Subscribe to the World's Most Powerful Newsletters
|
||||||||||||