The Wayback Machine - https://web.archive.org/web/20161208001223/http://php.sys-con.com/node/965189

Welcome!

PHP Authors: Liz McMillan, Carmen Gonzalez, Hovhannes Avoyan, Lori MacVittie, Trevor Parsons

Related Topics: Agile Computing, Java IoT, Adobe Flex, Open Source Cloud, Eclipse, Ruby-On-Rails

Agile Computing: 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
IoT solutions exploit operational data generated by Internet-connected smart “things” for the purpose of gaining operational insight and producing “better outcomes” (for example, create new business models, eliminate unscheduled maintenance, etc.). The explosive proliferation of IoT solutions will result in an exponential growth in the volume of IoT data, precipitating significant Information Governance issues: who owns the IoT data, what are the rights/duties of IoT solutions adopters towards t...
Businesses and business units of all sizes can benefit from cloud computing, but many don't want the cost, performance and security concerns of public cloud nor the complexity of building their own private clouds. Today, some cloud vendors are using artificial intelligence (AI) to simplify cloud deployment and management. In his session at 20th Cloud Expo, Ajay Gulati, Co-founder and CEO of ZeroStack, will discuss how AI can simplify cloud operations. He will cover the following topics: why clou...
As data explodes in quantity, importance and from new sources, the need for managing and protecting data residing across physical, virtual, and cloud environments grow with it. Managing data includes protecting it, indexing and classifying it for true, long-term management, compliance and E-Discovery. Commvault can ensure this with a single pane of glass solution – whether in a private cloud, a Service Provider delivered public cloud or a hybrid cloud environment – across the heterogeneous enter...
Everyone knows that truly innovative companies learn as they go along, pushing boundaries in response to market changes and demands. What's more of a mystery is how to balance innovation on a fresh platform built from scratch with the legacy tech stack, product suite and customers that continue to serve as the business' foundation. In his General Session at 19th Cloud Expo, Michael Chambliss, Head of Engineering at ReadyTalk, discussed why and how ReadyTalk diverted from healthy revenue and mor...
The many IoT deployments around the world are busy integrating smart devices and sensors into their enterprise IT infrastructures. Yet all of this technology – and there are an amazing number of choices – is of no use without the software to gather, communicate, and analyze the new data flows. Without software, there is no IT. In this power panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, Dave McCarthy, Director of Products at Bsquare Corporation; Alan Williamson, Principal...
The 20th International Cloud Expo has announced that its Call for Papers is open. Cloud Expo, to be held June 6-8, 2017, at the Javits Center in New York City, brings together Cloud Computing, Big Data, Internet of Things, DevOps, Containers, Microservices and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding business opportunity. Submit your speaking proposal ...
You have great SaaS business app ideas. You want to turn your idea quickly into a functional and engaging proof of concept. You need to be able to modify it to meet customers' needs, and you need to deliver a complete and secure SaaS application. How could you achieve all the above and yet avoid unforeseen IT requirements that add unnecessary cost and complexity? You also want your app to be responsive in any device at any time. In his session at 19th Cloud Expo, Mark Allen, General Manager of...
"IoT is going to be a huge industry with a lot of value for end users, for industries, for consumers, for manufacturers. How can we use cloud to effectively manage IoT applications," stated Ian Khan, Innovation & Marketing Manager at Solgeniakhela, in this SYS-CON.tv interview at @ThingsExpo, held November 3-5, 2015, at the Santa Clara Convention Center in Santa Clara, CA.
Successful digital transformation requires new organizational competencies and capabilities. Research tells us that the biggest impediment to successful transformation is human; consequently, the biggest enabler is a properly skilled and empowered workforce. In the digital age, new individual and collective competencies are required. In his session at 19th Cloud Expo, Bob Newhouse, CEO and founder of Agilitiv, drew together recent research and lessons learned from emerging and established compa...
Bert Loomis was a visionary. This general session will highlight how Bert Loomis and people like him inspire us to build great things with small inventions. In their general session at 19th Cloud Expo, Harold Hannon, Architect at IBM Bluemix, and Michael O'Neill, Strategic Business Development at Nvidia, discussed the accelerating pace of AI development and how IBM Cloud and NVIDIA are partnering to bring AI capabilities to "every day," on-demand. They also reviewed two "free infrastructure" pr...
Financial Technology has become a topic of intense interest throughout the cloud developer and enterprise IT communities. Accordingly, attendees at the upcoming 20th Cloud Expo at the Javits Center in New York, June 6-8, 2017, will find fresh new content in a new track called FinTech.
Information technology is an industry that has always experienced change, and the dramatic change sweeping across the industry today could not be truthfully described as the first time we've seen such widespread change impacting customer investments. However, the rate of the change, and the potential outcomes from today's digital transformation has the distinct potential to separate the industry into two camps: Organizations that see the change coming, embrace it, and successful leverage it; and...
"Dice has been around for the last 20 years. We have been helping tech professionals find new jobs and career opportunities," explained Manish Dixit, VP of Product and Engineering at Dice, in this SYS-CON.tv interview at 19th Cloud Expo, held November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA.
Extracting business value from Internet of Things (IoT) data doesn’t happen overnight. There are several requirements that must be satisfied, including IoT device enablement, data analysis, real-time detection of complex events and automated orchestration of actions. Unfortunately, too many companies fall short in achieving their business goals by implementing incomplete solutions or not focusing on tangible use cases. In his general session at @ThingsExpo, Dave McCarthy, Director of Products...
"At ROHA we develop an app called Catcha. It was developed after we spent a year meeting with, talking to, interacting with senior citizens watching them use their smartphones and talking to them about how they use their smartphones so we could get to know their smartphone behavior," explained Dave Woods, Chief Innovation Officer at ROHA, in this SYS-CON.tv interview at 19th Cloud Expo, held November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA.
SYS-CON Events has announced today that Roger Strukhoff has been named conference chair of Cloud Expo and @ThingsExpo 2017 New York. The 20th Cloud Expo and 7th @ThingsExpo will take place on June 6-8, 2017, at the Javits Center in New York City, NY. "The Internet of Things brings trillions of dollars of opportunity to developers and enterprise IT, no matter how you measure it," stated Roger Strukhoff. "More importantly, it leverages the power of devices and the Internet to enable us all to im...
We are always online. We access our data, our finances, work, and various services on the Internet. But we live in a congested world of information in which the roads were built two decades ago. The quest for better, faster Internet routing has been around for a decade, but nobody solved this problem. We’ve seen band-aid approaches like CDNs that attack a niche's slice of static content part of the Internet, but that’s it. It does not address the dynamic services-based Internet of today. It does...
"ReadyTalk is an audio and web video conferencing provider. We've really come to embrace WebRTC as the platform for our future of technology," explained Dan Cunningham, CTO of ReadyTalk, in this SYS-CON.tv interview at WebRTC Summit at 19th Cloud Expo, held November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA.
20th Cloud Expo, taking place June 6-8, 2017, at the Javits Center in New York City, NY, will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud strategy.
WebRTC is the future of browser-to-browser communications, and continues to make inroads into the traditional, difficult, plug-in web communications world. The 6th WebRTC Summit continues our tradition of delivering the latest and greatest presentations within the world of WebRTC. Topics include voice calling, video chat, P2P file sharing, and use cases that have already leveraged the power and convenience of WebRTC.