1 28 package net.sf.jasperreports.engine.data; 29 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import net.sf.jasperreports.engine.JRRewindableDataSource; 34 import net.sf.jasperreports.engine.query.JRHibernateQueryExecuter; 35 36 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 returnValues; 51 private Iterator 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 |