Search in sources :

Example 1 with Tuple

use of com.querydsl.core.Tuple in project Settler by EmhyrVarEmreis.

the class CategoryService method getCategoriesWithValue.

public ResponseEntity<List<CategoryWithValueDTO>> getCategoriesWithValue(Long userId) {
    if (Objects.isNull(userId) || userId < 0) {
        userId = Security.currentUser().getId();
    }
    QCategory category = QCategory.category;
    QTransaction transaction = QTransaction.transaction;
    List<Tuple> fetch = new JPAQuery<>(entityManager).from(category, transaction).select(category, transaction.value.sum()).where(transaction.creator.id.eq(userId)).where(transaction.categories.contains(category)).groupBy(category.code, category.description).orderBy(transaction.value.sum().desc()).fetch();
    ModelMapper preparedModelMapper = getModelMapper();
    List<CategoryWithValueDTO> categoryWithValueDTOList = new ArrayList<>(fetch.size());
    for (Tuple tuple : fetch) {
        Category c = tuple.get(category);
        Double d = tuple.get(transaction.value.sum());
        if (Objects.nonNull(c)) {
            categoryWithValueDTOList.add(new CategoryWithValueDTO(userId, preparedModelMapper.map(c, CategoryDTO.class), d));
        }
    }
    return new ResponseEntity<>(categoryWithValueDTOList, HttpStatus.OK);
}
Also used : QTransaction(pl.morecraft.dev.settler.domain.QTransaction) ResponseEntity(org.springframework.http.ResponseEntity) Category(pl.morecraft.dev.settler.domain.Category) QCategory(pl.morecraft.dev.settler.domain.QCategory) ArrayList(java.util.ArrayList) CategoryWithValueDTO(pl.morecraft.dev.settler.web.dto.CategoryWithValueDTO) QCategory(pl.morecraft.dev.settler.domain.QCategory) Tuple(com.querydsl.core.Tuple) ModelMapper(org.modelmapper.ModelMapper)

Example 2 with Tuple

use of com.querydsl.core.Tuple in project tutorials by eugenp.

the class QueryDSLIntegrationTest method whenGroupingByTitle_thenReturnsTuples.

@Test
public void whenGroupingByTitle_thenReturnsTuples() {
    QBlogPost blogPost = QBlogPost.blogPost;
    NumberPath<Long> count = Expressions.numberPath(Long.class, "c");
    List<Tuple> userTitleCounts = queryFactory.select(blogPost.title, blogPost.id.count().as(count)).from(blogPost).groupBy(blogPost.title).orderBy(count.desc()).fetch();
    assertEquals("Hello World!", userTitleCounts.get(0).get(blogPost.title));
    assertEquals(new Long(2), userTitleCounts.get(0).get(count));
    assertEquals("My Second Post", userTitleCounts.get(1).get(blogPost.title));
    assertEquals(new Long(1), userTitleCounts.get(1).get(count));
}
Also used : QBlogPost(org.baeldung.querydsl.intro.entities.QBlogPost) Tuple(com.querydsl.core.Tuple)

Example 3 with Tuple

use of com.querydsl.core.Tuple in project querydsl by querydsl.

the class GroupByIterateTest method map22.

@Test
public void map22() {
    CloseableIterator<Map<Integer, String>> results = MAP2_RESULTS.transform(groupBy(postId).iterate(map(commentId, commentText)));
    List<Map<Integer, String>> actual = IteratorAdapter.asList(results);
    Object commentId = null;
    Map<Integer, String> comments = null;
    List<Map<Integer, String>> expected = new LinkedList<Map<Integer, String>>();
    for (Iterator<Tuple> iterator = MAP2_RESULTS.iterate(); iterator.hasNext(); ) {
        Tuple tuple = iterator.next();
        Object[] array = tuple.toArray();
        if (comments == null || !(commentId == array[0] || commentId != null && commentId.equals(array[0]))) {
            comments = new LinkedHashMap<Integer, String>();
            expected.add(comments);
        }
        commentId = array[0];
        @SuppressWarnings("unchecked") Pair<Integer, String> pair = (Pair<Integer, String>) array[1];
        comments.put(pair.getFirst(), pair.getSecond());
    }
    assertEquals(expected.toString(), actual.toString());
}
Also used : Tuple(com.querydsl.core.Tuple) Pair(com.mysema.commons.lang.Pair) Test(org.junit.Test)

Example 4 with Tuple

use of com.querydsl.core.Tuple in project querydsl by querydsl.

the class GroupByIterateTest method map4.

@Test
public void map4() {
    CloseableIterator<Map<Map<Integer, String>, String>> results = MAP4_RESULTS.transform(groupBy(postId).iterate(map(map(postId, commentText), postName)));
    List<Map<Map<Integer, String>, String>> actual = IteratorAdapter.asList(results);
    Object commentId = null;
    Map<Map<Integer, String>, String> comments = null;
    List<Map<Map<Integer, String>, String>> expected = new LinkedList<Map<Map<Integer, String>, String>>();
    for (Iterator<Tuple> iterator = MAP4_RESULTS.iterate(); iterator.hasNext(); ) {
        Tuple tuple = iterator.next();
        Object[] array = tuple.toArray();
        if (comments == null || !(commentId == array[0] || commentId != null && commentId.equals(array[0]))) {
            comments = new LinkedHashMap<Map<Integer, String>, String>();
            expected.add(comments);
        }
        commentId = array[0];
        @SuppressWarnings("unchecked") Pair<Pair<Integer, String>, String> pair = (Pair<Pair<Integer, String>, String>) array[1];
        Pair<Integer, String> first = pair.getFirst();
        Map<Integer, String> posts = Collections.singletonMap(first.getFirst(), first.getSecond());
        comments.put(posts, pair.getSecond());
    }
    assertEquals(expected.toString(), actual.toString());
}
Also used : Tuple(com.querydsl.core.Tuple) Pair(com.mysema.commons.lang.Pair) Test(org.junit.Test)

Example 5 with Tuple

use of com.querydsl.core.Tuple in project querydsl by querydsl.

the class GroupByMapTest method map3.

@Test
public void map3() {
    Map<Integer, Map<Integer, Map<Integer, String>>> actual = MAP3_RESULTS.transform(groupBy(postId).as(map(postId, map(commentId, commentText))));
    Map<Integer, Map<Integer, Map<Integer, String>>> expected = new LinkedHashMap<Integer, Map<Integer, Map<Integer, String>>>();
    for (Iterator<Tuple> iterator = MAP3_RESULTS.iterate(); iterator.hasNext(); ) {
        Tuple tuple = iterator.next();
        Object[] array = tuple.toArray();
        Map<Integer, Map<Integer, String>> posts = expected.get(array[0]);
        if (posts == null) {
            posts = new LinkedHashMap<Integer, Map<Integer, String>>();
            expected.put((Integer) array[0], posts);
        }
        @SuppressWarnings("unchecked") Pair<Integer, Pair<Integer, String>> pair = (Pair<Integer, Pair<Integer, String>>) array[1];
        Integer first = pair.getFirst();
        Map<Integer, String> comments = posts.computeIfAbsent(first, k -> new LinkedHashMap<Integer, String>());
        Pair<Integer, String> second = pair.getSecond();
        comments.put(second.getFirst(), second.getSecond());
    }
    assertEquals(expected.toString(), actual.toString());
}
Also used : Tuple(com.querydsl.core.Tuple) Pair(com.mysema.commons.lang.Pair) Test(org.junit.Test)

Aggregations

Tuple (com.querydsl.core.Tuple)55 Test (org.junit.Test)41 ExcludeIn (com.querydsl.core.testutil.ExcludeIn)10 Pair (com.mysema.commons.lang.Pair)7 QTuple (com.querydsl.core.types.QTuple)6 Expression (com.querydsl.core.types.Expression)4 StringPath (com.querydsl.core.types.dsl.StringPath)4 AbstractBaseTest (com.querydsl.sql.AbstractBaseTest)4 QCat (com.querydsl.jpa.domain.QCat)3 SAnimal (com.querydsl.jpa.domain.sql.SAnimal)3 QEmployee (com.querydsl.sql.domain.QEmployee)3 ArrayList (java.util.ArrayList)3 Ignore (org.junit.Ignore)3 QObject (com.evolveum.midpoint.repo.sqale.qmodel.object.QObject)2 ResultListRowTransformer (com.evolveum.midpoint.repo.sqlbase.mapping.ResultListRowTransformer)2 GetOperationOptions (com.evolveum.midpoint.schema.GetOperationOptions)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 SystemException (com.evolveum.midpoint.util.exception.SystemException)2 QueryMetadata (com.querydsl.core.QueryMetadata)2 Group (com.querydsl.core.group.Group)2