Search in sources :

Example 1 with DataSourceResult

use of hse.holuhoev.datasource.util.DataSourceResult 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 2 with DataSourceResult

use of hse.holuhoev.datasource.util.DataSourceResult in project hse-cws by holuhoev.

the class LecturerWorkloadDatasource method getLecturerSumWorkload.

public DataSourceResult getLecturerSumWorkload(final Integer chairId, final String fio, final LocalDate fromDate, final LocalDate toDate, final Integer top, final Integer skip, final Boolean fetchTotal) {
    QLecturer qLecturer = QLecturer.lecturer;
    QLecturerWorkload qLecturerWorkload = QLecturerWorkload.lecturerWorkload;
    BooleanBuilder lecturerBuilder = new BooleanBuilder();
    BooleanBuilder workloadBuilder = new BooleanBuilder();
    if (chairId != null) {
        lecturerBuilder.and(qLecturer.chairId.eq(chairId));
    }
    if (fio != null && !fio.isEmpty()) {
        lecturerBuilder.and(qLecturer.fio.containsIgnoreCase(fio));
    }
    if (fromDate != null) {
        workloadBuilder.and(qLecturerWorkload.date.after(fromDate));
    }
    if (toDate != null) {
        workloadBuilder.and(qLecturerWorkload.date.before(toDate));
    }
    Iterable<Lecturer> lecturers;
    if (top != null) {
        String orderByString = "fio";
        Pageable limit = PageRequest.of(skip, top, Sort.Direction.ASC, orderByString);
        lecturers = lecturerRepository.findAll(lecturerBuilder, limit);
    } else {
        lecturers = lecturerRepository.findAll(lecturerBuilder);
    }
    List<LecturerSumWorkload> result = StreamSupport.stream(lecturers.spliterator(), false).map(lecturer -> {
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(workloadBuilder).and(qLecturerWorkload.lecturerId.eq(lecturer.getId()));
        Integer workload = StreamSupport.stream(lecturerWorkloadRepository.findAll(builder).spliterator(), false).mapToInt(LecturerWorkload::getWorkload).sum();
        return new LecturerSumWorkload(lecturer.getFio(), workload, lecturer.getId());
    }).collect(Collectors.toList());
    Map<String, Object> hints = new HashMap<>();
    hints.put("paging", true);
    if (fetchTotal != null && fetchTotal) {
        Long count = lecturerWorkloadRepository.count(lecturerBuilder);
        hints.put("total", count);
    }
    return DataSourceResult.create(result, hints);
}
Also used : LecturerRepository(hse.holuhoev.repo.LecturerRepository) Autowired(org.springframework.beans.factory.annotation.Autowired) PageRequest(org.springframework.data.domain.PageRequest) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) BooleanBuilder(com.querydsl.core.BooleanBuilder) HashMap(java.util.HashMap) hse.holuhoev.domain(hse.holuhoev.domain) Collectors(java.util.stream.Collectors) List(java.util.List) DataSourceResult(hse.holuhoev.datasource.util.DataSourceResult) Service(org.springframework.stereotype.Service) LocalDate(java.time.LocalDate) Map(java.util.Map) LecturerWorkloadRepository(hse.holuhoev.repo.LecturerWorkloadRepository) 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)

Aggregations

Strings.isNullOrEmpty (com.google.common.base.Strings.isNullOrEmpty)2 BooleanBuilder (com.querydsl.core.BooleanBuilder)2 DataSourceResult (hse.holuhoev.datasource.util.DataSourceResult)2 hse.holuhoev.domain (hse.holuhoev.domain)2 LocalDate (java.time.LocalDate)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 StreamSupport (java.util.stream.StreamSupport)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 PageRequest (org.springframework.data.domain.PageRequest)2 Pageable (org.springframework.data.domain.Pageable)2 Sort (org.springframework.data.domain.Sort)2 Service (org.springframework.stereotype.Service)2 LecturerRepository (hse.holuhoev.repo.LecturerRepository)1 LecturerWorkloadRepository (hse.holuhoev.repo.LecturerWorkloadRepository)1 StudentRepository (hse.holuhoev.repo.StudentRepository)1 StudentWorkloadRepository (hse.holuhoev.repo.StudentWorkloadRepository)1