macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

An Introduction to WebObjects
Pages: 1, 2, 3

You will be using the pk Attribute as your Primary Key. EOF will handle its generation for you; however, you will need to indicate that it is to be used as a Primary Key. Therefore, click in the Key column.

After you've modeled your Entities, you'll want to double check that the External type (the type of data your database will store the information as) and the Internal type (the type of Object EO will use) for each Attribute. Some of this may seem tedious, especially when you start building larger models, but the attention you pay to details here will pay off tremendously in the future. Once you're happy with your Model, you can have EOModeler create your database for you. Simply click on the SQL button and then Execute SQL. If all goes well, you'll have a your database.

I also need to mention that EOModeler can reverse-engineer a previously created database. So, if you just happen to get the call requesting to build a system on a current database, you'll be able to have EOModeler do the heavy lifting for you. WO has a great technology called Direct To Web (D2W) that can give you web-driven access to a database in minutes. At first glance D2W may look like a toy, but trust me -- it's not.

Store Variables in the Database

In order to move forward with this article, you need a way to get information into the database. There are a number of ways to accomplish this, but since this is a WO/EO article, I figured I'd detour just briefly.

In Project Builder, create a new File. The Assistant will present itself. You should select the Display Group Component, located under the WebObjects heading. You should then name your new component UserGroup. After naming your component, you will be presented with the choice of an Entity, which should be a simple choice, as you only have one.

The Assistant will then walk you through a number of questions. When choosing the Layout, you should select "Selected Record" and "Matching Records" for the configuration.

What you are actually doing is creating a simple page to Search, Edit, Create, and Delete information stored in the User table of the database. You will then be prompted through additional configuration options. When you have walked through the steps, click Finished.

Feel free to do this part of the article a few times, each time selecting different options. It should prove insightful.

Now you are going to add a hyperlink on your Main page and link it to the new UserGroup page. This will allow you to add new Users to the database. You would obviously only use this UserGroup page as an administrator of your site, as it's not restricted in what it can accomplish.

When you created the UserGroup page, a WODisplayGroup was created and configured for you. WebObjects provides a class called WODisplayGroup, which is used in the UserGroup page. You are going to use a WODisplayGroup for your Main page, as well. My feelings on WODisplyGroup? "There's gold in them thar hills."

There are three ways to get a WODisplayGroup into an already existing page. You can add it by:

  1. Writing the code directly in the .java file.
  2. Adding a Key in WOBuilder.
  3. Dragging the Entity from EOModeler to WOBuilder.

I typically use method (2) or (3). For this article, you'll use option (3), since it's the most fun.

From the EOModel window, click and drag your User Entity to the WOBuilder window for your Main page. You will be prompted to "Add & Configure" the new DisplayGroup being created. Name the DisplayGroup userDisplayGroup. The default configuration will be fine.

You can now bind the username and password WOTextFields in your Login panel to the userDisplayGroup. For the username, you're going to traverse the hierarchy in the Object Browser to userDisplayGroup>queryMatch>username. You will do the same for the password.

Next, you should bind the Submit button. Connect it to the userDisplayGroup>qualifyDataSource method. When a user clicks Submit, the WODisplayGroup will use the supplied username/password to try to find a match in the database.

Next, add a WOHyperlink to the page. Name it "Create" and bind it to the UserGroup page. You can accomplish this by using the pop-up menu to the right of the pageName key in the Inspector Panel.

On the Main.wo page, add a couple of WOConditional elements. You'll notice they have a plus sign on them. Click on one of the plus signs and change it to a minus sign. Inside of the WOConditional with the plus, type "Please log in..." In the one with the minus, type "Thank you, " and then add a WOString . Bind this WOString to userDisplayGroup>selectedObject>username. Your completed Login panel should look similar to this:

Now that you've taken advantage of Apple's generosity, you can greatly reduce the amount of code you need to maintain. Your entire Main.java file can now look like this:


import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;
import com.webobjects.eoaccess.*;

public class Main extends WOComponent {
    public WODisplayGroup userDisplayGroup;

    public Main(WOContext context) {
        super(context);
    }

}

Go ahead and "Build and Run" your application. Initially, you won't have any data in your database. In order to insert some data, you will use the "Create" hyperlink. The UserGroup page will allow you to create a new user and save the information appropriately.

Since you are now connected to a database, the information is persistent. Try stopping your application and running it again. Try using the Login panel. The information you've entered for users should be the same as it was when you stopped.

Apple provides sample code with WebObjects for creating a Login Panel using the Basic Authentication protocol. It is up to you (or the boss man) to decide whether Basic Authentication protocol is acceptable for your project.

Parting Thought

As I mentioned earlier, the Apple Music Store is a WebObjects application. When you access the Apple Music Store, you'll notice that you can find out what music other people are buying, whether it's through the Top 10 list or the "...other people bought" suggestion. Of course, you can also do a simple search. Obviously, Apple isn't guessing at this information, so you can conclude they're keeping the information in a persistent store: a database. The WebObjects set of frameworks makes all of this possible.

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