Wednesday, February 24, 2010

How to create a simple Naked Objects Application in NetBeans

Naked Objects is an open source application framework that lets you concentrate on domain objects and automagically generates a GUI for interacting with them. This sounds (and actually is) very interesting, as the domain is what we should most interested in, instead of boilerplate, as everybody who is fond of DDD knows.

Being NetBeans my favourite IDE, as you might have suspected, I'll try to jot down some notes on how to use it to create a very simple Naked Objects application.

First things first, you have to download the Naked Objects framework; you have various options, I chose to download the zip file for Ant. After unzipping the file you should find a folder named lib that contains - o great wonder and surprise - all the necessary libs.

As I suppose I could use all this stuff in more than one project, I created a new library in NetBeans:


Click on the New Library... button and you'll be prompted a library name and type


Then click on the Add Jar/Folder... button and add all the jars in the lib folder. You should get something like this:


Ok, now we can proceed to creating a new application. As a disclaimer, this is not necessarily the best way to do it, but it is how my inner cogs work (or don't work). If yours are different, you'll have to compose all the pieces of the puzzle in a different order.

Let's suppose we want to use the DnD GUI, so I go with a new Java Application:


Click on the Next> button and enter a name for your project; don't use a dedicated folder for libraries and don't have the IDE create a main class for you.


Click on the Finish button. Ok, now we should instruct our project to be a Naked Object Application. Right click the Libraries node to add all necessary references:


Select the library you've previosly created and you're almost ready to go.

Now, if you try to run the application NetBeans complains because you have not chosen a Main Class. Right click the project node and the Run node in the Categories tree, and insert org.nakedobjects.runtime.NakedObjects as the Main Class.


Click on the OK button. Ok, now we can try to run the application... just to get another complain


Exception in thread "main" org.nakedobjects.metamodel.commons.exceptions.NakedObjectException: failed to load 'nakedobjects.properties'; tried using: [file system (directory 'config'), file system (directory 'src/main/webapp/WEB-INF'), context loader classpath]

It seems fair... after all we have to provide some configuration. Create a new properties file, name it nakedobjects and put it in the config folder. Obviously this is not enough, and you can check it yourself trying to run the application: NetBeans will complain again (don't shoot the messenger) about the lack of services.

To define services, we have to write some code. Let's suppose we want to deal with all Walt Disney characters - I hope there are no legal issues with this - so let's create a class that represents one:


package it.moz.noapp;

import org.nakedobjects.applib.AbstractDomainObject;
import org.nakedobjects.applib.annotation.DescribedAs;
import org.nakedobjects.applib.annotation.MemberOrder;

@DescribedAs("Character")
public class Character extends AbstractDomainObject {

private String firstname;
private String lastName;

@MemberOrder(sequence = "1")
public String getFirstname() {
resolve(firstname);
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
objectChanged();
}

@MemberOrder(sequence = "2")
public String getLastName() {
resolve(lastName);
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
objectChanged();
}

public String title() {
return getFirstname() + " " + getLastName();
}
}

Notice how this class, that is a domain object, extends the class AbstractDomainObject provided by the framework. The annotation @DescribedAs tells you how the object will be referred to in the GUI, while @MemberOrder defines the order in which members are presented. You can find every detail on the Naked Objects website.

The next step is telling our application that such a domain object exists, so we'll add the following line in our nakedobject.properties file:

nakedobjects.services=repository#it.moz.noapp.Character

Ok, let's start the application...


uh oh... User name and password? ehrm... let's provide the application with them! The problem is that if you want to add an empty file - unlike for properties files - NetBeans asks you whether you want to add it to the src folder or to the test folder, i.e. the folders (already) defined in the project. As you want neither, you must manually add an empty passwords file to the config folder. Before complaining about NetBeans, keep in mind that you can always define the config folder as a new source package folder, but let's skip this option.

Fill in a username and a password...
user:pass
And that's it, you can start your application!


To create a new instance you right-click the Characters icon and select the appropriate item from the drop down list, insert the desired values and here's your brand new character. Rinse and repeat and you'll get something like this:


As you can see, you now have a Disney Character directory without having written a single line of code but your domain class.

Unluckily, if you close your application all your efforts have been vain, so you should add the support for persistence. But this will be the subject for another post...

Friday, February 19, 2010

How to simply avoid a bad impression

You never get a second chance to give a second first impression. That's why you want your first impression to be impressive.

Yet, many organizations fail in this. Sadly, it is because people fail.

A simple example is a presentation brochure: a company writes to a set of prospects telling them how wonderful they are and why everybody should instantly drop all their current suppliers to take advantage of the awesome partnership they are offering.

What's wrong in this? Actually nothing, as commerce is based on advertising.

But.

If you're trying to tell me how irreplaceable you are, at least try to avoid misspells! How good can a company be, if they don't even take the time to spellcheck their booklets? And booklets are what everybody else is supposed to see! Can I trust a company that does things this carelessly? If this is how they manage communications, how will they deal with all my oh-so-important-megastuff?

The same applies with everyday communications: even if you're only sending a mail as a service desk operator, try to reread it and spellcheck it. It only costs a few seconds and greatly improves your image. Well, maybe I'm too hopeful... maybe spellchecking won't help, but failing to do it could cause a certain harm. You could be labelled as careless, distract, ignorant, and so on and so forth (at least, as far as I'm concerned).

Personally, it also annoys me a little. Don't you think I deserve a little more attention than this?

Unluckily this happens almost everywhere: whenever a colleague of mine sends out such a mail, it annoys me even more, because he's in the same team in which I am, and as a result I automatically become "one of the lazybones of that crappy company". Well, I'm not a lazybone and the company I work for is not crappy, so would you please invest some of the time our company pays you for to avoid embarassing us all?

It only takes some seconds...

BTW, to walk the walk and not only talk the talk, from now on I'll have to double-check everything... or else be prepared to be fired upon ;-)

Thursday, February 18, 2010

The importance of unit tests

Today I made a small change in a class in the application I'm working on. Being a diligent monkey, before adding the new feature I wrote a small dedicated test. The test failed, I added the feature, the test passed, I cleaned up a little bit (not too much, having just added three SLOCs) and finally committed the class in our SCM before hopping on to the next feature.

After a while our Continuous Integration Server pitilessly sent me a mail in which it informed me that the build was broken. No, better yet, that I broke the build, causing not one but 17 tests to fail. O shame and amazement, how was it possible? I had even written a test...

Despite all apparences, this is very good news. Why, you ask, grasshopper? Let me ask you a question: when do you want to know you have a problem? talking about software development, as soon as possible. Those 17 failures have saved me from delivering buggy code that would upset the user and set the stage for some annoying debugging sessions.

This has only been possible because of the test harness: all the time we spent writing tests has, once more, paid off well. So, without forgetting to add a test for the previously unchecked condition, I fixed the code in double-quick time.

I wish all managers understood this: writing tests is not a cost, but a tremendously effective cost saving tool.

Mud and glory

These are two words that often go together when you're talking about rugby. I won't talk about glory here, but sure enough yesterday evening we had plenty of mud. And I thought that Crespi was bad... What a fool!

As the icing on the cake, I also somehow managed to get a massive blow in the breastbone, which prevented me to normally continue the training. This should teach me (remind me) what the right position is when you are about to impact on a defender. Instead of making for the changing room, I went on running on my own and did not abandon the field, opting instead to collect some more mud before taking a (cold) shower.

That brings me back to square one... to be honest, not that I'd gone much farther than that so far...

Wednesday, February 17, 2010

Milano Rugby Festival is back

Read here all you need to know about the 2010 edition. Who will win the Cassie Pieenar Trophy this year?

Friday, February 12, 2010

Sun product roadmap

Found on the Java home page:
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.

...shiver...

Thursday, February 4, 2010

The cost of software

May I suggest this post by Uncle Bob on the cost of software? Ops I already did it!

Wednesday, February 3, 2010

Where has all the blue gone?

Today I visited the java web page and... I was shocked! I knew something like this would happen, but now it looks kinda weird...


Besides, if you try to reach Sun website it gets even weirder as you land on the official Oracle website...

Monday, February 1, 2010

That means asking for it...

The testimonials section of the Chuck Norris plugin for Hudson states that

jrr wrote a Greasemonkey script called de-norris hudson that performs client-side removal of ChuckNorris.

It is said that shortly after Chuck Norris wrote a RoundhouseKick script called de-jrr world that performs a complete removal of jrr. Obviously, nobody has ever seen jrr since.

BTW, thanks to Fabrizio that is always a source of inspiration, we're using the Chuck Norris plugin too.