Skip to content
Snippets Groups Projects
Commit 96d9cc2e authored by Chris Lemaire's avatar Chris Lemaire
Browse files

Abstract getUsers method and adhere to data flow in RTS

parent d9e24984
No related branches found
No related tags found
No related merge requests found
...@@ -22,9 +22,8 @@ import java.util.ArrayList; ...@@ -22,9 +22,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.OptionalDouble; import java.util.OptionalDouble;
import java.util.function.Function; import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
...@@ -184,43 +183,27 @@ public class Course implements Serializable { ...@@ -184,43 +183,27 @@ public class Course implements Serializable {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public List<User> getRequestHandlers() { private List<User> getUsers(Predicate<Role> f) {
return roles.stream() return roles.stream()
.filter(r -> r instanceof Assistant || r instanceof Manager || r instanceof Teacher) .filter(f)
.map(Role::getUser) .map(Role::getUser)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public List<User> getAssistants() { public List<User> getAssistants() {
return roles.stream() return getUsers(r -> r instanceof Assistant);
.filter(r -> r instanceof Assistant)
.map(Role::getUser)
.collect(Collectors.toList());
} }
public List<User> getManagers() { public List<User> getManagers() {
return roles.stream() return getUsers(r -> r instanceof Manager);
.filter(r -> r instanceof Manager)
.map(Role::getUser)
.collect(Collectors.toList());
} }
public List<User> getTeachers() { public List<User> getTeachers() {
return roles.stream() return getUsers(r -> r instanceof Teacher);
.filter(r -> r instanceof Teacher)
.map(Role::getUser)
.collect(Collectors.toList());
} }
public List<User> getPrivileged() { public List<User> getPrivileged() {
Stream<User> teaches = getTeachers().stream(); return getUsers(r -> r instanceof Assistant || r instanceof Manager || r instanceof Teacher);
Stream<User> manages = getManagers().stream();
Stream<User> assists = getAssistants().stream();
return Stream
.of(teaches, manages, assists)
.flatMap(Function.identity())
.collect(Collectors.toList());
} }
public List<Role> getRoles(Class<Role> role) { public List<Role> getRoles(Class<Role> role) {
......
...@@ -172,7 +172,7 @@ public class RequestTableService { ...@@ -172,7 +172,7 @@ public class RequestTableService {
})); }));
// Add the created model attributes // Add the created model attributes
addToModel(model, activeLabs, filteredRequests, filters); addToModel(model, courses, activeLabs, filteredRequests, filters);
model.addAttribute("queued", queueCount); model.addAttribute("queued", queueCount);
} }
...@@ -218,47 +218,32 @@ public class RequestTableService { ...@@ -218,47 +218,32 @@ public class RequestTableService {
(currentGroup.or(currentUser)).and(predicate), pageable); (currentGroup.or(currentUser)).and(predicate), pageable);
// Add the created model attributes // Add the created model attributes
addToModel(model, labs, filteredRequests, filters); addToModel(model, courses, labs, filteredRequests, filters);
} }
/** /**
* Adds the given information to the model using the variable names used in the "request/list.html" page. * Adds the given information to the model using the variable names used in the "request/list.html" page.
* *
* @param model The model to The model to add attributes to. * @param model The model to The model to add attributes to.
* @param courses The courses that should be displayed in filters.
* @param labs The labs that should be displayed in filters. * @param labs The labs that should be displayed in filters.
* @param requests The requests that should be displayed on the page. * @param requests The requests that should be displayed on the page.
* @param filters The state of the filters. * @param filters The state of the filters.
*/ */
private void addToModel(Model model, private void addToModel(Model model,
List<Course> courses,
List<Lab> labs, List<Lab> labs,
Page<Request> requests, Page<Request> requests,
Object filters) { Object filters) {
List<Room> rooms = labs.stream()
.flatMap(lab -> lab.getRooms().stream())
.distinct()
.collect(Collectors.toList());
// Fetch the courses from the given list of labs to make sure only currently active
// or relevant courses are shown.
List<Course> courses = labs.stream()
.map(Lab::getCourse)
.distinct()
.collect(Collectors.toList());
List<User> assistants = courses.stream()
.flatMap(course -> course.getRequestHandlers().stream())
.distinct()
.collect(Collectors.toList());
model model
.addAttribute("rooms", rooms) .addAttribute("rooms", rooms(labs))
.addAttribute("requestTypes", requestTypeRepository.findAll()) .addAttribute("requestTypes", requestTypeRepository.findAll())
.addAttribute("courses", courses) .addAttribute("courses", courses)
.addAttribute("activeLabs", labs) .addAttribute("activeLabs", labs)
.addAttribute("requests", requests) .addAttribute("requests", requests)
.addAttribute("state", filters) .addAttribute("state", filters)
.addAttribute("assignments", assignments(labs)) .addAttribute("assignments", assignments(labs))
.addAttribute("assistants", assistants); .addAttribute("assistants", assistants(courses));
} }
/** /**
...@@ -320,6 +305,32 @@ public class RequestTableService { ...@@ -320,6 +305,32 @@ public class RequestTableService {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
/**
* Flatmaps the given labs to a list of distinct rooms.
*
* @param labs The labs in which to find rooms.
* @return The list of unique rooms available to the given labs.
*/
private List<Room> rooms(List<Lab> labs) {
return labs.stream()
.flatMap(lab -> lab.getRooms().stream())
.distinct()
.collect(Collectors.toList());
}
/**
* Flatmaps the given courses to a list of distinct request-assignable users.
*
* @param courses The courses to find assistants in.
* @return The list of unique assistants in the given set of courses.
*/
private List<User> assistants(List<Course> courses) {
return courses.stream()
.flatMap(course -> course.getPrivileged().stream())
.distinct()
.collect(Collectors.toList());
}
/** /**
* Uses the given HttpServletRequest to extract the path of the current endpoint. This can in turn be used * Uses the given HttpServletRequest to extract the path of the current endpoint. This can in turn be used
* as the key to store filters on. * as the key to store filters on.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment