Search in sources :

Example 1 with JpaRepositoryFilterBase

use of io.crnk.jpa.JpaRepositoryFilterBase 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 JpaRepositoryFilterBase

use of io.crnk.jpa.JpaRepositoryFilterBase in project crnk-framework by crnk-project.

the class JpaListenerTest method testaAddRemove.

@Test
public void testaAddRemove() throws InstantiationException, IllegalAccessException {
    JpaRepositoryFilterBase filter = Mockito.spy(new JpaRepositoryFilterBase());
    module.addFilter(filter);
    Assert.assertEquals(1, module.getFilters().size());
    module.removeFilter(filter);
    Assert.assertEquals(0, module.getFilters().size());
}
Also used : JpaRepositoryFilterBase(io.crnk.jpa.JpaRepositoryFilterBase) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Example 3 with JpaRepositoryFilterBase

use of io.crnk.jpa.JpaRepositoryFilterBase in project crnk-framework by crnk-project.

the class JpaListenerTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() throws InstantiationException, IllegalAccessException {
    JpaRepositoryFilterBase filter = Mockito.spy(new JpaRepositoryFilterBase());
    module.addFilter(filter);
    QuerySpec querySpec = new QuerySpec(TestEntity.class);
    ResourceList<TestEntity> list = repo.findAll(querySpec);
    Assert.assertEquals(5, list.size());
    Mockito.verify(filter, Mockito.times(1)).filterQuerySpec(Mockito.eq(repo), Mockito.eq(querySpec));
    Mockito.verify(filter, Mockito.times(1)).filterResults(Mockito.eq(repo), Mockito.eq(querySpec), Mockito.eq(list));
    Mockito.verify(filter, Mockito.times(1)).filterExecutor(Mockito.eq(repo), Mockito.eq(querySpec), Mockito.any(JpaQueryExecutor.class));
    Mockito.verify(filter, Mockito.times(1)).filterTuples(Mockito.eq(repo), Mockito.eq(querySpec), Mockito.anyList());
    Mockito.verify(filter, Mockito.times(1)).filterQuery(Mockito.eq(repo), Mockito.eq(querySpec), Mockito.any(JpaQuery.class));
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) JpaRepositoryFilterBase(io.crnk.jpa.JpaRepositoryFilterBase) QuerySpec(io.crnk.core.queryspec.QuerySpec) JpaQuery(io.crnk.jpa.query.JpaQuery) JpaQueryExecutor(io.crnk.jpa.query.JpaQueryExecutor) Test(org.junit.Test) AbstractJpaTest(io.crnk.jpa.query.AbstractJpaTest)

Aggregations

JpaRepositoryFilterBase (io.crnk.jpa.JpaRepositoryFilterBase)3 QuerySpec (io.crnk.core.queryspec.QuerySpec)2 AbstractJpaTest (io.crnk.jpa.query.AbstractJpaTest)2 JpaQuery (io.crnk.jpa.query.JpaQuery)2 Test (org.junit.Test)2 JPAQuery (com.querydsl.jpa.impl.JPAQuery)1 QTestEntity (io.crnk.jpa.model.QTestEntity)1 TestEntity (io.crnk.jpa.model.TestEntity)1 JpaQueryExecutor (io.crnk.jpa.query.JpaQueryExecutor)1 QuerydslExpressionFactory (io.crnk.jpa.query.querydsl.QuerydslExpressionFactory)1 QuerydslQueryFactory (io.crnk.jpa.query.querydsl.QuerydslQueryFactory)1 EntityManager (javax.persistence.EntityManager)1