Changes for page Your First Rest Project

Last modified by Steve Peery on 2013/09/06 11:02

From version 31.1
edited by Pascal Robert
on 2011/12/27 23:07
Change comment: There is no comment for this version
To version 36.1
edited by pauldlynch
on 2012/05/28 04:09
Change comment: Minor spelling, and make method names in text match code.

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.probert
1 +XWiki.pauldlynch
Content
... ... @@ -77,7 +77,7 @@
77 77  
78 78  If you did everything well, the list of attributes should look like this:
79 79  
80 -You will notice that the attributes have a column with a lock in it. When a lock is present, it will use the value of that attribute for //UPDATE ... WHERE attribute = ''// statement. This is to do optimistic locking, aka to prevent data conflict when the data object was modified by two different users. Using timestamps for optimistic locking is not a good idea because for certain RDBMS, the value can be different because of milliseconds, so remove the locks on the **lastModified** and **creationDate** attributes. The final list should look like this:
80 +You will notice that the attributes have a column with a lock in it. When a lock is present, it will use the value of that attribute for //UPDATE ... WHERE attribute = ''// statement. This is to do optimistic locking, aka to prevent data conflict when the data object was modified by two different users. Using timestamps for optimistic locking is not a good idea because for certain RDBMS, the value can be different because of milliseconds, so remove the locks on the **creationDate** attribute. The final list should look like this:
81 81  
82 82  Next step is to create the **Author** entity. Create a new entity with **Author** at its name (and also as the table name), and for the class name, use **your.app.model.Author**. The attributes for this entity are:
83 83  
... ... @@ -88,7 +88,7 @@
88 88  
89 89  Final list of attributes should look like this:
90 90  
91 -Now, it's time to link the two entities together. A Author can have multiple blog entries, and a BlogEntry can only have one author. To create the relationship (the join), right-click on **Author** and select **New Relationship**. On your right, select **BlogEntry** in the list. On your left, select **to many BlogEntries**, and on your left, select **to one Author**. Now, in BlogEntry, we need to store the primary key of the author so that we can make the join. The relationship builder allow us to add that attribute, so make sure **and a new foreign key named** is checked (it is checked by default). The **Create Relationship** pane should look like this:
91 +Now, it's time to link the two entities together. An Author can have multiple blog entries, and a BlogEntry can only have one author. To create the relationship (the join), right-click on **Author** and select **New Relationship**. On your right, select **BlogEntry** in the list. On your left, select **to many BlogEntries**, and on your right, select **to one Author**. Now, in BlogEntry, we need to store the primary key of the author so that we can make the join. The relationship builder allow us to add that attribute, so make sure **and a new foreign key named** is checked (it is checked by default). The **Create Relationship** pane should look like this:
92 92  
93 93  If you check in the **Outline** tab, you should see that **Author** now have a **blogEntries** relationship, and **BlogEntry** have a **author** relationship.
94 94  
... ... @@ -124,7 +124,7 @@
124 124  
125 125  == Using migrations ==
126 126  
127 -Migrations allow you to create the tables and columns (and some types of constraint). **Entity Modeler** have support to generate the code for the first migration, which is called "migration 0". To do that, open the EOModel (**BlogModel EOModel** in the **Resources** folder), right-click on the model name and select **Generate Migration**.
127 +Migrations allow you to create the tables and columns (and some types of constraint). **Entity Modeler** has support to generate the code for the first migration, which is called "migration 0". To do that, open the EOModel (**BlogModel EOModel** in the **Resources** folder), right-click on the model name and select **Generate Migration**.
128 128  
129 129  Copy the generated code in the clipboard. Close **Entity Modeler** and in the main Eclipse window, right-click on **Sources**, select **New** and select **Class**.
130 130  
... ... @@ -206,7 +206,7 @@
206 206  In Project Wonder, *Action* at the end of a method is a convention for REST and Direct Actions, when you call those methods from certain components, you don't need to add the *Action* part.
207 207  {{/info}}
208 208  
209 -For this tutorial, we will implement the **createAction** and **showAction** methods. But first, we need to create a key filter. A key filter will... filter the input and the output of REST request so that you don't have to send all attributes for a blog entry. For example, we want to show the details for an author, but we don't want to show the password for the author (in real-life, the password would be encrypted)
209 +For this tutorial, we will implement the **createAction** and **indexAction** methods. But first, we need to create a key filter. A key filter will... filter the input and the output of REST request so that you don't have to send all attributes for a blog entry. For example, we want to show the details for an author, but we don't want to show the password for the author (in real-life, the password would be encrypted)
210 210  
211 211  Add this method in **BlogEntryController**:
212 212  
... ... @@ -225,7 +225,7 @@
225 225  
226 226  {{/code}}
227 227  
228 -Now, let's implement the **creationAction** method:
228 +Now, let's implement the **createAction** method:
229 229  
230 230  {{code}}
231 231  
... ... @@ -239,7 +239,7 @@
239 239  
240 240  In 3 lines of code, you can create an object based on the request, save the new object to the database and return the new object in the response. Not bad, eh?
241 241  
242 -Last step in the controller: implementing the **showAction** method. Again, the code is simple:
242 +Last step in the controller: implementing the **indexAction** method. Again, the code is simple:
243 243  
244 244  {{code}}
245 245  
... ... @@ -310,7 +310,7 @@
310 310  
311 311  {{code}}
312 312  
313 - @Override
313 +@Override
314 314   protected boolean isAutomaticHtmlRoutingEnabled() {
315 315   return true;
316 316   }
... ... @@ -317,9 +317,9 @@
317 317  
318 318  {{/code}}
319 319  
320 -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**.
320 +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**.
321 321  
322 -The next step to get it to work is to make **BlogEntryIndexPage** to implements the **er.rest.routes.IERXRouteComponent** interface.
322 +The next step to get it to work is to make **BlogEntryIndexPage** to implement the **er.rest.routes.IERXRouteComponent** interface.
323 323  
324 324  {{code}}
325 325  
... ... @@ -333,7 +333,7 @@
333 333  
334 334  {{code}}
335 335  
336 - public NSArray<BlogEntry> entries() {
336 +public NSArray<BlogEntry> entries() {
337 337   EOEditingContext ec = ERXEC.newEditingContext();
338 338   return BlogEntry.fetchAllBlogEntries(ec, BlogEntry.CREATION_DATE.descs());
339 339   }
... ... @@ -344,12 +344,12 @@
344 344  
345 345  {{code}}
346 346  
347 - private BlogEntry entryItem;
347 +private BlogEntry entryItem;
348 348  
349 349   public BlogEntry entryItem() {
350 350   return entryItem;
351 351   }
352 -
352 +
353 353   public void setEntryItem(BlogEntry entryItem) {
354 354   this.entryItem = entryItem;
355 355   }
... ... @@ -360,7 +360,7 @@
360 360  
361 361  {{code}}
362 362  
363 - <wo:loop list="$entries" item="$entryItem">
363 +<wo:loop list="$entries" item="$entryItem">
364 364   <p><wo:str value="$entryItem.title" /></p>
365 365   <p><wo:str value="$entryItem.author.fullName" /></p>
366 366   </wo:loop>
... ... @@ -369,4 +369,66 @@
369 369  
370 370  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.
371 371  
372 -If you go to //http:~/~/192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.html_, you will see the list of blog entries!//
372 +If you go to [[http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.html]], you will see the list of blog entries
373 +
374 +Now that we have a list of blog entries, let's make a page to show the content of a blog entry. Create a new component named **BlogEntryShowPage**.
375 +
376 +Open **BlogEntryShowPage.java** and make sure the class implements&nbsp;**er.rest.routes.IERXRouteComponent**.
377 +
378 +{{code}}
379 +
380 +import er.rest.routes.IERXRouteComponent;
381 +
382 +public class BlogEntryShowPage extends WOComponent implements IERXRouteComponent {
383 +
384 +{{/code}}
385 +
386 +We need to add other methods to receive the BlogEntry object from the controller. In **BlogEntryShowPage.java**, add:
387 +
388 +{{code}}
389 +
390 +private BlogEntry blogEntry;
391 +
392 + @ERXRouteParameter
393 + public void setBlogEntry(BlogEntry blogEntryFromController) {
394 + this.blogEntry = blogEntryFromController;
395 + }
396 +
397 + public BlogEntry blogEntry() {
398 + return this.blogEntry;
399 + }
400 +
401 +{{/code}}
402 +
403 +The **@ERXRouteParameter** annotation tells the REST framework that it can automatically receive an object from the controller. And again, it's convention at work. You have to use the annotation and the setter name should be //set<EntityName>//, so for a BlogEntry, it's //setBlogEntry//, for a Author, it will be //setAuthor//.
404 +
405 +The Java part of the work is done, so save the Java class. It's time to work on the component part. Open **BlogEntryShowPage.wo** and between the <body></body> part, add:
406 +
407 +{{code}}
408 +
409 +<h1><wo:str value="$blogEntry.title" /></h1>
410 + <p><wo:str value="$blogEntry.content" /></p>
411 + <p>Created on: <wo:str value="$blogEntry.creationDate" dateformat="%Y/%m/%d" /></p>
412 + <p>Added by: <wo:str value="$blogEntry.author.fullName" /></p>
413 +
414 +{{/code}}
415 +
416 +Our view component is done, the only thing remaining is a link for the blog entry list (BlogEntryIndexPage) to the view page (BlogEntryShowPage). Save **BlogEntryShowPage.wo** and open **BlogEntryIndexPage.wo**. We are going to add a link on the title, you will replace to replace this:
417 +
418 +{{code}}
419 +
420 +<p><wo:str value="$entryItem.title" /></p>
421 +
422 +{{/code}}
423 +
424 +with:
425 +
426 +{{code}}
427 +
428 +<p><wo:ERXRouteLink entityName="BlogEntry" record="$entryItem" action="show"><wo:str value="$entryItem.title" /></wo:ERXRouteLink></p>
429 +
430 +{{/code}}
431 +
432 +Save the component and run the app. Go to [[http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.html]] to get the list of posts, and you should see a link on the title. Click on it, and now you get the full details of the blog entry
433 +
434 +The REST part of this tutorial is now complete, you can now switch to the next part of the tutorial.