2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.service
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.List
;
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
;
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
;
28 * @author a.kohlbecker
32 public class CdmFilterablePagingProvider
<T
extends IdentifiableEntity
, V
extends T
> implements FilterablePagingProvider
<V
>, FilterableCountProvider
{
35 private static final List
<String
> DEFAULT_INIT_STRATEGY
= Arrays
.asList("$");
37 private static final Logger logger
= Logger
.getLogger(CdmFilterablePagingProvider
.class);
39 private int pageSize
= 20;
41 private IIdentifiableEntityService
<T
> service
;
43 private Class
<V
> type
= null;
45 private MatchMode matchMode
= MatchMode
.ANYWHERE
;
47 private List
<OrderHint
> orderHints
= OrderHint
.ORDER_BY_TITLE_CACHE
.asList();
49 List
<String
> initStrategy
= DEFAULT_INIT_STRATEGY
;
51 private List
<Criterion
> criteria
= new ArrayList
<>();
53 private List
<Restriction
<?
>> restrictions
= new ArrayList
<>();
57 * @return the matchMode
59 protected MatchMode
getMatchMode() {
64 * @param matchMode the matchMode to set
66 protected void setMatchMode(MatchMode matchMode
) {
67 this.matchMode
= matchMode
;
71 * @return the orderHints
73 protected List
<OrderHint
> getOrderHints() {
78 * @param orderHints the orderHints to set
80 protected void setOrderHints(List
<OrderHint
> orderHints
) {
81 this.orderHints
= orderHints
;
85 * With defaults for matchMode = MatchMode.ANYWHERE and orderHints = OrderHint.ORDER_BY_TITLE_CACHE
88 public CdmFilterablePagingProvider(IIdentifiableEntityService
<T
> service
) {
93 * With defaults for matchMode = MatchMode.ANYWHERE and orderHints = OrderHint.ORDER_BY_TITLE_CACHE
96 public CdmFilterablePagingProvider(IIdentifiableEntityService
<T
> service
, Class
<V
> type
) {
99 this.service
= service
;
101 // Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
105 public CdmFilterablePagingProvider(IIdentifiableEntityService
<T
> service
, MatchMode matchMode
, List
<OrderHint
> orderHints
) {
106 this(service
, null, matchMode
, orderHints
);
109 public <S
extends T
> CdmFilterablePagingProvider(IIdentifiableEntityService
<T
> service
, Class
<V
> type
, MatchMode matchMode
, List
<OrderHint
> orderHints
) {
112 this.service
= service
;
113 this.matchMode
= matchMode
;
114 this.orderHints
= orderHints
;
116 // Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
122 @SuppressWarnings("unchecked")
124 public List
<V
> findEntities(int firstRow
, String filter
) {
128 Integer pageIndex
= firstRow
/ pageSize
;
130 if(!restrictions
.isEmpty()){
131 page
= (Pager
<V
>) service
.findByTitleWithRestrictions(
142 page
= (Pager
<V
>) service
.findByTitle(
154 if(logger
.isTraceEnabled()){
155 logger
.trace("findEntities() - page: " + page
.getCurrentIndex() + "/" + page
.getPagesAvailable() + " totalRecords: " + page
.getCount() + "\n" + page
.getRecords());
157 return page
.getRecords();
163 @SuppressWarnings("unchecked")
165 public int size(String filter
) {
170 if(!restrictions
.isEmpty()){
171 page
= (Pager
<V
>) service
.findByTitleWithRestrictions(
182 page
= (Pager
<V
>) service
.findByTitle(
194 if(logger
.isTraceEnabled()){
195 logger
.trace("size() - count: " + page
.getCount().intValue());
197 return page
.getCount().intValue();
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");
210 * @return the pageSize
212 public int getPageSize() {
217 * @param pageSize the pageSize to set
219 public void setPageSize(int pageSize
) {
220 this.pageSize
= pageSize
;
224 * @return the initStrategy
226 public List
<String
> getInitStrategy() {
231 * @param initStrategy the initStrategy to set
233 public void setInitStrategy(List
<String
> initStrategy
) {
234 this.initStrategy
= initStrategy
;
238 * The list of criteria is initially empty.
240 * @return the criteria
242 public List
<Criterion
> getCriteria() {
246 public void addCriterion(Criterion criterion
){
247 criteria
.add(criterion
);
251 * The list of restrictions is initially empty.
253 * @return the restrictions
255 public List
<Restriction
<?
>> getRestrictions() {
259 public void addRestriction(Restriction
<?
> restriction
){
260 restrictions
.add(restriction
);