Changes for page Your First Rest Project
Last modified by Steve Peery on 2013/09/06 11:02
From version 5.1
edited by Pascal Robert
on 2011/12/27 12:48
on 2011/12/27 12:48
Change comment:
There is no comment for this version
To version 10.1
edited by Pascal Robert
on 2011/12/27 14:28
on 2011/12/27 14:28
Change comment:
There is no comment for this version
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -43,9 +43,9 @@ 43 43 44 44 An EOModel consists of entities, attributes and relationships. When using it in a RDBMS context, an entity is a table (or a view), an attribute is a table column and a relationship is a join between two tables. 45 45 46 -To create the EOModel, in the project right-click on the project name and select **New** -> **EOModel**.46 +To create the EOModel, in the project right-click on the project name and select **New** > **EOModel**. 47 47 48 -Name it **BlogModel** and in the plugin list, select **H2**. Click **Finish**. 48 +Name it **BlogModel** and in the plugin list, select **H2**. Click **Finish**. 49 49 50 50 The model should show up in a window that looks like this: 51 51 ... ... @@ -53,7 +53,7 @@ 53 53 54 54 In the Entity Modeler window, click on **Default**, and for the **URL** field, type {{code}}jdbc:h2:~/BlogTutorial{{/code}}. When the database will be created, it will be stored in your home directory (/Users/youruser/ on OS X). 55 55 56 -Now, right-click on **BlogModel** and select **New Entity**. 56 +Now, right-click on **BlogModel** and select **New Entity**. 57 57 58 58 Type the following details in the **Basic** tab: 59 59 ... ... @@ -73,7 +73,7 @@ 73 73 74 74 Now, repeat the last two steps to create the other attributes for the **BlogEntry** entity, with the following values: 75 75 76 -|=Attribute name|=Column|=Prototype 76 +|= Attribute name |= Column |= Prototype 77 77 | content | content | longtext 78 78 | lastModified | lastModified | dateTime 79 79 | creationDate | creationDate | dateTime ... ... @@ -84,7 +84,7 @@ 84 84 85 85 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: 86 86 87 -|=Attribute name|=Column|=Prototype 87 +|= Attribute name |= Column |= Prototype 88 88 | firstName | firstName | varchar50 89 89 | lastName | lastName | varchar50 90 90 | email | email | varchar100 ... ... @@ -91,3 +91,101 @@ 91 91 | passwd | passwd | varchar16 92 92 93 93 Final list of attributes should look like this: 94 + 95 +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: 96 + 97 +If you check in the **Outline** tab, you should see that **Author** now have a **blogEntries** relationship, and **BlogEntry** have a **author** relationship. 98 + 99 +You are now ready to save the model. Save it (File > Save) and close the **Entity Modeler** window. If you open the **Sources** in the main Eclipse window, you will notice that the **Sources** folder contains a package named **your.app.model**. 100 + 101 +That package have four Java classes: **Author**, **Author**, **BlogEntry** and **BlogEntry**. Those classes were generated by Veogen, a templating engine build on Velocity. The two classes that starts with a underscore are recreated every time you change the EOModel, so if you want to change something in those classes, you need to change the template (no need for that right now). But you can change freely the two classes that don't have the underscore, and this is what we will be doing. 102 + 103 +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: 104 + 105 +{{code}} 106 + 107 +public String fullName() { 108 + return this.firstName() + " " + this.lastName(); 109 + } 110 + 111 +{{/code}} 112 + 113 +Nothing fancy here. Now, let's use migrations to actually create the database. 114 + 115 +== Using migrations == 116 + 117 +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**. 118 + 119 +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**. 120 + 121 +Type **your.app.model.migrations** as the package and **BlogModel0** as the name of the class. Click **Finish**. 122 + 123 +In the **Sources** folder, open the **your.app.model.migrations** package, a class named **BlogModel0** should be there. Delete everything in that file **EXCEPT** the first line (which should be //package your.app.model.migrations//) and paste the code that was generated by **Entity Modeler**. Save the file. 124 + 125 +One last step: migrations are disabled by default. To enable them, you need to uncomment two properties in the **Properties** file that is located in the **Resources** folder. Open that file (double-click on it). 126 + 127 +Remove the pound char in front of those two properties: 128 + 129 +{{code}} 130 + 131 +#er.migration.migrateAtStartup=true 132 +#er.migration.createTablesIfNecessary=true 133 + 134 +{{/code}} 135 + 136 +After removing the pound char, the two properties should look like this: 137 + 138 +{{code}} 139 + 140 +er.migration.migrateAtStartup=true 141 +er.migration.createTablesIfNecessary=true 142 + 143 +{{/code}} 144 + 145 +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: 146 + 147 +{{code}} 148 + 149 +BlogRest[62990] INFO er.extensions.migration.ERXMigrator - Upgrading BlogModel to version 0 with migration 'your.app.model.migrations.BlogModel0@4743bf3d' 150 +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, passwd VARCHAR(16) NOT NULL) 151 +BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing ALTER TABLE Author ADD PRIMARY KEY (id) 152 +BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing CREATE TABLE BlogEntry(authorID INTEGER NOT NULL, content TIMESTAMP NOT NULL, creationDate TIMESTAMP NOT NULL, id INTEGER NOT NULL, lastModified TIMESTAMP NOT NULL, title VARCHAR(255) NOT NULL) 153 +BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing ALTER TABLE BlogEntry ADD PRIMARY KEY (id) 154 +BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing ALTER TABLE BlogEntry ADD CONSTRAINT "FOREIGN_KEY_BLOGENTRY_AUTHORID_AUTHOR_ID" FOREIGN KEY (authorID) REFERENCES Author (id) 155 +BlogRest[62990] DEBUG NSLog - evaluateExpression: <er.h2.jdbcadaptor.ERH2PlugIn$H2Expression: "UPDATE _dbupdater SET version = ? WHERE modelname = ?" withBindings: 1:0(version), 2:"BlogModel"(modelName)> 156 + 157 +{{/code}} 158 + 159 +If you see this and that the application is running (it should open a window in your favorite browser), migration worked and your database have been created, congratulations You can now stop the application (click the square red button in Eclipse's Console tab) and continue to the next step. 160 + 161 += Creating REST controllers and routes = 162 + 163 +Project Wonder contains a framework called ERRest, which follow the same patterns as Ruby on Rails REST concepts. Using REST-style URLs is perfect for building a public blog and to create REST services to manage posting over HTTP with JSON, XML or other formats. 164 + 165 +By default, a REST route in ERRest will generate a link like this: 166 + 167 +{{code}} 168 +/cgi-bin/WebObjects/AppName.woa/ra/EntityName/id 169 +{{/code}} 170 + 171 +So for our case, to get the first blog posting from BlogRest, the URL will look like this: 172 + 173 +{{code}} 174 +/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries/1.html 175 +{{/code}} 176 + 177 +{{info}} 178 +You can shorten the URL by using mod_rewrite in Apache httpd 179 +{{/info}} 180 + 181 +. 182 + 183 +== Creating controllers == 184 + 185 +ERRest needs controllers to act as a broker between working with the objects and the routes. So let's create a controller for BlogEntry. 186 + 187 +== Adding the routes == 188 + 189 +== Adding posts and authors with curl == 190 + 191 +== Adding HTML views for blog posts ==