KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > misc > RecordPaginator


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.core.misc;
66
67 import com.jcorporate.expresso.core.controller.ControllerException;
68 import com.jcorporate.expresso.core.controller.ControllerRequest;
69 import com.jcorporate.expresso.core.dataobjects.DataObject;
70 import com.jcorporate.expresso.core.db.DBException;
71 import com.jcorporate.expresso.core.dbobj.MultiDBObject;
72 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
73 import com.jcorporate.expresso.services.dbobj.DBObjLimit;
74
75 import java.io.IOException JavaDoc;
76 import java.io.ObjectInputStream JavaDoc;
77 import java.io.ObjectOutputStream JavaDoc;
78 import java.io.Serializable JavaDoc;
79 import java.util.ArrayList JavaDoc;
80 import java.util.List JavaDoc;
81
82
83 /**
84  * <p>Copyright: Copyright (c) 2001-2002 JCorporate Ltd.<p>
85  * <p>This class takes care of the low-level logic for when dealing
86  * with &quote;pages&quote; of data. It is used in the Download Controller
87  * to allow paging through masses of download files, as well as in <code>DBMaint</code> for
88  * paging through the data records.</p>
89  *
90  * @author Michael Rimov
91  */

92 public class RecordPaginator
93         implements Serializable JavaDoc {
94
95     /**
96      * Are there more records that can be retrieved
97      */

98     private boolean moreRecords;
99
100     /**
101      * Are there previous records that can be retrieved
102      */

103     private boolean previousRecords;
104
105     /**
106      * What is the page number of the current record set.
107      */

108     private int pageNumber = 1;
109
110
111     private int pageLimit;
112
113     /**
114      * What is the start record number of the current set
115      */

116     private int startRecordNumber;
117
118     /**
119      * What is the end record number of the current set.
120      */

121     private int endRecordNumber;
122
123     /**
124      * Boolean that states whether a DBObject.count() should be issued before
125      * issuing the search and retrieve call.
126      */

127     private boolean countRecords;
128
129     /**
130      * Total Number of records retrieved
131      */

132     private int totalRecordCount;
133
134     public RecordPaginator() {
135     }
136
137     // //
138
// Serialization Methods //
139
// //
140
private void readObject(ObjectInputStream JavaDoc ois)
141             throws ClassNotFoundException JavaDoc, IOException JavaDoc {
142         ois.defaultReadObject();
143     }
144
145     private void writeObject(ObjectOutputStream JavaDoc oos)
146             throws IOException JavaDoc {
147         oos.defaultWriteObject();
148     }
149
150     // //
151
// Getter/Setter Methods for attributes //
152
// //
153
// //
154
public void setMoreRecords(boolean newMoreRecords) {
155         moreRecords = newMoreRecords;
156     }
157
158     public boolean isMoreRecords() {
159         return moreRecords;
160     }
161
162     public void setPreviousRecords(boolean newPreviousRecords) {
163         previousRecords = newPreviousRecords;
164     }
165
166     public boolean isPreviousRecords() {
167         return previousRecords;
168     }
169
170     public void setPageNumber(int newPageNumber) {
171         pageNumber = newPageNumber;
172     }
173
174     public int getPageNumber() {
175         return pageNumber;
176     }
177
178     public void setStartRecordNumber(int newStartRecordNumber) {
179         startRecordNumber = newStartRecordNumber;
180     }
181
182     /**
183      * Modified code based upon xin lee.
184      *
185      * @return the Start Record Number
186      */

187     public int getStartRecordNumber() {
188         if (endRecordNumber > startRecordNumber) {
189             return startRecordNumber;
190         } else {
191             return endRecordNumber;
192         }
193     }
194
195     /**
196      * Sets the ending record number
197      *
198      * @param newEndRecordNumber int for the new end record number
199      */

200     public void setEndRecordNumber(int newEndRecordNumber) {
201         endRecordNumber = newEndRecordNumber;
202     }
203
204     /**
205      * Returns the end record number for a particular page.
206      *
207      * @return integer &gt;= 0
208      */

209     public int getEndRecordNumber() {
210         return endRecordNumber;
211     }
212
213     /**
214      * Sets whether or not the total record count should be used or not.
215      *
216      * @param newCountRecords true if you wish for the recordset to retrieve
217      * a total record count.
218      */

219     public void setCountRecords(boolean newCountRecords) {
220         countRecords = newCountRecords;
221     }
222
223     /**
224      * Returns true if the system is expected to get the total record count.
225      *
226      * @return true if the record count will be active.
227      */

228     public boolean isCountRecords() {
229         return countRecords;
230     }
231
232     /**
233      * Returns the total record count retrieved for this database search
234      *
235      * @return total record count as long.
236      */

237     public long getTotalRecordCount() {
238         return totalRecordCount;
239     }
240
241     /**
242      * Get the number of pages [using recorg pagination] that exist for this record.
243      *
244      * @return
245      */

246     public int getPageCount() {
247         if (pageLimit == 0) {
248             return 1;
249         } else {
250             int pageCount = (int) (this.getTotalRecordCount() / pageLimit);
251             int mod = (int) (this.getTotalRecordCount() % pageLimit);
252             if (mod != 0 || pageCount == 0) {
253                 pageCount++;
254             }
255             return pageCount;
256         }
257     }
258
259
260     // //
261
// Bean Methods //
262
// //
263
/**
264      * Returns a search and retrieve list from the DBObject who's criteria
265      * you have set. Also sets its internal attributes such as start record number
266      * etc.
267      *
268      * @param searchCriteria The DBObject to search against
269      * @param sortKey the field to search against.
270      * @return an ArrayList of DBObjects retrieved by the searchCriteria
271      */

272     public ArrayList JavaDoc searchAndRetrieve(DataObject searchCriteria, String JavaDoc sortKey)
273             throws DBException {
274         if (isCountRecords()) {
275             totalRecordCount = searchCriteria.count();
276         }
277         if (getPageNumber() > 1) {
278             setPreviousRecords(true);
279         }
280
281         Integer JavaDoc pageLimitObj = (Integer JavaDoc) searchCriteria.getAttribute("pageLimit");
282
283         if (pageLimitObj == null) {
284             this.setPageLimitAttribute(searchCriteria);
285             pageLimitObj = (Integer JavaDoc) searchCriteria.getAttribute("pageLimit");
286         }
287
288
289         if (pageLimitObj != null) {
290             pageLimit = pageLimitObj.intValue();
291         } else {
292             pageLimit = 0;
293         }
294
295         searchCriteria.setMaxRecords(pageLimit);
296         searchCriteria.setOffsetRecord(pageLimit * (getPageNumber() - 1));
297
298         ArrayList JavaDoc allRecords = (ArrayList JavaDoc) searchCriteria.searchAndRetrieveList(sortKey);
299
300         if (!isCountRecords()) {
301             totalRecordCount = allRecords.size();
302         }
303
304         startRecordNumber = (pageLimit * (getPageNumber() - 1)) + 1;
305         endRecordNumber = startRecordNumber + allRecords.size() - 1;
306
307         if (endRecordNumber >= totalRecordCount) {
308             setMoreRecords(false);
309         } else {
310             setMoreRecords(true);
311         }
312
313         return allRecords;
314     }
315 // //
316
// Bean Methods //
317
// //
318
/**
319      * Returns a search and retrieve list from the DBObject who's criteria
320      * you have set. Also sets its internal attributes such as start record number
321      * etc.
322      *
323      * @param searchCriteria The DBObject to search against
324      * @param sortKey the field to search against.
325      * @return an ArrayList of DBObjects retrieved by the searchCriteria
326      */

327     public List JavaDoc searchAndRetrieve(MultiDBObject searchCriteria, String JavaDoc sortKey)
328             throws DBException {
329         if (isCountRecords()) {
330             totalRecordCount = searchCriteria.count();
331         }
332         if (getPageNumber() > 1) {
333             setPreviousRecords(true);
334         }
335
336         Integer JavaDoc pageLimitObj = (Integer JavaDoc) searchCriteria.getAttribute("pageLimit");
337
338         if (pageLimitObj == null) {
339             this.setPageLimitAttribute(searchCriteria);
340             pageLimitObj = (Integer JavaDoc) searchCriteria.getAttribute("pageLimit");
341         }
342
343         if (pageLimitObj != null) {
344             pageLimit = pageLimitObj.intValue();
345         } else {
346             pageLimit = 0;
347         }
348
349         searchCriteria.setMaxRecords(pageLimit);
350         searchCriteria.setOffsetRecord(pageLimit * (getPageNumber() - 1));
351
352         List JavaDoc allRecords = searchCriteria.searchAndRetrieveList(sortKey);
353
354         if (!isCountRecords()) {
355             totalRecordCount = allRecords.size();
356         }
357
358         startRecordNumber = (pageLimit * (getPageNumber() - 1)) + 1;
359         endRecordNumber = startRecordNumber + allRecords.size() - 1;
360
361         if (endRecordNumber >= totalRecordCount) {
362             setMoreRecords(false);
363         } else {
364             setMoreRecords(true);
365         }
366
367         return allRecords;
368     }
369
370
371     /**
372      * Sets the size of the page limit. It uses the DBObject Limit to define
373      * how many records per page are displayed
374      *
375      * @param dbObj The dbobject to set for.
376      */

377     protected void setPageLimitAttribute(DataObject dbObj)
378             throws DBException {
379
380         /* Now see if there is a "page limit" for this object */
381         DBObjLimit dl = new DBObjLimit(SecuredDBObject.SYSTEM_ACCOUNT);
382         dl.setDataContext(dbObj.getDataContext());
383         dl.setField("DBObjectName", ((Object JavaDoc) dbObj).getClass().getName());
384
385         int pageLimit = 0;
386
387         if (dl.find()) {
388             try {
389                 pageLimit = new Integer JavaDoc(dl.getField("PageLimit")).intValue();
390             } catch (NumberFormatException JavaDoc ne) {
391                 throw new DBException("Can't use limit of '" +
392                         dl.getField("PageLimit") + "' for " +
393                         ((Object JavaDoc) dbObj).getClass().getName());
394             }
395
396             dbObj.setMaxRecords((getPageNumber() * pageLimit) + 1);
397             dbObj.setAttribute("pageLimit", new Integer JavaDoc(pageLimit));
398         } else {
399
400             //Set it per controller defaults then.
401
dl.setField("DBObjectName",
402                     "com.jcorporate.expresso.services.dbobj.ControllerDefault");
403
404             if (dl.find()) {
405                 try {
406                     pageLimit = new Integer JavaDoc(dl.getField("PageLimit")).intValue();
407                 } catch (NumberFormatException JavaDoc ne) {
408                     throw new DBException("Can't use limit of '" +
409                             dl.getField("PageLimit") + "' for " +
410                             ((Object JavaDoc) dbObj).getClass().getName());
411                 }
412
413                 dbObj.setMaxRecords((getPageNumber() * pageLimit) + 1);
414                 dbObj.setAttribute("pageLimit", new Integer JavaDoc(pageLimit));
415             } else {
416                 pageLimit = 0;
417             }
418         }
419     }
420
421     /**
422      * Sets the size of the page limit. It uses the DBObject Limit to define
423      * how many records per page are displayed
424      *
425      * @param dbObj The dbobject to set for.
426      */

427     protected void setPageLimitAttribute(MultiDBObject dbObj)
428             throws DBException {
429
430         /* Now see if there is a "page limit" for this object */
431         DBObjLimit dl = new DBObjLimit(SecuredDBObject.SYSTEM_ACCOUNT);
432         dl.setDataContext(dbObj.getDBName());
433         dl.setField("DBObjectName", ((Object JavaDoc) dbObj).getClass().getName());
434
435         int pageLimit = 0;
436
437         if (dl.find()) {
438             try {
439                 pageLimit = new Integer JavaDoc(dl.getField("PageLimit")).intValue();
440             } catch (NumberFormatException JavaDoc ne) {
441                 throw new DBException("Can't use limit of '" +
442                         dl.getField("PageLimit") + "' for " +
443                         ((Object JavaDoc) dbObj).getClass().getName());
444             }
445
446             dbObj.setMaxRecords((getPageNumber() * pageLimit) + 1);
447             dbObj.setAttribute("pageLimit", new Integer JavaDoc(pageLimit));
448         } else {
449
450             //Set it per controller defaults then.
451
dl.setField("DBObjectName",
452                     "com.jcorporate.expresso.services.dbobj.ControllerDefault");
453
454             if (dl.find()) {
455                 try {
456                     pageLimit = new Integer JavaDoc(dl.getField("PageLimit")).intValue();
457                 } catch (NumberFormatException JavaDoc ne) {
458                     throw new DBException("Can't use limit of '" +
459                             dl.getField("PageLimit") + "' for " +
460                             ((Object JavaDoc) dbObj).getClass().getName());
461                 }
462
463                 dbObj.setMaxRecords((getPageNumber() * pageLimit) + 1);
464                 dbObj.setAttribute("pageLimit", new Integer JavaDoc(pageLimit));
465             } else {
466                 pageLimit = 0;
467             }
468         }
469     }
470
471     /**
472      * Sets the page number based upon the controller request object.
473      * If the page parameters is not included with the request, then page
474      * is set to zero.
475      *
476      * @param request The controller request fed to the controller from which the
477      * function can extract the page= controller parameter.
478      * @throws ControllerException if page= is not a number.
479      */

480     public void setPageNumber(ControllerRequest request)
481             throws ControllerException {
482         String JavaDoc pg = request.getParameter("page");
483
484         if (pg != null) {
485             try {
486                 this.setPageNumber(Integer.parseInt(pg));
487             } catch (NumberFormatException JavaDoc nfe) {
488                 throw new ControllerException("Page number parameter is not an integer");
489             }
490         } else {
491             setPageNumber(1);
492         }
493     }
494 }
Popular Tags