Changes for page Your First Rest Project
Last modified by Steve Peery on 2013/09/06 11:02
From version 23.1
edited by Filippo Laurìa
on 2013/07/22 12:41
on 2013/07/22 12:41
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. filippolauria1 +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,12 +185,8 @@ 185 185 186 186 If you did everything well, the list of attributes should look like this: 187 187 188 -[[image:attach:list_wlock.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 -[[image:attach:list.png]] 193 - 194 194 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: 195 195 196 196 |=((( ... ... @@ -224,8 +224,6 @@ 224 224 225 225 Final list of attributes should look like this: 226 226 227 - 228 - 229 229 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: 230 230 231 231 If you check in the **Outline** tab, you should see that **Author** now have a **blogEntries** relationship, and **BlogEntry** have a **author** relationship. ... ... @@ -237,6 +237,7 @@ 237 237 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: 238 238 239 239 {{code}} 230 + 240 240 public String fullName() { 241 241 return this.firstName() + " " + this.lastName(); 242 242 } ... ... @@ -246,6 +246,7 @@ 246 246 Nothing fancy here. Now open **BlogEntry.java** and add the following method: 247 247 248 248 {{code}} 240 + 249 249 @Override 250 250 public void awakeFromInsertion(EOEditingContext editingContext) { 251 251 super.awakeFromInsertion(editingContext); ... ... @@ -275,6 +275,7 @@ 275 275 Remove the pound char in front of those two properties: 276 276 277 277 {{code}} 270 + 278 278 #er.migration.migrateAtStartup=true 279 279 #er.migration.createTablesIfNecessary=true 280 280 ... ... @@ -283,6 +283,7 @@ 283 283 After removing the pound char, the two properties should look like this: 284 284 285 285 {{code}} 279 + 286 286 er.migration.migrateAtStartup=true 287 287 er.migration.createTablesIfNecessary=true 288 288 ... ... @@ -291,6 +291,7 @@ 291 291 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: 292 292 293 293 {{code}} 288 + 294 294 BlogRest[62990] INFO er.extensions.migration.ERXMigrator - Upgrading BlogModel to version 0 with migration 'your.app.model.migrations.BlogModel0@4743bf3d' 295 295 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) 296 296 BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing ALTER TABLE Author ADD PRIMARY KEY (id) ... ... @@ -346,6 +346,7 @@ 346 346 Add this method in **BlogEntryController**: 347 347 348 348 {{code}} 344 + 349 349 protected ERXKeyFilter filter() { 350 350 ERXKeyFilter personFilter = ERXKeyFilter.filterWithAttributes(); 351 351 personFilter.setAnonymousUpdateEnabled(true); ... ... @@ -362,6 +362,7 @@ 362 362 Now, let's implement the **createAction** method: 363 363 364 364 {{code}} 361 + 365 365 public WOActionResults createAction() throws Throwable { 366 366 BlogEntry entry = create(filter()); 367 367 editingContext().saveChanges(); ... ... @@ -375,6 +375,7 @@ 375 375 Last step in the controller: implementing the **indexAction** method. Again, the code is simple: 376 376 377 377 {{code}} 375 + 378 378 public WOActionResults indexAction() throws Throwable { 379 379 NSArray<BlogEntry> entries = BlogEntry.fetchAllBlogEntries(editingContext()); 380 380 return response(entries, filter()); ... ... @@ -391,6 +391,7 @@ 391 391 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: 392 392 393 393 {{code}} 392 + 394 394 ERXRouteRequestHandler restRequestHandler = new ERXRouteRequestHandler(); 395 395 restRequestHandler.addDefaultRoutes(BlogEntry.ENTITY_NAME); 396 396 ERXRouteRequestHandler.register(restRequestHandler); ... ... @@ -400,7 +400,7 @@ 400 400 401 401 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. 402 402 403 -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"]] 404 404 405 405 == Adding posts and authors with curl == 406 406 ... ... @@ -415,6 +415,7 @@ 415 415 The response should look this: 416 416 417 417 {{code}} 417 + 418 418 HTTP/1.0 201 Apple WebObjects 419 419 Content-Length: 249 420 420 x-webobjects-loadaverage: 0 ... ... @@ -427,6 +427,7 @@ 427 427 To get a list of blog entries: 428 428 429 429 {{code}} 430 + 430 430 curl -X GET http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.json 431 431 432 432 {{/code}} ... ... @@ -438,6 +438,7 @@ 438 438 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: 439 439 440 440 {{code}} 442 + 441 441 @Override 442 442 protected boolean isAutomaticHtmlRoutingEnabled() { 443 443 return true; ... ... @@ -450,6 +450,7 @@ 450 450 The next step to get it to work is to make **BlogEntryIndexPage** to implement the **er.rest.routes.IERXRouteComponent** interface. 451 451 452 452 {{code}} 455 + 453 453 import er.rest.routes.IERXRouteComponent; 454 454 455 455 public class BlogEntryIndexPage extends WOComponent implements IERXRouteComponent { ... ... @@ -459,6 +459,7 @@ 459 459 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: 460 460 461 461 {{code}} 465 + 462 462 public NSArray<BlogEntry> entries() { 463 463 EOEditingContext ec = ERXEC.newEditingContext(); 464 464 return BlogEntry.fetchAllBlogEntries(ec, BlogEntry.CREATION_DATE.descs()); ... ... @@ -469,6 +469,7 @@ 469 469 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**: 470 470 471 471 {{code}} 476 + 472 472 private BlogEntry entryItem; 473 473 474 474 public BlogEntry entryItem() { ... ... @@ -484,6 +484,7 @@ 484 484 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: 485 485 486 486 {{code}} 492 + 487 487 <wo:loop list="$entries" item="$entryItem"> 488 488 <p><wo:str value="$entryItem.title" /></p> 489 489 <p><wo:str value="$entryItem.author.fullName" /></p> ... ... @@ -500,6 +500,7 @@ 500 500 Open **BlogEntryShowPage.java** and make sure the class implements **er.rest.routes.IERXRouteComponent**. 501 501 502 502 {{code}} 509 + 503 503 import er.rest.routes.IERXRouteComponent; 504 504 505 505 public class BlogEntryShowPage extends WOComponent implements IERXRouteComponent { ... ... @@ -509,6 +509,7 @@ 509 509 We need to add other methods to receive the BlogEntry object from the controller. In **BlogEntryShowPage.java**, add: 510 510 511 511 {{code}} 519 + 512 512 private BlogEntry blogEntry; 513 513 514 514 @ERXRouteParameter ... ... @@ -527,6 +527,7 @@ 527 527 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: 528 528 529 529 {{code}} 538 + 530 530 <h1><wo:str value="$blogEntry.title" /></h1> 531 531 <p><wo:str value="$blogEntry.content" /></p> 532 532 <p>Created on: <wo:str value="$blogEntry.creationDate" dateformat="%Y/%m/%d" /></p> ... ... @@ -537,6 +537,7 @@ 537 537 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: 538 538 539 539 {{code}} 549 + 540 540 <p><wo:str value="$entryItem.title" /></p> 541 541 542 542 {{/code}} ... ... @@ -544,6 +544,7 @@ 544 544 with: 545 545 546 546 {{code}} 557 + 547 547 <p><wo:ERXRouteLink entityName="BlogEntry" record="$entryItem" action="show"><wo:str value="$entryItem.title" /></wo:ERXRouteLink></p> 548 548 549 549 {{/code}}