import java.util.Arrays;
import java.util.List;
-import org.apache.log4j.Logger;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.vaadin.viritin.fields.LazyComboBox.FilterableCountProvider;
/**
* @author a.kohlbecker
* @since Jun 7, 2017
- *
*/
-public class CdmFilterablePagingProvider<T extends IdentifiableEntity, V extends T> implements FilterablePagingProvider<V>, FilterableCountProvider {
+public class CdmFilterablePagingProvider<T extends IdentifiableEntity, V extends T>
+ implements FilterablePagingProvider<V>, FilterableCountProvider {
+ private static final Logger logger = LogManager.getLogger();
private static final List<String> DEFAULT_INIT_STRATEGY = Arrays.asList("$");
public static final String QUERY_STRING_PLACEHOLDER = "{query-string}";
- private static final Logger logger = Logger.getLogger(CdmFilterablePagingProvider.class);
-
private int pageSize = 20;
private IIdentifiableEntityService<T> service;
private List<OrderHint> orderHints = OrderHint.ORDER_BY_TITLE_CACHE.asList();
- List<String> initStrategy = DEFAULT_INIT_STRATEGY;
+ private List<String> initStrategy = DEFAULT_INIT_STRATEGY;
private List<Criterion> criteria = new ArrayList<>();
private List<Restriction<?>> restrictions = new ArrayList<>();
-
- /**
- * @return the matchMode
- */
protected MatchMode getMatchMode() {
return matchMode;
}
-
- /**
- * @param matchMode the matchMode to set
- */
protected void setMatchMode(MatchMode matchMode) {
this.matchMode = matchMode;
}
- /**
- * @return the orderHints
- */
protected List<OrderHint> getOrderHints() {
return orderHints;
}
-
- /**
- * @param orderHints the orderHints to set
- */
protected void setOrderHints(List<OrderHint> orderHints) {
this.orderHints = orderHints;
}
/**
* With defaults for matchMode = MatchMode.ANYWHERE and orderHints = OrderHint.ORDER_BY_TITLE_CACHE
- *
*/
public CdmFilterablePagingProvider(IIdentifiableEntityService<T> service) {
this(service, null);
/**
* With defaults for matchMode = MatchMode.ANYWHERE and orderHints = OrderHint.ORDER_BY_TITLE_CACHE
- *
*/
public CdmFilterablePagingProvider(IIdentifiableEntityService<T> service, Class<V> type) {
super();
this.type = type;
this.service = service;
- // Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
+ // LogUtils.setLevel("org.hibernate.SQL", Level.TRACE);
}
-
public CdmFilterablePagingProvider(IIdentifiableEntityService<T> service, MatchMode matchMode, List<OrderHint> orderHints) {
this(service, null, matchMode, orderHints);
}
public <S extends T> CdmFilterablePagingProvider(IIdentifiableEntityService<T> service, Class<V> type, MatchMode matchMode, List<OrderHint> orderHints) {
- super();
this.type = type;
this.service = service;
this.matchMode = matchMode;
this.orderHints = orderHints;
- // Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
+ // LogUtils.setLevel("org.hibernate.SQL", Level.TRACE);
}
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("unchecked")
@Override
public List<V> findEntities(int firstRow, String filter) {
logger.trace("findEntities() - page: " + page.getCurrentIndex() + "/" + page.getPagesAvailable() + " totalRecords: " + page.getCount() + "\n" + page.getRecords());
}
- // Logger.getLogger("org.hibernate.SQL").setLevel(Level.WARN);
+ // LogUtils.setLevel("org.hibernate.SQL", Level.TRACE);
return page.getRecords();
}
- /**
- * {@inheritDoc}
- */
- @SuppressWarnings("unchecked")
@Override
public int size(String filter) {
checkNotMixed();
- Pager<V> page;
-
clearSession(); // clear the session from remains of previous service calls, see issue #7559
+ long count = 0;
if(!restrictions.isEmpty()){
- // Logger.getLogger("org.hibernate.SQL").setLevel(Level.TRACE);
+ // LogUtils.setLevel("org.hibernate.SQL", Level.TRACE);
List<Restriction<?>> preparedRestrictions = prepareRestrictions(filter, matchMode);
- page = service.findByTitleWithRestrictions(
- type,
- filter,
- matchMode,
- preparedRestrictions,
- 1,
- 0,
- null,
- null
- );
+ count = service.countByTitleWithRestrictions(type, filter, matchMode, preparedRestrictions);
} else {
- page = service.findByTitle(
- type,
- filter,
- matchMode,
- criteria,
- 1,
- 0,
- null,
- null
- );
+ count = service.countByTitle(type, filter, matchMode, criteria);
}
if(logger.isTraceEnabled()){
- logger.trace("size() - count: " + page.getCount().intValue());
+ logger.trace("size() - count: " + count);
}
- return page.getCount().intValue();
+ return Long.valueOf(count).intValue();
}
/**
}
}
- /**
- * @return
- */
private List<Restriction<?>> prepareRestrictions(String filter, MatchMode matchMode) {
List<Restriction<?>> prepared = new ArrayList<>(restrictions.size());
for(Restriction<?> r : restrictions) {
values.add(v);
}
}
- prepared.add(new Restriction(r.getPropertyName(), r.getOperator(), r.getMatchMode(), values.toArray(new Object[values.size()])));
+ prepared.add(new Restriction<>(r.getPropertyName(), r.getOperator(), r.getMatchMode(), values.toArray(new Object[values.size()])));
}
return prepared;
}
- /**
- *
- */
protected void checkNotMixed() {
if(!restrictions.isEmpty() && !criteria.isEmpty()){
throw new RuntimeException("Citeria and Restrictions must not be used at the same time");
}
}
- /**
- * @return the pageSize
- */
public int getPageSize() {
return pageSize;
}
- /**
- * @param pageSize the pageSize to set
- */
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
- /**
- * @return the initStrategy
- */
public List<String> getInitStrategy() {
return initStrategy;
}
- /**
- * @param initStrategy the initStrategy to set
- */
public void setInitStrategy(List<String> initStrategy) {
this.initStrategy = initStrategy;
}
public void addRestriction(Restriction<?> restriction){
restrictions.add(restriction);
}
-}
+}
\ No newline at end of file