KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.DateFormat JavaDoc;
19
20 import javax.ejb.FinderException JavaDoc;
21
22 import org.apache.tapestry.IExternalPage;
23 import org.apache.tapestry.IRequestCycle;
24 import org.apache.tapestry.event.PageEvent;
25 import org.apache.tapestry.event.PageRenderListener;
26 import org.apache.tapestry.html.BasePage;
27 import org.apache.tapestry.vlib.VirtualLibraryEngine;
28 import org.apache.tapestry.vlib.ejb.Book;
29 import org.apache.tapestry.vlib.ejb.IOperations;
30
31 /**
32  * Shows the details of a book, and allows the user to borrow it.
33  *
34  * @author Howard Lewis Ship
35  * @version $Id: ViewBook.java,v 1.7 2004/02/19 17:37:48 hlship Exp $
36  *
37  **/

38
39 public abstract class ViewBook extends BasePage implements IExternalPage, PageRenderListener
40 {
41     private DateFormat JavaDoc _dateFormat;
42
43     public abstract Integer JavaDoc getBookId();
44
45     public abstract void setBookId(Integer JavaDoc bookId);
46
47     public abstract Book getBook();
48
49     public abstract void setBook(Book value);
50
51     public void activateExternalPage(Object JavaDoc[] parameters, IRequestCycle cycle)
52     {
53         Integer JavaDoc bookId = (Integer JavaDoc) parameters[0];
54
55         setBookId(bookId);
56     }
57
58     private void readBook()
59     {
60         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
61         Integer JavaDoc bookId = getBookId();
62
63         int i = 0;
64         while (true)
65         {
66             IOperations bean = vengine.getOperations();
67
68             try
69             {
70                 setBook(bean.getBook(bookId));
71
72                 return;
73             }
74             catch (FinderException JavaDoc ex)
75             {
76                 vengine.presentError("Book not found in database.", getRequestCycle());
77                 return;
78             }
79             catch (RemoteException JavaDoc ex)
80             {
81                 vengine.rmiFailure(
82                     "Remote exception obtaining information for book #" + bookId + ".",
83                     ex,
84                     i++);
85             }
86         }
87
88     }
89
90     public DateFormat JavaDoc getDateFormat()
91     {
92         if (_dateFormat == null)
93             _dateFormat =
94                 DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, getLocale());
95
96         return _dateFormat;
97     }
98
99     public void pageBeginRender(PageEvent event)
100     {
101         if (getBook() == null)
102             readBook();
103     }
104
105 }
Popular Tags