Work in progress
Now that our data model is in a framework and that we have a basic REST application working, we are going to build a DirectToWeb application to manage the blog system. But first, what is DirectToWeb (D2W)? D2W is a rules-based application model where you can change the behavior of the application by rules. D2W is perfect for "admin" apps, for applications that share a common model or for "CRUD" (Create Read Update Destroy) applications.
So like I said, we are going to build a D2W app that will allow us to update and create blog entries and authors. Let's start by creating a Wonder D2W Application in Eclipse:
Name it D2WBlog. The next step is to link the BlogCommon framework to the D2W app. To do so, right-click on D2WBlog and select Build Path -> Configure Build Path.
In the Libraries tab, click on Add Library. Select WebObjects Frameworks and click Next. Check BlogCommon and H2PlugIn from the list and click Finish. The Libraries tab should look like this:
It's time to run the app. Right-click on Application.java and select Run As -> WO Application. By default, a D2W app will always display a login form and the login method is connected to a DirectAction that should handle the authentification. The default implementation of that DirectAction (the loginAction method in the DirectAction class) don't do anything special:
public WOActionResults loginAction() { String username = request().stringFormValueForKey("username"); String password = request().stringFormValueForKey("password"); return D2W.factory().defaultPage(session()); }
Just click the Login button and you will get into the application. Navigate in the application, but you will see that it don't do anything special and you don't see data. You can terminate the app (by clicking the red stop button in Eclipse's console).
The first thing we will do is to make authentification to work. We don't have a password in our data model so authentification will only be done on the email address (yes, we know it's not very secure). The first thing we need to do is to store the logged author into the session so that we can access it from all pages. To do so, open the Session class and add the following code:
import er.extensions.foundation.ERXThreadStorage; private Author _author; public Author author() { return _author; } public void setAuthor(Author author) { _author = author; ERXThreadStorage.takeValueForKey(author(), "author"); }
Open the DirectAction class and rewrite the loginAction method like this:
import er.extensions.eof.ERXEC; import er.extensions.foundation.ERXStringUtilities; import your.app.model.Author; public WOActionResults loginAction() { WOComponent nextPage = null; String email = request().stringFormValueForKey("username"); String errorMessage = null; try { Author user = Author.fetchAuthor(ERXEC.newEditingContext(), Author.EMAIL.eq(email)); ((Session) session()).setAuthor(user); nextPage = ((Session) session()).navController().homeAction(); } catch (NoSuchElementException e) { errorMessage = "No user found for that combination of username and password."; } catch (Exception e) { // TODO: handle exception } if (!ERXStringUtilities.stringIsNullOrEmpty(errorMessage)) { nextPage = pageWithName(Main.class); nextPage.takeValueForKey(errorMessage, "errorMessage"); nextPage.takeValueForKey(email, "username"); } return nextPage; }
The loginAction logic is quite simple: we check the value from the form, we check in the data source if the email address exists, if that's the case we store it in the session and we redirect the user to the default (navController().homeAction()) page, if now the user stays on the Main page.
We are done with the login logic, so we can move to the next step: working on the navigation. The navigation is quite simple: we want two tabs, Posts and Authors, when one of the two tabs is selected, we display the list of objects for the selected tabs, and we have two links: one to create a new blog entry or a new author, and the other to query (search) for matching objects.
The navigation structure is done in a "plist" file. If you are a Mac OS X guy, you probably know what is a plist, but if not, a plist is like a JSON structure to hold key/values. The plist file is located in the Resources folder, the file's name is NavigationMenu.plist. By default, Eclipse will open the file in Xcode, which might not be what we want. To open it in a text editor in Eclipse, right-click on NavigationMenu.plist and select Open With -> Text Editor.
Edit the file with the content is like this:
( { name = Root; directActionClass = DirectAction; directActionName = default; children = "session.navigationRootChoice"; childrenChoices = { home = ( Posts, Authors, ); }; }, { name = "Posts"; action = "session.navController.listPostsAction"; children = ("CreatePost","SearchPosts"); }, { name = CreatePost; action = "session.navController.createPostAction"; }, { name = SearchPosts; action = "session.navController.searchPostsAction"; }, { name = Authors; action = "session.navController.listAuthorsAction"; children = ("CreateAuthor","SearchAuthors"); }, { name = CreateAuthor; action = "session.navController.createAuthorAction"; }, { name = SearchAuthors; action = "session.navController.searchAuthorsAction"; } )
The first array defines what the top level navigation is going to be, and this is where we define the two tabs (the _childrenChoice_ dictionary). After that, we define the other parts of the navigation. You see references to action = session.navController. This is the action (method) that will be called for the specified navigation element, so let's create those methods in the MainNavigationController class.