Changes for page Your First Rest Project

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

From version 22.1
edited by Filippo Laurìa
on 2013/07/22 12:39
Change comment: There is no comment for this version
To version 19.1
edited by skcodes
on 2013/05/13 12:53
Change comment: Migrated to Confluence 4.0

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.filippolauria
1 +XWiki.skcodes
Content
... ... @@ -1,5 +3,3 @@
1 -
2 -
3 3  {{toc/}}
4 4  
5 5  = Introduction =
... ... @@ -122,8 +122,6 @@
122 122  
123 123  The model should show up in a window that looks like this:
124 124  
125 -[[image:attach:EOModeler.png]]
126 -
127 127  If it didn't show up, the window might have opened behind the main Eclipse window. If that's the case, open the **Window** menu and select the windows that have //Entity Modeler// in its name.
128 128  
129 129  In the Entity Modeler window, click on **Default**, and for the **URL** field, type
... ... @@ -185,8 +185,6 @@
185 185  
186 186  If you did everything well, the list of attributes should look like this:
187 187  
188 -[[image:attach:list.png]]
189 -
190 190  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:
191 191  
192 192  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:
... ... @@ -222,8 +222,6 @@
222 222  
223 223  Final list of attributes should look like this:
224 224  
225 -[[image:attach:list.png]]
226 -
227 227  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:
228 228  
229 229  If you check in the **Outline** tab, you should see that **Author** now have a **blogEntries** relationship, and **BlogEntry** have a **author** relationship.
... ... @@ -235,6 +235,7 @@
235 235  What we are going to do is to write a simple method that returns the full name of an author, e.g. a method that simply concatenate the first name, a space and the last name of the author. To do so, double-click on **Author.java** and add the following methods:
236 236  
237 237  {{code}}
230 +
238 238  public String fullName() {
239 239   return this.firstName() + " " + this.lastName();
240 240   }
... ... @@ -244,6 +244,7 @@
244 244  Nothing fancy here. Now open **BlogEntry.java** and add the following method:
245 245  
246 246  {{code}}
240 +
247 247   @Override
248 248   public void awakeFromInsertion(EOEditingContext editingContext) {
249 249   super.awakeFromInsertion(editingContext);
... ... @@ -273,6 +273,7 @@
273 273  Remove the pound char in front of those two properties:
274 274  
275 275  {{code}}
270 +
276 276  #er.migration.migrateAtStartup=true
277 277  #er.migration.createTablesIfNecessary=true
278 278  
... ... @@ -281,6 +281,7 @@
281 281  After removing the pound char, the two properties should look like this:
282 282  
283 283  {{code}}
279 +
284 284  er.migration.migrateAtStartup=true
285 285  er.migration.createTablesIfNecessary=true
286 286  
... ... @@ -289,6 +289,7 @@
289 289  You are now ready to start the application so that it creates the database! To do so, right-click on **Application.java** (in the **your.app** folder) and select **Run As** -> **WOApplication**. In Eclipse's Console tab, you should see some output, including something similar to:
290 290  
291 291  {{code}}
288 +
292 292  BlogRest[62990] INFO er.extensions.migration.ERXMigrator - Upgrading BlogModel to version 0 with migration 'your.app.model.migrations.BlogModel0@4743bf3d'
293 293  BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing CREATE TABLE Author(email VARCHAR(100) NOT NULL, firstName VARCHAR(50) NOT NULL, id INTEGER NOT NULL, lastName VARCHAR(50) NOT NULL)
294 294  BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing ALTER TABLE Author ADD PRIMARY KEY (id)
... ... @@ -344,6 +344,7 @@
344 344  Add this method in **BlogEntryController**:
345 345  
346 346  {{code}}
344 +
347 347  protected ERXKeyFilter filter() {
348 348   ERXKeyFilter personFilter = ERXKeyFilter.filterWithAttributes();
349 349   personFilter.setAnonymousUpdateEnabled(true);
... ... @@ -360,6 +360,7 @@
360 360  Now, let's implement the **createAction** method:
361 361  
362 362  {{code}}
361 +
363 363  public WOActionResults createAction() throws Throwable {
364 364   BlogEntry entry = create(filter());
365 365   editingContext().saveChanges();
... ... @@ -373,6 +373,7 @@
373 373  Last step in the controller: implementing the **indexAction** method. Again, the code is simple:
374 374  
375 375  {{code}}
375 +
376 376  public WOActionResults indexAction() throws Throwable {
377 377   NSArray<BlogEntry> entries = BlogEntry.fetchAllBlogEntries(editingContext());
378 378   return response(entries, filter());
... ... @@ -389,6 +389,7 @@
389 389  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:
390 390  
391 391  {{code}}
392 +
392 392  ERXRouteRequestHandler restRequestHandler = new ERXRouteRequestHandler();
393 393   restRequestHandler.addDefaultRoutes(BlogEntry.ENTITY_NAME);
394 394   ERXRouteRequestHandler.register(restRequestHandler);
... ... @@ -398,7 +398,7 @@
398 398  
399 399  The **addDefaultRoutes** method do all of the required magic, and use convention. That's why we had to name the controller **BlogEntryController**, because the convention is <EntityName>Controller.
400 400  
401 -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_>>url:http://youripsomeport||shape="rect"]]
402 +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_>>url:http://yourip:someport/cgi-bin/WebObjects/BlogRest.woa_||shape="rect"]]
402 402  
403 403  == Adding posts and authors with curl ==
404 404  
... ... @@ -413,6 +413,7 @@
413 413  The response should look this:
414 414  
415 415  {{code}}
417 +
416 416  HTTP/1.0 201 Apple WebObjects
417 417  Content-Length: 249
418 418  x-webobjects-loadaverage: 0
... ... @@ -425,6 +425,7 @@
425 425  To get a list of blog entries:
426 426  
427 427  {{code}}
430 +
428 428  curl -X GET http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.json
429 429  
430 430  {{/code}}
... ... @@ -436,6 +436,7 @@
436 436  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:
437 437  
438 438  {{code}}
442 +
439 439  @Override
440 440   protected boolean isAutomaticHtmlRoutingEnabled() {
441 441   return true;
... ... @@ -448,6 +448,7 @@
448 448  The next step to get it to work is to make **BlogEntryIndexPage** to implement the **er.rest.routes.IERXRouteComponent** interface.
449 449  
450 450  {{code}}
455 +
451 451  import er.rest.routes.IERXRouteComponent;
452 452  
453 453  public class BlogEntryIndexPage extends WOComponent implements IERXRouteComponent {
... ... @@ -457,6 +457,7 @@
457 457  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:
458 458  
459 459  {{code}}
465 +
460 460  public NSArray<BlogEntry> entries() {
461 461   EOEditingContext ec = ERXEC.newEditingContext();
462 462   return BlogEntry.fetchAllBlogEntries(ec, BlogEntry.CREATION_DATE.descs());
... ... @@ -467,6 +467,7 @@
467 467  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**:
468 468  
469 469  {{code}}
476 +
470 470  private BlogEntry entryItem;
471 471  
472 472   public BlogEntry entryItem() {
... ... @@ -482,6 +482,7 @@
482 482  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:
483 483  
484 484  {{code}}
492 +
485 485  <wo:loop list="$entries" item="$entryItem">
486 486   <p><wo:str value="$entryItem.title" /></p>
487 487   <p><wo:str value="$entryItem.author.fullName" /></p>
... ... @@ -498,6 +498,7 @@
498 498  Open **BlogEntryShowPage.java** and make sure the class implements **er.rest.routes.IERXRouteComponent**.
499 499  
500 500  {{code}}
509 +
501 501  import er.rest.routes.IERXRouteComponent;
502 502  
503 503  public class BlogEntryShowPage extends WOComponent implements IERXRouteComponent {
... ... @@ -507,6 +507,7 @@
507 507  We need to add other methods to receive the BlogEntry object from the controller. In **BlogEntryShowPage.java**, add:
508 508  
509 509  {{code}}
519 +
510 510  private BlogEntry blogEntry;
511 511  
512 512   @ERXRouteParameter
... ... @@ -525,6 +525,7 @@
525 525  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:
526 526  
527 527  {{code}}
538 +
528 528  <h1><wo:str value="$blogEntry.title" /></h1>
529 529   <p><wo:str value="$blogEntry.content" /></p>
530 530   <p>Created on: <wo:str value="$blogEntry.creationDate" dateformat="%Y/%m/%d" /></p>
... ... @@ -535,6 +535,7 @@
535 535  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:
536 536  
537 537  {{code}}
549 +
538 538  <p><wo:str value="$entryItem.title" /></p>
539 539  
540 540  {{/code}}
... ... @@ -542,6 +542,7 @@
542 542  with:
543 543  
544 544  {{code}}
557 +
545 545  <p><wo:ERXRouteLink entityName="BlogEntry" record="$entryItem" action="show"><wo:str value="$entryItem.title" /></wo:ERXRouteLink></p>
546 546  
547 547  {{/code}}