Search in sources :

Example 1 with InternalHibernateGenericStore

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) QueryPlan(org.hisp.dhis.query.planner.QueryPlan) Predicate(javax.persistence.criteria.Predicate) InternalHibernateGenericStore(org.hisp.dhis.hibernate.InternalHibernateGenericStore)

Example 2 with InternalHibernateGenericStore

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();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Schema(org.hisp.dhis.schema.Schema) QueryPlan(org.hisp.dhis.query.planner.QueryPlan) Predicate(javax.persistence.criteria.Predicate) InternalHibernateGenericStore(org.hisp.dhis.hibernate.InternalHibernateGenericStore)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Predicate (javax.persistence.criteria.Predicate)2 InternalHibernateGenericStore (org.hisp.dhis.hibernate.InternalHibernateGenericStore)2 QueryPlan (org.hisp.dhis.query.planner.QueryPlan)2 Schema (org.hisp.dhis.schema.Schema)2 ArrayList (java.util.ArrayList)1