Search in sources :

Example 16 with SearchResult

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

the class UserRESTService method searchStudents.

@GET
@Path("/students")
@RESTPermit(handling = Handling.INLINE)
public Response searchStudents(@QueryParam("searchString") String searchString, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("10") Integer maxResults, @QueryParam("userGroupIds") List<Long> userGroupIds, @QueryParam("myUserGroups") Boolean myUserGroups, @QueryParam("workspaceIds") List<Long> workspaceIds, @QueryParam("myWorkspaces") Boolean myWorkspaces, @QueryParam("userEntityId") Long userEntityId, @DefaultValue("false") @QueryParam("includeInactiveStudents") Boolean includeInactiveStudents, @DefaultValue("false") @QueryParam("includeHidden") Boolean includeHidden, @QueryParam("flags") Long[] flagIds) {
    if (!sessionController.isLoggedIn()) {
        return Response.status(Status.FORBIDDEN).build();
    }
    if (CollectionUtils.isNotEmpty(userGroupIds) && Boolean.TRUE.equals(myUserGroups)) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    if (CollectionUtils.isNotEmpty(workspaceIds) && Boolean.TRUE.equals(myWorkspaces)) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    List<Flag> flags = null;
    if (flagIds != null && flagIds.length > 0) {
        flags = new ArrayList<>(flagIds.length);
        for (Long flagId : flagIds) {
            Flag flag = flagController.findFlagById(flagId);
            if (flag == null) {
                return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid flag id %d", flagId)).build();
            }
            if (!flagController.hasFlagPermission(flag, sessionController.getLoggedUser())) {
                return Response.status(Status.FORBIDDEN).entity(String.format("You don't have permission to use flag %d", flagId)).build();
            }
            flags.add(flag);
        }
    }
    List<fi.otavanopisto.muikku.rest.model.Student> students = new ArrayList<>();
    UserEntity loggedUser = sessionController.getLoggedUserEntity();
    Set<Long> userGroupFilters = null;
    Set<Long> workspaceFilters = null;
    if (!sessionController.hasEnvironmentPermission(RoleFeatures.ACCESS_ONLY_GROUP_STUDENTS)) {
        if ((myUserGroups != null) && myUserGroups) {
            userGroupFilters = new HashSet<Long>();
            // Groups where user is a member
            List<UserGroupEntity> userGroups = userGroupEntityController.listUserGroupsByUserIdentifier(sessionController.getLoggedUser());
            for (UserGroupEntity userGroup : userGroups) {
                userGroupFilters.add(userGroup.getId());
            }
        } else if (!CollectionUtils.isEmpty(userGroupIds)) {
            userGroupFilters = new HashSet<Long>();
            // Defined user groups
            userGroupFilters.addAll(userGroupIds);
        }
    } else {
        // User can only list users from his/her own user groups
        userGroupFilters = new HashSet<Long>();
        // Groups where user is a member and the ids of the groups
        List<UserGroupEntity> userGroups = userGroupEntityController.listUserGroupsByUserIdentifier(sessionController.getLoggedUser());
        Set<Long> accessibleUserGroupEntityIds = userGroups.stream().map(UserGroupEntity::getId).collect(Collectors.toSet());
        if (CollectionUtils.isNotEmpty(userGroupIds)) {
            // if there are specified user groups, they need to be subset of the groups that the user can access
            if (!CollectionUtils.isSubCollection(userGroupIds, accessibleUserGroupEntityIds))
                return Response.status(Status.BAD_REQUEST).build();
            userGroupFilters.addAll(userGroupIds);
        } else {
            userGroupFilters.addAll(accessibleUserGroupEntityIds);
        }
    }
    List<SchoolDataIdentifier> userIdentifiers = null;
    if (flags != null) {
        if (userIdentifiers == null) {
            userIdentifiers = new ArrayList<>();
        }
        userIdentifiers.addAll(flagController.getFlaggedStudents(flags));
    }
    if (Boolean.TRUE.equals(includeInactiveStudents)) {
        if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.LIST_INACTIVE_STUDENTS)) {
            if (userEntityId == null) {
                return Response.status(Status.FORBIDDEN).build();
            } else {
                if (!sessionController.getLoggedUserEntity().getId().equals(userEntityId)) {
                    return Response.status(Status.FORBIDDEN).build();
                }
            }
        }
    }
    if (Boolean.TRUE.equals(includeHidden)) {
        if (!sessionController.hasEnvironmentPermission(MuikkuPermissions.LIST_HIDDEN_STUDENTS)) {
            if (userEntityId == null) {
                return Response.status(Status.FORBIDDEN).build();
            } else {
                if (!sessionController.getLoggedUserEntity().getId().equals(userEntityId)) {
                    return Response.status(Status.FORBIDDEN).build();
                }
            }
        }
    }
    if (userEntityId != null) {
        List<SchoolDataIdentifier> userEntityIdentifiers = new ArrayList<>();
        UserEntity userEntity = userEntityController.findUserEntityById(userEntityId);
        if (userEntity == null) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid userEntityId %d", userEntityId)).build();
        }
        List<UserSchoolDataIdentifier> schoolDataIdentifiers = userSchoolDataIdentifierController.listUserSchoolDataIdentifiersByUserEntity(userEntity);
        for (UserSchoolDataIdentifier schoolDataIdentifier : schoolDataIdentifiers) {
            userEntityIdentifiers.add(new SchoolDataIdentifier(schoolDataIdentifier.getIdentifier(), schoolDataIdentifier.getDataSource().getIdentifier()));
        }
        if (userIdentifiers == null) {
            userIdentifiers = userEntityIdentifiers;
        } else {
            userIdentifiers.retainAll(userEntityIdentifiers);
        }
    }
    if ((myWorkspaces != null) && myWorkspaces) {
        // Workspaces where user is a member
        List<WorkspaceEntity> workspaces = workspaceUserEntityController.listWorkspaceEntitiesByUserEntity(loggedUser);
        Set<Long> myWorkspaceIds = new HashSet<Long>();
        for (WorkspaceEntity ws : workspaces) myWorkspaceIds.add(ws.getId());
        workspaceFilters = new HashSet<>(myWorkspaceIds);
    } else if (!CollectionUtils.isEmpty(workspaceIds)) {
        // Defined workspaces
        workspaceFilters = new HashSet<>(workspaceIds);
    }
    SearchProvider elasticSearchProvider = getProvider("elastic-search");
    if (elasticSearchProvider != null) {
        String[] fields = new String[] { "firstName", "lastName", "nickName", "email" };
        SearchResult result = elasticSearchProvider.searchUsers(searchString, fields, Arrays.asList(EnvironmentRoleArchetype.STUDENT), userGroupFilters, workspaceFilters, userIdentifiers, includeInactiveStudents, includeHidden, false, firstResult, maxResults);
        List<Map<String, Object>> results = result.getResults();
        boolean hasImage = false;
        if (results != null && !results.isEmpty()) {
            for (Map<String, Object> o : results) {
                String studentId = (String) o.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;
                }
                UserEntity userEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
                String emailAddress = userEntity != null ? userEmailEntityController.getUserDefaultEmailAddress(userEntity, true) : null;
                Date studyStartDate = getDateResult(o.get("studyStartDate"));
                Date studyEndDate = getDateResult(o.get("studyEndDate"));
                Date studyTimeEnd = getDateResult(o.get("studyTimeEnd"));
                students.add(new fi.otavanopisto.muikku.rest.model.Student(studentIdentifier.toId(), (String) o.get("firstName"), (String) o.get("lastName"), (String) o.get("nickName"), (String) o.get("studyProgrammeName"), hasImage, (String) o.get("nationality"), (String) o.get("language"), (String) o.get("municipality"), (String) o.get("school"), emailAddress, studyStartDate, studyEndDate, studyTimeEnd, (String) o.get("curriculumIdentifier"), userEntity.getUpdatedByStudent()));
            }
        }
    }
    return Response.ok(students).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) UserGroupEntity(fi.otavanopisto.muikku.model.users.UserGroupEntity) SearchProvider(fi.otavanopisto.muikku.search.SearchProvider) SearchResult(fi.otavanopisto.muikku.search.SearchResult) FlagStudent(fi.otavanopisto.muikku.model.users.FlagStudent) Student(fi.otavanopisto.muikku.rest.model.Student) Flag(fi.otavanopisto.muikku.model.users.Flag) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) Date(java.util.Date) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) Student(fi.otavanopisto.muikku.rest.model.Student) Map(java.util.Map) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 17 with SearchResult

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

the class ElasticSearchProvider method searchWorkspaces.

@Override
public SearchResult searchWorkspaces(String schoolDataSource, List<String> subjects, List<String> identifiers, List<SchoolDataIdentifier> educationTypes, List<SchoolDataIdentifier> curriculumIdentifiers, String freeText, List<WorkspaceAccess> accesses, SchoolDataIdentifier accessUser, boolean includeUnpublished, int start, int maxResults, List<Sort> sorts) {
    if (identifiers != null && identifiers.isEmpty()) {
        return new SearchResult(0, 0, new ArrayList<Map<String, Object>>(), 0);
    }
    BoolQueryBuilder query = boolQuery();
    freeText = sanitizeSearchString(freeText);
    try {
        if (!includeUnpublished) {
            query.must(termQuery("published", Boolean.TRUE));
        }
        if (accesses != null) {
            BoolQueryBuilder accessQuery = boolQuery();
            for (WorkspaceAccess access : accesses) {
                switch(access) {
                    case LOGGED_IN:
                    case ANYONE:
                        accessQuery.should(termQuery("access", access));
                        break;
                    case MEMBERS_ONLY:
                        BoolQueryBuilder memberQuery = boolQuery();
                        IdsQueryBuilder idsQuery = idsQuery("Workspace");
                        for (SchoolDataIdentifier userWorkspace : getUserWorkspaces(accessUser)) {
                            idsQuery.addIds(String.format("%s/%s", userWorkspace.getIdentifier(), userWorkspace.getDataSource()));
                        }
                        memberQuery.must(idsQuery);
                        memberQuery.must(termQuery("access", access));
                        accessQuery.should(memberQuery);
                        break;
                }
            }
            query.must(accessQuery);
        }
        if (StringUtils.isNotBlank(schoolDataSource)) {
            query.must(termQuery("schoolDataSource", schoolDataSource.toLowerCase()));
        }
        if (subjects != null && !subjects.isEmpty()) {
            query.must(termsQuery("subjectIdentifier", subjects));
        }
        if (educationTypes != null && !educationTypes.isEmpty()) {
            List<String> educationTypeIds = new ArrayList<>(educationTypes.size());
            for (SchoolDataIdentifier educationType : educationTypes) {
                educationTypeIds.add(educationType.toId());
            }
            query.must(termsQuery("educationTypeIdentifier.untouched", educationTypeIds));
        }
        if (!CollectionUtils.isEmpty(curriculumIdentifiers)) {
            List<String> curriculumIds = new ArrayList<>(curriculumIdentifiers.size());
            for (SchoolDataIdentifier curriculumIdentifier : curriculumIdentifiers) {
                curriculumIds.add(curriculumIdentifier.toId());
            }
            query.must(boolQuery().should(termsQuery("curriculumIdentifiers.untouched", curriculumIds)).should(boolQuery().mustNot(existsQuery("curriculumIdentifiers"))).minimumNumberShouldMatch(1));
        }
        if (identifiers != null) {
            query.must(termsQuery("identifier", identifiers));
        }
        if (StringUtils.isNotBlank(freeText)) {
            String[] words = freeText.split(" ");
            for (int i = 0; i < words.length; i++) {
                if (StringUtils.isNotBlank(words[i])) {
                    query.must(boolQuery().should(prefixQuery("name", words[i])).should(prefixQuery("description", words[i])).should(prefixQuery("subject", words[i])).should(prefixQuery("staffMembers.firstName", words[i])).should(prefixQuery("staffMembers.lastName", words[i])));
                }
            }
        }
        SearchRequestBuilder requestBuilder = elasticClient.prepareSearch("muikku").setTypes("Workspace").setFrom(start).setSize(maxResults);
        if (sorts != null && !sorts.isEmpty()) {
            for (Sort sort : sorts) {
                requestBuilder.addSort(sort.getField(), SortOrder.valueOf(sort.getOrder().name()));
            }
        }
        SearchResponse response = requestBuilder.setQuery(query).execute().actionGet();
        List<Map<String, Object>> searchResults = new ArrayList<Map<String, Object>>();
        SearchHits searchHits = response.getHits();
        long totalHitCount = searchHits.getTotalHits();
        SearchHit[] results = searchHits.getHits();
        for (SearchHit hit : results) {
            Map<String, Object> hitSource = hit.getSource();
            hitSource.put("indexType", hit.getType());
            searchResults.add(hitSource);
        }
        SearchResult result = new SearchResult(start, maxResults, searchResults, totalHitCount);
        return result;
    } catch (Exception e) {
        logger.log(Level.SEVERE, "ElasticSearch query failed unexpectedly", e);
        return new SearchResult(0, 0, new ArrayList<Map<String, Object>>(), 0);
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchResult(fi.otavanopisto.muikku.search.SearchResult) UnknownHostException(java.net.UnknownHostException) WorkspaceAccess(fi.otavanopisto.muikku.model.workspace.WorkspaceAccess) SearchResponse(org.elasticsearch.action.search.SearchResponse) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) HashMap(java.util.HashMap) Map(java.util.Map)

Example 18 with SearchResult

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

the class ElasticSearchProvider method searchUsers.

@Override
public SearchResult searchUsers(String text, String[] textFields, Collection<EnvironmentRoleArchetype> archetypes, Collection<Long> groups, Collection<Long> workspaces, Collection<SchoolDataIdentifier> userIdentifiers, Boolean includeInactiveStudents, Boolean includeHidden, Boolean onlyDefaultUsers, int start, int maxResults, Collection<String> fields, Collection<SchoolDataIdentifier> excludeSchoolDataIdentifiers, Date startedStudiesBefore, Date studyTimeEndsBefore) {
    try {
        long now = OffsetDateTime.now().toEpochSecond();
        text = sanitizeSearchString(text);
        BoolQueryBuilder query = boolQuery();
        if (!Boolean.TRUE.equals(includeHidden)) {
            query.mustNot(termQuery("hidden", true));
        }
        if (Boolean.TRUE.equals(onlyDefaultUsers)) {
            query.must(termQuery("isDefaultIdentifier", true));
        }
        if (StringUtils.isNotBlank(text) && !ArrayUtils.isEmpty(textFields)) {
            String[] words = text.split(" ");
            for (int i = 0; i < words.length; i++) {
                if (StringUtils.isNotBlank(words[i])) {
                    BoolQueryBuilder fieldBuilder = boolQuery();
                    for (String textField : textFields) {
                        fieldBuilder.should(prefixQuery(textField, words[i]));
                    }
                    query.must(fieldBuilder);
                }
            }
        }
        if (excludeSchoolDataIdentifiers != null) {
            IdsQueryBuilder excludeIdsQuery = idsQuery("User");
            for (SchoolDataIdentifier excludeSchoolDataIdentifier : excludeSchoolDataIdentifiers) {
                excludeIdsQuery.addIds(String.format("%s/%s", excludeSchoolDataIdentifier.getIdentifier(), excludeSchoolDataIdentifier.getDataSource()));
            }
            query.mustNot(excludeIdsQuery);
        }
        if (startedStudiesBefore != null) {
            query.must(rangeQuery("studyStartDate").lt((long) startedStudiesBefore.getTime() / 1000));
        }
        if (studyTimeEndsBefore != null) {
            query.must(rangeQuery("studyTimeEnd").lt((long) studyTimeEndsBefore.getTime() / 1000));
        }
        if (archetypes != null) {
            List<String> archetypeNames = new ArrayList<>(archetypes.size());
            for (EnvironmentRoleArchetype archetype : archetypes) {
                archetypeNames.add(archetype.name().toLowerCase());
            }
            query.must(termsQuery("archetype", archetypeNames.toArray(new String[0])));
        }
        if (groups != null) {
            query.must(termsQuery("groups", ArrayUtils.toPrimitive(groups.toArray(new Long[0]))));
        }
        if (workspaces != null) {
            query.must(termsQuery("workspaces", ArrayUtils.toPrimitive(workspaces.toArray(new Long[0]))));
        }
        if (userIdentifiers != null) {
            IdsQueryBuilder includeIdsQuery = idsQuery("User");
            for (SchoolDataIdentifier userIdentifier : userIdentifiers) {
                includeIdsQuery.addIds(String.format("%s/%s", userIdentifier.getIdentifier(), userIdentifier.getDataSource()));
            }
            query.must(includeIdsQuery);
        }
        if (includeInactiveStudents == false) {
            /**
             * List only active users.
             *
             * Active user is
             * - staff member (teacher, manager, study guider, study programme leader, administrator)
             * - student that has study start date (in the past) and no study end date
             * - student that has study start date (in the past) and study end date in the future
             * - student that has no study start and end date but belongs to an active workspace
             *
             * Active workspace is
             * - published and
             * - either has no start/end date or current date falls between them
             */
            Set<Long> activeWorkspaceEntityIds = getActiveWorkspaces();
            query.must(boolQuery().should(termsQuery("archetype", EnvironmentRoleArchetype.TEACHER.name().toLowerCase(), EnvironmentRoleArchetype.MANAGER.name().toLowerCase(), EnvironmentRoleArchetype.STUDY_GUIDER.name().toLowerCase(), EnvironmentRoleArchetype.STUDY_PROGRAMME_LEADER.name().toLowerCase(), EnvironmentRoleArchetype.ADMINISTRATOR.name().toLowerCase())).should(boolQuery().must(termQuery("archetype", EnvironmentRoleArchetype.STUDENT.name().toLowerCase())).must(existsQuery("studyStartDate")).must(rangeQuery("studyStartDate").lte(now)).mustNot(existsQuery("studyEndDate"))).should(boolQuery().must(termQuery("archetype", EnvironmentRoleArchetype.STUDENT.name().toLowerCase())).must(existsQuery("studyStartDate")).must(rangeQuery("studyStartDate").lte(now)).must(existsQuery("studyEndDate")).must(rangeQuery("studyEndDate").gte(now))).should(boolQuery().must(termQuery("archetype", EnvironmentRoleArchetype.STUDENT.name().toLowerCase())).mustNot(existsQuery("studyEndDate")).mustNot(existsQuery("studyStartDate")).must(termsQuery("workspaces", ArrayUtils.toPrimitive(activeWorkspaceEntityIds.toArray(new Long[0]))))));
        }
        SearchRequestBuilder requestBuilder = elasticClient.prepareSearch("muikku").setTypes("User").setFrom(start).setSize(maxResults);
        if (CollectionUtils.isNotEmpty(fields)) {
            requestBuilder.addFields(fields.toArray(new String[0]));
        }
        SearchResponse response = requestBuilder.setQuery(query).addSort("_score", SortOrder.DESC).addSort("lastName", SortOrder.ASC).addSort("firstName", SortOrder.ASC).execute().actionGet();
        List<Map<String, Object>> searchResults = new ArrayList<Map<String, Object>>();
        SearchHits searchHits = response.getHits();
        long totalHitCount = searchHits.getTotalHits();
        SearchHit[] results = searchHits.getHits();
        for (SearchHit hit : results) {
            Map<String, Object> hitSource = hit.getSource();
            if (hitSource == null) {
                hitSource = new HashMap<>();
                for (String key : hit.getFields().keySet()) {
                    hitSource.put(key, hit.getFields().get(key).getValue().toString());
                }
            }
            hitSource.put("indexType", hit.getType());
            searchResults.add(hitSource);
        }
        SearchResult result = new SearchResult(start, maxResults, searchResults, totalHitCount);
        return result;
    } catch (Exception e) {
        logger.log(Level.SEVERE, "ElasticSearch query failed unexpectedly", e);
        return new SearchResult(0, 0, new ArrayList<Map<String, Object>>(), 0);
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) EnvironmentRoleArchetype(fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchResult(fi.otavanopisto.muikku.search.SearchResult) UnknownHostException(java.net.UnknownHostException) SearchResponse(org.elasticsearch.action.search.SearchResponse) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with SearchResult

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

the class WorkspaceRESTService method listWorkspaceStudents.

@GET
@Path("/workspaces/{ID}/students")
@RESTPermit(handling = Handling.INLINE)
public Response listWorkspaceStudents(@PathParam("ID") Long workspaceEntityId, @QueryParam("active") Boolean active, @QueryParam("requestedAssessment") Boolean requestedAssessment, @QueryParam("assessed") Boolean assessed, @QueryParam("studentIdentifier") String studentId, @QueryParam("search") String searchString, @QueryParam("flags") Long[] flagIds, @QueryParam("maxResults") Integer maxResults, @QueryParam("orderBy") String orderBy) {
    List<SchoolDataIdentifier> studentIdentifiers = null;
    if (StringUtils.isNotBlank(studentId)) {
        SchoolDataIdentifier studentIdentifier = SchoolDataIdentifier.fromId(studentId);
        if (studentIdentifier == null) {
            return Response.status(Status.BAD_REQUEST).entity(String.format("Malformed student identifier %s", studentId)).build();
        }
        studentIdentifiers = Collections.singletonList(studentIdentifier);
    }
    // Workspace
    WorkspaceEntity workspaceEntity = workspaceController.findWorkspaceEntityById(workspaceEntityId);
    if (workspaceEntity == null) {
        return Response.status(Status.NOT_FOUND).build();
    }
    // Access check
    if (!sessionController.hasWorkspacePermission(MuikkuPermissions.LIST_WORKSPACE_MEMBERS, workspaceEntity)) {
        if (studentIdentifiers == null || studentIdentifiers.size() != 1 || !studentIdentifiers.get(0).equals(sessionController.getLoggedUser())) {
            return Response.status(Status.FORBIDDEN).build();
        }
    }
    List<fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser> workspaceUsers = null;
    if (searchString != null) {
        studentIdentifiers = new ArrayList<>();
        Iterator<SearchProvider> searchProviderIterator = searchProviders.iterator();
        if (!searchProviderIterator.hasNext()) {
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity("No search provider found").build();
        }
        SearchProvider elasticSearchProvider = searchProviderIterator.next();
        if (elasticSearchProvider != null) {
            String[] fields = new String[] { "firstName", "lastName", "nickName", "email" };
            SearchResult result = elasticSearchProvider.searchUsers(searchString, fields, Arrays.asList(EnvironmentRoleArchetype.STUDENT), (Collection<Long>) null, Collections.singletonList(workspaceEntityId), (Collection<SchoolDataIdentifier>) null, Boolean.FALSE, Boolean.FALSE, false, 0, maxResults != null ? maxResults : Integer.MAX_VALUE);
            List<Map<String, Object>> results = result.getResults();
            if (results != null && !results.isEmpty()) {
                for (Map<String, Object> o : results) {
                    String foundStudentId = (String) o.get("id");
                    if (StringUtils.isBlank(foundStudentId)) {
                        logger.severe("Could not process user found from search index because it had a null id");
                        continue;
                    }
                    String[] studentIdParts = foundStudentId.split("/", 2);
                    SchoolDataIdentifier foundStudentIdentifier = studentIdParts.length == 2 ? new SchoolDataIdentifier(studentIdParts[0], studentIdParts[1]) : null;
                    if (foundStudentIdentifier == null) {
                        logger.severe(String.format("Could not process user found from search index with id %s", studentId));
                        continue;
                    }
                    studentIdentifiers.add(foundStudentIdentifier);
                }
            }
        }
    }
    List<Flag> flags = null;
    if (flagIds != null && flagIds.length > 0) {
        flags = new ArrayList<>(flagIds.length);
        for (Long flagId : flagIds) {
            Flag flag = flagController.findFlagById(flagId);
            if (flag == null) {
                return Response.status(Status.BAD_REQUEST).entity(String.format("Invalid flag id %d", flagId)).build();
            }
            if (!flagController.hasFlagPermission(flag, sessionController.getLoggedUser())) {
                return Response.status(Status.FORBIDDEN).entity(String.format("You don't have permission to use flag %d", flagId)).build();
            }
            flags.add(flag);
        }
    }
    if (flags != null) {
        List<SchoolDataIdentifier> flaggedStudents = flagController.getFlaggedStudents(flags);
        if (studentIdentifiers != null) {
            studentIdentifiers.retainAll(flaggedStudents);
        } else {
            studentIdentifiers = flaggedStudents;
        }
    }
    List<WorkspaceStudent> result = new ArrayList<>();
    if (studentIdentifiers != null) {
        workspaceUsers = new ArrayList<>();
        for (SchoolDataIdentifier studentIdentifier : studentIdentifiers) {
            WorkspaceUserEntity wue = workspaceUserEntityController.findWorkspaceUserByWorkspaceEntityAndUserIdentifier(workspaceEntity, studentIdentifier);
            if (wue == null) {
                continue;
            }
            if (active != null && !active.equals(wue.getActive())) {
                continue;
            }
            WorkspaceUser workspaceUser = workspaceController.findWorkspaceUser(wue);
            if (workspaceUser == null) {
                continue;
            }
            workspaceUsers.add(workspaceUser);
        }
    } else {
        // Students via WorkspaceSchoolDataBridge
        workspaceUsers = workspaceController.listWorkspaceStudents(workspaceEntity);
    }
    if (workspaceUsers == null || workspaceUsers.isEmpty()) {
        return Response.noContent().build();
    }
    Map<String, WorkspaceUserEntity> workspaceUserEntityMap = new HashMap<>();
    List<WorkspaceUserEntity> workspaceUserEntities = workspaceUserEntityController.listWorkspaceUserEntities(workspaceEntity);
    for (WorkspaceUserEntity workspaceUserEntity : workspaceUserEntities) {
        workspaceUserEntityMap.put(new SchoolDataIdentifier(workspaceUserEntity.getIdentifier(), workspaceUserEntity.getUserSchoolDataIdentifier().getDataSource().getIdentifier()).toId(), workspaceUserEntity);
    }
    if (maxResults != null && workspaceUsers.size() > maxResults) {
        workspaceUsers.subList(maxResults, workspaceUsers.size() - 1).clear();
    }
    for (fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser workspaceUser : workspaceUsers) {
        SchoolDataIdentifier workspaceUserIdentifier = workspaceUser.getIdentifier();
        WorkspaceUserEntity workspaceUserEntity = workspaceUserEntityMap.get(workspaceUserIdentifier.toId());
        if (workspaceUserEntity == null) {
            logger.log(Level.WARNING, String.format("Workspace student %s does not exist in Muikku", workspaceUserIdentifier.toId()));
            continue;
        }
        if (active == null || active.equals(workspaceUserEntity.getActive())) {
            if (requestedAssessment != null) {
                boolean hasAssessmentRequest = workspaceUserEntity != null && !assessmentRequestController.listByWorkspaceUser(workspaceUserEntity).isEmpty();
                if (requestedAssessment != hasAssessmentRequest) {
                    continue;
                }
            }
            if (assessed != null) {
                boolean isAssessed = !gradingController.listWorkspaceAssessments(workspaceUser.getWorkspaceIdentifier(), workspaceUser.getUserIdentifier()).isEmpty();
                if (assessed != isAssessed) {
                    continue;
                }
            }
            SchoolDataIdentifier userIdentifier = workspaceUser.getUserIdentifier();
            User user = userController.findUserByIdentifier(userIdentifier);
            if (user != null) {
                UserEntity userEntity = null;
                if (workspaceUserEntity != null) {
                    userEntity = workspaceUserEntity.getUserSchoolDataIdentifier().getUserEntity();
                } else {
                    userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(user.getSchoolDataSource(), user.getIdentifier());
                }
                result.add(createRestModel(userEntity, user, workspaceUser, workspaceUserEntity != null && workspaceUserEntity.getActive()));
            } else {
                logger.log(Level.SEVERE, String.format("Could not find user for identifier %s", userIdentifier));
            }
        }
    }
    // Sorting
    if (StringUtils.equals(orderBy, "name")) {
        Collections.sort(result, new Comparator<WorkspaceStudent>() {

            @Override
            public int compare(WorkspaceStudent o1, WorkspaceStudent o2) {
                String s1 = String.format("%s, %s", StringUtils.defaultString(o1.getLastName(), ""), StringUtils.defaultString(o1.getFirstName(), ""));
                String s2 = String.format("%s, %s", StringUtils.defaultString(o2.getLastName(), ""), StringUtils.defaultString(o2.getFirstName(), ""));
                return s1.compareTo(s2);
            }
        });
    }
    // Response
    return Response.ok(result).build();
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) User(fi.otavanopisto.muikku.schooldata.entity.User) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) SearchProvider(fi.otavanopisto.muikku.search.SearchProvider) SearchResult(fi.otavanopisto.muikku.search.SearchResult) Flag(fi.otavanopisto.muikku.model.users.Flag) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) WorkspaceStudent(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceStudent) Map(java.util.Map) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Example 20 with SearchResult

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

the class AssessmentRequestNotificationStrategy method sendNotifications.

@Override
public void sendNotifications() {
    Collection<Long> groups = getGroups();
    if (groups.isEmpty()) {
        return;
    }
    // #2867: Do no notify anyone that has been notified about this before (i.e. triggers only once after studies have started)
    List<SchoolDataIdentifier> studentIdentifiersAlreadyNotified = assesmentRequestNotificationController.listNotifiedSchoolDataIdentifiers();
    // Get a batch of students that have been studying for more than 60 days
    Date since = Date.from(OffsetDateTime.now().minusDays(NOTIFICATION_THRESHOLD_DAYS).toInstant());
    SearchResult searchResult = assesmentRequestNotificationController.searchActiveStudentIds(groups, FIRST_RESULT + offset, MAX_RESULTS, studentIdentifiersAlreadyNotified, since);
    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;
    }
    // #2867: Filter the batch to only contain students that have no evaluation activity whatsoever
    List<SchoolDataIdentifier> studentIdentifiers = getStudentIdentifiersWithoutEvaluationActivity(searchResult);
    for (SchoolDataIdentifier studentIdentifier : studentIdentifiers) {
        UserEntity studentEntity = userEntityController.findUserEntityByUserIdentifier(studentIdentifier);
        if (studentEntity != null) {
            Locale studentLocale = localeController.resolveLocale(LocaleUtils.toLocale(studentEntity.getLocale()));
            Map<String, Object> templateModel = new HashMap<>();
            templateModel.put("locale", studentLocale);
            templateModel.put("localeHelper", jadeLocaleHelper);
            String notificationContent = renderNotificationTemplate("assessment-request-notification", templateModel);
            notificationController.sendNotification(localeController.getText(studentLocale, "plugin.timednotifications.notification.category"), localeController.getText(studentLocale, "plugin.timednotifications.notification.assesmentrequest.subject"), notificationContent, studentEntity, studentIdentifier, "assesmentrequest");
            assesmentRequestNotificationController.createAssesmentRequestNotification(studentIdentifier);
        } else {
            logger.log(Level.SEVERE, String.format("Cannot send notification to student with identifier %s because UserEntity was not found", studentIdentifier.toId()));
        }
    }
}
Also used : SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) Locale(java.util.Locale) HashMap(java.util.HashMap) SearchResult(fi.otavanopisto.muikku.search.SearchResult) Date(java.util.Date) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity)

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