Search in sources :

Example 66 with CriteriaQuery

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

the class DefaultIdmAutomaticRoleAttributeService method getCriteriaForRulesByContract.

/**
 * Return all criteria for given rules by contract
 * Compose all specification for identity/contract and rules
 *
 * @param automaticRoleId
 * @param rules
 * @param onlyNew
 * @param passed
 * @param identityId
 * @param contractId
 * @return
 */
private Specification<IdmIdentityContract> getCriteriaForRulesByContract(UUID automaticRoleId, List<IdmAutomaticRoleAttributeRuleDto> rules, boolean passed, UUID contractId) {
    Specification<IdmIdentityContract> criteria = new Specification<IdmIdentityContract>() {

        private static final long serialVersionUID = 1L;

        @Override
        public Predicate toPredicate(Root<IdmIdentityContract> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> predicates = new ArrayList<>();
            // 
            if (contractId != null) {
                predicates.add(cb.equal(root.get(AbstractEntity_.id), contractId));
            }
            // If passed add condition for valid now or in future and disabled false
            if (passed) {
                // 
                predicates.add(// 
                cb.and(// 
                cb.or(// 
                cb.greaterThanOrEqualTo(root.get(IdmIdentityContract_.validTill), LocalDate.now()), // 
                cb.isNull(root.get(IdmIdentityContract_.validTill))), // 
                cb.equal(root.get(IdmIdentityContract_.disabled), Boolean.FALSE)));
            // 
            }
            // 
            Subquery<IdmIdentityRole> subquery = query.subquery(IdmIdentityRole.class);
            Root<IdmIdentityRole> subRoot = subquery.from(IdmIdentityRole.class);
            subquery.select(subRoot);
            subquery.where(cb.and(// correlation attr
            cb.equal(subRoot.get(IdmIdentityRole_.identityContract), root), cb.equal(subRoot.get(IdmIdentityRole_.automaticRole).get(IdmAutomaticRole_.id), automaticRoleId)));
            // 
            if (passed) {
                predicates.add(cb.isNull(subquery));
            } else {
                predicates.add(cb.exists(subquery));
            }
            // 
            List<Predicate> predicatesFromRules = new ArrayList<>(rules.size());
            for (IdmAutomaticRoleAttributeRuleDto rule : rules) {
                // compose all predicate from rules
                Predicate predicate = DefaultIdmAutomaticRoleAttributeService.this.getPredicateForRuleByContract(rule, root, query, cb, passed);
                predicatesFromRules.add(predicate);
            }
            // 
            if (!predicatesFromRules.isEmpty()) {
                if (!passed) {
                    // if we find all rules that not pass is necessary add 'or' statement between predicates from rules
                    Predicate or = cb.or(predicatesFromRules.toArray(new Predicate[predicatesFromRules.size()]));
                    predicates.add(or);
                } else {
                    predicates.addAll(predicatesFromRules);
                }
            }
            return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        }
    };
    return criteria;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ArrayList(java.util.ArrayList) Specification(org.springframework.data.jpa.domain.Specification) Predicate(javax.persistence.criteria.Predicate) IdmIdentityRole(eu.bcvsolutions.idm.core.model.entity.IdmIdentityRole) IdmAutomaticRoleAttributeRuleDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleDto) IdmIdentityContract(eu.bcvsolutions.idm.core.model.entity.IdmIdentityContract)

Example 67 with CriteriaQuery

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

the class DefaultFormServiceIntegrationTest method findOwnerByCriteria.

@Test
public void findOwnerByCriteria() {
    IdmRoleDto owner = getHelper().createRole();
    IdmRoleDto ownerTwo = getHelper().createRole();
    IdmFormDefinitionDto formDefinition = formService.getDefinition(IdmRole.class);
    IdmFormAttributeDto attribute = formDefinition.getMappedAttributeByCode("extAttr");
    // 
    formService.saveValues(owner.getId(), IdmRole.class, attribute, Lists.newArrayList("test"));
    formService.saveValues(ownerTwo.getId(), IdmRole.class, attribute, Lists.newArrayList("test2"));
    Specification<IdmRole> criteria = new Specification<IdmRole>() {

        private static final long serialVersionUID = 1L;

        public Predicate toPredicate(Root<IdmRole> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            Subquery<IdmRoleFormValue> subquery = query.subquery(IdmRoleFormValue.class);
            Root<IdmRoleFormValue> subRoot = subquery.from(IdmRoleFormValue.class);
            subquery.select(subRoot);
            Predicate predicate = builder.and(builder.equal(subRoot.get(IdmRoleFormValue_.owner), root), builder.equal(subRoot.get(IdmRoleFormValue_.formAttribute).get(IdmFormAttribute_.id), attribute.getId()), builder.equal(subRoot.get(IdmRoleFormValue_.stringValue), "test"));
            subquery.where(predicate);
            // 
            return query.where(builder.exists(subquery)).getRestriction();
        }
    };
    List<IdmRole> roles = roleRepository.findAll(criteria);
    assertEquals(1, roles.size());
    assertEquals(owner.getId(), roles.get(0).getId());
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) IdmFormDefinitionDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto) Specification(org.springframework.data.jpa.domain.Specification) Predicate(javax.persistence.criteria.Predicate) IdmFormAttributeDto(eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto) IdmRole(eu.bcvsolutions.idm.core.model.entity.IdmRole) IdmRoleFormValue(eu.bcvsolutions.idm.core.model.entity.eav.IdmRoleFormValue) DefaultAttachmentManagerIntegrationTest(eu.bcvsolutions.idm.core.ecm.service.impl.DefaultAttachmentManagerIntegrationTest) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Test(org.junit.Test)

Example 68 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project summer by foxsugar.

the class TodayChargeServiceImpl method findAN.

// 按顺序查询
public List<Charge> findAN(long agentId, String pathValue, Date start, Date end) {
    Specification<Charge> specification = new Specification<Charge>() {

        @Override
        public Predicate toPredicate(Root<Charge> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> predicates = new ArrayList<>();
            predicates.add(cb.equal(root.get(pathValue).as(Long.class), agentId));
            predicates.add(cb.between(root.get("createtime").as(Date.class), start, end));
            predicates.add(cb.equal(root.get("status").as(Integer.class), 1));
            predicates.add(cb.equal(root.get("chargeType").as(Integer.class), MONEY_TYPE));
            ;
            Predicate[] pre = new Predicate[predicates.size()];
            return query.where(predicates.toArray(pre)).getRestriction();
        }
    };
    Sort.Order order1 = new Sort.Order(Sort.Direction.ASC, "userid");
    Sort.Order order2 = new Sort.Order(Sort.Direction.ASC, "orderId");
    Sort sort = new Sort(order1, order2);
    List<Charge> list = chargeDao.findAll(specification, sort);
    return list;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Root(javax.persistence.criteria.Root) Charge(com.code.server.db.model.Charge) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Specification(org.springframework.data.jpa.domain.Specification) Predicate(javax.persistence.criteria.Predicate) Sort(org.springframework.data.domain.Sort)

Example 69 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.

the class ConcatTest method testConcat.

@Test
public void testConcat() throws Exception {
    EntityManager entityManager = getOrCreateEntityManager();
    entityManager.getTransaction().begin();
    try {
        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery query = cb.createQuery();
        Root<TestEntity> testEntity = query.from(TestEntity.class);
        query.select(testEntity).where(cb.equal(testEntity.get("name"), cb.concat("test", cb.literal("_1"))));
        final List results = entityManager.createQuery(query).getResultList();
        entityManager.getTransaction().commit();
        assertThat(results.size(), is(1));
    } catch (Exception e) {
        if (entityManager.getTransaction().isActive()) {
            entityManager.getTransaction().rollback();
        }
        throw e;
    } finally {
        entityManager.close();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) List(java.util.List) Test(org.junit.Test)

Example 70 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project hibernate-orm by hibernate.

the class PredicateTest method testExplicitBuilderBooleanHandling.

@Test
public void testExplicitBuilderBooleanHandling() {
    // just checking syntax of the resulting query
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    // note : these may fail on various matrix db jobs depending on how the dialect handles booleans
    {
        CriteriaQuery<CreditCard> criteriaQuery = builder.createQuery(CreditCard.class);
        Root<CreditCard> root = criteriaQuery.from(CreditCard.class);
        criteriaQuery.where(builder.isFalse(root.get(CreditCard_.approved)));
        em.createQuery(criteriaQuery).getResultList();
    }
    {
        CriteriaQuery<Order> criteriaQuery = builder.createQuery(Order.class);
        Root<Order> root = criteriaQuery.from(Order.class);
        criteriaQuery.where(builder.isFalse(root.get(Order_.creditCard).get(CreditCard_.approved)));
        em.createQuery(criteriaQuery).getResultList();
    }
    em.getTransaction().commit();
    em.close();
}
Also used : Order(org.hibernate.jpa.test.metamodel.Order) EntityManager(javax.persistence.EntityManager) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) CreditCard(org.hibernate.jpa.test.metamodel.CreditCard) Test(org.junit.Test) AbstractMetamodelSpecificTest(org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)

Aggregations

CriteriaQuery (javax.persistence.criteria.CriteriaQuery)155 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)112 EntityManager (javax.persistence.EntityManager)109 Query (javax.persistence.Query)109 TypedQuery (javax.persistence.TypedQuery)69 Predicate (javax.persistence.criteria.Predicate)51 EntityTransaction (javax.persistence.EntityTransaction)45 Root (javax.persistence.criteria.Root)40 List (java.util.List)22 Path (javax.persistence.criteria.Path)18 Test (org.testng.annotations.Test)16 UserTransaction (javax.transaction.UserTransaction)15 Person (org.datanucleus.samples.annotations.models.company.Person)15 Manager (org.datanucleus.samples.jpa.query.Manager)14 Specification (org.springframework.data.jpa.domain.Specification)14 Item (org.jpwh.model.querying.Item)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 Join (javax.persistence.criteria.Join)10 Session (org.hibernate.Session)9