KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 import javax.ejb.FinderException JavaDoc;
20
21 import org.apache.tapestry.IRequestCycle;
22 import org.apache.tapestry.vlib.ActivatePage;
23 import org.apache.tapestry.vlib.IMessageProperty;
24 import org.apache.tapestry.vlib.VirtualLibraryEngine;
25 import org.apache.tapestry.vlib.Visit;
26 import org.apache.tapestry.vlib.components.Browser;
27 import org.apache.tapestry.vlib.ejb.Book;
28 import org.apache.tapestry.vlib.ejb.IBookQuery;
29 import org.apache.tapestry.vlib.ejb.IOperations;
30 import org.apache.tapestry.vlib.ejb.SortColumn;
31 import org.apache.tapestry.vlib.ejb.SortOrdering;
32
33 /**
34  * Shows a list of the user's books, allowing books to be editted or
35  * even deleted.
36  *
37  * <p>Note that, unlike elsewhere, book titles do not link to the
38  * {@link ViewBook} page. It seems to me there would be a conflict between
39  * that behavior and the edit behavior; making the book titles not be links
40  * removes the ambiguity over what happens when the book title is clicked
41  * (view vs. edit).
42  *
43  * @author Howard Lewis Ship
44  * @version $Id: MyLibrary.java,v 1.16 2004/02/19 17:37:48 hlship Exp $
45  *
46  **/

47
48 public abstract class MyLibrary
49     extends ActivatePage
50     implements IMessageProperty
51 {
52     public abstract void setOwnedQuery(IBookQuery value);
53
54     public abstract IBookQuery getOwnedQuery();
55
56     public abstract SortColumn getSortColumn();
57
58     public abstract boolean isDescending();
59
60     private Browser _browser;
61
62     public void finishLoad()
63     {
64         _browser = (Browser) getComponent("browser");
65     }
66
67     public void activate(IRequestCycle cycle)
68     {
69         runQuery();
70
71         cycle.activate(this);
72     }
73
74     public void requery(IRequestCycle cycle)
75     {
76         runQuery();
77     }
78
79     private void runQuery()
80     {
81         Visit visit = (Visit) getVisit();
82         Integer JavaDoc userId = visit.getUserId();
83
84         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
85
86         SortOrdering ordering = new SortOrdering(getSortColumn(), isDescending());
87
88         int i = 0;
89         while (true)
90         {
91             try
92             {
93                 IBookQuery query = getOwnedQuery();
94
95                 if (query == null)
96                 {
97                     query = vengine.createNewQuery();
98                     setOwnedQuery(query);
99                 }
100
101                 int count = query.ownerQuery(userId, ordering);
102
103                 if (count != _browser.getResultCount())
104                     _browser.initializeForResultCount(count);
105
106                 break;
107             }
108             catch (RemoteException JavaDoc ex)
109             {
110                 vengine.rmiFailure("Remote exception accessing owned books.", ex, i++);
111
112                 setOwnedQuery(null);
113             }
114         }
115     }
116
117     /**
118      * Listener invoked to allow a user to edit a book.
119      *
120      **/

121
122     public void editBook(IRequestCycle cycle)
123     {
124         Object JavaDoc[] parameters = cycle.getServiceParameters();
125         Integer JavaDoc bookId = (Integer JavaDoc) parameters[0];
126         EditBook page = (EditBook) cycle.getPage("EditBook");
127
128         page.beginEdit(cycle, bookId);
129     }
130
131     /**
132      * Listener invoked to allow a user to delete a book.
133      *
134      **/

135
136     public void deleteBook(IRequestCycle cycle)
137     {
138         Object JavaDoc[] parameters = cycle.getServiceParameters();
139         Integer JavaDoc bookId = (Integer JavaDoc) parameters[0];
140
141         ConfirmBookDelete page = (ConfirmBookDelete) cycle.getPage("ConfirmBookDelete");
142         page.selectBook(bookId, cycle);
143     }
144
145     private void returnBook(Integer JavaDoc bookId)
146     {
147         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
148
149         int i = 0;
150         while (true)
151         {
152             try
153             {
154                 IOperations operations = vengine.getOperations();
155                 Book book = operations.returnBook(bookId);
156
157                 setMessage(format("returned-book", book.getTitle()));
158
159                 break;
160             }
161             catch (FinderException JavaDoc ex)
162             {
163                 setError(format("unable-to-return-book", ex.getMessage()));
164                 return;
165             }
166             catch (RemoteException JavaDoc ex)
167             {
168                 vengine.rmiFailure("Remote exception returning book.", ex, i++);
169             }
170         }
171
172         runQuery();
173     }
174
175 }
Popular Tags