KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > pages > admin > TransferBooksTransfer


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.admin;
16
17 import java.rmi.RemoteException JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.ejb.FinderException JavaDoc;
21
22 import org.apache.hivemind.ApplicationRuntimeException;
23 import org.apache.tapestry.IRequestCycle;
24 import org.apache.tapestry.Tapestry;
25 import org.apache.tapestry.event.PageEvent;
26 import org.apache.tapestry.event.PageRenderListener;
27 import org.apache.tapestry.form.IPropertySelectionModel;
28 import org.apache.tapestry.vlib.AdminPage;
29 import org.apache.tapestry.vlib.EntitySelectionModel;
30 import org.apache.tapestry.vlib.IErrorProperty;
31 import org.apache.tapestry.vlib.IMessageProperty;
32 import org.apache.tapestry.vlib.VirtualLibraryEngine;
33 import org.apache.tapestry.vlib.ejb.Book;
34 import org.apache.tapestry.vlib.ejb.IBookQuery;
35 import org.apache.tapestry.vlib.ejb.IOperations;
36 import org.apache.tapestry.vlib.ejb.Person;
37
38 /**
39  * Second page in Transfer Books wizard; allows the books owned by the
40  * from user to be selected and transfered to the target user.
41  *
42  * @author Howard Lewis Ship
43  * @version $Id: TransferBooksTransfer.java,v 1.11 2004/04/30 15:17:21 hlship Exp $
44  * @since 3.0
45  *
46  **/

47
48 public abstract class TransferBooksTransfer extends AdminPage implements PageRenderListener
49 {
50     public abstract Person getFromUser();
51
52     public abstract void setFromUser(Person fromUser);
53
54     public abstract Integer JavaDoc getFromUserId();
55
56     public abstract void setFromUserId(Integer JavaDoc fromUserId);
57
58     public abstract Integer JavaDoc getToUserId();
59
60     public abstract void setToUserId(Integer JavaDoc toUserId);
61
62     public abstract Person getToUser();
63
64     public abstract void setToUser(Person toUser);
65
66     public abstract List JavaDoc getSelectedBooks();
67
68     public abstract IPropertySelectionModel getUserBookModel();
69
70     public abstract void setUserBookModel(IPropertySelectionModel userBookModel);
71
72     public void activate(IRequestCycle cycle, Integer JavaDoc fromUserId, Integer JavaDoc toUserId)
73     {
74         Person fromUser = readPerson(fromUserId);
75
76         IPropertySelectionModel model = buildUserBookModel(fromUser);
77
78         if (model.getOptionCount() == 0)
79         {
80             IErrorProperty page = (IErrorProperty) cycle.getPage();
81             page.setError(format("user-has-no-books", fromUser.getNaturalName()));
82             return;
83         }
84
85         setFromUserId(fromUserId);
86         setFromUser(fromUser);
87
88         setToUserId(toUserId);
89
90         setUserBookModel(model);
91
92         cycle.activate(this);
93     }
94
95     /**
96      * Only properties toUserPK and fromUserPK are persistent. We need to refresh
97      * the fromUser, toUser and userBookModel properties at the start
98      * of each request cycle.
99      *
100      **/

101
102     public void pageBeginRender(PageEvent event)
103     {
104         Person fromUser = getFromUser();
105
106         if (fromUser == null)
107         {
108             fromUser = readPerson(getFromUserId());
109             setFromUser(fromUser);
110         }
111
112         if (getUserBookModel() == null)
113             setUserBookModel(buildUserBookModel(fromUser));
114
115         Person toUser = getToUser();
116         if (toUser == null)
117         {
118             toUser = readPerson(getToUserId());
119             setToUser(toUser);
120         }
121     }
122
123     public void formSubmit(IRequestCycle cycle)
124     {
125         List JavaDoc selectedBooks = getSelectedBooks();
126
127         int count = Tapestry.size(selectedBooks);
128
129         if (count == 0)
130         {
131             setError(getMessage("no-books-selected"));
132             return;
133         }
134
135         Integer JavaDoc[] keys = (Integer JavaDoc[]) selectedBooks.toArray(new Integer JavaDoc[count]);
136         Person toUser = getToUser();
137
138         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
139
140         int i = 0;
141         while (true)
142         {
143             try
144             {
145                 IOperations operations = vengine.getOperations();
146
147                 operations.transferBooks(toUser.getId(), keys);
148
149                 break;
150             }
151             catch (FinderException JavaDoc ex)
152             {
153                 throw new ApplicationRuntimeException(ex);
154             }
155             catch (RemoteException JavaDoc ex)
156             {
157                 vengine.rmiFailure("Unable to transfer ownership of books.", ex, i++);
158             }
159         }
160
161         Person fromUser = getFromUser();
162         IMessageProperty selectPage = (TransferBooksSelect) cycle.getPage("TransferBooksSelect");
163         selectPage.setMessage(
164             format(
165                 "transfered-books",
166                 Integer.toString(count),
167                 fromUser.getNaturalName(),
168                 toUser.getNaturalName()));
169         cycle.activate(selectPage);
170     }
171
172     private IPropertySelectionModel buildUserBookModel(Person user)
173     {
174         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
175         Book[] books = null;
176
177         int i = 0;
178         while (true)
179         {
180             books = null;
181
182             try
183             {
184                 IBookQuery query = vengine.createNewQuery();
185
186                 int count = query.ownerQuery(user.getId(), null);
187
188                 if (count > 0)
189                     books = query.get(0, count);
190
191                 break;
192             }
193             catch (RemoteException JavaDoc ex)
194             {
195                 vengine.rmiFailure(
196                     "Unable to retrieve books owned by " + user.getNaturalName() + ".",
197                     ex,
198                     i++);
199             }
200         }
201
202         EntitySelectionModel model = new EntitySelectionModel();
203
204         if (books != null)
205             for (i = 0; i < books.length; i++)
206                 model.add(books[i].getId(), books[i].getTitle());
207
208         return model;
209     }
210
211     private Person readPerson(Integer JavaDoc personId)
212     {
213         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
214
215         return vengine.readPerson(personId);
216     }
217 }
218
Popular Tags