1

greater projectionservice refactoring

This commit is contained in:
Christoph
2020-04-06 17:21:09 +02:00
parent 5da796d66d
commit 7f05e0fb10
15 changed files with 282 additions and 191 deletions

View File

@ -42,14 +42,14 @@ public class APIController {
public GroupRequestWrapper updateGroups(@ApiParam("Letzter Status des Anfragestellers") @PathVariable Long lastEventId) throws EventException {
List<Event> events = eventStoreService.getNewEvents(lastEventId);
return APIService.wrap(eventStoreService.getMaxEventId(), projectionService.projectEventList(events));
return APIService.wrap(eventStoreService.getMaxEventId(), ProjectionService.projectEventList(events));
}
@GetMapping("/getGroupIdsOfUser/{userId}")
@Secured("ROLE_api_user")
@ApiOperation("Gibt alle Gruppen zurück, in denen sich ein Teilnehmer befindet")
public List<String> getGroupIdsOfUser(@ApiParam("Teilnehmer dessen groupIds zurückgegeben werden sollen") @PathVariable String userId) {
return projectionService.getUserGroups(userId).stream()
return projectionService.projectGroupsByUser(userId).stream()
.map(group -> group.getId().toString())
.collect(Collectors.toList());
}
@ -59,7 +59,7 @@ public class APIController {
@ApiOperation("Gibt die Gruppe mit der als Parameter mitgegebenden groupId zurück")
public Group getGroupById(@ApiParam("GruppenId der gefordeten Gruppe") @PathVariable String groupId) throws EventException {
List<Event> eventList = eventStoreService.getEventsOfGroup(UUID.fromString(groupId));
List<Group> groups = projectionService.projectEventList(eventList);
List<Group> groups = ProjectionService.projectEventList(eventList);
if (groups.isEmpty()) {
return null;

View File

@ -45,7 +45,7 @@ public class GroupCreationController {
Account account = KeyCloakService.createAccountFromPrincipal(token);
model.addAttribute("account", account);
model.addAttribute("lectures", projectionService.getAllLecturesWithVisibilityPublic());
model.addAttribute("lectures", projectionService.projectLectures());
return "createOrga";
}
@ -88,7 +88,7 @@ public class GroupCreationController {
Account account = KeyCloakService.createAccountFromPrincipal(token);
model.addAttribute("account", account);
model.addAttribute("lectures", projectionService.getAllLecturesWithVisibilityPublic());
model.addAttribute("lectures", projectionService.projectLectures());
return "createStudent";
}

View File

@ -50,7 +50,7 @@ public class GroupDetailsController {
HttpServletRequest request,
@PathVariable("id") String groupId) {
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account);
UUID parentId = group.getParent();
@ -93,7 +93,7 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account);
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
validationService.throwIfNoAdmin(group, user);
@ -118,7 +118,7 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account);
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
validationService.throwIfNoAdmin(group, user);
validationService.checkFields(title, description);
@ -135,7 +135,7 @@ public class GroupDetailsController {
@PathVariable("id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
User user = new User(account);
validationService.throwIfNoAdmin(group, user);
@ -156,7 +156,7 @@ public class GroupDetailsController {
@RequestParam("user_id") String userId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
User principle = new User(account);
User user = new User(userId, "", "", "");
@ -181,7 +181,7 @@ public class GroupDetailsController {
@RequestParam("group_id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
validationService.throwIfNewMaximumIsValid(maximum, group);
@ -200,7 +200,7 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token);
User principle = new User(account);
User user = new User(userId, "", "", "");
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
validationService.throwIfNoAdmin(group, principle);
@ -222,7 +222,7 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account);
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
validationService.throwIfUserAlreadyInGroup(group, user);
validationService.throwIfGroupFull(group);
@ -242,7 +242,7 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account);
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
groupService.deleteUser(account, user, group);
@ -257,7 +257,7 @@ public class GroupDetailsController {
Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account);
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
validationService.throwIfNoAdmin(group, user);

View File

@ -37,7 +37,7 @@ public class GruppenfindungController {
User user = new User(account);
model.addAttribute("account", account);
model.addAttribute("gruppen", projectionService.getUserGroups(user));
model.addAttribute("gruppen", projectionService.projectGroupsByUser(user));
model.addAttribute("user", user);
return "index";

View File

@ -66,7 +66,7 @@ public class SearchAndInviteController {
@RequestParam("id") String groupId) {
Account account = KeyCloakService.createAccountFromPrincipal(token);
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
UUID parentId = group.getParent();
Group parent = groupService.getParent(parentId);
User user = new User(account);
@ -89,7 +89,7 @@ public class SearchAndInviteController {
Model model,
@PathVariable("link") String link) {
Group group = projectionService.getGroupById(inviteService.getGroupIdFromLink(link));
Group group = projectionService.projectSingleGroupById(inviteService.getGroupIdFromLink(link));
validationService.throwIfGroupNotExisting(group.getTitle());
@ -111,7 +111,7 @@ public class SearchAndInviteController {
Account account = KeyCloakService.createAccountFromPrincipal(token);
User user = new User(account);
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
validationService.throwIfUserAlreadyInGroup(group, user);
validationService.throwIfGroupFull(group);

View File

@ -22,4 +22,16 @@ public class User {
familyname = account.getFamilyname();
email = account.getEmail();
}
/**
* User identifizieren sich über die Id, mehr wird also manchmal nicht benötigt.
*
* @param userId Die User Id
*/
public User(String userId) {
id = userId;
givenname = "";
familyname = "";
email = "";
}
}

View File

@ -14,64 +14,68 @@ public interface EventRepository extends CrudRepository<EventDTO, Long> {
// ####################################### GROUP IDs #########################################
@Query("SELECT DISTINCT group_id FROM event"
+ "WHERE user_id = :userId AND event_type = :type")
+ " WHERE user_id = :userId AND event_type = :type")
List<String> findGroupIdsByUserAndType(@Param("userId") String userId,
@Param("type") String type);
@Query("SELECT DISTINCT group_id FROM event"
+ "WHERE event_id > :status")
+ " WHERE event_id > :status")
List<String> findGroupIdsWhereEventIdGreaterThanStatus(@Param("status") Long status);
// ####################################### EVENT DTOs ########################################
@Query("SELECT * from event"
+ "WHERE group_id = :groupId")
List<EventDTO> findEventDTOsByGroup(@Param("groupId") String groupId);
@Query("SELECT * FROM event"
+ " WHERE group_id IN (:groupIds) ")
List<EventDTO> findEventDTOsByGroup(@Param("groupIds") List<String> groupIds);
@Query("SELECT * FROM event"
+ "WHERE group_id IN (:groupIds) ")
List<EventDTO> findEventDTOsByGroups(@Param("groupIds") List<String> groupIds);
+ " WHERE group_id IN (:userIds) ")
List<EventDTO> findEventDTOsByUser(@Param("groupIds") List<String> userIds);
@Query("SELECT * FROM event"
+ "WHERE event_type = :type")
List<EventDTO> findEventDTOsByType(@Param("type") String type);
+ " WHERE event_type IN (:types)")
List<EventDTO> findEventDTOsByType(@Param("types") List<String> types);
@Query("SELECT * FROM event"
+ "WHERE event_type IN (:types)")
List<EventDTO> findEventDTOsByTypes(@Param("types") List<String> types);
+ " WHERE event_type IN (:types) AND group_id IN (:groupIds)")
List<EventDTO> findEventDTOsByGroupAndType(@Param("types") List<String> types,
@Param("groupIds") List<String> groupIds);
@Query("SELECT * FROM event"
+ "WHERE event_type = :type AND user_id = :userId")
List<EventDTO> findEventDTOsByUserAndType(@Param("type") String type,
+ " WHERE event_type IN (:types) AND user_id = :userId")
List<EventDTO> findEventDTOsByUserAndType(@Param("types") List<String> types,
@Param("userId") String userId);
@Query("SELECT * FROM event"
+ "WHERE event_type IN (:types) AND user_id = :userId")
List<EventDTO> findEventDTOsByUserAndTypes(@Param("types") List<String> types,
@Param("userId") String userId);
// ################################ LATEST EVENT DTOs ########################################
@Query("WITH ranked_events AS ("
+ "SELECT *, ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY id DESC) AS rn"
+ "FROM messages"
+ "WHERE user_id = :userId AND event_type IN (AddUserEvent, DeleteUserEvent)"
+ "SELECT *, ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY event_id DESC) AS rn"
+ " FROM event"
+ " WHERE user_id = :userId AND event_type IN ('AddUserEvent', 'DeleteUserEvent')"
+ ")"
+ "SELECT * FROM ranked_events WHERE rn = 1;")
List<EventDTO> findLatestEventDTOsPartitionedByGroupByUser(@Param("userId") String userId);
@Query("WITH ranked_events AS ("
+ "SELECT *, ROW_NUMBER() OVER (PARTITION BY group_id ORDER BY event_id DESC) AS rn"
+ " FROM event"
+ " WHERE event_type IN (:types)"
+ ")"
+ "SELECT * FROM ranked_events WHERE rn = 1;")
List<EventDTO> findLatestEventDTOsPartitionedByGroupByType(@Param("types") List<String> types);
// ######################################### COUNT ###########################################
@Query("SELECT MAX(event_id) FROM event")
Long findMaxEventId();
@Query("SELECT COUNT(*) FROM event"
+ "WHERE event_type = :type AND group_id = :groupId")
+ " WHERE event_type = :type AND group_id = :groupId")
Long countEventDTOsByGroupAndType(@Param("type") String type,
@Param("groupId") String groupId);
@Query("SELECT COUNT(*) FROM event"
+ "WHERE group_id = :groupId AND user_id = :userId AND event_type = :type")
+ " WHERE group_id = :groupId AND user_id = :userId AND event_type = :type")
Long countEventDTOsByGroupIdAndUserAndType(@Param("groupId") String groupId,
@Param("userId") String userId,
@Param("type") String type);

View File

@ -2,6 +2,8 @@ package mops.gruppen2.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import mops.gruppen2.domain.dto.EventDTO;
import mops.gruppen2.domain.event.AddUserEvent;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.exception.BadPayloadException;
import mops.gruppen2.repository.EventRepository;
@ -10,6 +12,8 @@ import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@ -25,6 +29,8 @@ public class EventStoreService {
this.eventStore = eventStore;
}
//########################################### SAVE ###########################################
/**
* Erzeugt ein DTO aus einem Event und speicher es.
*
@ -54,6 +60,14 @@ public class EventStoreService {
}
}
//########################################### DTOs ###########################################
public static List<EventDTO> getDTOsFromEvents(List<Event> events) {
return events.stream()
.map(EventStoreService::getDTOFromEvent)
.collect(Collectors.toList());
}
/**
* Erzeugt aus einem Event Objekt ein EventDTO Objekt.
*
@ -62,14 +76,17 @@ public class EventStoreService {
* @return EventDTO (Neues DTO)
*/
public static EventDTO getDTOFromEvent(Event event) {
String payload = "";
try {
payload = JsonService.serializeEvent(event);
String payload = JsonService.serializeEvent(event);
return new EventDTO(null,
event.getGroupId().toString(),
event.getUserId(),
getEventType(event),
payload);
} catch (JsonProcessingException e) {
LOG.error("Event ({}) konnte nicht serialisiert werden!", event.getClass());
LOG.error("Event ({}) konnte nicht serialisiert werden!", e.getMessage());
throw new BadPayloadException(EventStoreService.class.toString());
}
return new EventDTO(null, event.getGroupId().toString(), event.getUserId(), getEventType(event), payload);
}
/**
@ -89,7 +106,7 @@ public class EventStoreService {
try {
return JsonService.deserializeEvent(dto.getEvent_payload());
} catch (JsonProcessingException e) {
LOG.error("Payload\n {}\n konnte nicht deserialisiert werden!", dto.getEvent_payload());
LOG.error("Payload\n {}\n konnte nicht deserialisiert werden!", e.getMessage());
throw new BadPayloadException(EventStoreService.class.toString());
}
}
@ -107,6 +124,8 @@ public class EventStoreService {
return event.getClass().getName().substring(lastDot + 1);
}
//######################################## GET EVENTS ########################################
/**
* Sucht in der DB alle Zeilen raus welche eine der Gruppen_ids hat.
* Wandelt die Zeilen in Events um und gibt davon eine Liste zurück.
@ -115,27 +134,32 @@ public class EventStoreService {
*
* @return Liste an Events
*/
//TODO: EventStoreService
public List<Event> getGroupEvents(List<UUID> groupIds) {
List<EventDTO> eventDTOS = new ArrayList<>();
for (UUID groupId : groupIds) {
eventDTOS.addAll(eventStore.findEventDTOByGroupId(groupId.toString()));
eventDTOS.addAll(eventStore.findEventDTOsByGroup(Collections.singletonList(groupId.toString())));
}
return getEventsFromDTOs(eventDTOS);
}
public List<Event> getGroupEvents(UUID groupId) {
return getEventsFromDTOs(eventStore.findEventDTOsByGroup(Collections.singletonList(groupId.toString())));
}
/**
* Findet alle Events welche ab dem neuen Status hinzugekommen sind.
* Sucht alle Events mit event_id > status
* Findet alle Events, welche ab dem neuen Status hinzugekommen sind.
* Sucht alle Events mit event_id > status.
*
* @param status Die Id des zuletzt gespeicherten Events
*
* @return Liste von neueren Events
*/
public List<Event> getNewEvents(Long status) {
List<String> groupIdsThatChanged = eventStore.findNewEventSinceStatus(status);
List<String> groupIdsThatChanged = eventStore.findGroupIdsWhereEventIdGreaterThanStatus(status);
List<EventDTO> groupEventDTOS = eventStore.findAllEventsOfGroups(groupIdsThatChanged);
List<EventDTO> groupEventDTOS = eventStore.findEventDTOsByGroup(groupIdsThatChanged);
return getEventsFromDTOs(groupEventDTOS);
}
@ -143,7 +167,7 @@ public class EventStoreService {
long highestEvent = 0;
try {
highestEvent = eventStore.getHighesEventID();
highestEvent = eventStore.findMaxEventId();
} catch (NullPointerException e) {
LOG.debug("Eine maxId von 0 wurde zurückgegeben, da keine Events vorhanden sind.");
}
@ -159,7 +183,7 @@ public class EventStoreService {
* @return Liste aus Events
*/
public List<Event> getEventsOfGroup(UUID groupId) {
List<EventDTO> eventDTOList = eventStore.findEventDTOByGroupId(groupId.toString());
List<EventDTO> eventDTOList = eventStore.findEventDTOsByGroup(Collections.singletonList(groupId.toString()));
return getEventsFromDTOs(eventDTOList);
}
@ -171,7 +195,7 @@ public class EventStoreService {
* @return Liste aus GruppenIds
*/
public List<UUID> findGroupIdsByUser(String userId) {
return eventStore.findGroupIdsWhereUserId(userId, "AddUserEvent").stream().map(UUID::fromString).collect(Collectors.toList());
return eventStore.findGroupIdsByUserAndType(userId, "AddUserEvent").stream().map(UUID::fromString).collect(Collectors.toList());
}
/**
@ -182,7 +206,67 @@ public class EventStoreService {
*
* @return true or false
*/
//TODO: irgendwie fischig
boolean userInGroup(UUID groupId, String userId) {
return eventStore.countEventsByGroupIdAndUserIdAndEventType(groupId.toString(), userId, "AddUserEvent") > eventStore.countEventsByGroupIdAndUserIdAndEventType(groupId.toString(), userId, "DeleteUserEvent");
return eventStore.countEventDTOsByGroupIdAndUserAndType(groupId.toString(), userId, "AddUserEvent")
> eventStore.countEventDTOsByGroupIdAndUserAndType(groupId.toString(), userId, "DeleteUserEvent");
}
private static List<String> uuidsToString(List<UUID> ids) {
return ids.stream()
.map(UUID::toString)
.collect(Collectors.toList());
}
/**
* Liefert Gruppen-Ids von existierenden (ungelöschten) Gruppen.
*
* @return GruppenIds (UUID) als Liste
*/
List<UUID> findExistingGroupIds() {
List<Event> createEvents = findLatestEventsFromGroupByType("CreateGroupEvent",
"DeleteGroupEvent");
return createEvents.stream()
.filter(event -> event instanceof CreateGroupEvent)
.map(Event::getGroupId)
.collect(Collectors.toList());
}
/**
* Liefert Gruppen-Ids von existierenden (ungelöschten) Gruppen.
*
* @return GruppenIds (UUID) als Liste
*/
List<UUID> findExistingUserGroups(String userId) {
List<Event> userEvents = findLatestEventsFromGroupByUser(userId);
return userEvents.stream()
.filter(event -> event instanceof AddUserEvent)
.map(Event::getGroupId)
.collect(Collectors.toList());
}
// ######################################## QUERIES ##########################################
List<Event> findEventsByTypes(String... types) {
return getEventsFromDTOs(eventStore.findEventDTOsByType(Arrays.asList(types)));
}
List<Event> findEventsByType(String type) {
return getEventsFromDTOs(eventStore.findEventDTOsByType(Collections.singletonList(type)));
}
List<Event> findEventsByGroupsAndTypes(List<UUID> groupIds, String... types) {
return getEventsFromDTOs(eventStore.findEventDTOsByGroupAndType(Arrays.asList(types),
uuidsToString(groupIds)));
}
List<Event> findLatestEventsFromGroupByUser(String userId) {
return getEventsFromDTOs(eventStore.findLatestEventDTOsPartitionedByGroupByUser(userId));
}
List<Event> findLatestEventsFromGroupByType(String... types) {
return getEventsFromDTOs(eventStore.findLatestEventDTOsPartitionedByGroupByType(Arrays.asList(types)));
}
}

View File

@ -121,7 +121,7 @@ public class GroupService {
//TODO: GroupService/eventbuilderservice
void addUserList(List<User> newUsers, UUID groupId) {
for (User user : newUsers) {
Group group = projectionService.getGroupById(groupId);
Group group = projectionService.projectSingleGroupById(groupId);
if (group.getMembers().contains(user)) {
LOG.info("Benutzer {} ist bereits in Gruppe", user.getId());
} else {
@ -146,7 +146,7 @@ public class GroupService {
//TODO: GroupService/eventbuilderservice
public void updateRole(User user, UUID groupId) throws EventException {
UpdateRoleEvent updateRoleEvent;
Group group = projectionService.getGroupById(groupId);
Group group = projectionService.projectSingleGroupById(groupId);
validationService.throwIfNotInGroup(group, user);
if (group.getRoles().get(user.getId()) == ADMIN) {
@ -165,7 +165,7 @@ public class GroupService {
//TODO: GroupService
public void addUsersFromCsv(Account account, MultipartFile file, String groupId) {
Group group = projectionService.getGroupById(UUID.fromString(groupId));
Group group = projectionService.projectSingleGroupById(UUID.fromString(groupId));
List<User> newUserList = CsvService.readCsvFile(file);
removeOldUsersFromNewUsers(group.getMembers(), newUserList);
@ -206,7 +206,7 @@ public class GroupService {
public Group getParent(UUID parentId) {
Group parent = new Group();
if (!idIsEmpty(parentId)) {
parent = projectionService.getGroupById(parentId);
parent = projectionService.projectSingleGroupById(parentId);
}
return parent;
}

View File

@ -7,7 +7,6 @@ import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.GroupNotFoundException;
import mops.gruppen2.repository.EventRepository;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@ -19,19 +18,35 @@ import java.util.UUID;
import java.util.stream.Collectors;
/**
* Liefert verschiedene Projektionen auf Gruppen
* Liefert verschiedene Projektionen auf Gruppen.
* Benötigt ausschließlich den EventStoreService.
*/
@Service
public class ProjectionService {
private final EventRepository eventRepository;
private final EventStoreService eventStoreService;
public ProjectionService(EventRepository eventRepository, EventStoreService eventStoreService) {
this.eventRepository = eventRepository;
public ProjectionService(EventStoreService eventStoreService) {
this.eventStoreService = eventStoreService;
}
/**
* Konstruiert Gruppen aus einer Liste von Events.
*
* @param events Liste an Events
*
* @return Liste an Projizierten Gruppen
*
* @throws EventException Projektionsfehler
*/
public static List<Group> projectEventList(List<Event> events) throws EventException {
Map<UUID, Group> groupMap = new HashMap<>();
events.forEach(event -> event.apply(getOrCreateGroup(groupMap, event.getGroupId())));
return new ArrayList<>(groupMap.values());
}
/**
* Gibt die Gruppe mit der richtigen Id aus der übergebenen Map wieder, existiert diese nicht
* wird die Gruppe erstellt und der Map hizugefügt.
@ -41,7 +56,6 @@ public class ProjectionService {
*
* @return Die gesuchte Gruppe
*/
//TODO: ProjectionService
private static Group getOrCreateGroup(Map<UUID, Group> groups, UUID groupId) {
if (!groups.containsKey(groupId)) {
groups.put(groupId, new Group());
@ -51,105 +65,77 @@ public class ProjectionService {
}
/**
* Wird verwendet bei der Suche nach Gruppen: Titel, Beschreibung werden benötigt.
* Außerdem wird beachtet, ob der eingeloggte User bereits in entsprechenden Gruppen mitglied ist.
* Projiziert öffentliche Gruppen.
* Die Gruppen enthalten Metainformationen: Titel, Beschreibung und MaxUserAnzahl.
* Außerdem wird noch beachtet, ob der eingeloggte User bereits in entsprechenden Gruppen mitglied ist.
*
* @return Liste von projizierten Gruppen
*
* @throws EventException Projektionsfehler
*/
//TODO: ProjectionService
//TODO Rename
@Cacheable("groups")
public List<Group> getAllGroupWithVisibilityPublic(String userId) throws EventException {
List<Event> groupEvents = EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
groupEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupDescriptionEvent")));
groupEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent")));
groupEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
groupEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateUserMaxEvent")));
//TODO: remove userID param
public List<Group> projectPublicGroups(String userId) throws EventException {
List<UUID> groupIds = eventStoreService.findExistingGroupIds();
List<Event> events = eventStoreService.findEventsByGroupsAndTypes(groupIds,
"CreateGroupEvent",
"UpdateGroupDescriptionEvent",
"UpdateGroupTitleEvent",
"UpdateUserMaxEvent");
List<Group> visibleGroups = projectEventList(groupEvents);
List<Group> groups = projectEventList(events);
SearchService.sortByGroupType(visibleGroups);
SearchService.sortByGroupType(groups); //TODO: auslagern?
return visibleGroups.stream()
.filter(group -> group.getType() != null)
.filter(group -> !eventStoreService.userInGroup(group.getId(), userId))
.filter(group -> group.getVisibility() == Visibility.PUBLIC)
.collect(Collectors.toList());
return groups.stream()
.filter(group -> group.getVisibility() == Visibility.PUBLIC)
.filter(group -> !eventStoreService.userInGroup(group.getId(), userId)) //TODO: slow
.collect(Collectors.toList());
}
/**
* Wird verwendet beim Gruppe erstellen bei der Parent-Auswahl: nur Titel benötigt.
* Projiziert Vorlesungen.
* Projektionen enthalten nur Metainformationen: Titel.
*
* @return List of groups
* @return Liste von Veranstaltungen
*/
@Cacheable("groups")
//TODO: ProjectionService
public List<Group> getAllLecturesWithVisibilityPublic() {
List<Event> createEvents = EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
createEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
createEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent")));
createEvents.addAll(EventStoreService.getEventsFromDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
public List<Group> projectLectures() {
List<UUID> groupIds = eventStoreService.findExistingGroupIds();
List<Event> events = eventStoreService.findEventsByGroupsAndTypes(groupIds,
"CreateGroupEvent",
"UpdateGroupTitleEvent");
List<Group> visibleGroups = projectEventList(createEvents);
List<Group> lectures = projectEventList(events);
return visibleGroups.stream()
.filter(group -> group.getType() == GroupType.LECTURE)
.filter(group -> group.getVisibility() == Visibility.PUBLIC)
.collect(Collectors.toList());
return lectures.stream()
.filter(group -> group.getType() == GroupType.LECTURE)
.collect(Collectors.toList());
}
/**
* Erzeugt eine neue Map wo Gruppen aus den Events erzeugt und den Gruppen_ids zugeordnet werden.
* Die Gruppen werden als Liste zurückgegeben.
*
* @param events Liste an Events
*
* @return Liste an Projizierten Gruppen
*
* @throws EventException Projektionsfehler
*/
//TODO: ProjectionService
public List<Group> projectEventList(List<Event> events) throws EventException {
Map<UUID, Group> groupMap = new HashMap<>();
events.parallelStream()
.forEachOrdered(event -> event.apply(getOrCreateGroup(groupMap, event.getGroupId())));
return new ArrayList<>(groupMap.values());
}
//TODO: ProjectionService
@Cacheable("groups")
public List<Group> getUserGroups(String userId) throws EventException {
return getUserGroups(new User(userId, "", "", ""));
public List<Group> projectGroupsByUser(String userId) throws EventException {
return projectGroupsByUser(new User(userId));
}
/**
* Gibt eine Liste aus Gruppen zurück, in denen sich der übergebene User befindet.
* Projiziert Gruppen, in welchen der User aktuell teilnimmt.
* Die Gruppen enthalten nur Metainformationen: Titel und Beschreibung.
*
* @param user Der User
*
* @return Liste aus Gruppen
*/
//TODO: ProjectionService
//TODO: Nur AddUserEvents + DeleteUserEvents betrachten
@Cacheable("groups")
public List<Group> getUserGroups(User user) {
List<UUID> groupIds = eventStoreService.findGroupIdsByUser(user.getId());
List<Event> events = eventStoreService.getGroupEvents(groupIds);
List<Group> groups = projectEventList(events);
List<Group> newGroups = new ArrayList<>();
public List<Group> projectGroupsByUser(User user) {
List<UUID> groupIds = eventStoreService.findExistingUserGroups(user.getId());
List<Event> groupEvents = eventStoreService.findEventsByGroupsAndTypes(groupIds,
"CreateGroupEvent",
"UpdateGroupTitleEvent",
"UpdateGroupDescriptionEvent",
"DeleteGroupEvent");
for (Group group : groups) {
if (group.getMembers().contains(user)) {
newGroups.add(group);
}
}
SearchService.sortByGroupType(newGroups);
return newGroups;
return projectEventList(groupEvents);
}
/**
@ -161,16 +147,12 @@ public class ProjectionService {
*
* @throws EventException Wenn die Gruppe nicht gefunden wird
*/
//TODO: ProjectionService
public Group getGroupById(UUID groupId) throws EventException {
List<UUID> groupIds = new ArrayList<>();
groupIds.add(groupId);
public Group projectSingleGroupById(UUID groupId) throws GroupNotFoundException {
try {
List<Event> events = eventStoreService.getGroupEvents(groupIds);
List<Event> events = eventStoreService.getGroupEvents(groupId);
return projectEventList(events).get(0);
} catch (IndexOutOfBoundsException e) {
throw new GroupNotFoundException("@UserService");
throw new GroupNotFoundException(ProjectionService.class.toString());
}
}
}

View File

@ -51,9 +51,9 @@ public class SearchService {
@Cacheable("groups")
public List<Group> findGroupWith(String search, Account account) throws EventException {
if (search.isEmpty()) {
return projectionService.getAllGroupWithVisibilityPublic(account.getName());
return projectionService.projectPublicGroups(account.getName());
}
return projectionService.getAllGroupWithVisibilityPublic(account.getName()).parallelStream().filter(group -> group.getTitle().toLowerCase().contains(search.toLowerCase()) || group.getDescription().toLowerCase().contains(search.toLowerCase())).collect(Collectors.toList());
return projectionService.projectPublicGroups(account.getName()).parallelStream().filter(group -> group.getTitle().toLowerCase().contains(search.toLowerCase()) || group.getDescription().toLowerCase().contains(search.toLowerCase())).collect(Collectors.toList());
}
}

View File

@ -68,7 +68,7 @@ public class ValidationService {
}
boolean checkIfGroupEmpty(UUID groupId) {
return projectionService.getGroupById(groupId).getMembers().isEmpty();
return projectionService.projectSingleGroupById(groupId).getMembers().isEmpty();
}
public void throwIfNoAdmin(Group group, User user) {