KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cayenne > tutorial > tapestry > pages > AddArtistPage


1 package cayenne.tutorial.tapestry.pages;
2
3 import org.apache.tapestry.IRequestCycle;
4 import org.apache.tapestry.event.PageEvent;
5 import org.objectstyle.cayenne.access.DataContext;
6
7 import cayenne.tutorial.tapestry.domain.Artist;
8
9 /**
10  * A page to create a new Artist and save him/her in the database.
11  *
12  * @author Eric Schneider
13  */

14 public abstract class AddArtistPage extends EditorPage {
15
16     // properties are defined as abstract setters and getters
17
// and are declared in AddArtistPage.page file
18
public abstract void setArtist(Artist value);
19     public abstract Artist getArtist();
20
21     public void saveArtistAction(IRequestCycle cycle) {
22         DataContext context = getVisitDataContext();
23
24         // obtain local reference to artist abstract property
25
Artist artist = getArtist();
26
27         if (!assertNotNull(artist.getArtistName())) {
28             appendHtmlToErrorMessage("You must provide a name.");
29         }
30
31         if (!assertNotNull(artist.getDateOfBirth())) {
32             appendHtmlToErrorMessage("You must provide a DOB.");
33         }
34
35         if (getHasErrorMessage()) {
36             return;
37         }
38
39         // since new artist wasn't registered, register it before save
40
context.registerNewObject(artist);
41
42         // commit to the database
43
context.commitChanges();
44
45         BrowseArtistsPage nextPage =
46             (BrowseArtistsPage) cycle.getPage("BrowseArtistsPage");
47
48         // update the next page if it has cached artists
49
// to avoid unneeded refetch
50
if (nextPage.getArtistList() != null) {
51             nextPage.getArtistList().add(artist);
52         }
53
54         cycle.activate(nextPage);
55     }
56
57     public void pageBeginRender(PageEvent event) {
58         // create new artist when page is initialized.
59
// Do not intsert it into DataContext just yet.
60
// Instead if we register it here, and the user abandons
61
// the page, we will have to find a way to rollback the context,
62
// to avoid grabage carried over to other pages
63
setArtist(new Artist());
64     }
65 }
66
Popular Tags