KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > ejb > impl > BookQueryBean


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.ejb.impl;
16
17 import java.sql.Connection JavaDoc;
18 import java.sql.ResultSet JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.tapestry.contrib.ejb.XEJBException;
24 import org.apache.tapestry.contrib.jdbc.IStatement;
25 import org.apache.tapestry.contrib.jdbc.StatementAssembly;
26 import org.apache.tapestry.vlib.ejb.Book;
27 import org.apache.tapestry.vlib.ejb.MasterQueryParameters;
28 import org.apache.tapestry.vlib.ejb.SortOrdering;
29
30 /**
31  * Implementation of a stateful session bean used to query the
32  * {@link org.apache.tapestry.vlib.ejb.IBook}
33  * entity and cache the results. It can then download the results, in chunks,
34  * to the client ... this is used to support clients that which to display
35  * the results a page at a time (with random access to the pages of results).
36  *
37  * <p>To avoid a lot of duplicate code for things like finding the JDBC {@link
38  * Connection} and querying the IBook entity, we subclass from
39  * {@link OperationsBean}.
40  *
41  * @see org.apache.tapestry.vlib.ejb.IBookQuery
42  * @see org.apache.tapestry.vlib.ejb.IBookQueryHome
43  *
44  * @version $Id: BookQueryBean.java,v 1.5 2004/02/19 17:38:07 hlship Exp $
45  * @author Howard Lewis Ship
46  *
47  **/

48
49 public class BookQueryBean extends OperationsBean
50 {
51
52     /**
53      * Stores the results from the most recent query.
54      *
55      **/

56
57     private Book[] _results;
58
59     /**
60      * Releases any results.
61      *
62      **/

63
64     public void ejbRemove()
65     {
66         _results = null;
67     }
68
69     // Business methods
70

71     /**
72      * Returns the number of results from the most recent query.
73      *
74      **/

75
76     public int getResultCount()
77     {
78         if (_results == null)
79             return 0;
80
81         return _results.length;
82     }
83
84     /**
85      * Gets a subset of the results from the query.
86      *
87      **/

88
89     public Book[] get(int offset, int length)
90     {
91         Book[] result;
92
93         if (offset < 0)
94             return null;
95
96         int finalLength = Math.min(length, _results.length - offset);
97
98         if (finalLength < 0)
99             return null;
100
101         // Create a new array and copy the requested
102
// results into it.
103

104         result = new Book[finalLength];
105         System.arraycopy(_results, offset, result, 0, finalLength);
106
107         return result;
108     }
109
110     /**
111      * The master query is for querying by some mixture of title, author
112      * and publisher.
113      *
114      **/

115
116     public int masterQuery(MasterQueryParameters parameters, SortOrdering sortOrdering)
117     {
118         IStatement statement = null;
119         Connection JavaDoc connection = null;
120
121         // Forget any current results.
122

123         _results = null;
124
125         try
126         {
127             connection = getConnection();
128
129             try
130             {
131                 statement = buildMasterQuery(connection, parameters, sortOrdering);
132             }
133             catch (SQLException JavaDoc ex)
134             {
135                 throw new XEJBException("Unable to create query statement.", ex);
136             }
137
138             processQuery(statement);
139
140         }
141         finally
142         {
143             close(connection, statement, null);
144         }
145
146         return getResultCount();
147     }
148
149     /**
150      * Queries on books owned by a given person, sorted by title.
151      *
152      **/

153
154     public int ownerQuery(Integer JavaDoc ownerId, SortOrdering sortOrdering)
155     {
156         IStatement statement = null;
157         Connection JavaDoc connection = null;
158
159         // Forget any current results.
160

161         _results = null;
162
163         try
164         {
165             connection = getConnection();
166
167             try
168             {
169                 statement = buildPersonQuery(connection, "owner.PERSON_ID", ownerId, sortOrdering);
170             }
171             catch (SQLException JavaDoc ex)
172             {
173                 throw new XEJBException("Unable to create query statement.", ex);
174             }
175
176             processQuery(statement);
177
178         }
179         finally
180         {
181             close(connection, statement, null);
182         }
183
184         return getResultCount();
185     }
186
187     /**
188      * Queries on books held (borrowed) by a given person, sorted by title.
189      *
190      **/

191
192     public int holderQuery(Integer JavaDoc holderId, SortOrdering sortOrdering)
193     {
194         IStatement statement = null;
195         Connection JavaDoc connection = null;
196
197         // Forget any current results.
198

199         _results = null;
200
201         try
202         {
203             connection = getConnection();
204
205             try
206             {
207                 statement =
208                     buildPersonQuery(connection, "holder.PERSON_ID", holderId, sortOrdering);
209             }
210             catch (SQLException JavaDoc ex)
211             {
212                 throw new XEJBException("Unable to create query statement.", ex);
213             }
214
215             processQuery(statement);
216
217         }
218         finally
219         {
220             close(connection, statement, null);
221         }
222
223         return getResultCount();
224     }
225
226     public int borrowerQuery(Integer JavaDoc borrowerId, SortOrdering sortOrdering)
227     {
228         IStatement statement = null;
229         Connection JavaDoc connection = null;
230
231         // Forget any current results.
232

233         _results = null;
234
235         try
236         {
237             connection = getConnection();
238
239             try
240             {
241                 statement = buildBorrowerQuery(connection, borrowerId, sortOrdering);
242             }
243             catch (SQLException JavaDoc ex)
244             {
245                 throw new XEJBException("Unable to create query statement.", ex);
246             }
247
248             processQuery(statement);
249
250         }
251         finally
252         {
253             close(connection, statement, null);
254         }
255
256         return getResultCount();
257     }
258
259     private void processQuery(IStatement statement)
260     {
261         ResultSet JavaDoc set = null;
262
263         try
264         {
265             set = statement.executeQuery();
266         }
267         catch (SQLException JavaDoc ex)
268         {
269             throw new XEJBException("Unable to execute query.", ex);
270         }
271
272         try
273         {
274             processQueryResults(set);
275         }
276         catch (SQLException JavaDoc ex)
277         {
278             throw new XEJBException("Unable to process query results.", ex);
279         }
280         finally
281         {
282             close(null, null, set);
283         }
284     }
285
286     private void processQueryResults(ResultSet JavaDoc set) throws SQLException JavaDoc
287     {
288         List JavaDoc list = new ArrayList JavaDoc();
289         Object JavaDoc[] columns = new Object JavaDoc[Book.N_COLUMNS];
290
291         while (set.next())
292         {
293             Book book = convertRowToBook(set, columns);
294
295             list.add(book);
296         }
297
298         _results = new Book[list.size()];
299         _results = (Book[]) list.toArray(_results);
300     }
301
302     private IStatement buildMasterQuery(
303         Connection JavaDoc connection,
304         MasterQueryParameters parameters,
305         SortOrdering ordering)
306         throws SQLException JavaDoc
307     {
308         String JavaDoc title = parameters.getTitle();
309         String JavaDoc author = parameters.getAuthor();
310         Integer JavaDoc publisherId = parameters.getPublisherId();
311         Integer JavaDoc ownerId = parameters.getOwnerId();
312     
313         StatementAssembly assembly = buildBaseBookQuery();
314
315         addSubstringSearch(assembly, "book.TITLE", title);
316         addSubstringSearch(assembly, "book.AUTHOR", author);
317
318         // Hide books that are not visible to the master query.
319

320         assembly.addSep(" AND ");
321         assembly.add("book.HIDDEN = 0");
322
323         if (publisherId != null)
324         {
325             assembly.addSep(" AND ");
326             assembly.add("book.PUBLISHER_ID = ");
327             assembly.addParameter(publisherId);
328         }
329         
330         if (ownerId != null)
331         {
332             assembly.addSep(" AND ");
333             assembly.add("book.OWNER_ID = ");
334             assembly.addParameter(ownerId);
335         }
336
337         addSortOrdering(assembly, ordering);
338
339         return assembly.createStatement(connection);
340     }
341
342     private IStatement buildPersonQuery(
343         Connection JavaDoc connection,
344         String JavaDoc personColumn,
345         Integer JavaDoc personId,
346         SortOrdering sortOrdering)
347         throws SQLException JavaDoc
348     {
349         StatementAssembly assembly = buildBaseBookQuery();
350
351         assembly.addSep(" AND ");
352         assembly.add(personColumn);
353         assembly.add(" = ");
354         assembly.addParameter(personId);
355
356         addSortOrdering(assembly, sortOrdering);
357
358         return assembly.createStatement(connection);
359     }
360
361     private IStatement buildBorrowerQuery(
362         Connection JavaDoc connection,
363         Integer JavaDoc borrowerId,
364         SortOrdering sortOrdering)
365         throws SQLException JavaDoc
366     {
367         StatementAssembly assembly = buildBaseBookQuery();
368
369         // Get books held by the borrower but not owned by the borrower.
370

371         assembly.addSep(" AND ");
372         assembly.add("book.HOLDER_ID = ");
373         assembly.addParameter(borrowerId);
374         assembly.addSep(" AND ");
375         assembly.add("book.HOLDER_ID <> book.OWNER_ID");
376
377         addSortOrdering(assembly, sortOrdering);
378
379         return assembly.createStatement(connection);
380     }
381
382 }
Popular Tags