Search in sources :

Example 6 with Specification

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;
}
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 7 with Specification

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());
}
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 8 with Specification

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;
}
Also used : Charge(com.code.server.db.model.Charge) Specification(org.springframework.data.jpa.domain.Specification)

Example 9 with Specification

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;
}
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 10 with Specification

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);
}
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) AuthorizableType(eu.bcvsolutions.idm.core.security.api.dto.AuthorizableType) Predicate(javax.persistence.criteria.Predicate) AuthorizableService(eu.bcvsolutions.idm.core.security.api.service.AuthorizableService) BasePermission(eu.bcvsolutions.idm.core.security.api.domain.BasePermission)

Aggregations

Specification (org.springframework.data.jpa.domain.Specification)19 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)14 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)14 Root (javax.persistence.criteria.Root)14 Predicate (javax.persistence.criteria.Predicate)12 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)4 Charge (com.code.server.db.model.Charge)3 LocalDateTime (java.time.LocalDateTime)3 Sort (org.springframework.data.domain.Sort)3 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)2 IdmFormAttributeDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormAttributeDto)2 IdmFormDefinitionDto (eu.bcvsolutions.idm.core.eav.api.dto.IdmFormDefinitionDto)2 IdmRole (eu.bcvsolutions.idm.core.model.entity.IdmRole)2 IdmRoleFormValue (eu.bcvsolutions.idm.core.model.entity.eav.IdmRoleFormValue)2 BasePermission (eu.bcvsolutions.idm.core.security.api.domain.BasePermission)2 AuthorizableType (eu.bcvsolutions.idm.core.security.api.dto.AuthorizableType)2 AuthorizableService (eu.bcvsolutions.idm.core.security.api.service.AuthorizableService)2 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)2 DateTimeFormatter (java.time.format.DateTimeFormatter)2