KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > components > Borrow


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.components;
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.BaseComponent;
23 import org.apache.tapestry.IRequestCycle;
24 import org.apache.tapestry.vlib.VirtualLibraryEngine;
25 import org.apache.tapestry.vlib.Visit;
26 import org.apache.tapestry.vlib.ejb.Book;
27 import org.apache.tapestry.vlib.ejb.BorrowException;
28 import org.apache.tapestry.vlib.ejb.IOperations;
29 import org.apache.tapestry.vlib.pages.Home;
30
31 /**
32  * Implements the Borrow link that appears on many pages.
33  *
34  * <table border=1>
35  * <tr> <th>Parameter</th> <th>Type</th> <th>Read / Write </th> <th>Required</th> <th>Default</th> <th>Description</th>
36  * </tr>
37  *
38  * <tr>
39  * <td>book</td>
40  * <td>{@link Book}</td>
41  * <td>R</td>
42  * <td>yes</td>
43  * <td>&nbsp;</td>
44  * <td>The book to create a link to.
45  * </td>
46  * </tr>
47  *
48  * </table>
49  *
50  * <p>Informal parameters are not allowed. A body is not allowed.
51  *
52  * @author Howard Lewis Ship
53  * @version $Id: Borrow.java,v 1.10 2004/04/30 15:17:22 hlship Exp $
54  *
55  **/

56
57 public abstract class Borrow extends BaseComponent
58 {
59     public abstract Book getBook();
60
61     public boolean isLinkDisabled()
62     {
63         Visit visit = (Visit) getPage().getVisit();
64
65         if (!visit.isUserLoggedIn())
66             return true;
67
68         // If the user is logged in, they can borrow it if they are
69
// not already holding it and aren't the owner.
70

71         Book book = getBook();
72
73         // If the book is not lendable, then disable the link.
74

75         if (!book.isLendable())
76             return true;
77
78         // Otherwise, disabled the link if already holding it.
79

80         return visit.isLoggedInUser(book.getHolderId());
81     }
82
83     public void borrow(IRequestCycle cycle)
84     {
85         Object JavaDoc[] parameters = cycle.getServiceParameters();
86         Integer JavaDoc bookPK = (Integer JavaDoc) parameters[0];
87
88         Visit visit = (Visit) getPage().getVisit();
89         Home home = (Home) cycle.getPage("Home");
90         VirtualLibraryEngine vengine = (VirtualLibraryEngine)cycle.getEngine();
91
92         int i = 0;
93         while (true)
94         {
95             try
96             {
97                 IOperations bean = vengine.getOperations();
98                 Book book = bean.borrowBook(bookPK, visit.getUserId());
99
100                 home.setMessage("Borrowed: " + book.getTitle());
101
102                 break;
103             }
104             catch (BorrowException ex)
105             {
106                 vengine.presentError(ex.getMessage(), cycle);
107                 return;
108             }
109             catch (FinderException JavaDoc ex)
110             {
111                 throw new ApplicationRuntimeException("Unable to find book or user. ", ex);
112             }
113             catch (RemoteException JavaDoc ex)
114             {
115                 vengine.rmiFailure("Remote exception borrowing book.", ex, i++);
116             }
117         }
118
119         cycle.activate(home);
120     }
121
122 }
Popular Tags