Search in sources :

Example 21 with SearchResult

use of fi.otavanopisto.muikku.search.SearchResult in project muikku by otavanopisto.

the class NoPassedCoursesNotificationStrategy method getStudentsToNotify.

public List<SchoolDataIdentifier> getStudentsToNotify() {
    Collection<Long> groups = getGroups();
    if (groups.isEmpty()) {
        return Collections.emptyList();
    }
    Date thresholdDate = Date.from(OffsetDateTime.now().minusDays(NOTIFICATION_THRESHOLD_DAYS).toInstant());
    List<SchoolDataIdentifier> studentIdentifierAlreadyNotified = noPassedCoursesNotificationController.listNotifiedSchoolDataIdentifiersAfter(thresholdDate);
    SearchResult searchResult = noPassedCoursesNotificationController.searchActiveStudentIds(groups, FIRST_RESULT + offset, MAX_RESULTS, studentIdentifierAlreadyNotified, thresholdDate);
    logger.log(Level.INFO, String.format("%s processing %d/%d", getClass().getSimpleName(), offset, searchResult.getTotalHitCount()));
    if ((offset + MAX_RESULTS) > searchResult.getTotalHitCount()) {
        offset = 0;
    } else {
        offset += MAX_RESULTS;
    }
    List<SchoolDataIdentifier> studentIdentifiers = new ArrayList<>();
    for (Map<String, Object> result : searchResult.getResults()) {
        String studentId = (String) result.get("id");
        if (StringUtils.isBlank(studentId)) {
            logger.severe("Could not process user found from search index because it had a null id");
            continue;
        }
        String[] studentIdParts = studentId.split("/", 2);
        SchoolDataIdentifier studentIdentifier = studentIdParts.length == 2 ? new SchoolDataIdentifier(studentIdParts[0], studentIdParts[1]) : null;
        if (studentIdentifier == null) {
            logger.severe(String.format("Could not process user found from search index with id %s", studentId));
            continue;
        }
        User student = userController.findUserByIdentifier(studentIdentifier);
        if ((student != null) && isNotifiedStudent(student.getStudyStartDate(), student.getStudyEndDate(), OffsetDateTime.now(), NOTIFICATION_THRESHOLD_DAYS)) {
            Long passedCourseCount = noPassedCoursesNotificationController.countPassedCoursesByStudentIdentifierSince(studentIdentifier, Date.from(student.getStudyStartDate().toInstant()));
            if (passedCourseCount == null) {
                logger.severe(String.format("Could not read course count for %s", studentId));
                continue;
            } else if (passedCourseCount < MIN_PASSED_COURSES) {
                studentIdentifiers.add(studentIdentifier);
            }
        }
    }
    return studentIdentifiers;
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) User(fi.otavanopisto.muikku.schooldata.entity.User) ArrayList(java.util.ArrayList) SearchResult(fi.otavanopisto.muikku.search.SearchResult) Date(java.util.Date)

Aggregations

SearchResult (fi.otavanopisto.muikku.search.SearchResult)21 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)17 Map (java.util.Map)16 SchoolDataIdentifier (fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier)13 UserEntity (fi.otavanopisto.muikku.model.users.UserEntity)10 SearchProvider (fi.otavanopisto.muikku.search.SearchProvider)9 SearchResponse (org.elasticsearch.action.search.SearchResponse)8 SearchHit (org.elasticsearch.search.SearchHit)8 SearchHits (org.elasticsearch.search.SearchHits)8 WorkspaceEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceEntity)7 GET (javax.ws.rs.GET)7 Path (javax.ws.rs.Path)7 WorkspaceUserEntity (fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity)6 UnknownHostException (java.net.UnknownHostException)6 Date (java.util.Date)6 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)6 UserSchoolDataIdentifier (fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier)5 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)5 User (fi.otavanopisto.muikku.schooldata.entity.User)4