Welcome!

Web 2.0 Authors: Elizabeth White, Greg Ness, AppDynamics Blog, David H Deans, Trevor Parsons

Related Topics: Web 2.0, Java, Adobe Flex, Open Source, Eclipse, Ruby

Web 2.0: Article

Java Kicks Ruby on Rails in the Butt

This article demonstrates that Java is more productive than Ruby

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 framework

Ruby 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:

  • Ruby is a dynamically typed [7] language, as Smalltalk [8]. I prefer statically typed [9] languages.

  • Scaffolding is passive code generation, as IDE wizards or AppFuse [10]. I prefer active code generation [11], or even better, no code generation at all.

  • Relational database centric: the code generators and ActiveRecord promote think first in tables after in classes. I prefer a more pure OO, as Hibernate [12], JPA [4] or even ODBMS [13].

  • MVC [14]: I'm looking for something newer and better that an old MVC framework.

The Java problem: Java developers

The 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 way

A 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 goal

This is the main screen of the wanted application:

Basically, the app's supposed to do three things:

  • Display a list of all recipes.

  • Create new recipes and edit existing recipes.

  • Assign a recipe to a category (like "dessert" or "soup").

The Ruby on Rails first sprint

The 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;
drop table if exists categories;
create table categories (
id int not null auto_increment,
name varchar(100) not null default '',
primary key(id)
) engine=InnoDB;

create table recipes (
id int not null auto_increment,
category_id int not null,
title varchar(100) not null default '',
description varchar(255) null,
date date null,
instructions text null,
constraint fk_recipes_categories foreign key
(category_id) references categories(id),
primary key(id)
) engine=InnoDB;

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 result

This is the resulting application:

New category entry page

New category added!

New recepy entry page

Little work. Little result.

The JPA on OX first sprint

Go 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;

import java.util.*;
import javax.persistence.*;
import org.openxava.annotations.*;

@Entity
@View(members="title; description; date; instructions")
public class Recipe {

@Id @GeneratedValue @Hidden
private Integer id;

@Required @Column(length=100)
private String title;

@Column(length=255)
private String description;

private Date date;

@Stereotype("HTML_TEXT")
private String instructions;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

public String getInstructions() {
return instructions;
}

public void setInstructions(String instructions) {
this.instructions = instructions;
}

}

Category.java:

package org.openxava.cookbook.model;

import java.util.*;

import javax.persistence.*;

import org.openxava.annotations.*;

@Entity
public class Category {

@Id @GeneratedValue @Hidden
private Integer id;

@Required @Column(length=100)
private String name;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

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 result

This is the resulting application:

 List Mode

 Detail mode

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 controllers

Rails has generated for you the controller logic for the basic CRUD, you can see it here:

RnR controller

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 relationship

For adding a relationship from category to recipe in Ruby you have to write the next code in category.rb:

Rnr relationship category to recipe

and this one in recipe.rb:

Rnr relationship recipe to category

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
for="recipe_category_id">Category</label><br/>
<%= select("recipe", "category_id", Category.find(:all).collect
{|c| [c.name, c.id] }) %></p>

The result is:

Rnr recipe with category

For its part, in OpenXava you have to define the relationship using JPA in Category.java:

@ManyToOne(optional=false) @DescriptionsList
private Category category;

and in Recipe.java:

 @OneToMany(mappedBy="category")
private Collection<Recipe> recipes;

And you do not need to touch any HTML-like code. You application will show just this:

OpenXava Recipe with Category

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:

OpenXava Category with recipes

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 value

The 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)
private Date date;

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.

Conclusion

The 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...
[2] http://www.onlamp.com/pub/a/onlamp/2007/01/05/revisiting-ruby-on-rails-r...
[3] http://en.wikipedia.org/wiki/Plain_Old_Java_Object
[4] http://en.wikipedia.org/wiki/Java_Persistence_API
[5] http://www.openxava.org/
[6] http://www.rubyonrails.org/
[7] http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing
[8] http://en.wikipedia.org/wiki/Smalltalk
[9] http://en.wikipedia.org/wiki/Type_system#Static_typing
[10] http://en.wikipedia.org/wiki/AppFuse
[11] http://c2.com/cgi-bin/wiki?ActiveCodeGeneration
[12] http://www.hibernate.org
[13] http://en.wikipedia.org/wiki/ODBMS
[14] http://en.wikipedia.org/wiki/Model-view-controller
[15] http://en.wikipedia.org/wiki/Design_Patterns
[16] http://en.wikipedia.org/wiki/Web_services
[17] http://www.omg.org/mda/
[18] http://www.trailsframework.org/
[19] http://www.nakedobjects.org/
[20] http://romaframework.xwiki.org
[21] http://jmatter.org/
[22] http://jcp.org/en/jsr/detail?id=168
[23] http://openxava.wikispaces.com/controllers_en
[24] http://java.dzone.com/sites/all/files/rails-vs-ox010.jpg
[25] http://java.dzone.com/sites/all/files/rails-vs-ox020.jpg
[26] http://java.dzone.com/sites/all/files/rails-vs-ox030.jpg
[27] http://java.dzone.com/sites/all/files/rails-vs-ox040.jpg

More Stories By Javier Paniza

Javier Paniza is the project lead for OpenXava project. He works as software developer at Gestión 400, a software company for public administration in Spain. He has been developing with Java Enterprise since 1998. Also he has been J2EE mentor for development teams in banking projects.

@ThingsExpo Stories
The BPM world is going through some evolution or changes where traditional business process management solutions really have nowhere to go in terms of development of the road map. In this demo at 15th Cloud Expo, Kyle Hansen, Director of Professional Services at AgilePoint, shows AgilePoint’s unique approach to dealing with this market circumstance by developing a rapid application composition or development framework.
SYS-CON Events announced today that Windstream, a leading provider of advanced network and cloud communications, has been named “Silver Sponsor” of SYS-CON's 16th International Cloud Expo®, which will take place on June 9–11, 2015, at the Javits Center in New York, NY. Windstream (Nasdaq: WIN), a FORTUNE 500 and S&P 500 company, is a leading provider of advanced network communications, including cloud computing and managed services, to businesses nationwide. The company also offers broadband, phone and digital TV services to consumers primarily in rural areas.
The Internet of Things is not new. Historically, smart businesses have used its basic concept of leveraging data to drive better decision making and have capitalized on those insights to realize additional revenue opportunities. So, what has changed to make the Internet of Things one of the hottest topics in tech? In his session at @ThingsExpo, Chris Gray, Director, Embedded and Internet of Things, discussed the underlying factors that are driving the economics of intelligent systems. Discover how hardware commoditization, the ubiquitous nature of connectivity, and the emergence of Big Data a...
"BSQUARE is in the business of selling software solutions for smart connected devices. It's obvious that IoT has moved from being a technology to being a fundamental part of business, and in the last 18 months people have said let's figure out how to do it and let's put some focus on it, " explained Dave Wagstaff, VP & Chief Architect, at BSQUARE Corporation, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
The major cloud platforms defy a simple, side-by-side analysis. Each of the major IaaS public-cloud platforms offers their own unique strengths and functionality. Options for on-site private cloud are diverse as well, and must be designed and deployed while taking existing legacy architecture and infrastructure into account. Then the reality is that most enterprises are embarking on a hybrid cloud strategy and programs. In this Power Panel at 15th Cloud Expo (http://www.CloudComputingExpo.com), moderated by Ashar Baig, Research Director, Cloud, at Gigaom Research, Nate Gordon, Director of T...
SYS-CON Events announced today that IDenticard will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. IDenticard™ is the security division of Brady Corp (NYSE: BRC), a $1.5 billion manufacturer of identification products. We have small-company values with the strength and stability of a major corporation. IDenticard offers local sales, support and service to our customers across the United States and Canada. Our partner network encompasses some 300 of the world's leading systems integrators and security s...

ARMONK, N.Y., Nov. 20, 2014 /PRNewswire/ --  IBM (NYSE: IBM) today announced that it is bringing a greater level of control, security and flexibility to cloud-based application development and delivery with a single-tenant version of Bluemix, IBM's platform-as-a-service. The new platform enables developers to build ap...

“In the past year we've seen a lot of stabilization of WebRTC. You can now use it in production with a far greater degree of certainty. A lot of the real developments in the past year have been in things like the data channel, which will enable a whole new type of application," explained Peter Dunkley, Technical Director at Acision, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
DevOps Summit 2015 New York, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that it is now accepting Keynote Proposals. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development cycles that produce software that is obsolete at launch. DevOps may be disruptive, but it is essential.
"People are a lot more knowledgeable about APIs now. There are two types of people who work with APIs - IT people who want to use APIs for something internal and the product managers who want to do something outside APIs for people to connect to them," explained Roberto Medrano, Executive Vice President at SOA Software, in this SYS-CON.tv interview at Cloud Expo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
Nigeria has the largest economy in Africa, at more than US$500 billion, and ranks 23rd in the world. A recent re-evaluation of Nigeria's true economic size doubled the previous estimate, and brought it well ahead of South Africa, which is a member (unlike Nigeria) of the G20 club for political as well as economic reasons. Nigeria's economy can be said to be quite diverse from one point of view, but heavily dependent on oil and gas at the same time. Oil and natural gas account for about 15% of Nigera's overall economy, but traditionally represent more than 90% of the country's exports and as...
The Internet of Things is a misnomer. That implies that everything is on the Internet, and that simply should not be - especially for things that are blurring the line between medical devices that stimulate like a pacemaker and quantified self-sensors like a pedometer or pulse tracker. The mesh of things that we manage must be segmented into zones of trust for sensing data, transmitting data, receiving command and control administrative changes, and peer-to-peer mesh messaging. In his session at @ThingsExpo, Ryan Bagnulo, Solution Architect / Software Engineer at SOA Software, focused on desi...
"At our booth we are showing how to provide trust in the Internet of Things. Trust is where everything starts to become secure and trustworthy. Now with the scaling of the Internet of Things it becomes an interesting question – I've heard numbers from 200 billion devices next year up to a trillion in the next 10 to 15 years," explained Johannes Lintzen, Vice President of Sales at Utimaco, in this SYS-CON.tv interview at @ThingsExpo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
"For over 25 years we have been working with a lot of enterprise customers and we have seen how companies create applications. And now that we have moved to cloud computing, mobile, social and the Internet of Things, we see that the market needs a new way of creating applications," stated Jesse Shiah, CEO, President and Co-Founder of AgilePoint Inc., in this SYS-CON.tv interview at 15th Cloud Expo, held Nov 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events announced today that Gridstore™, the leader in hyper-converged infrastructure purpose-built to optimize Microsoft workloads, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Gridstore™ is the leader in hyper-converged infrastructure purpose-built for Microsoft workloads and designed to accelerate applications in virtualized environments. Gridstore’s hyper-converged infrastructure is the industry’s first all flash version of HyperConverged Appliances that include both compute and storag...
Today’s enterprise is being driven by disruptive competitive and human capital requirements to provide enterprise application access through not only desktops, but also mobile devices. To retrofit existing programs across all these devices using traditional programming methods is very costly and time consuming – often prohibitively so. In his session at @ThingsExpo, Jesse Shiah, CEO, President, and Co-Founder of AgilePoint Inc., discussed how you can create applications that run on all mobile devices as well as laptops and desktops using a visual drag-and-drop application – and eForms-buildi...
We certainly live in interesting technological times. And no more interesting than the current competing IoT standards for connectivity. Various standards bodies, approaches, and ecosystems are vying for mindshare and positioning for a competitive edge. It is clear that when the dust settles, we will have new protocols, evolved protocols, that will change the way we interact with devices and infrastructure. We will also have evolved web protocols, like HTTP/2, that will be changing the very core of our infrastructures. At the same time, we have old approaches made new again like micro-services...
Code Halos - aka "digital fingerprints" - are the key organizing principle to understand a) how dumb things become smart and b) how to monetize this dynamic. In his session at @ThingsExpo, Robert Brown, AVP, Center for the Future of Work at Cognizant Technology Solutions, outlined research, analysis and recommendations from his recently published book on this phenomena on the way leading edge organizations like GE and Disney are unlocking the Internet of Things opportunity and what steps your organization should be taking to position itself for the next platform of digital competition.
The 3rd International Internet of @ThingsExpo, co-located with the 16th International Cloud Expo - to be held June 9-11, 2015, at the Javits Center in New York City, NY - announces that its Call for Papers is now open. The Internet of Things (IoT) is the biggest idea since the creation of the Worldwide Web more than 20 years ago.
As the Internet of Things unfolds, mobile and wearable devices are blurring the line between physical and digital, integrating ever more closely with our interests, our routines, our daily lives. Contextual computing and smart, sensor-equipped spaces bring the potential to walk through a world that recognizes us and responds accordingly. We become continuous transmitters and receivers of data. In his session at @ThingsExpo, Andrew Bolwell, Director of Innovation for HP's Printing and Personal Systems Group, discussed how key attributes of mobile technology – touch input, sensors, social, and ...