KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.PageRedirectException;
25 import org.apache.tapestry.Tapestry;
26 import org.apache.tapestry.event.PageEvent;
27 import org.apache.tapestry.event.PageRenderListener;
28 import org.apache.tapestry.form.IPropertySelectionModel;
29 import org.apache.tapestry.vlib.EntitySelectionModel;
30 import org.apache.tapestry.vlib.IActivate;
31 import org.apache.tapestry.vlib.Protected;
32 import org.apache.tapestry.vlib.VirtualLibraryEngine;
33 import org.apache.tapestry.vlib.Visit;
34 import org.apache.tapestry.vlib.ejb.Book;
35 import org.apache.tapestry.vlib.ejb.IBookQuery;
36 import org.apache.tapestry.vlib.ejb.IOperations;
37 import org.apache.tapestry.vlib.ejb.Person;
38
39 /**
40  * Used to manage giving away of books to other users.
41  *
42  * @author Howard Lewis Ship
43  * @version $Id: GiveAwayBooks.java,v 1.11 2004/04/30 15:17:21 hlship Exp $
44  * @since 3.0
45  *
46  **/

47
48 public abstract class GiveAwayBooks extends Protected implements PageRenderListener
49 {
50     public abstract IPropertySelectionModel getBooksModel();
51
52     public abstract void setBooksModel(IPropertySelectionModel booksModel);
53
54     public abstract IPropertySelectionModel getPersonModel();
55
56     public abstract void setPersonModel(IPropertySelectionModel personModel);
57
58     public abstract List JavaDoc getSelectedBooks();
59
60     public abstract Integer JavaDoc getTargetUserId();
61
62     public void formSubmit(IRequestCycle cycle)
63     {
64         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
65
66         Integer JavaDoc targetUserId = getTargetUserId();
67
68         Person target = vengine.readPerson(targetUserId);
69
70         List JavaDoc selectedBooks = getSelectedBooks();
71
72         int count = Tapestry.size(selectedBooks);
73
74         if (count == 0)
75         {
76             setError(format("select-at-least-one-book", target.getNaturalName()));
77             return;
78         }
79
80         Integer JavaDoc[] bookIds = (Integer JavaDoc[]) selectedBooks.toArray(new Integer JavaDoc[count]);
81
82         int i = 0;
83         while (true)
84         {
85             IOperations operations = vengine.getOperations();
86
87             try
88             {
89                 operations.transferBooks(targetUserId, bookIds);
90
91                 break;
92             }
93             catch (FinderException JavaDoc ex)
94             {
95                 throw new ApplicationRuntimeException(ex);
96             }
97             catch (RemoteException JavaDoc ex)
98             {
99                 vengine.rmiFailure(getMessage("update-failure"), ex, i++);
100             }
101         }
102
103         MyLibrary myLibrary = (MyLibrary) cycle.getPage("MyLibrary");
104
105         myLibrary.setMessage(
106             format("transfered-books", Integer.toString(count), target.getNaturalName()));
107
108         myLibrary.activate(cycle);
109     }
110
111     private IPropertySelectionModel buildPersonModel()
112     {
113         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
114         Visit visit = (Visit) vengine.getVisit();
115         Integer JavaDoc userPK = visit.getUserId();
116
117         Person[] persons = null;
118
119         int i = 0;
120         while (true)
121         {
122             IOperations operations = vengine.getOperations();
123
124             try
125             {
126                 persons = operations.getPersons();
127
128                 break;
129             }
130             catch (RemoteException JavaDoc ex)
131             {
132                 vengine.rmiFailure(getMessage("read-users-failure"), ex, i++);
133             }
134         }
135
136         EntitySelectionModel result = new EntitySelectionModel();
137
138         for (i = 0; i < persons.length; i++)
139         {
140
141             Person p = persons[i];
142             Integer JavaDoc pk = p.getId();
143
144             if (pk.equals(userPK))
145                 continue;
146
147             result.add(pk, p.getNaturalName());
148         }
149
150         return result;
151     }
152
153     public void pageBeginRender(PageEvent event)
154     {
155         IPropertySelectionModel model = getBooksModel();
156
157         if (model == null)
158         {
159             model = buildBooksModel();
160             setBooksModel(model);
161         }
162
163         model = getPersonModel();
164         if (model == null)
165         {
166             model = buildPersonModel();
167             setPersonModel(model);
168         }
169     }
170
171     private IPropertySelectionModel buildBooksModel()
172     {
173         VirtualLibraryEngine vengine = (VirtualLibraryEngine) getEngine();
174         Visit visit = (Visit) vengine.getVisit();
175         Integer JavaDoc userPK = visit.getUserId();
176         Book[] books = null;
177
178         int i = 0;
179         while (true)
180         {
181             books = null;
182
183             try
184             {
185                 IBookQuery query = vengine.createNewQuery();
186
187                 int count = query.ownerQuery(userPK, null);
188
189                 if (count > 0)
190                     books = query.get(0, count);
191
192                 break;
193             }
194             catch (RemoteException JavaDoc ex)
195             {
196                 vengine.rmiFailure(getMessage("read-books-failure"), ex, i++);
197             }
198         }
199
200         EntitySelectionModel result = new EntitySelectionModel();
201
202         if (books != null)
203             for (i = 0; i < books.length; i++)
204                 result.add(books[i].getId(), books[i].getTitle());
205
206         return result;
207
208     }
209
210     public void pageValidate(PageEvent event)
211     {
212         super.pageValidate(event);
213
214         IPropertySelectionModel model = buildBooksModel();
215
216         if (model.getOptionCount() == 0)
217         {
218             IRequestCycle cycle = getRequestCycle();
219             IActivate page = (IActivate) cycle.getPage("MyLibrary");
220
221             page.activate(cycle);
222
223             throw new PageRedirectException(page);
224         }
225
226         setBooksModel(model);
227     }
228
229 }
230
Popular Tags