1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.service;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.hibernate.criterion.Criterion;
|
17
|
import org.vaadin.viritin.fields.LazyComboBox.FilterableCountProvider;
|
18
|
import org.vaadin.viritin.fields.LazyComboBox.FilterablePagingProvider;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.service.IIdentifiableEntityService;
|
21
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
22
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
23
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
24
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
25
|
|
26
|
/**
|
27
|
* @author a.kohlbecker
|
28
|
* @since Jun 7, 2017
|
29
|
*
|
30
|
*/
|
31
|
public class CdmFilterablePagingProvider<T extends IdentifiableEntity, V extends T> implements FilterablePagingProvider<V>, FilterableCountProvider {
|
32
|
|
33
|
|
34
|
private static final List<String> DEFAULT_INIT_STRATEGY = Arrays.asList("$");
|
35
|
|
36
|
private static final Logger logger = Logger.getLogger(CdmFilterablePagingProvider.class);
|
37
|
|
38
|
private int pageSize = 20;
|
39
|
|
40
|
private IIdentifiableEntityService<T> service;
|
41
|
|
42
|
private Class<V> type = null;
|
43
|
|
44
|
private MatchMode matchMode = MatchMode.ANYWHERE;
|
45
|
|
46
|
private List<OrderHint> orderHints = OrderHint.ORDER_BY_TITLE_CACHE.asList();
|
47
|
|
48
|
List<String> initStrategy = DEFAULT_INIT_STRATEGY;
|
49
|
|
50
|
private List<Criterion> criteria = new ArrayList<>();
|
51
|
|
52
|
|
53
|
/**
|
54
|
* @return the matchMode
|
55
|
*/
|
56
|
protected MatchMode getMatchMode() {
|
57
|
return matchMode;
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* @param matchMode the matchMode to set
|
62
|
*/
|
63
|
protected void setMatchMode(MatchMode matchMode) {
|
64
|
this.matchMode = matchMode;
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* @return the orderHints
|
69
|
*/
|
70
|
protected List<OrderHint> getOrderHints() {
|
71
|
return orderHints;
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* @param orderHints the orderHints to set
|
76
|
*/
|
77
|
protected void setOrderHints(List<OrderHint> orderHints) {
|
78
|
this.orderHints = orderHints;
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* With defaults for matchMode = MatchMode.ANYWHERE and orderHints = OrderHint.ORDER_BY_TITLE_CACHE
|
83
|
*
|
84
|
*/
|
85
|
public CdmFilterablePagingProvider(IIdentifiableEntityService<T> service) {
|
86
|
this(service, null);
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* With defaults for matchMode = MatchMode.ANYWHERE and orderHints = OrderHint.ORDER_BY_TITLE_CACHE
|
91
|
*
|
92
|
*/
|
93
|
public CdmFilterablePagingProvider(IIdentifiableEntityService<T> service, Class<V> type) {
|
94
|
super();
|
95
|
this.type = type;
|
96
|
this.service = service;
|
97
|
}
|
98
|
|
99
|
|
100
|
public CdmFilterablePagingProvider(IIdentifiableEntityService<T> service, MatchMode matchMode, List<OrderHint> orderHints) {
|
101
|
this(service, null, matchMode, orderHints);
|
102
|
}
|
103
|
|
104
|
public <S extends T> CdmFilterablePagingProvider(IIdentifiableEntityService<T> service, Class<V> type, MatchMode matchMode, List<OrderHint> orderHints) {
|
105
|
super();
|
106
|
this.type = type;
|
107
|
this.service = service;
|
108
|
this.matchMode = matchMode;
|
109
|
this.orderHints = orderHints;
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* {@inheritDoc}
|
114
|
*/
|
115
|
@Override
|
116
|
public List<V> findEntities(int firstRow, String filter) {
|
117
|
|
118
|
Integer pageIndex = firstRow / pageSize;
|
119
|
Pager<V> page = (Pager<V>) service.findByTitle(
|
120
|
type,
|
121
|
filter,
|
122
|
matchMode,
|
123
|
criteria,
|
124
|
pageSize,
|
125
|
pageIndex ,
|
126
|
orderHints,
|
127
|
initStrategy
|
128
|
);
|
129
|
if(logger.isTraceEnabled()){
|
130
|
logger.trace("findEntities() - page: " + page.getCurrentIndex() + "/" + page.getPagesAvailable() + " totalRecords: " + page.getCount() + "\n" + page.getRecords());
|
131
|
}
|
132
|
return page.getRecords();
|
133
|
}
|
134
|
|
135
|
/**
|
136
|
* {@inheritDoc}
|
137
|
*/
|
138
|
@Override
|
139
|
public int size(String filter) {
|
140
|
|
141
|
Pager<V> page = (Pager<V>) service.findByTitle(
|
142
|
type,
|
143
|
filter,
|
144
|
matchMode,
|
145
|
criteria,
|
146
|
1,
|
147
|
0,
|
148
|
null,
|
149
|
null
|
150
|
);
|
151
|
if(logger.isTraceEnabled()){
|
152
|
logger.trace("size() - count: " + page.getCount().intValue());
|
153
|
}
|
154
|
return page.getCount().intValue();
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* @return the pageSize
|
159
|
*/
|
160
|
public int getPageSize() {
|
161
|
return pageSize;
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* @param pageSize the pageSize to set
|
166
|
*/
|
167
|
public void setPageSize(int pageSize) {
|
168
|
this.pageSize = pageSize;
|
169
|
}
|
170
|
|
171
|
/**
|
172
|
* @return the initStrategy
|
173
|
*/
|
174
|
public List<String> getInitStrategy() {
|
175
|
return initStrategy;
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* @param initStrategy the initStrategy to set
|
180
|
*/
|
181
|
public void setInitStrategy(List<String> initStrategy) {
|
182
|
this.initStrategy = initStrategy;
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* The list of criteria is initially empty.
|
187
|
*
|
188
|
* @return the criteria
|
189
|
*/
|
190
|
public List<Criterion> getCriteria() {
|
191
|
return criteria;
|
192
|
}
|
193
|
}
|