BrightSide Workbench Full Report + Source Code
DaoIterator.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.jpa.iterator;
20 
21 import java.util.List;
22 import org.turro.elephant.db.WhereClause;
23 import org.turro.jpa.Dao;
24 
31 public abstract class DaoIterator<E, ID> {
32 
33  protected List<E> items;
34  protected Dao dao;
35  protected int _total = -1;
36 
37  private WhereClause wc, cwc;
38  private int _page = 10, _curr = -1;
39  private boolean end;
40 
41  public DaoIterator(Dao dao) {
42  this.dao = dao;
43  }
44 
45  public int getPage() {
46  return _page;
47  }
48 
49  public void setPage(int page) {
50  this._page = page;
51  }
52 
53  public List<E> getItems() {
54  return items;
55  }
56 
57  public int getTotalCount() {
58  initClauses();
59  return _total;
60  }
61 
62  public List<E> getTotalItems() {
63  initClauses();
64  return dao.getResultList(wc);
65  }
66 
67  public int item() {
68  return _curr;
69  }
70 
71  public void current(int value) {
72  value = (value * _page) - _page;
73  if(value >= _total) {
74  value = _total - _page;
75  end = true;
76  }
77  if(value < 0) {
78  _curr = 0;
79  } else {
80  _curr = value;
81  }
82  loadItems();
83  }
84 
85  public int current() {
86  if(_curr > -1) {
87  return (int) Math.ceil((_curr) / (_page + 0.0d));
88  }
89  return -1;
90  }
91 
92  public int pages() {
93  if(_total > -1) {
94  return (int) Math.ceil((_total + 0.0d) / (_page + 0.0d));
95  }
96  return -1;
97  }
98 
99  public boolean beginning() {
100  return _curr <= 0;
101  }
102 
103  public boolean end() {
104  return end;
105  }
106 
107  public void next() {
108  current(current() + 1);
109  }
110 
111  public void previous() {
112  current(current() - 1);
113  }
114 
115  public boolean isEmpty() {
116  return items == null || items.size() == 0;
117  }
118 
119  protected void initClauses() {
120  setWhereClause();
121  setCountClause();
122  }
123 
124  protected abstract WhereClause getWhereClause();
125  protected abstract WhereClause getCountClause();
126 
127  private void setWhereClause() {
128  if(this.wc == null) {
129  this.wc = getWhereClause();
130  _curr = -1;
131  end = false;
132  }
133  }
134 
135  private void setCountClause() {
136  if(this.cwc == null) {
137  this.cwc = getCountClause();
138  _total = _total = ((Number) dao.getSingleResult(cwc)).intValue();
139  }
140  }
141 
142  private void loadItems() {
143  items = dao.getResultList(wc, _curr, _page);
144  }
145 
146 }
Object getSingleResult(WhereClause wc)
Definition: Dao.java:380
abstract WhereClause getCountClause()
abstract WhereClause getWhereClause()