use of fi.otavanopisto.muikku.model.workspace.WorkspaceAccess in project muikku by otavanopisto.
the class CoursePickerRESTService method listWorkspaces.
@GET
@Path("/workspaces/")
@RESTPermitUnimplemented
public Response listWorkspaces(@QueryParam("search") String searchString, @QueryParam("subjects") List<String> subjects, @QueryParam("educationTypes") List<String> educationTypeIds, @QueryParam("curriculums") List<String> curriculumIds, @QueryParam("minVisits") Long minVisits, @QueryParam("includeUnpublished") @DefaultValue("false") Boolean includeUnpublished, @QueryParam("myWorkspaces") @DefaultValue("false") Boolean myWorkspaces, @QueryParam("orderBy") List<String> orderBy, @QueryParam("firstResult") @DefaultValue("0") Integer firstResult, @QueryParam("maxResults") @DefaultValue("50") Integer maxResults, @Context Request request) {
List<CoursePickerWorkspace> workspaces = new ArrayList<>();
boolean doMinVisitFilter = minVisits != null;
UserEntity userEntity = myWorkspaces ? sessionController.getLoggedUserEntity() : null;
List<WorkspaceEntity> workspaceEntities = null;
String schoolDataSourceFilter = null;
List<String> workspaceIdentifierFilters = null;
if (doMinVisitFilter) {
if (userEntity != null) {
workspaceEntities = workspaceVisitController.listWorkspaceEntitiesByMinVisitsOrderByLastVisit(userEntity, minVisits);
} else {
workspaceEntities = workspaceVisitController.listWorkspaceEntitiesByMinVisitsOrderByLastVisit(sessionController.getLoggedUserEntity(), minVisits);
}
} else {
if (userEntity != null) {
workspaceEntities = workspaceUserEntityController.listActiveWorkspaceEntitiesByUserEntity(userEntity);
}
}
Iterator<SearchProvider> searchProviderIterator = searchProviders.iterator();
if (searchProviderIterator.hasNext()) {
SearchProvider searchProvider = searchProviderIterator.next();
SearchResult searchResult = null;
if (workspaceEntities != null) {
workspaceIdentifierFilters = new ArrayList<>();
for (WorkspaceEntity workspaceEntity : workspaceEntities) {
if (schoolDataSourceFilter == null) {
schoolDataSourceFilter = workspaceEntity.getDataSource().getIdentifier();
}
workspaceIdentifierFilters.add(workspaceEntity.getIdentifier());
}
}
List<WorkspaceAccess> accesses = new ArrayList<>(Arrays.asList(WorkspaceAccess.ANYONE));
if (sessionController.isLoggedIn()) {
accesses.add(WorkspaceAccess.LOGGED_IN);
accesses.add(WorkspaceAccess.MEMBERS_ONLY);
}
List<Sort> sorts = null;
if (orderBy != null && orderBy.contains("alphabet")) {
sorts = new ArrayList<>();
sorts.add(new Sort("name.untouched", Sort.Order.ASC));
}
List<SchoolDataIdentifier> educationTypes = null;
if (educationTypeIds != null) {
educationTypes = new ArrayList<>(educationTypeIds.size());
for (String educationTypeId : educationTypeIds) {
SchoolDataIdentifier educationTypeIdentifier = SchoolDataIdentifier.fromId(educationTypeId);
if (educationTypeIdentifier != null) {
educationTypes.add(educationTypeIdentifier);
} else {
return Response.status(Status.BAD_REQUEST).entity(String.format("Malformed education type identifier", educationTypeId)).build();
}
}
}
List<SchoolDataIdentifier> curriculumIdentifiers = null;
if (curriculumIds != null) {
curriculumIdentifiers = new ArrayList<>(curriculumIds.size());
for (String curriculumId : curriculumIds) {
SchoolDataIdentifier curriculumIdentifier = SchoolDataIdentifier.fromId(curriculumId);
if (curriculumIdentifier != null) {
curriculumIdentifiers.add(curriculumIdentifier);
} else {
return Response.status(Status.BAD_REQUEST).entity(String.format("Malformed curriculum identifier", curriculumId)).build();
}
}
}
searchResult = searchProvider.searchWorkspaces(schoolDataSourceFilter, subjects, workspaceIdentifierFilters, educationTypes, curriculumIdentifiers, searchString, accesses, sessionController.getLoggedUser(), includeUnpublished, firstResult, maxResults, sorts);
schoolDataBridgeSessionController.startSystemSession();
try {
List<Map<String, Object>> results = searchResult.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];
SchoolDataIdentifier workspaceIdentifier = new SchoolDataIdentifier(identifier, dataSource);
WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceByDataSourceAndIdentifier(workspaceIdentifier.getDataSource(), workspaceIdentifier.getIdentifier());
if (workspaceEntity != null) {
String name = (String) result.get("name");
String nameExtension = (String) result.get("nameExtension");
String description = (String) result.get("description");
boolean canSignup = getCanSignup(workspaceEntity);
boolean isCourseMember = getIsAlreadyOnWorkspace(workspaceEntity);
String educationTypeId = (String) result.get("educationTypeIdentifier");
String educationTypeName = null;
if (StringUtils.isNotBlank(educationTypeId)) {
EducationType educationType = null;
SchoolDataIdentifier educationTypeIdentifier = SchoolDataIdentifier.fromId(educationTypeId);
if (educationTypeIdentifier == null) {
logger.severe(String.format("Malformatted educationTypeIdentifier %s", educationTypeId));
} else {
educationType = courseMetaController.findEducationType(educationTypeIdentifier.getDataSource(), educationTypeIdentifier.getIdentifier());
}
if (educationType != null) {
educationTypeName = educationType.getName();
}
}
if (StringUtils.isNotBlank(name)) {
workspaces.add(createRestModel(workspaceEntity, name, nameExtension, description, educationTypeName, canSignup, isCourseMember));
} else {
logger.severe(String.format("Search index contains workspace %s that does not have a name", workspaceIdentifier));
}
} else {
logger.severe(String.format("Search index contains workspace %s that does not exits on the school data system", workspaceIdentifier));
}
}
}
}
} finally {
schoolDataBridgeSessionController.endSystemSession();
}
} else {
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
if (workspaces.isEmpty()) {
return Response.noContent().build();
}
if (orderBy.contains("lastVisit")) {
Collections.sort(workspaces, new Comparator<CoursePickerWorkspace>() {
@Override
public int compare(CoursePickerWorkspace workspace1, CoursePickerWorkspace workspace2) {
if (workspace1.getLastVisit() == null || workspace2.getLastVisit() == null) {
return 0;
}
if (workspace1.getLastVisit().before(workspace2.getLastVisit())) {
return 1;
}
if (workspace1.getLastVisit().after(workspace2.getLastVisit())) {
return -1;
}
return 0;
}
});
}
return Response.ok(workspaces).build();
}
use of fi.otavanopisto.muikku.model.workspace.WorkspaceAccess 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);
}
}
Aggregations