KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > data > JRHibernateListDataSource


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.data;
29
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import net.sf.jasperreports.engine.JRRewindableDataSource;
34 import net.sf.jasperreports.engine.query.JRHibernateQueryExecuter;
35
36 /**
37  * Hibernate data source that uses <code>org.hibernate.Query.list()</code>.
38  * <p/>
39  * The query result can be paginated by not retrieving all the rows at once.
40  *
41  * @author Lucian Chirita (lucianc@users.sourceforge.net)
42  * @version $Id: JRHibernateListDataSource.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
43  * @see net.sf.jasperreports.engine.query.JRHibernateQueryExecuterFactory#PROPERTY_HIBERNATE_QUERY_LIST_PAGE_SIZE
44  */

45 public class JRHibernateListDataSource extends JRHibernateAbstractDataSource implements JRRewindableDataSource
46 {
47     private final int pageSize;
48     private int pageCount;
49     private boolean nextPage;
50     private List JavaDoc returnValues;
51     private Iterator JavaDoc iterator;
52
53     public JRHibernateListDataSource(JRHibernateQueryExecuter queryExecuter, boolean useFieldDescription, int pageSize)
54     {
55         super(queryExecuter, useFieldDescription, false);
56
57         this.pageSize = pageSize;
58         
59         pageCount = 0;
60         fetchPage();
61     }
62
63     protected void fetchPage()
64     {
65         if (pageSize <= 0)
66         {
67             returnValues = queryExecuter.list();
68             nextPage = false;
69         }
70         else
71         {
72             returnValues = queryExecuter.list(pageCount * pageSize, pageSize);
73             nextPage = returnValues.size() == pageSize;
74         }
75
76         ++pageCount;
77
78         initIterator();
79     }
80
81     public boolean next()
82     {
83         if (iterator == null)
84         {
85             return false;
86         }
87         
88         boolean hasNext = iterator.hasNext();
89         if (!hasNext && nextPage)
90         {
91             fetchPage();
92             hasNext = iterator != null && iterator.hasNext();
93         }
94         
95         if (hasNext)
96         {
97             setCurrentRowValue(iterator.next());
98         }
99
100         return hasNext;
101     }
102
103     public void moveFirst()
104     {
105         if (pageCount == 1)
106         {
107             initIterator();
108         }
109         else
110         {
111             pageCount = 0;
112             fetchPage();
113         }
114     }
115
116     private void initIterator()
117     {
118         iterator = returnValues == null ? null : returnValues.iterator();
119     }
120 }
121
Popular Tags