KickJava   Java API By Example, From Geeks To Geeks.

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


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.hivemind.ApplicationRuntimeException;
22 import org.apache.tapestry.IRequestCycle;
23 import org.apache.tapestry.vlib.ActivatePage;
24 import org.apache.tapestry.vlib.IMessageProperty;
25 import org.apache.tapestry.vlib.VirtualLibraryEngine;
26 import org.apache.tapestry.vlib.Visit;
27 import org.apache.tapestry.vlib.components.Browser;
28 import org.apache.tapestry.vlib.ejb.Book;
29 import org.apache.tapestry.vlib.ejb.IBookQuery;
30 import org.apache.tapestry.vlib.ejb.IOperations;
31 import org.apache.tapestry.vlib.ejb.SortColumn;
32 import org.apache.tapestry.vlib.ejb.SortOrdering;
33
34 /**
35  * Shows a list of books the user has borrowed.
36  *
37  * @author Howard Lewis Ship
38  * @version $Id: BorrowedBooks.java,v 1.15 2004/04/30 15:17:21 hlship Exp $
39  *
40  **/

41
42 public abstract class BorrowedBooks extends ActivatePage implements IMessageProperty
43 {
44     private Browser _browser;
45
46     public abstract void setBorrowedQuery(IBookQuery value);
47
48     public abstract IBookQuery getBorrowedQuery();
49
50     public abstract SortColumn getSortColumn();
51
52     public abstract boolean isDescending();
53
54     public void finishLoad()
55     {
56         _browser = (Browser) getComponent("browser");
57     }
58
59     public void activate(IRequestCycle cycle)
60     {
61         runQuery();
62
63         cycle.activate(this);
64     }
65
66     /**
67      * Invoked as listener method after the sortColumn or
68      * descending properties are changed.
69      *
70      * @param cycle
71      * @since 3.0
72      *
73      *
74      **/

75
76     public void requery(IRequestCycle cycle)
77     {
78         runQuery();
79     }
80
81     private void runQuery()
82     {
83         Visit visit = (Visit) getVisit();
84         Integer JavaDoc userPK = visit.getUserId();
85
86         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
87
88         SortOrdering ordering = new SortOrdering(getSortColumn(), isDescending());
89
90         int i = 0;
91         while (true)
92         {
93             try
94             {
95                 IBookQuery query = getBorrowedQuery();
96
97                 if (query == null)
98                 {
99                     query = vengine.createNewQuery();
100                     setBorrowedQuery(query);
101                 }
102
103                 int count = query.borrowerQuery(userPK, ordering);
104
105                 if (count != _browser.getResultCount())
106                     _browser.initializeForResultCount(count);
107
108                 break;
109             }
110             catch (RemoteException JavaDoc ex)
111             {
112                 vengine.rmiFailure("Remote exception finding borrowed books.", ex, i++);
113
114                 setBorrowedQuery(null);
115             }
116         }
117     }
118
119     /**
120      * Listener used to return a book.
121      *
122      **/

123
124     public void returnBook(IRequestCycle cycle)
125     {
126         Object JavaDoc[] parameters = cycle.getServiceParameters();
127         Integer JavaDoc bookPK = (Integer JavaDoc) parameters[0];
128
129         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
130         IOperations operations = vengine.getOperations();
131
132         try
133         {
134             Book book = operations.returnBook(bookPK);
135
136             setMessage(format("returned-book", book.getTitle()));
137
138             runQuery();
139         }
140         catch (FinderException JavaDoc ex)
141         {
142             setError(format("unable-to-return-book", ex.getMessage()));
143             return;
144         }
145         catch (RemoteException JavaDoc ex)
146         {
147             throw new ApplicationRuntimeException(ex);
148         }
149     }
150
151 }
Popular Tags