Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Nothing fancy here. Now open BlogEntry.java and add the following method:

Code Block
	@Override
	public void awakeFromInsertion(EOEditingContext editingContext) {
	  super.awakeFromInsertion(editingContext);
	  this.setCreationDate(new NSTimestamp());
	}

...

Info

You can shorten the URL by using mod_rewrite in Apache httpd

...

Creating controllers

ERRest needs controllers to act as a broker between working with the objects and the routes. So let's create a controller for BlogEntry.

...

Add this method in BlogEntryController:

Code Block
  protected ERXKeyFilter filter() {
    ERXKeyFilter personFilter = ERXKeyFilter.filterWithAttributes();
    personFilter.setAnonymousUpdateEnabled(true);

    ERXKeyFilter filter = ERXKeyFilter.filterWithAttributes();
    filter.include(BlogEntry.AUTHOR, personFilter);
    filter.setUnknownKeyIgnored(true);

    return filter;
  }

Now, let's implement the creationAction method:

Code Block
  public WOActionResults createAction() throws Throwable {
    BlogEntry entry = create(filter());
    editingContext().saveChanges();
    return response(entry, filter());
  }

...

Last step in the controller: implementing the showAction method. Again, the code is simple:

Code Block
  public WOActionResults indexAction() throws Throwable {
    NSArray<BlogEntry> entries = BlogEntry.fetchAllBlogEntries(editingContext());
    return response(entries, filter());
  }

...

A route in ERRest is simply a way to define the URL for the entities and to specify which controller the route should use. When your controller extends from ERXDefaultRouteController, it's easy to register a controller and a route. In Application.java, in the Application constructor, add the following code:

Code Block
    ERXRouteRequestHandler restRequestHandler = new ERXRouteRequestHandler();
    restRequestHandler.addDefaultRoutes(BlogEntry.ENTITY_NAME);
    ERXRouteRequestHandler.register(restRequestHandler);
    setDefaultRequestHandler(restRequestHandler);

...

We are now reading to add and list blog postings! Start the application and take notice of the URL. It should be something like _http://yourip:someport/cgi-bin/WebObjects/BlogRest.woa_Image Removed

Adding posts and authors with curl

...

To create a blog entry, you need to use the POST HTTP method. We will use JSON as the format since it's a bit less chatty than XML. So if the URL to the application is http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa_Image Removed, the full _curl command will be:

...

Code Block
curl -X GET http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.json

You can stop the application and proceed to the next step.

Adding HTML views for blog posts

Now, let's build a HTML view for blog posts (you don't want your readers to get your posts by JSON, right?). Again, we will use convention to make it work easily. Open up BlogEntryController and add the following method:

Code Block

  @Override
  protected boolean isAutomaticHtmlRoutingEnabled() {
    return true;
  }

Switching the return value of this method says that we will follow a certain convention for HTML components. The convention for automatic HTML routing is that the component should be named <EntityName><Action>Page.wo. So in our case, the component will be BlogEntryIndexPage. Right-click on the project name in Eclipse and select New -> WOComponent. Change the name to BlogEntryIndexPage and check the Create HTML contents button. Click Finish.

The next step to get it to work is to make BlogEntryIndexPage to implements the er.rest.routes.IERXRouteComponent interface.

Code Block

import er.rest.routes.IERXRouteComponent;

public class BlogEntryIndexPage extends WOComponent implements IERXRouteComponent {

So now, the automatic HTML routing will send the request for ra/blogEntries.html to the BlogEntryIndexPage component. But we don't have any content in this component, so let's make a method to fetch all blog entries per creation date in descending order. So in BlogEntryIndexPage.java, add the following method:

Code Block

    public NSArray<BlogEntry> entries() {
      EOEditingContext ec = ERXEC.newEditingContext();
      return BlogEntry.fetchAllBlogEntries(ec, BlogEntry.CREATION_DATE.descs());
    }

We need to use that method in a WORepetition, and for that loop, we need a BlogEntry variable to iterate in the list, so add the following code to BlogEntryIndexPage.java:

Code Block

    private BlogEntry entryItem;

    public BlogEntry entryItem() {
      return entryItem;
    }
    
    public void setEntryItem(BlogEntry entryItem) {
      this.entryItem = entryItem;
    }

The Java part is done, so let's add the loop inside the component. Open BlogEntryIndexPage.wo (it's located in the Component folder) and right after the <body> tag, add:

Code Block

    <wo:loop list="$entries" item="$entryItem">
      <p><wo:str value="$entryItem.title" /></p>
      <p><wo:str value="$entryItem.author.fullName" /></p>
    </wo:loop>

That component code will loop over the blog entries and display the title of the entry + the name of the author. Save everything and run the application.

If you go to _http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.html_Image Added, you will see the list of blog entries!