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;
}
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());
}
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;
}
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();
}
}
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();
}
Aggregations