use of org.hisp.dhis.hibernate.InternalHibernateGenericStore in project dhis2-core by dhis2.
the class JpaCriteriaQueryEngine method query.
@Override
public List<T> query(Query query) {
Schema schema = query.getSchema();
Class<T> klass = (Class<T>) schema.getKlass();
InternalHibernateGenericStore<T> store = (InternalHibernateGenericStore<T>) getStore(klass);
if (store == null) {
return new ArrayList<>();
}
if (query.getUser() == null) {
query.setUser(currentUserService.getCurrentUser());
}
if (!query.isPlannedQuery()) {
QueryPlan queryPlan = queryPlanner.planQuery(query, true);
query = queryPlan.getPersistedQuery();
}
CriteriaBuilder builder = sessionFactory.getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = builder.createQuery(klass);
Root<T> root = criteriaQuery.from(klass);
if (query.isEmpty()) {
Predicate predicate = builder.conjunction();
predicate.getExpressions().addAll(store.getSharingPredicates(builder, query.getUser()).stream().map(t -> t.apply(root)).collect(Collectors.toList()));
criteriaQuery.where(predicate);
TypedQuery<T> typedQuery = sessionFactory.getCurrentSession().createQuery(criteriaQuery);
typedQuery.setFirstResult(query.getFirstResult());
typedQuery.setMaxResults(query.getMaxResults());
return typedQuery.getResultList();
}
Predicate predicate = buildPredicates(builder, root, query);
predicate.getExpressions().addAll(store.getSharingPredicates(builder, query.getUser()).stream().map(t -> t.apply(root)).collect(Collectors.toList()));
criteriaQuery.where(predicate);
if (!query.getOrders().isEmpty()) {
criteriaQuery.orderBy(query.getOrders().stream().map(o -> o.isAscending() ? builder.asc(root.get(o.getProperty().getFieldName())) : builder.desc(root.get(o.getProperty().getFieldName()))).collect(Collectors.toList()));
}
TypedQuery<T> typedQuery = sessionFactory.getCurrentSession().createQuery(criteriaQuery);
typedQuery.setFirstResult(query.getFirstResult());
typedQuery.setMaxResults(query.getMaxResults());
if (query.isCacheable()) {
typedQuery.setHint("org.hibernate.cacheable", true);
typedQuery.setHint("org.hibernate.cacheRegion", getQueryCacheRegionName(klass, typedQuery));
}
return typedQuery.getResultList();
}
use of org.hisp.dhis.hibernate.InternalHibernateGenericStore in project dhis2-core by dhis2.
the class JpaCriteriaQueryEngine method count.
@Override
public long count(Query query) {
Schema schema = query.getSchema();
Class<T> klass = (Class<T>) schema.getKlass();
InternalHibernateGenericStore<T> store = (InternalHibernateGenericStore<T>) getStore(klass);
if (store == null) {
return 0;
}
if (query.getUser() == null) {
query.setUser(currentUserService.getCurrentUser());
}
if (!query.isPlannedQuery()) {
QueryPlan queryPlan = queryPlanner.planQuery(query, true);
query = queryPlan.getPersistedQuery();
}
CriteriaBuilder builder = sessionFactory.getCriteriaBuilder();
CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class);
Root<T> root = criteriaQuery.from(klass);
criteriaQuery.select(builder.count(root));
Predicate predicate = buildPredicates(builder, root, query);
predicate.getExpressions().addAll(store.getSharingPredicates(builder, query.getUser()).stream().map(t -> t.apply(root)).collect(Collectors.toList()));
criteriaQuery.where(predicate);
if (!query.getOrders().isEmpty()) {
criteriaQuery.orderBy(query.getOrders().stream().map(o -> o.isAscending() ? builder.asc(root.get(o.getProperty().getName())) : builder.desc(root.get(o.getProperty().getName()))).collect(Collectors.toList()));
}
TypedQuery<Long> typedQuery = sessionFactory.getCurrentSession().createQuery(criteriaQuery);
return typedQuery.getSingleResult();
}
Aggregations