Last modified by Bastian Triller on 2021/08/07 03:59

From version 12.1
edited by Pascal Robert
on 2012/08/09 04:59
Change comment: There is no comment for this version
To version 11.1
edited by Pascal Robert
on 2012/08/09 04:05
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -160,7 +160,7 @@
160 160  import er.extensions.eof.ERXEC;
161 161  
162 162  public class AdminMainPage extends ERXComponent {
163 -
163 +
164 164   private ERXBatchingDisplayGroup<BlogEntry> _dg;
165 165  
166 166   public AdminMainPage(WOContext context) {
... ... @@ -171,42 +171,42 @@
171 171   _dg.setDataSource(dataSource);
172 172   _dg.setObjectArray(BlogEntry.fetchBlogEntries(editingContext(), BlogEntry.AUTHOR.eq(session().loggedAuthor()), BlogEntry.LAST_MODIFIED.descs()));
173 173   }
174 -
174 +
175 175   public ERXBatchingDisplayGroup<BlogEntry> displayGroup() {
176 176   return this._dg;
177 177   }
178 -
178 +
179 179   private String _emailAddress;
180 -
180 +
181 181   public String emailAddress() {
182 182   return this._emailAddress;
183 183   }
184 -
184 +
185 185   public void setEmailAddress(String emailAddress) {
186 186   this._emailAddress = emailAddress;
187 187   }
188 -
188 +
189 189   private BlogEntry _blogEntryItem;
190 190  
191 191   public void setBlogEntryItem(BlogEntry blogEntryItem) {
192 192   this._blogEntryItem = blogEntryItem;
193 193   }
194 -
194 +
195 195   public BlogEntry blogEntryItem() {
196 196   return this._blogEntryItem;
197 197   }
198 -
198 +
199 199   @Override
200 200   public Session session() {
201 201   return ((Session)super.session());
202 202   }
203 -
203 +
204 204   public boolean isLogged() {
205 205   return ((session()).loggedAuthor() == null) ? false: true;
206 206   }
207 -
207 +
208 208   private EOEditingContext _ec;
209 -
209 +
210 210   public EOEditingContext editingContext() {
211 211   if (_ec == null) {
212 212   _ec = ERXEC.newEditingContext();
... ... @@ -213,13 +213,13 @@
213 213   }
214 214   return _ec;
215 215   }
216 -
216 +
217 217   private String _errorMessage = null;
218 -
218 +
219 219   public String errorMessage() {
220 220   return this._errorMessage;
221 221   }
222 -
222 +
223 223   public WOActionResults login() {
224 224   Author loggedAuthor = Author.validateLogin(editingContext(), _emailAddress);
225 225   if (loggedAuthor != null) {
... ... @@ -228,7 +228,7 @@
228 228   _errorMessage = "Invalid email address";
229 229   }
230 230   return null;
231 - }
231 + }
232 232  }
233 233  
234 234  {{/code}}
... ... @@ -239,6 +239,7 @@
239 239  
240 240  {{code}}
241 241  
242 +
242 242   <wo:AjaxUpdateContainer id="main">
243 243   <wo:if condition="$isLogged">
244 244   <wo:if condition="$displayGroup.hasMultipleBatches">
... ... @@ -252,24 +252,16 @@
252 252   <wo:link action="$displayGroup.displayNextBatch">Next</wo:link>
253 253   </div>
254 254   </wo:if>
255 - <table>
256 - <tr>
257 - <th><wo:WOSortOrder displayGroup="$displayGroup" key="title" /> Title</th>
258 - <th>Author</th>
259 - <th><wo:WOSortOrder displayGroup="$displayGroup" key="creationDate" /> Created on</th>
260 - <th><wo:WOSortOrder displayGroup="$displayGroup" key="lastModified" /> Last modified</th>
261 - </tr>
262 - <wo:loop list="$displayGroup.displayedObjects" item="$blogEntryItem">
263 - <tr>
264 - <td>
265 - <wo:str value="$blogEntryItem.title" />
266 - </td>
267 - <td> <wo:str value="$blogEntryItem.author.fullName" /> </td>
268 - <td> <wo:str value="$blogEntryItem.creationDate" dateformat="%Y/%m/%d" /> </td>
269 - <td> <wo:str value="$blogEntryItem.lastModified" dateformat="%Y/%m/%d" /> </td>
270 - </tr>
271 - </wo:loop>
272 - </table>
256 + <wo:loop list="$displayGroup.displayedObjects" item="$blogEntryItem">
257 + <p><wo:str value="$blogEntryItem.title" /></p>
258 + <p><wo:str value="$blogEntryItem.content" escapeHTML="false" /></p>
259 + <p>Created by
260 + <wo:str value="$blogEntryItem.author.fullName" />
261 + on
262 + <wo:str value="$blogEntryItem.creationDate" />
263 + </p>
264 + <hr />
265 + </wo:loop>
273 273   </wo:if>
274 274   <wo:else>
275 275   <wo:if condition="$errorMessage">
... ... @@ -291,7 +291,7 @@
291 291  
292 292  {{code}}
293 293  
294 -<wo:link pageName="AdminMainPage">Admin</wo:link>
287 + <wo:link pageName="AdminMainPage">Admin</wo:link>
295 295  
296 296  {{/code}}
297 297  
... ... @@ -298,128 +298,3 @@
298 298  Save everything, run the app and try to login. If login is not successful, you will get an error message. If login is valid, you will see the blog entries that you created.
299 299  
300 300  For the last part of this tutorial, we are going to add a link on each blog entry in the list that will bring us to a edit page where we can modify a blog entry. We are also going to add a link to create a new blog entry.
301 -
302 -Create a new component, and name it **EditBlogEntry**. Open **EditBlogEntry.java** and override the code with:
303 -
304 -{{code}}
305 -
306 -package your.app.components;
307 -
308 -import your.app.Session;
309 -import your.app.model.Author;
310 -import your.app.model.BlogEntry;
311 -
312 -import com.webobjects.appserver.WOActionResults;
313 -import com.webobjects.appserver.WOContext;
314 -import com.webobjects.eocontrol.EOEditingContext;
315 -
316 -import er.extensions.components.ERXComponent;
317 -import er.extensions.eof.ERXEC;
318 -import er.extensions.eof.ERXEOControlUtilities;
319 -
320 -public class EditBlogEntry extends ERXComponent {
321 -
322 - public EditBlogEntry(WOContext context) {
323 - super(context);
324 - }
325 -
326 - private BlogEntry _blogEntry;
327 -
328 - public BlogEntry blogEntry() {
329 - return this._blogEntry;
330 - }
331 -
332 - public void setBlogEntry(BlogEntry blogEntry) {
333 - if (blogEntry == null) {
334 - this._blogEntry = ERXEOControlUtilities.createAndInsertObject(editingContext(), BlogEntry.class);
335 - Author localUser = ERXEOControlUtilities.localInstanceOfObject(editingContext(), session().loggedAuthor());
336 - this._blogEntry.setAuthorRelationship(localUser);
337 - } else {
338 - this._blogEntry = ERXEOControlUtilities.localInstanceOfObject(editingContext(), blogEntry);
339 - }
340 - }
341 -
342 - private EOEditingContext _ec;
343 -
344 - public EOEditingContext editingContext() {
345 - if (_ec == null) {
346 - _ec = ERXEC.newEditingContext();
347 - }
348 - return _ec;
349 - }
350 -
351 - @Override
352 - public Session session() {
353 - return ((Session)super.session());
354 - }
355 -
356 - public WOActionResults save() {
357 - editingContext().saveChanges();
358 - return pageWithName(AdminMainPage.class);
359 - }
360 -}
361 -
362 -{{/code}}
363 -
364 -Open **EditBlogEntry.wo** and between the <body> tag, add the following:
365 -
366 -{{code}}
367 -
368 - <wo:form>
369 - <div>
370 - <label>Title:</label>
371 - <wo:textfield value="$blogEntry.title" />
372 - </div>
373 - <div>
374 - <label>Content:</label>
375 - <wo:text value="$blogEntry.content" rows="20" cols="80" />
376 - </div>
377 - <div>Author: <wo:str value="$session.loggedAuthor.fullName" /></div>
378 - <div><wo:submitButton action="$save" value="Save changes" /></div>
379 - </wo:form>
380 -
381 -{{/code}}
382 -
383 -We now have a form to edit or create a blog entry. Save the component and the Java class, and open **AdminMainPage.java** to add the following code:
384 -
385 -{{code}}
386 -
387 - public WOActionResults editBlogEntry() {
388 - EditBlogEntry nextPage = pageWithName(EditBlogEntry.class);
389 - nextPage.setBlogEntry(_blogEntryItem);
390 - return nextPage;
391 - }
392 -
393 - public WOActionResults createBlogEntry() {
394 - EditBlogEntry nextPage = pageWithName(EditBlogEntry.class);
395 - nextPage.setBlogEntry(null);
396 - return nextPage;
397 - }
398 -
399 -{{/code}}
400 -
401 -Open **AdminMainPage.wo** and just after <wo:if condition="$isLogged">, add the following line:
402 -
403 -{{code}}
404 -
405 -<div><wo:link action="$createBlogEntry">Create a new blog entry</wo:link></div>
406 -
407 -{{/code}}
408 -
409 -Find this line:
410 -
411 -{{code}}
412 -
413 -<wo:str value="$blogEntryItem.title" />
414 -
415 -{{/code}}
416 -
417 -and replace it with:
418 -
419 -{{code}}
420 -
421 -<wo:link action="$editBlogEntry"><wo:str value="$blogEntryItem.title" /></wo:link>
422 -
423 -{{/code}}
424 -
425 -Save everything, run the app, click on the "admin" link, login and check if you can create or edit a blog entry. Everything should be working, and just created your first stateful Project Wonder application!