From 27f1f3cbdcb7a9ff117b6cef7d5bd4612616645e Mon Sep 17 00:00:00 2001 From: Josefina Revilla Date: Wed, 20 Sep 2023 16:12:24 -0300 Subject: [PATCH] Implements getItemCount with filters --- .../src/main/java/org/jpos/ee/DBManager.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java b/modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java index eb9a700b45..21226bc138 100644 --- a/modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java +++ b/modules/dbsupport/src/main/java/org/jpos/ee/DBManager.java @@ -55,6 +55,32 @@ public int getItemCount() { return db.session().createQuery(query).getSingleResult().intValue(); } + public int getItemCount(DBFilter... filters) { + CriteriaBuilder cb = db.session().getCriteriaBuilder(); + // CriteriaQuery query = createCriteriaQuery(false, (DBFilter) null); + CriteriaQuery countQuery = cb.createQuery(Long.class); + Root 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 getAll(int offset, int limit, Map orders) { CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder(); CriteriaQuery query = criteriaBuilder.createQuery(clazz);