1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.api.service.pager.impl;
|
12
|
|
13
|
import java.io.Serializable;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
|
19
|
import org.apache.commons.logging.Log;
|
20
|
import org.apache.commons.logging.LogFactory;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
23
|
|
24
|
public abstract class AbstractPagerImpl<T> implements Pager<T>, Serializable {
|
25
|
|
26
|
protected static Integer MAX_PAGE_LABELS = 3;
|
27
|
protected static String LABEL_DIVIDER = " - ";
|
28
|
private static Log log = LogFactory.getLog(DefaultPagerImpl.class);
|
29
|
protected Integer pagesAvailable;
|
30
|
protected Integer prevIndex;
|
31
|
protected Integer nextIndex;
|
32
|
protected Integer currentIndex;
|
33
|
protected Map<Integer,String> pageNumbers;
|
34
|
protected Integer firstRecord;
|
35
|
protected Integer lastRecord;
|
36
|
protected Integer count;
|
37
|
protected List<T> records;
|
38
|
protected String suggestion;
|
39
|
protected ArrayList<Integer> indices;
|
40
|
protected Integer pageSize;
|
41
|
|
42
|
/**
|
43
|
* Constructor
|
44
|
*
|
45
|
* @param currentIndex the page of this result set (0-based), can be null
|
46
|
* @param count the total number of results available for this query
|
47
|
* @param pageSize The size of pages (can be null if all results should be returned if available)
|
48
|
* @param records A list of objects in this page (can be empty if there were no results)
|
49
|
* @param suggestion a suggested query that would improve the search (only applicable for free-text / lucene queries)
|
50
|
*/
|
51
|
public AbstractPagerImpl(Integer currentIndex, Integer count, Integer pageSize, List<T> records, String suggestion) {
|
52
|
this(currentIndex,count,pageSize,records);
|
53
|
this.suggestion = suggestion;
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Constructor
|
58
|
*
|
59
|
* @param currentIndex the page of this result set (0-based), can be null
|
60
|
* @param count the total number of results available for this query
|
61
|
* @param pageSize The size of pages (can be null or 0 if all results should be returned if available)
|
62
|
* @param records A list of objects in this page (can be empty if there were no results)
|
63
|
*/
|
64
|
public AbstractPagerImpl(Integer currentIndex, Integer count, Integer pageSize, List<T> records) {
|
65
|
if(currentIndex != null) {
|
66
|
this.currentIndex = currentIndex;
|
67
|
} else {
|
68
|
this.currentIndex = 0;
|
69
|
}
|
70
|
|
71
|
this.pageSize = pageSize;
|
72
|
this.pageNumbers = new HashMap<Integer,String>();
|
73
|
indices = new ArrayList<Integer>();
|
74
|
if(count == 0) {
|
75
|
pagesAvailable = 1;
|
76
|
} else if(pageSize != null && pageSize != 0) {
|
77
|
if( 0 == count % pageSize) {
|
78
|
pagesAvailable = count / pageSize;
|
79
|
|
80
|
Integer labelsStart = 0;
|
81
|
if(this.currentIndex > DefaultPagerImpl.MAX_PAGE_LABELS) {
|
82
|
labelsStart = this.currentIndex - DefaultPagerImpl.MAX_PAGE_LABELS ;
|
83
|
}
|
84
|
|
85
|
Integer labelsEnd = pagesAvailable.intValue();
|
86
|
if((pagesAvailable - this.currentIndex) > DefaultPagerImpl.MAX_PAGE_LABELS) {
|
87
|
labelsEnd = this.currentIndex + DefaultPagerImpl.MAX_PAGE_LABELS;
|
88
|
}
|
89
|
|
90
|
for(int index = labelsStart; index < labelsEnd; index++) {
|
91
|
indices.add(index);
|
92
|
String startLabel = getLabel(index * pageSize);
|
93
|
String endLabel = getLabel(((index + 1) * pageSize) - 1);
|
94
|
pageNumbers.put(index,createLabel(startLabel,endLabel));
|
95
|
}
|
96
|
} else {
|
97
|
pagesAvailable = (count / pageSize) + 1; //12
|
98
|
|
99
|
Integer labelsStart = 0;
|
100
|
if(this.currentIndex > DefaultPagerImpl.MAX_PAGE_LABELS) {
|
101
|
labelsStart = this.currentIndex - DefaultPagerImpl.MAX_PAGE_LABELS;
|
102
|
}
|
103
|
|
104
|
Integer labelsEnd = pagesAvailable.intValue();
|
105
|
if((pagesAvailable - this.currentIndex) > DefaultPagerImpl.MAX_PAGE_LABELS ) {
|
106
|
labelsEnd = this.currentIndex + DefaultPagerImpl.MAX_PAGE_LABELS;
|
107
|
for(int index = labelsStart; index < labelsEnd; index++) {
|
108
|
indices.add(index);
|
109
|
|
110
|
String startLabel = getLabel(index * pageSize);
|
111
|
String endLabel = getLabel(((index + 1) * pageSize) - 1);
|
112
|
pageNumbers.put(index,createLabel(startLabel,endLabel));
|
113
|
}
|
114
|
|
115
|
|
116
|
} else {
|
117
|
for(int index = labelsStart; index < (labelsEnd -1); index++) {
|
118
|
indices.add(index);
|
119
|
String startLabel = getLabel(index * pageSize);
|
120
|
String endLabel = getLabel(((index + 1) * pageSize) - 1);
|
121
|
pageNumbers.put(index,createLabel(startLabel,endLabel));
|
122
|
}
|
123
|
indices.add(pagesAvailable.intValue() - 1);
|
124
|
String startLabel = getLabel((pagesAvailable.intValue() - 1) * pageSize);
|
125
|
String endLabel = getLabel(count.intValue() - 1);
|
126
|
pageNumbers.put(pagesAvailable.intValue() - 1,createLabel(startLabel,endLabel));
|
127
|
}
|
128
|
|
129
|
|
130
|
}
|
131
|
} else {
|
132
|
pagesAvailable = 1;
|
133
|
}
|
134
|
|
135
|
if(pagesAvailable == 1) {
|
136
|
nextIndex = null;
|
137
|
prevIndex = null;
|
138
|
} else {
|
139
|
if(0 < this.currentIndex) {
|
140
|
prevIndex = this.currentIndex - 1;
|
141
|
}
|
142
|
if(this.currentIndex < (pagesAvailable - 1)) {
|
143
|
nextIndex = this.currentIndex + 1;
|
144
|
}
|
145
|
}
|
146
|
if(pageSize == null) {
|
147
|
this.firstRecord = 1;
|
148
|
this.lastRecord = records.size();
|
149
|
} else {
|
150
|
this.firstRecord = (this.currentIndex * pageSize) + 1;
|
151
|
this.lastRecord = (this.currentIndex * pageSize) + records.size();
|
152
|
}
|
153
|
|
154
|
this.count = count;
|
155
|
this.records = records;
|
156
|
}
|
157
|
|
158
|
protected abstract String createLabel(String startLabel, String endLabel);
|
159
|
|
160
|
protected String getLabel(Integer i) {
|
161
|
Integer label = new Integer(i + 1);
|
162
|
return label.toString();
|
163
|
}
|
164
|
|
165
|
public Integer getPagesAvailable() {
|
166
|
return pagesAvailable;
|
167
|
}
|
168
|
|
169
|
public Integer getNextIndex() {
|
170
|
return nextIndex;
|
171
|
}
|
172
|
|
173
|
public Integer getPrevIndex() {
|
174
|
return prevIndex;
|
175
|
}
|
176
|
|
177
|
public Integer getCurrentIndex() {
|
178
|
return currentIndex;
|
179
|
}
|
180
|
|
181
|
public String getPageNumber(int index) {
|
182
|
return pageNumbers.get(index);
|
183
|
}
|
184
|
|
185
|
public List<Integer> getIndices() {
|
186
|
return indices;
|
187
|
}
|
188
|
|
189
|
public Integer getCount() {
|
190
|
return count;
|
191
|
}
|
192
|
|
193
|
public Integer getFirstRecord() {
|
194
|
return firstRecord;
|
195
|
}
|
196
|
|
197
|
public Integer getLastRecord() {
|
198
|
return lastRecord;
|
199
|
}
|
200
|
|
201
|
public List<T> getRecords() {
|
202
|
return records;
|
203
|
}
|
204
|
|
205
|
public String getSuggestion() {
|
206
|
return suggestion;
|
207
|
}
|
208
|
|
209
|
public Integer getPageSize() {
|
210
|
return pageSize;
|
211
|
}
|
212
|
|
213
|
}
|