use of org.springframework.data.jpa.domain.Specification 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 org.springframework.data.jpa.domain.Specification 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 org.springframework.data.jpa.domain.Specification in project summer by foxsugar.
the class HomeServiceImpl method findChargeByUserId.
@Override
public Charge findChargeByUserId(long userId) {
Specification<Charge> specification = new Specification<Charge>() {
@Override
public Predicate toPredicate(Root<Charge> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Path path = root.get("userid");
Predicate predicate = path.as(Long.class).in(Arrays.asList(userId));
return predicate;
}
};
Charge charge = chargeDao.findOne(specification);
return charge;
}
use of org.springframework.data.jpa.domain.Specification 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 org.springframework.data.jpa.domain.Specification in project CzechIdMng by bcvsolutions.
the class AbstractReadDtoService method findEntities.
protected Page<E> findEntities(F filter, Pageable pageable, BasePermission... permission) {
// transform filter to criteria
Specification<E> criteria = new Specification<E>() {
public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
List<Predicate> predicates = new ArrayList<>();
// if filter is null, no filter predicates will be built
if (filter != null) {
predicates.addAll(AbstractReadDtoService.this.toPredicates(root, query, builder, filter));
}
//
// permisions are not evaluated, if no permission was given or authorizable type is null (=> authorization policies are not supported)
BasePermission[] permissions = PermissionUtils.trimNull(permission);
if (!ObjectUtils.isEmpty(permissions) && (AbstractReadDtoService.this instanceof AuthorizableService)) {
AuthorizableType authorizableType = ((AuthorizableService<?>) AbstractReadDtoService.this).getAuthorizableType();
if (authorizableType != null && authorizableType.getType() != null) {
predicates.add(getAuthorizationManager().getPredicate(root, query, builder, permissions));
}
}
//
return query.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
}
};
return getRepository().findAll(criteria, pageable);
}
Aggregations