use of fi.otavanopisto.muikku.search.SearchResult in project muikku by otavanopisto.
the class UserRESTService method searchUsers.
@GET
@Path("/users")
@RESTPermitUnimplemented
public Response searchUsers(@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("archetype") String archetype, @DefaultValue("false") @QueryParam("onlyDefaultUsers") Boolean onlyDefaultUsers) {
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();
UserEntity loggedUser = sessionController.getLoggedUserEntity();
EnvironmentRoleArchetype roleArchetype = archetype != null ? EnvironmentRoleArchetype.valueOf(archetype) : null;
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);
}
}
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<Long>(myWorkspaceIds);
} else if (!CollectionUtils.isEmpty(workspaceIds)) {
// Defined workspaces
workspaceFilters = new HashSet<Long>(workspaceIds);
}
SearchProvider elasticSearchProvider = getProvider("elastic-search");
if (elasticSearchProvider != null) {
String[] fields = new String[] { "firstName", "lastName", "nickName", "email" };
SearchResult result = elasticSearchProvider.searchUsers(searchString, fields, roleArchetype != null ? Arrays.asList(roleArchetype) : null, userGroupFilters, workspaceFilters, null, false, false, onlyDefaultUsers, firstResult, maxResults);
List<Map<String, Object>> results = result.getResults();
boolean hasImage = false;
List<fi.otavanopisto.muikku.rest.model.User> ret = new ArrayList<fi.otavanopisto.muikku.rest.model.User>();
if (!results.isEmpty()) {
for (Map<String, Object> o : results) {
String[] id = ((String) o.get("id")).split("/", 2);
UserEntity userEntity = userEntityController.findUserEntityByDataSourceAndIdentifier(id[1], id[0]);
if (userEntity != null) {
String emailAddress = userEmailEntityController.getUserDefaultEmailAddress(userEntity, true);
Date studyStartDate = getDateResult(o.get("studyStartDate"));
Date studyTimeEnd = getDateResult(o.get("studyTimeEnd"));
ret.add(new fi.otavanopisto.muikku.rest.model.User(userEntity.getId(), (String) o.get("firstName"), (String) o.get("lastName"), (String) o.get("nickName"), hasImage, (String) o.get("nationality"), (String) o.get("language"), (String) o.get("municipality"), (String) o.get("school"), emailAddress, studyStartDate, studyTimeEnd));
}
}
return Response.ok(ret).build();
} else
return Response.noContent().build();
}
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
use of fi.otavanopisto.muikku.search.SearchResult in project muikku by otavanopisto.
the class TranscriptOfRecordsController method listWorkspaceIdentifiersBySubjectIdentifierAndCourseNumber.
public List<VopsWorkspace> listWorkspaceIdentifiersBySubjectIdentifierAndCourseNumber(String schoolDataSource, String subjectIdentifier, int courseNumber) {
List<VopsWorkspace> retval = new ArrayList<>();
SearchProvider searchProvider = getProvider("elastic-search");
if (searchProvider != null) {
SearchResult sr = searchProvider.searchWorkspaces(schoolDataSource, subjectIdentifier, courseNumber);
List<Map<String, Object>> results = sr.getResults();
for (Map<String, Object> result : results) {
String searchId = (String) result.get("id");
if (StringUtils.isNotBlank(searchId)) {
String[] id = searchId.split("/", 2);
if (id.length == 2) {
String dataSource = id[1];
String identifier = id[0];
String educationTypeId = (String) result.get("educationSubtypeIdentifier");
String name = (String) result.get("name");
String description = (String) result.get("description");
@SuppressWarnings("unchecked") ArrayList<String> curriculums = (ArrayList<String>) result.get("curriculumIdentifiers");
SchoolDataIdentifier workspaceIdentifier = new SchoolDataIdentifier(identifier, dataSource);
SchoolDataIdentifier educationSubtypeIdentifier = SchoolDataIdentifier.fromId(educationTypeId);
Set<SchoolDataIdentifier> curriculumIdentifiers = new HashSet<>();
for (String curriculum : curriculums) {
curriculumIdentifiers.add(SchoolDataIdentifier.fromId(curriculum));
}
retval.add(new VopsWorkspace(workspaceIdentifier, educationSubtypeIdentifier, curriculumIdentifiers, name, description));
}
}
}
}
return retval;
}
use of fi.otavanopisto.muikku.search.SearchResult in project muikku by otavanopisto.
the class ElasticSearchProvider method searchWorkspaces.
@Override
public SearchResult searchWorkspaces(String schoolDataSource, String subjectIdentifier, int courseNumber) {
BoolQueryBuilder query = boolQuery();
query.must(termQuery("published", Boolean.TRUE));
query.must(termQuery("subjectIdentifier", subjectIdentifier));
query.must(termQuery("courseNumber", courseNumber));
// query.must(termQuery("access", WorkspaceAccess.LOGGED_IN));
SearchRequestBuilder requestBuilder = elasticClient.prepareSearch("muikku").setTypes("Workspace").setFrom(0).setSize(50).setQuery(query);
// logger.log(Level.INFO, "searchWorkspaces query: " + requestBuilder.internalBuilder());
SearchResponse response = requestBuilder.execute().actionGet();
List<Map<String, Object>> searchResults = new ArrayList<Map<String, Object>>();
SearchHits searchHits = response.getHits();
SearchHit[] results = searchHits.getHits();
long totalHits = searchHits.getTotalHits();
for (SearchHit hit : results) {
Map<String, Object> hitSource = hit.getSource();
hitSource.put("indexType", hit.getType());
searchResults.add(hitSource);
}
SearchResult result = new SearchResult(0, 50, searchResults, totalHits);
return result;
}
use of fi.otavanopisto.muikku.search.SearchResult in project muikku by otavanopisto.
the class ElasticSearchProvider method matchAllSearch.
@Override
public SearchResult matchAllSearch(int start, int maxResults) {
try {
SearchResponse response = elasticClient.prepareSearch().setQuery(matchAllQuery()).setFrom(start).setSize(maxResults).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);
}
}
use of fi.otavanopisto.muikku.search.SearchResult in project muikku by otavanopisto.
the class ElasticSearchProvider method findUser.
@Override
public SearchResult findUser(Long userEntityId, boolean includeInactive) {
// Query that checks activity based on user having a study end date set
BoolQueryBuilder query = boolQuery();
if (!includeInactive) {
query.mustNot(existsQuery("studyEndDate"));
}
query.must(termQuery("userEntityId", userEntityId));
// Search
SearchRequestBuilder requestBuilder = elasticClient.prepareSearch("muikku").setTypes("User");
// Results processing
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();
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);
}
return new SearchResult(0, searchResults.size(), searchResults, totalHitCount);
}
Aggregations