Search in sources :

Example 1 with QuerydslExpressionFactory

use of io.crnk.jpa.query.querydsl.QuerydslExpressionFactory in project crnk-framework by crnk-project.

the class DtoMappingTest method setupModule.

@Override
protected void setupModule(JpaModule module, boolean server) {
    super.setupModule(module, server);
    if (server) {
        EntityManager entityManager = module.getEntityManager();
        QuerydslExpressionFactory<QTestEntity> basicComputedValueFactory = new QuerydslExpressionFactory<QTestEntity>() {

            @Override
            public Expression<String> getExpression(QTestEntity parent, JPAQuery<?> jpaQuery) {
                return parent.stringValue.upper();
            }
        };
        QuerydslExpressionFactory<QTestEntity> complexComputedValueFactory = new QuerydslExpressionFactory<QTestEntity>() {

            @Override
            public Expression<Long> getExpression(QTestEntity parent, JPAQuery<?> jpaQuery) {
                QTestEntity root = QTestEntity.testEntity;
                QTestEntity sub = new QTestEntity("subquery");
                return JPAExpressions.select(sub.id.count()).from(sub).where(sub.id.lt(root.id));
            }
        };
        QuerydslQueryFactory queryFactory = (QuerydslQueryFactory) module.getQueryFactory();
        queryFactory.registerComputedAttribute(TestEntity.class, TestDTO.ATTR_COMPUTED_UPPER_STRING_VALUE, String.class, basicComputedValueFactory);
        queryFactory.registerComputedAttribute(TestEntity.class, TestDTO.ATTR_COMPUTED_NUMBER_OF_SMALLER_IDS, Long.class, complexComputedValueFactory);
        module.addRepository(JpaRepositoryConfig.builder(TestEntity.class, TestDTO.class, new TestDTOMapper(entityManager)).build());
        module.addRepository(JpaRepositoryConfig.builder(RelatedEntity.class, RelatedDTO.class, new RelatedDTOMapper(entityManager)).build());
        module.addFilter(new JpaRepositoryFilterBase() {

            @Override
            public <T> JpaQuery<T> filterQuery(Object repository, QuerySpec querySpec, JpaQuery<T> query) {
                query.setDistinct(true);
                return query;
            }
        });
    }
}
Also used : QuerydslQueryFactory(io.crnk.jpa.query.querydsl.QuerydslQueryFactory) JPAQuery(com.querydsl.jpa.impl.JPAQuery) JpaQuery(io.crnk.jpa.query.JpaQuery) QuerydslExpressionFactory(io.crnk.jpa.query.querydsl.QuerydslExpressionFactory) EntityManager(javax.persistence.EntityManager) QTestEntity(io.crnk.jpa.model.QTestEntity) JpaRepositoryFilterBase(io.crnk.jpa.JpaRepositoryFilterBase) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 2 with QuerydslExpressionFactory

use of io.crnk.jpa.query.querydsl.QuerydslExpressionFactory in project crnk-framework by crnk-project.

the class QuerydslQueryBackend method getAttribute.

@Override
public Expression<?> getAttribute(final Expression<?> expression, MetaAttribute pathElement) {
    if (pathElement instanceof MetaComputedAttribute) {
        ComputedAttributeRegistryImpl virtualAttrs = queryImpl.getComputedAttrs();
        QuerydslExpressionFactory expressionFactory = (QuerydslExpressionFactory) virtualAttrs.get((MetaComputedAttribute) pathElement);
        return expressionFactory.getExpression(expression, getQuery());
    } else {
        return QuerydslUtils.get(expression, pathElement.getName());
    }
}
Also used : QuerydslExpressionFactory(io.crnk.jpa.query.querydsl.QuerydslExpressionFactory) ComputedAttributeRegistryImpl(io.crnk.jpa.internal.query.ComputedAttributeRegistryImpl) MetaComputedAttribute(io.crnk.jpa.internal.query.MetaComputedAttribute)

Example 3 with QuerydslExpressionFactory

use of io.crnk.jpa.query.querydsl.QuerydslExpressionFactory in project crnk-framework by crnk-project.

the class QuerydslQueryBackend method doJoin.

@Override
public Expression<?> doJoin(MetaAttribute targetAttr, JoinType joinType, Expression<?> parent) {
    if (targetAttr instanceof MetaComputedAttribute) {
        MetaComputedAttribute computedAttr = (MetaComputedAttribute) targetAttr;
        QuerydslExpressionFactory expressionFactory = (QuerydslExpressionFactory<?>) queryImpl.getComputedAttrs().get(computedAttr);
        return expressionFactory.getExpression(parent, getQuery());
    } else {
        Expression<Object> expression = QuerydslUtils.get(parent, targetAttr.getName());
        querydslQuery.getMetadata().addJoin(QuerydslUtils.convertJoinType(joinType), expression);
        return expression;
    }
}
Also used : QuerydslExpressionFactory(io.crnk.jpa.query.querydsl.QuerydslExpressionFactory) MetaDataObject(io.crnk.meta.model.MetaDataObject) MetaComputedAttribute(io.crnk.jpa.internal.query.MetaComputedAttribute)

Aggregations

QuerydslExpressionFactory (io.crnk.jpa.query.querydsl.QuerydslExpressionFactory)3 MetaComputedAttribute (io.crnk.jpa.internal.query.MetaComputedAttribute)2 JPAQuery (com.querydsl.jpa.impl.JPAQuery)1 QuerySpec (io.crnk.core.queryspec.QuerySpec)1 JpaRepositoryFilterBase (io.crnk.jpa.JpaRepositoryFilterBase)1 ComputedAttributeRegistryImpl (io.crnk.jpa.internal.query.ComputedAttributeRegistryImpl)1 QTestEntity (io.crnk.jpa.model.QTestEntity)1 JpaQuery (io.crnk.jpa.query.JpaQuery)1 QuerydslQueryFactory (io.crnk.jpa.query.querydsl.QuerydslQueryFactory)1 MetaDataObject (io.crnk.meta.model.MetaDataObject)1 EntityManager (javax.persistence.EntityManager)1