Skip to content

Commit

Permalink
Implements getItemCount with filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfinc committed Sep 20, 2023
1 parent a070780 commit 27f1f3c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ public int getItemCount() {
return db.session().createQuery(query).getSingleResult().intValue();
}

public int getItemCount(DBFilter<T>... filters) {
CriteriaBuilder cb = db.session().getCriteriaBuilder();
// CriteriaQuery<Card> query = createCriteriaQuery(false, (DBFilter<Card>) null);
CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
Root<T> root = countQuery.from(clazz);
Predicate combinedPredicate = cb.and(
Arrays.stream(filters)
.filter(f -> f != null)
.map(f -> f.createPredicate(cb, root))
.toArray(Predicate[]::new)
);

Predicate[] mgrFilters = buildFilters(root);
if (mgrFilters != null) {
Predicate[] nonNullPredicates = Arrays.stream(mgrFilters).filter(f -> f != null).toArray(Predicate[]::new);
Predicate mgrPredicate = cb.and(nonNullPredicates);
combinedPredicate = cb.and(mgrPredicate, combinedPredicate);
}

countQuery.select(cb.count(root));
countQuery.where(combinedPredicate);
Long totalCount = db.session().createQuery(countQuery).getSingleResult();
return totalCount.intValue();

}

public List<T> getAll(int offset, int limit, Map<String,Boolean> orders) {
CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
CriteriaQuery<T> query = criteriaBuilder.createQuery(clazz);
Expand Down

0 comments on commit 27f1f3c

Please sign in to comment.