MacDevCenter    
 Published on MacDevCenter (http://www.macdevcenter.com/)
 See this if you're having trouble printing code examples


Using WebObjects "Direct to Web" Technology

by Josh Paul
11/04/2003

Welcome to another installment of "WebObjects, EnterpriseObjects and You." Last time, we introduced you to the incredible WebObjects and EnterpriseObjects technologies. In this article, we'll acquaint you with the wonderfully powerful "Direct to Web" (D2W) technology.

RBRAD

Many of you have heard of Rapid Application Development (RAD). D2W takes RAD and injects a shot of technology steroids to create RBRAD, or Rule-Based Rapid Application Development. Through the use of an EOModel file and a D2WModel file, you're going to create a full-blown web application in just a few simple steps. In fact, you won't even have to work directly with the D2WModel. Don't believe me? Well, read on...

In order to exploit D2W's advantages, you'll want to start off with an EOModel. Fortunately, in the last article, you created a simple EOModel for maintaining information about Users. So, I'll skip over creating the EOModel and jump directly into D2W. However, just in case you cannot find your project, your EOModel should look similar to this:

Open ProjectBuilder and create a new Project. Select the "Direct to Web Application" option. Next, name your project and continue to select the default setup by clicking the "Next" button until you come upon the "Choose EOModels" window. At this point you'll want to "Add" your EOModel.

After adding your EOModel file, you'll be asked to choose a "Look" for your project. I usually select the "Neutral" option, but you're free to select whichever option suits you. In fact, I recommend going back through the previous steps a few times and selecting a different Look each time.

You're done ... sort of. ProjectBuilder offers you the option to "Build and launch project now", which will literally build and launch a full-blown web application. You should keep the option selected and click the "Finish" button.

Voila!

When the application launches, it will display the Main.wo component in your web browser. Since there's no real logic behind the Main.wo component yet, you'll be able to log into your application by simply clicking on the "Login" button.

Once you've logged into your application, you'll discover you can easily search, create, edit, and even delete information in your database. All of the pages being created use a "Rule" file called a d2wmodel. Simply by looking at the full-blown web application before you, you catch a small glimpse of the amazing RBRAD world.

Customization Using the Assistant

After wandering around your new web application, go ahead and "Logout" of the application. You'll notice that on your Main.wo component there's a small checkbox labeled "Assistant." Go ahead and check it off and log into your application again

The Assistant

Since you've requested the "Assistant" you'll discover a new "Customize" button on the left side of the window. When you decide to customize your application, a Java applet will start. A small frame will then be displayed at the bottom of your web browser to inform you of the Assistant's status.

A new window will open and allow you to customize your D2WModel file. By using the Assistant, you'll be able to customize your application without having to muck around with the XML behind it. You can also customize your D2WModel file using the Rule Editor application, but we'll stick to the Assistant ... for now.

Initially, you'll be presented with a window explaining that your current page does not support customization. The initial page is called a D2WQueryAllPage and will literally allow you to query any table in your database. Since this is a broad search page, customizing it would be cumbersome, therefore we're not going to try.

The Query Page

In order to customize the application, we're going to need a customizable page to be displayed. Fortunately, most pages are customizable, so we'll start of with a "Query" page. You can get a "Query" page by clicking the "Search" button.

Your Assistant should now look similar to this:

You probably don't want to look up Users by their password's, so why don't you hide that information from the page. In order to do this, simply select the password attribute in the Assistant and click the GreenArrow:Left button and then click the "Save" button.

Your web page should automatically reload. Additionally, the password field should no longer be available. Go ahead and "Search DB" for a User using the username field. You can leave the username field blank if you'd like to retrieve all Users from your database.

The List Page

The next page you'll see, after searching the database, will be a D2WListPage. This page should provide a list of Users, based on the criteria you entered for the search. Based on the last article, I received the following list for my search:

Within the "List" page you can:

Since you're searching for Users based on their username, you probably want to list the username before the password. In order to accomplish this goal, simply select the username in the Assistant (which should have updated its information to reflect the current page; neat!) and click on the GreenArrow:Up button. You can then click the "Save" button in the Assistant. When you're back to using your web browser, click on the various Sort buttons to get a feel for how they work.

The Edit Page

What fun would a database-driven site be without new and changing information? The only way to get new information, or changed information, onto the site is to "Edit" it, right? So, go ahead and choose a User to edit by clicking on the edit button.

Ugh. Our User's password is displayed before the username. Well, that's easy to remedy. Simply follow the same steps as before:

  1. Select username in the Assistant
  2. Click the GreenArrow:Up button
  3. Click the "Save" button

For fun, go ahead and change the username to something else and click the "Save" button. You'll be returned to the "List" page and your newly entered information will be displayed.

The Delete Page

For kicks, try using the "Delete" button. You should receive a "Confirm" page, asking you for a confirmation of your intent to delete the User you've selected. I won't have any input at this point...it's your data, so you get to choose whether you want to keep it around.

A little D2W insight

Well, you've either removed a User from your database or spared them until the next round of experiments (insert evil laugh). Either way, why don't you go "Home" for a little reflection on what you've learned? Your "Home" page is the D2WQueryAll page. For now, we'll keep it that way.

When you click on the "New" button, you'll receive an "Edit" page. At first inspection this may seem a little odd, but if you think about it, when you create a new User aren't you just creating an empty row in the database and editing it? Well, what do you know, those engineer's over at Apple might be onto something after all.

If you leave either (or both) the username or password fields empty and try to save the information, you should get an error.

Hey! How did WebObjects know that Users who don't have username's or password's shouldn't be saved to the database? The answer is simple: your EOModel.

The EOModel

If you open your EOModel file and inspect the User Entity, you should discover that the username and password attributes do not allow for NULL values.

You can confirm this by looking under the Allow Null Value column. If the column is empty, and it should be, when your application attempts to save information to the database the wonderful EnterpriseObjects framework does it's magic. Your EOModel will help to inform you (or your clients) the current data does not conform to the model and therefore cannot be saved.

The D2WModel

As I mentioned earlier, there is a D2WModel file helping your application on it's way to completion. For now, go ahead and logout of your application and Stop it in ProjectBuilder. While using ProjectBuilder, you can find your D2WModel (actually, your user.d2wmodel) file under the "Resources" group.

Being the hacker that you are, you're bound to open this file sooner or later. You might as well make it sooner.

When you open the user.d2wmodel file, the RuleEditor application should launch. Here comes the fun...

Your user.d2wmodel is a collection of Rules. Each Rule is made up of a few parts:

Since you used the Assistant to customize your application, you already have a few Rules in your user.d2wmodel file. I'm going to help you dissect one of them:

The highlighted Rule can be read as follows:

If the current task (i.e. action) is "Edit" and the current Entity's (i.e. table) name is "User"
then
display the username and password attributes (i.e. columns) on the current page.

In other words, when someone wants to "Edit" a User, give them access to the username and password fields.

So, what about this whole Priority business? In order to demonstrate it, go ahead and copy the highlighted Rule then paste it. Then select the newly created Rule. You should change the Right-Hand Side's Value from (username, password) to (username).

You will also want to change the Rule's Priority to a number higher than 100...like 101. You should then save your changes.

Back to ProjectBuilder

Next, "Launch" your application. As before, you can login without entering any credentials (you should really fix that...see the previous article). When you go to create a "New" User, you will receive the "Edit" page, just like before.

Only this time, something has changed. Sure enough, you can only enter information for the User's username. This is going to cause major problems, since you can't save a User without a password. Go ahead, just try it!

You should leave your web browser open, since your going to be going back to it very shortly.

Open your user.d2wmodel again and change the priority of semi-new Rule to a number less than 100...like 99. Save your changes and go back to your web browser. Once you're back using your web browser again, click on the "Cancel"
button. After receiving the "Home" page, try creating a new User again. You should now be back to the original option of username and password.

What has happened is D2W evaluated the Rules in your D2WModel. When D2W came across two LHS entries that matched, D2W looked to the Priority to determine which Rule to use. Not only that, but it did so without requiring you to rebuild your application. That's what I'd call Rapid.

Throw D2W an uppercut

So, tracking your Users username's and password's isn't enough. I think you'll want to track a little more information, maybe their First and Last names. If you open your EOModel file, you can easily add this information...so you might as well.

Change the User Entity

To create the nameFirst and nameLast attributes, I simply performed a copy and paste combination using the username attribute. I then renamed both the Name and Column information to reflect my new criteria. I also decided to allow NULL values by checking the option under the Allow Null Value column.

Since I've edited my EOModel, my database needs to contain a similar schema. EOModeler used to allow you to "Synchronize Schema..." fairly reliably (you can still find this command under the "Model" menu). However, with the change to JDBC, the success of a synchronization will depend on your database vendor (and its Plugin). In order to get in sync, you can either:

Being the daring person that I am, and the fact I'm using a test database, I chose to simply use EOModeler's SQL generation. By clicking the SQL button in EOModeler, you will be presented with a variety of options. Based on my selections, when I click "Execute SQL", EOModeler is going to drop the User table (and the data contained in it). If you are not using a test database do not drop your table...but you already know that...right?

Once you have your EOModel and database in sync, you can return back to ProjectBuilder.

Work with the Assistant

When you "Build and Launch" your application, login, and browse around your site, you'll discover the changes you have made to the User Entity are not available. This is because you previously created a set of Rules and D2W isn't going to ignore them.

If you enable the Assistant (remember the checkbox on the Login page?) and navigate to an "Edit" page, you'll discover the Assistant lists the nameFirst and nameLast attributes in the left most browser (aka the key path browser).

Follow your intuition and select the nameFirst attribute and click on the GreenArrow:Right button. You should then find the nameFirst listed below the username and password attributes. When you click on the "Save" button in the Assistant window, your web browser should reload with your changes.

Moving Forward

From this point, you should have enough information to work with the D2WAssistant, EOModeler and RuleEditor to create a simple application for maintaining User information. Oh, and if you hadn't noticed, there's no code here to maintain (well, okay, the user.d2wmodel file is kind of code).

Hey! If you combine this article with the previous article, and sprinkle in a little ingenuity, you should be able to create a simple User account maintenance application. The bonus will be the fact you'll have very little code to maintain.

If you want to explore D2W further, I highly recommend Apple's documentation provided with WebObjects. I would also be remiss, if I did not mention the open source set of WebObjects frameworks and tools at Project WONDER. I must warn you, however, that WONDER is huge and not for the faint of heart.

Josh Paul is the founder and CEO of Aweli, a startup focused on digital video solutions, and the author of Digital Video Hacks. He has provided software and service solutions to entertainment production companies throughout Los Angeles and New York.


Return to the Mac DevCenter

Copyright © 2009 O'Reilly Media, Inc.