Search in sources :

Example 21 with JpaQueryParameters

use of org.hisp.dhis.hibernate.JpaQueryParameters in project dhis2-core by dhis2.

the class HibernateIdentifiableObjectStore method getByName.

/**
 * Uses query since name property might not be unique.
 */
@Override
public final T getByName(String name) {
    CriteriaBuilder builder = getCriteriaBuilder();
    JpaQueryParameters<T> param = new JpaQueryParameters<T>().addPredicates(getSharingPredicates(builder)).addPredicate(root -> builder.equal(root.get("name"), name));
    List<T> list = getList(builder, param);
    T object = list != null && !list.isEmpty() ? list.get(0) : null;
    if (!isReadAllowed(object, currentUserService.getCurrentUser())) {
        AuditLogUtil.infoWrapper(log, currentUserService.getCurrentUsername(), object, AuditLogUtil.ACTION_READ_DENIED);
        throw new ReadAccessDeniedException(String.valueOf(object));
    }
    return object;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) JpaQueryParameters(org.hisp.dhis.hibernate.JpaQueryParameters)

Example 22 with JpaQueryParameters

use of org.hisp.dhis.hibernate.JpaQueryParameters in project dhis2-core by dhis2.

the class HibernateIdentifiableObjectStore method getAllOrderedName.

@Override
public List<T> getAllOrderedName(int first, int max) {
    CriteriaBuilder builder = getCriteriaBuilder();
    JpaQueryParameters<T> param = new JpaQueryParameters<T>().addPredicates(getSharingPredicates(builder)).addOrder(root -> builder.asc(root.get("name"))).setFirstResult(first).setMaxResults(max);
    return getList(builder, param);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) JpaQueryParameters(org.hisp.dhis.hibernate.JpaQueryParameters)

Example 23 with JpaQueryParameters

use of org.hisp.dhis.hibernate.JpaQueryParameters in project dhis2-core by dhis2.

the class HibernateIdentifiableObjectStore method getCountLikeName.

@Override
public int getCountLikeName(String name) {
    CriteriaBuilder builder = getCriteriaBuilder();
    JpaQueryParameters<T> param = new JpaQueryParameters<T>().addPredicates(getSharingPredicates(builder)).addPredicate(root -> builder.like(builder.lower(root.get("name")), "%" + name.toLowerCase() + "%")).count(root -> builder.countDistinct(root.get("id")));
    return getCount(builder, param).intValue();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) HibernateGenericStore(org.hisp.dhis.hibernate.HibernateGenericStore) CurrentUserGroupInfo(org.hisp.dhis.user.CurrentUserGroupInfo) Date(java.util.Date) CreateAccessDeniedException(org.hisp.dhis.hibernate.exception.CreateAccessDeniedException) JsonbFunctions(org.hisp.dhis.hibernate.jsonb.type.JsonbFunctions) Function(java.util.function.Function) TypedQuery(javax.persistence.TypedQuery) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Lists(com.google.common.collect.Lists) Predicate(javax.persistence.criteria.Predicate) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) CurrentUserServiceTarget(org.hisp.dhis.user.CurrentUserServiceTarget) ObjectUtils(org.apache.commons.lang3.ObjectUtils) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) User(org.hisp.dhis.user.User) InternalHibernateGenericStore(org.hisp.dhis.hibernate.InternalHibernateGenericStore) Root(javax.persistence.criteria.Root) AuditLogUtil(org.hisp.dhis.common.AuditLogUtil) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) AccessStringHelper(org.hisp.dhis.security.acl.AccessStringHelper) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) Collection(java.util.Collection) JpaQueryUtils(org.hisp.dhis.query.JpaQueryUtils) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SessionFactory(org.hibernate.SessionFactory) Set(java.util.Set) JpaQueryParameters(org.hisp.dhis.hibernate.JpaQueryParameters) Collectors(java.util.stream.Collectors) Dashboard(org.hisp.dhis.dashboard.Dashboard) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CurrentUserService(org.hisp.dhis.user.CurrentUserService) AclService(org.hisp.dhis.security.acl.AclService) GenericDimensionalObjectStore(org.hisp.dhis.common.GenericDimensionalObjectStore) SharingUtils(org.hisp.dhis.util.SharingUtils)

Example 24 with JpaQueryParameters

use of org.hisp.dhis.hibernate.JpaQueryParameters in project dhis2-core by dhis2.

the class HibernateIdentifiableObjectStore method getByCode.

@Override
public List<T> getByCode(Collection<String> codes) {
    if (codes == null || codes.isEmpty()) {
        return new ArrayList<>();
    }
    CriteriaBuilder builder = getCriteriaBuilder();
    JpaQueryParameters<T> jpaQueryParameters = new JpaQueryParameters<T>().addPredicates(getSharingPredicates(builder)).addPredicate(root -> root.get("code").in(codes));
    return getList(builder, jpaQueryParameters);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) JpaQueryParameters(org.hisp.dhis.hibernate.JpaQueryParameters)

Example 25 with JpaQueryParameters

use of org.hisp.dhis.hibernate.JpaQueryParameters in project dhis2-core by dhis2.

the class HibernateIdentifiableObjectStore method getCountGeLastUpdated.

@Override
public int getCountGeLastUpdated(Date lastUpdated) {
    CriteriaBuilder builder = getCriteriaBuilder();
    JpaQueryParameters<T> param = new JpaQueryParameters<T>().addPredicates(getSharingPredicates(builder)).addPredicate(root -> builder.greaterThanOrEqualTo(root.get("lastUpdated"), lastUpdated)).count(root -> builder.countDistinct(root.get("id")));
    return getCount(builder, param).intValue();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) HibernateGenericStore(org.hisp.dhis.hibernate.HibernateGenericStore) CurrentUserGroupInfo(org.hisp.dhis.user.CurrentUserGroupInfo) Date(java.util.Date) CreateAccessDeniedException(org.hisp.dhis.hibernate.exception.CreateAccessDeniedException) JsonbFunctions(org.hisp.dhis.hibernate.jsonb.type.JsonbFunctions) Function(java.util.function.Function) TypedQuery(javax.persistence.TypedQuery) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Lists(com.google.common.collect.Lists) Predicate(javax.persistence.criteria.Predicate) UpdateAccessDeniedException(org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException) CurrentUserServiceTarget(org.hisp.dhis.user.CurrentUserServiceTarget) ObjectUtils(org.apache.commons.lang3.ObjectUtils) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) User(org.hisp.dhis.user.User) InternalHibernateGenericStore(org.hisp.dhis.hibernate.InternalHibernateGenericStore) Root(javax.persistence.criteria.Root) AuditLogUtil(org.hisp.dhis.common.AuditLogUtil) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) AccessStringHelper(org.hisp.dhis.security.acl.AccessStringHelper) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DeleteAccessDeniedException(org.hisp.dhis.hibernate.exception.DeleteAccessDeniedException) Collection(java.util.Collection) JpaQueryUtils(org.hisp.dhis.query.JpaQueryUtils) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SessionFactory(org.hibernate.SessionFactory) Set(java.util.Set) JpaQueryParameters(org.hisp.dhis.hibernate.JpaQueryParameters) Collectors(java.util.stream.Collectors) Dashboard(org.hisp.dhis.dashboard.Dashboard) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CurrentUserService(org.hisp.dhis.user.CurrentUserService) AclService(org.hisp.dhis.security.acl.AclService) GenericDimensionalObjectStore(org.hisp.dhis.common.GenericDimensionalObjectStore) SharingUtils(org.hisp.dhis.util.SharingUtils)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)30 JpaQueryParameters (org.hisp.dhis.hibernate.JpaQueryParameters)30 ArrayList (java.util.ArrayList)18 Function (java.util.function.Function)13 List (java.util.List)10 Date (java.util.Date)9 Set (java.util.Set)9 Collectors (java.util.stream.Collectors)9 Predicate (javax.persistence.criteria.Predicate)9 Root (javax.persistence.criteria.Root)9 SessionFactory (org.hibernate.SessionFactory)9 ReadAccessDeniedException (org.hisp.dhis.hibernate.exception.ReadAccessDeniedException)9 AclService (org.hisp.dhis.security.acl.AclService)9 CurrentUserService (org.hisp.dhis.user.CurrentUserService)9 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)8 Lists (com.google.common.collect.Lists)8 Collection (java.util.Collection)8 TypedQuery (javax.persistence.TypedQuery)8 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)8 Slf4j (lombok.extern.slf4j.Slf4j)8