ref #7523 fixing bug in CdmFilterablePagingProvider
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / service / CdmFilterablePagingProvider.java
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.dao.common.Restriction;
24 import eu.etaxonomy.cdm.persistence.query.MatchMode;
25 import eu.etaxonomy.cdm.persistence.query.OrderHint;
26
27 /**
28 * @author a.kohlbecker
29 * @since Jun 7, 2017
30 *
31 */
32 public class CdmFilterablePagingProvider<T extends IdentifiableEntity, V extends T> implements FilterablePagingProvider<V>, FilterableCountProvider {
33
34
35 private static final List<String> DEFAULT_INIT_STRATEGY = Arrays.asList("$");
36
37 private static final Logger logger = Logger.getLogger(CdmFilterablePagingProvider.class);
38
39 private int pageSize = 20;
40
41 private IIdentifiableEntityService<T> service;
42
43 private Class<V> type = null;
44
45 private MatchMode matchMode = MatchMode.ANYWHERE;
46
47 private List<OrderHint> orderHints = OrderHint.ORDER_BY_TITLE_CACHE.asList();
48
49 List<String> initStrategy = DEFAULT_INIT_STRATEGY;
50
51 private List<Criterion> criteria = new ArrayList<>();
52
53 private List<Restriction<?>> restrictions = new ArrayList<>();
54
55
56 /**
57 * @return the matchMode
58 */
59 protected MatchMode getMatchMode() {
60 return matchMode;
61 }
62
63 /**
64 * @param matchMode the matchMode to set
65 */
66 protected void setMatchMode(MatchMode matchMode) {
67 this.matchMode = matchMode;
68 }
69
70 /**
71 * @return the orderHints
72 */
73 protected List<OrderHint> getOrderHints() {
74 return orderHints;
75 }
76
77 /**
78 * @param orderHints the orderHints to set
79 */
80 protected void setOrderHints(List<OrderHint> orderHints) {
81 this.orderHints = orderHints;
82 }
83
84 /**
85 * With defaults for matchMode = MatchMode.ANYWHERE and orderHints = OrderHint.ORDER_BY_TITLE_CACHE
86 *
87 */
88 public CdmFilterablePagingProvider(IIdentifiableEntityService<T> service) {
89 this(service, null);
90 }
91
92 /**
93 * With defaults for matchMode = MatchMode.ANYWHERE and orderHints = OrderHint.ORDER_BY_TITLE_CACHE
94 *
95 */
96 public CdmFilterablePagingProvider(IIdentifiableEntityService<T> service, Class<V> type) {
97 super();
98 this.type = type;
99 this.service = service;
100
101 // Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
102 }
103
104
105 public CdmFilterablePagingProvider(IIdentifiableEntityService<T> service, MatchMode matchMode, List<OrderHint> orderHints) {
106 this(service, null, matchMode, orderHints);
107 }
108
109 public <S extends T> CdmFilterablePagingProvider(IIdentifiableEntityService<T> service, Class<V> type, MatchMode matchMode, List<OrderHint> orderHints) {
110 super();
111 this.type = type;
112 this.service = service;
113 this.matchMode = matchMode;
114 this.orderHints = orderHints;
115
116 // Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
117 }
118
119 /**
120 * {@inheritDoc}
121 */
122 @SuppressWarnings("unchecked")
123 @Override
124 public List<V> findEntities(int firstRow, String filter) {
125
126 checkNotMixed();
127
128 Integer pageIndex = firstRow / pageSize;
129 Pager<V> page;
130 if(!restrictions.isEmpty()){
131 page = (Pager<V>) service.findByTitleWithRestrictions(
132 type,
133 filter,
134 matchMode,
135 restrictions,
136 pageSize,
137 pageIndex ,
138 orderHints,
139 initStrategy
140 );
141 } else {
142 page = (Pager<V>) service.findByTitle(
143 type,
144 filter,
145 matchMode,
146 criteria,
147 pageSize,
148 pageIndex ,
149 orderHints,
150 initStrategy
151 );
152 }
153
154 if(logger.isTraceEnabled()){
155 logger.trace("findEntities() - page: " + page.getCurrentIndex() + "/" + page.getPagesAvailable() + " totalRecords: " + page.getCount() + "\n" + page.getRecords());
156 }
157 return page.getRecords();
158 }
159
160 /**
161 * {@inheritDoc}
162 */
163 @SuppressWarnings("unchecked")
164 @Override
165 public int size(String filter) {
166
167 checkNotMixed();
168
169 Pager<V> page;
170 if(!restrictions.isEmpty()){
171 page = (Pager<V>) service.findByTitleWithRestrictions(
172 type,
173 filter,
174 matchMode,
175 restrictions,
176 1,
177 0,
178 null,
179 null
180 );
181 } else {
182 page = (Pager<V>) service.findByTitle(
183 type,
184 filter,
185 matchMode,
186 criteria,
187 1,
188 0,
189 null,
190 null
191 );
192 }
193
194 if(logger.isTraceEnabled()){
195 logger.trace("size() - count: " + page.getCount().intValue());
196 }
197 return page.getCount().intValue();
198 }
199
200 /**
201 *
202 */
203 protected void checkNotMixed() {
204 if(!restrictions.isEmpty() && !criteria.isEmpty()){
205 throw new RuntimeException("Citeria and Restrictions must not be used at the same time");
206 }
207 }
208
209 /**
210 * @return the pageSize
211 */
212 public int getPageSize() {
213 return pageSize;
214 }
215
216 /**
217 * @param pageSize the pageSize to set
218 */
219 public void setPageSize(int pageSize) {
220 this.pageSize = pageSize;
221 }
222
223 /**
224 * @return the initStrategy
225 */
226 public List<String> getInitStrategy() {
227 return initStrategy;
228 }
229
230 /**
231 * @param initStrategy the initStrategy to set
232 */
233 public void setInitStrategy(List<String> initStrategy) {
234 this.initStrategy = initStrategy;
235 }
236
237 /**
238 * The list of criteria is initially empty.
239 *
240 * @return the criteria
241 */
242 public List<Criterion> getCriteria() {
243 return criteria;
244 }
245
246 public void addCriterion(Criterion criterion){
247 criteria.add(criterion);
248 }
249
250 /**
251 * The list of restrictions is initially empty.
252 *
253 * @return the restrictions
254 */
255 public List<Restriction<?>> getRestrictions() {
256 return restrictions;
257 }
258
259 public void addRestriction(Restriction<?> restriction){
260 restrictions.add(restriction);
261 }
262 }