Search in sources :

Example 56 with Predicate

use of javax.persistence.criteria.Predicate in project CzechIdMng by bcvsolutions.

the class DefaultIdmRoleService method toPredicates.

@Override
protected List<Predicate> toPredicates(Root<IdmRole> root, CriteriaQuery<?> query, CriteriaBuilder builder, IdmRoleFilter filter) {
    List<Predicate> predicates = super.toPredicates(root, query, builder, filter);
    // quick
    if (StringUtils.isNotEmpty(filter.getText())) {
        predicates.add(builder.or(builder.like(builder.lower(root.get(IdmRole_.name)), "%" + filter.getText().toLowerCase() + "%"), builder.like(builder.lower(root.get(IdmRole_.description)), "%" + filter.getText().toLowerCase() + "%")));
    }
    // role type
    if (filter.getRoleType() != null) {
        predicates.add(builder.equal(root.get(IdmRole_.roleType), filter.getRoleType()));
    }
    // property
    if (StringUtils.equals(IdmRole_.name.getName(), filter.getProperty())) {
        predicates.add(builder.equal(root.get(IdmRole_.name), filter.getValue()));
    }
    // guarantee
    if (filter.getGuaranteeId() != null) {
        Subquery<IdmRoleGuarantee> subquery = query.subquery(IdmRoleGuarantee.class);
        Root<IdmRoleGuarantee> subRoot = subquery.from(IdmRoleGuarantee.class);
        subquery.select(subRoot);
        subquery.where(builder.and(// correlation attr
        builder.equal(subRoot.get(IdmRoleGuarantee_.role), root), builder.equal(subRoot.get(IdmRoleGuarantee_.guarantee).get(IdmIdentity_.id), filter.getGuaranteeId())));
        predicates.add(builder.exists(subquery));
    }
    // role catalogue by forest index
    if (filter.getRoleCatalogueId() != null) {
        // TODO: use subquery - see DefaultIdmIdentityService#toPredicates
        IdmRoleCatalogue roleCatalogue = roleCatalogueRepository.findOne(filter.getRoleCatalogueId());
        Subquery<IdmRoleCatalogueRole> subquery = query.subquery(IdmRoleCatalogueRole.class);
        Root<IdmRoleCatalogueRole> subRoot = subquery.from(IdmRoleCatalogueRole.class);
        subquery.select(subRoot);
        subquery.where(builder.and(// correlation attr
        builder.equal(subRoot.get(IdmRoleCatalogueRole_.role), root), builder.between(subRoot.get(IdmRoleCatalogueRole_.roleCatalogue).get(IdmRoleCatalogue_.forestIndex).get(IdmForestIndexEntity_.lft), roleCatalogue.getLft(), roleCatalogue.getRgt())));
        predicates.add(builder.exists(subquery));
    }
    // 
    return predicates;
}
Also used : IdmRoleGuarantee(eu.bcvsolutions.idm.core.model.entity.IdmRoleGuarantee) IdmRoleCatalogue(eu.bcvsolutions.idm.core.model.entity.IdmRoleCatalogue) IdmRoleCatalogueRole(eu.bcvsolutions.idm.core.model.entity.IdmRoleCatalogueRole) Predicate(javax.persistence.criteria.Predicate)

Example 57 with Predicate

use of javax.persistence.criteria.Predicate in project CzechIdMng by bcvsolutions.

the class DefaultIdmTreeNodeService method toPredicates.

@Override
protected List<Predicate> toPredicates(Root<IdmTreeNode> root, CriteriaQuery<?> query, CriteriaBuilder builder, IdmTreeNodeFilter filter) {
    List<Predicate> predicates = super.toPredicates(root, query, builder, filter);
    // fulltext
    if (!StringUtils.isEmpty(filter.getText())) {
        predicates.add(builder.or(builder.like(builder.lower(root.get(IdmTreeNode_.code)), "%" + filter.getText().toLowerCase() + "%"), builder.like(builder.lower(root.get(IdmTreeNode_.name)), "%" + filter.getText().toLowerCase() + "%")));
    }
    // tree type
    if (filter.getTreeTypeId() != null) {
        predicates.add(builder.equal(root.get(IdmTreeNode_.treeType).get(AbstractEntity_.id), filter.getTreeTypeId()));
    }
    // parent node
    if (filter.getTreeNode() != null) {
        // TODO: bug - forest index needs tree type => same numbers in different trees
        if (filter.isRecursively()) {
            Subquery<IdmTreeNode> subquery = query.subquery(IdmTreeNode.class);
            Root<IdmTreeNode> subRoot = subquery.from(IdmTreeNode.class);
            subquery.select(subRoot);
            Path<IdmForestIndexEntity> forestIndexPath = subRoot.get(IdmTreeNode_.forestIndex);
            subquery.where(builder.and(builder.equal(subRoot.get(AbstractEntity_.id), filter.getTreeNode()), // Remember that between clause looks like this a >= x <= b, where a and b are boundaries, in our case lft+1 and rgt-1.
            builder.between(root.get(IdmTreeNode_.forestIndex).get(IdmForestIndexEntity_.lft), builder.sum(forestIndexPath.get(IdmForestIndexEntity_.lft), 1L), builder.diff(forestIndexPath.get(IdmForestIndexEntity_.rgt), 1L))));
            predicates.add(builder.exists(subquery));
        } else {
            predicates.add(builder.equal(root.get(IdmTreeNode_.parent).get(AbstractEntity_.id), filter.getTreeNode()));
        }
    }
    // default tree type
    if (filter.getDefaultTreeType() != null) {
        IdmTreeTypeDto defaultTreeType = treeTypeService.getDefaultTreeType();
        if (defaultTreeType == null) {
            // nothing to find
            predicates.add(builder.disjunction());
        } else {
            predicates.add(builder.equal(root.get(IdmTreeNode_.treeType).get(IdmTreeType_.id), defaultTreeType.getId()));
        }
    }
    // dyn property
    if (filter.getProperty() != null) {
        switch(filter.getProperty()) {
            case "name":
                predicates.add(builder.equal(root.get(IdmTreeNode_.name), filter.getValue()));
                break;
            case "code":
                predicates.add(builder.equal(root.get(IdmTreeNode_.code), filter.getValue()));
                break;
            case "externalId":
                predicates.add(builder.equal(root.get(IdmTreeNode_.externalId), filter.getValue()));
                break;
        }
    }
    // 
    return predicates;
}
Also used : IdmTreeTypeDto(eu.bcvsolutions.idm.core.api.dto.IdmTreeTypeDto) IdmTreeNode(eu.bcvsolutions.idm.core.model.entity.IdmTreeNode) IdmForestIndexEntity(eu.bcvsolutions.idm.core.model.entity.IdmForestIndexEntity) Predicate(javax.persistence.criteria.Predicate)

Example 58 with Predicate

use of javax.persistence.criteria.Predicate in project jo-client-platform by jo-source.

the class CriteriaQueryCreator method createContainsAllPredicate.

@SuppressWarnings("unchecked")
private Predicate createContainsAllPredicate(final CriteriaBuilder criteriaBuilder, final IConverter<Object, Object> converter, final AbstractQuery<?> query, final IArithmeticFilter filter, final Path<?> path, final boolean doFilterInversion) {
    final Collection<?> params = Arrays.asList(getParameters(filter, converter));
    final Collection<Expression<String>> newParams = new HashSet<Expression<String>>();
    final boolean toUpper = caseInsensitive && path.getJavaType() == String.class;
    for (final Object p : params) {
        if (p != null) {
            newParams.add(toExpression(p.toString(), criteriaBuilder));
        }
    }
    final Subquery<Long> subquery = query.subquery(Long.class);
    subquery.select(criteriaBuilder.count(criteriaBuilder.literal(1))).where((toUpper ? criteriaBuilder.upper((Expression<String>) path) : path).in(newParams.toArray(new Expression[newParams.size()])));
    final Predicate result = criteriaBuilder.ge(subquery, newParams.size());
    return invertPredicateIfNeeded(criteriaBuilder, result, filter, doFilterInversion);
}
Also used : Expression(javax.persistence.criteria.Expression) HashSet(java.util.HashSet) Predicate(javax.persistence.criteria.Predicate)

Example 59 with Predicate

use of javax.persistence.criteria.Predicate in project jo-client-platform by jo-source.

the class CriteriaQueryCreator method fillQuery.

@SuppressWarnings("unchecked")
private Root<?> fillQuery(final CriteriaQuery<?> query, final CriteriaBuilder criteriaBuilder, final Class<?> persistenceClass, final List<? extends IBeanKey> parentBeanKeys, final IFilter filter, final PARAM_TYPE parameter) {
    final Root<?> bean = query.from(persistenceClass);
    final List<Predicate> predicates = new LinkedList<Predicate>();
    final List<Object> parentIds = new LinkedList<Object>();
    if (parentBeanKeys != null) {
        for (final IBeanKey parentBeanKey : parentBeanKeys) {
            parentIds.add(parentBeanKey.getId());
        }
    }
    for (final IPredicateCreator<PARAM_TYPE> predicateCreator : predicateCreators) {
        final Predicate predicate = predicateCreator.createPredicate(criteriaBuilder, bean, query, (List<IBeanKey>) parentBeanKeys, parentIds, parameter);
        if (predicate != null) {
            predicates.add(predicate);
        }
    }
    for (final IFilter customFilter : filters) {
        predicates.add(createFilterPredicate(criteriaBuilder, bean, query, customFilter, parameter));
    }
    if (filter != null) {
        predicates.add(createFilterPredicate(criteriaBuilder, bean, query, filter, parameter));
    }
    query.where(predicates.toArray(new Predicate[predicates.size()]));
    return bean;
}
Also used : IBeanKey(org.jowidgets.cap.common.api.bean.IBeanKey) IFilter(org.jowidgets.cap.common.api.filter.IFilter) LinkedList(java.util.LinkedList) Predicate(javax.persistence.criteria.Predicate)

Aggregations

Predicate (javax.persistence.criteria.Predicate)59 EntityManager (javax.persistence.EntityManager)19 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)19 Test (org.junit.Test)17 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)11 ArrayList (java.util.ArrayList)9 Order (org.hibernate.jpa.test.metamodel.Order)8 HibernateException (org.hibernate.HibernateException)6 DAOException (org.jbei.ice.storage.DAOException)6 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)5 Root (javax.persistence.criteria.Root)5 LinkedList (java.util.LinkedList)3 List (java.util.List)3 TypedQuery (javax.persistence.TypedQuery)3 Study (com.odysseusinc.arachne.portal.model.Study)2 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)2 IdmIdentityContract (eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract)2 IdmIdentityRole (eu.bcvsolutions.idm.core.model.entity.IdmIdentityRole)2 IdmRoleCatalogueRole (eu.bcvsolutions.idm.core.model.entity.IdmRoleCatalogueRole)2 IdmTreeNode (eu.bcvsolutions.idm.core.model.entity.IdmTreeNode)2