Search in sources :

Example 26 with Pageable

use of org.springframework.data.domain.Pageable in project hse-cws by holuhoev.

the class StudentWorkloadDatasource method getStudentSumWorkload.

public DataSourceResult getStudentSumWorkload(final Integer groupId, final Integer studentId, final Integer facultyId, final Integer instituteId, final Course course, final String studentFio, final EducationType educationType, final LocalDate fromDate, final LocalDate toDate, final Integer top, final Integer skip, final Boolean fetchTotal, final String orderBy) {
    QStudent qStudent = QStudent.student;
    QStudentWorkload qStudentWorkload = QStudentWorkload.studentWorkload;
    BooleanBuilder studentBuilder = new BooleanBuilder();
    BooleanBuilder workloadBuilder = new BooleanBuilder();
    if (groupId != null) {
        studentBuilder.and(qStudent.groupID.eq(groupId));
    }
    if (studentId != null) {
        studentBuilder.and(qStudent.Id.eq(studentId));
    }
    if (facultyId != null) {
        studentBuilder.and(qStudent.facultyID.eq(facultyId));
    }
    if (instituteId != null) {
        studentBuilder.and(qStudent.instituteID.eq(instituteId));
    }
    if (course != null) {
        studentBuilder.and(qStudent.course.eq(course));
    }
    if (studentFio != null && !studentFio.isEmpty()) {
        studentBuilder.and(qStudent.fio.containsIgnoreCase(studentFio));
    }
    if (educationType != null) {
        studentBuilder.and(qStudent.educationType.eq(educationType));
    }
    if (fromDate != null) {
        workloadBuilder.and(qStudentWorkload.date.after(fromDate).or(qStudentWorkload.date.eq(fromDate)));
    }
    if (toDate != null) {
        workloadBuilder.and(qStudentWorkload.date.before(toDate).or(qStudentWorkload.date.before(toDate)));
    }
    Iterable<Student> students;
    if (top != null) {
        String orderByString = isNullOrEmpty(orderBy) ? "fio" : orderBy;
        Pageable limit = PageRequest.of(skip, top, Sort.Direction.ASC, orderByString);
        students = studentRepository.findAll(studentBuilder, limit);
    } else {
        students = studentRepository.findAll(studentBuilder);
    }
    Stream<Student> studentStream = StreamSupport.stream(students.spliterator(), false);
    List<StudentSumWorkload> result = studentStream.map(student -> {
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(workloadBuilder).and(qStudentWorkload.studentId.eq(student.getId()));
        // Если запросили тот период, по которому нет инфы, то запрашивать из руза и добавлять в загруженность
        // Еще надо смотреть, что есть такой date == toDate, если нет, то искать макисмальный, а остальное запросить
        Integer workload = StreamSupport.stream(studentWorkloadRepository.findAll(builder).spliterator(), false).mapToInt(StudentWorkload::getWorkload).sum();
        return new StudentSumWorkload(student.getFio(), workload, student.getId());
    }).collect(Collectors.toList());
    Map<String, Object> hints = new HashMap<>();
    hints.put("paging", true);
    if (fetchTotal != null && fetchTotal) {
        Long count = studentRepository.count(studentBuilder);
        hints.put("total", count);
    }
    return DataSourceResult.create(result, hints);
}
Also used : StudentWorkloadRepository(hse.holuhoev.repo.StudentWorkloadRepository) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) Autowired(org.springframework.beans.factory.annotation.Autowired) PageRequest(org.springframework.data.domain.PageRequest) BooleanBuilder(com.querydsl.core.BooleanBuilder) HashMap(java.util.HashMap) hse.holuhoev.domain(hse.holuhoev.domain) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) DataSourceResult(hse.holuhoev.datasource.util.DataSourceResult) Service(org.springframework.stereotype.Service) LocalDate(java.time.LocalDate) Map(java.util.Map) StudentRepository(hse.holuhoev.repo.StudentRepository) Optional(java.util.Optional) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) StreamSupport(java.util.stream.StreamSupport) HashMap(java.util.HashMap) Pageable(org.springframework.data.domain.Pageable) BooleanBuilder(com.querydsl.core.BooleanBuilder)

Example 27 with Pageable

use of org.springframework.data.domain.Pageable in project JavaForFun by gumartinm.

the class AdDescriptionRevisionServiceShould method test.

@Test
public void test() {
    AdDescriptionRepository adDescriptionRepository = mock(AdDescriptionRepository.class);
    AdDescriptionRevisionServiceImpl adDescriptionRevisionService = new AdDescriptionRevisionServiceImpl();
    adDescriptionRevisionService.setRepository(adDescriptionRepository);
    List<Revision<Integer, AdDescription>> adRevisions = new ArrayList<>();
    Pageable pageRequest = new PageRequest(0, 1);
    Page<Revision<Integer, AdDescription>> page = new PageImpl<>(adRevisions);
    given(adDescriptionRepository.findRevisions(1L, pageRequest)).willReturn(page);
    assertThat(adDescriptionRevisionService.findRevisions(1L, pageRequest), is(page));
}
Also used : PageImpl(org.springframework.data.domain.PageImpl) PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) Revision(org.springframework.data.history.Revision) ArrayList(java.util.ArrayList) AdDescriptionRepository(de.spring.example.persistence.repository.AdDescriptionRepository) Test(org.junit.Test)

Example 28 with Pageable

use of org.springframework.data.domain.Pageable in project tutorials by eugenp.

the class MongoTemplateQueryLiveTest method givenUsersExist_whenFindingByPage_thenUsersAreFoundByPage.

@Test
public void givenUsersExist_whenFindingByPage_thenUsersAreFoundByPage() {
    User user = new User();
    user.setName("Eric");
    user.setAge(45);
    mongoTemplate.insert(user);
    user = new User();
    user.setName("Antony");
    user.setAge(33);
    mongoTemplate.insert(user);
    user = new User();
    user.setName("Alice");
    user.setAge(35);
    mongoTemplate.insert(user);
    final Pageable pageableRequest = new PageRequest(0, 2);
    Query query = new Query();
    query.with(pageableRequest);
    List<User> users = mongoTemplate.find(query, User.class);
    assertThat(users.size(), is(2));
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) User(org.baeldung.model.User) Pageable(org.springframework.data.domain.Pageable) Query(org.springframework.data.mongodb.core.query.Query) Test(org.junit.Test)

Example 29 with Pageable

use of org.springframework.data.domain.Pageable in project tutorials by eugenp.

the class UserRepositoryLiveTest method givenUsersExist_whenFindingAllUsersWithPagination_thenUsersAreFoundAndOrderedOnPage.

@Test
public void givenUsersExist_whenFindingAllUsersWithPagination_thenUsersAreFoundAndOrderedOnPage() {
    User user = new User();
    user.setName("Brendan");
    mongoOps.insert(user);
    user = new User();
    user.setName("Adam");
    mongoOps.insert(user);
    final Pageable pageableRequest = new PageRequest(0, 1);
    final Page<User> page = userRepository.findAll(pageableRequest);
    List<User> users = page.getContent();
    assertThat(users.size(), is(1));
    assertThat(page.getTotalPages(), is(2));
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) User(org.baeldung.model.User) Pageable(org.springframework.data.domain.Pageable) Test(org.junit.Test)

Example 30 with Pageable

use of org.springframework.data.domain.Pageable in project tutorials-java by Artister.

the class ApplicationTests method pageTest.

@Test
public void pageTest() {
    /**
     * @see PageRequest
     * page 从0开始
     * size 大小
     */
    Pageable pageable = new PageRequest(0, 5);
    Page<User> Users = pagingSortRepository.findAll(pageable);
    out.println("总页数" + Users.getTotalPages());
    out.println("总记录数" + Users.getTotalElements());
    out.println("当前第几页" + Users.getNumber() + 1);
    out.println("当前页面的集合" + Users.getContent());
    out.println("当前页面的记录数" + Users.getNumberOfElements());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Pageable(org.springframework.data.domain.Pageable) User(org.ko.web.domain.User) Test(org.junit.Test)

Aggregations

Pageable (org.springframework.data.domain.Pageable)185 PageRequest (org.springframework.data.domain.PageRequest)88 Sort (org.springframework.data.domain.Sort)81 Test (org.junit.Test)39 PageImpl (org.springframework.data.domain.PageImpl)28 ArrayList (java.util.ArrayList)18 Collectors (java.util.stream.Collectors)17 List (java.util.List)15 Page (org.springframework.data.domain.Page)15 Autowired (org.springframework.beans.factory.annotation.Autowired)14 GetMapping (org.springframework.web.bind.annotation.GetMapping)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 Test (org.junit.jupiter.api.Test)11 UUID (java.util.UUID)10 ApiOperation (io.swagger.annotations.ApiOperation)9 Calendar (java.util.Calendar)9 java.util (java.util)8 Lists (com.google.common.collect.Lists)7 Map (java.util.Map)7 Transactional (org.springframework.transaction.annotation.Transactional)6