KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > pages > EditBook


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.vlib.pages;
16
17 import java.rmi.RemoteException JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.ejb.CreateException JavaDoc;
22 import javax.ejb.FinderException JavaDoc;
23
24 import org.apache.hivemind.ApplicationRuntimeException;
25 import org.apache.tapestry.IRequestCycle;
26 import org.apache.tapestry.Tapestry;
27 import org.apache.tapestry.event.PageEvent;
28 import org.apache.tapestry.event.PageRenderListener;
29 import org.apache.tapestry.vlib.Protected;
30 import org.apache.tapestry.vlib.VirtualLibraryEngine;
31 import org.apache.tapestry.vlib.ejb.IOperations;
32
33 /**
34  * Edits the properties of at book.
35  *
36  * @author Howard Lewis Ship
37  * @version $Id: EditBook.java,v 1.15 2004/04/30 15:17:21 hlship Exp $
38  **/

39
40 public abstract class EditBook extends Protected implements PageRenderListener
41 {
42     public abstract Map JavaDoc getAttributes();
43
44     public abstract void setAttributes(Map JavaDoc attributes);
45
46     public abstract String JavaDoc getPublisherName();
47
48     public abstract Integer JavaDoc getBookId();
49
50     public abstract void setBookId(Integer JavaDoc bookId);
51
52     /**
53      * Invoked (from {@link MyLibrary}) to begin editting a book.
54      * Gets the attributes from the {@link org.apache.tapestry.vlib.ejb.IBook}
55      * and updates the request cycle to render this page,
56      *
57      **/

58
59     public void beginEdit(IRequestCycle cycle, Integer JavaDoc bookId)
60     {
61         setBookId(bookId);
62
63         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
64
65         int i = 0;
66         while (true)
67         {
68             try
69             {
70                 // Get the attributes as a source for our input fields.
71

72                 IOperations operations = vengine.getOperations();
73
74                 setAttributes(operations.getBookAttributes(bookId));
75
76                 break;
77             }
78             catch (FinderException JavaDoc ex)
79             {
80                 throw new ApplicationRuntimeException(ex);
81             }
82             catch (RemoteException JavaDoc ex)
83             {
84                 vengine.rmiFailure(
85                     "Remote exception setting up page for book #" + bookId + ".",
86                     ex,
87                     i++);
88             }
89         }
90
91         cycle.activate(this);
92     }
93
94     /**
95      * Used to update the book when the form is submitted.
96      *
97      **/

98
99     public void formSubmit(IRequestCycle cycle)
100     {
101         Map JavaDoc attributes = getAttributes();
102
103         Integer JavaDoc publisherId = (Integer JavaDoc) attributes.get("publisherId");
104         String JavaDoc publisherName = getPublisherName();
105
106         if (publisherId == null && Tapestry.isBlank(publisherName))
107         {
108             setErrorField("inputPublisherName", getMessage("need-publisher-name"));
109             return;
110         }
111
112         if (publisherId != null && Tapestry.isNonBlank(publisherName))
113         {
114             setErrorField("inputPublisherName", getMessage("leave-publisher-name-empty"));
115             return;
116         }
117
118         // Check for an error from a validation field
119

120         if (isInError())
121             return;
122
123         // OK, do the update.
124

125         VirtualLibraryEngine vengine = (VirtualLibraryEngine)cycle.getEngine();
126         Integer JavaDoc bookId = getBookId();
127
128         int i = 0;
129         while (true)
130         {
131             IOperations bean = vengine.getOperations();
132
133             try
134             {
135                 if (publisherId != null)
136                     bean.updateBook(bookId, attributes);
137                 else
138                 {
139                     bean.updateBook(bookId, attributes, publisherName);
140                     vengine.clearCache();
141                 }
142
143                 break;
144             }
145             catch (FinderException JavaDoc ex)
146             {
147                 throw new ApplicationRuntimeException(ex);
148             }
149             catch (CreateException JavaDoc ex)
150             {
151                 throw new ApplicationRuntimeException(ex);
152             }
153             catch (RemoteException JavaDoc ex)
154             {
155                 vengine.rmiFailure("Remote exception updating book #" + bookId + ".", ex, i++);
156
157                 continue;
158             }
159         }
160
161         MyLibrary page = (MyLibrary) cycle.getPage("MyLibrary");
162         page.setMessage(format("updated-book", attributes.get("title")));
163         page.activate(cycle);
164       
165     }
166
167     public void pageBeginRender(PageEvent event)
168     {
169         if (getAttributes() == null)
170             setAttributes(new HashMap JavaDoc());
171     }
172
173 }
Popular Tags