1

Merge remote-tracking branch 'origin/master' into showInviteLink

# Conflicts:
#	src/main/java/mops/gruppen2/controller/Gruppen2Controller.java
#	src/main/java/mops/gruppen2/service/GroupService.java
This commit is contained in:
[Mahgs]
2020-03-24 14:51:33 +01:00
21 changed files with 306 additions and 158 deletions

View File

@ -52,6 +52,7 @@ public class Gruppen2Controller {
private final InviteLinkRepositoryService inviteLinkRepositoryService;
private final Gruppen2Config gruppen2Config;
private final Logger logger;
private final Logger logger;
private final String serverURL = "localhost:8080";
@ -61,6 +62,7 @@ public class Gruppen2Controller {
this.userService = userService;
this.controllerService = controllerService;
this.inviteLinkRepositoryService = inviteLinkRepositoryService;
logger = Logger.getLogger("Gruppen2ControllerLogger");
this.gruppen2Config = gruppen2Config;
this.logger = Logger.getLogger("gruppen2ControllerLogger");
}
@ -99,12 +101,16 @@ public class Gruppen2Controller {
@RequestParam("description") String description,
@RequestParam(value = "visibility", required = false) Boolean visibility,
@RequestParam(value = "lecture", required = false) Boolean lecture,
@RequestParam("userMaximum") Long userMaximum,
@RequestParam(value = "userMaximum", required = false) Long userMaximum,
@RequestParam(value = "maxInfiniteUsers", required = false) Boolean maxInfiniteUsers,
@RequestParam(value = "parent", required = false) Long parent,
@RequestParam(value = "file", required = false) MultipartFile file) throws IOException, EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<User> userList = new ArrayList<>();
if(userMaximum == null){
userMaximum = 100000L;
}
if (!file.isEmpty()) {
try {
userList = CsvService.read(file.getInputStream());
@ -112,15 +118,17 @@ public class Gruppen2Controller {
userMaximum = Long.valueOf(userList.size()) + userMaximum;
}
} catch (UnrecognizedPropertyException | CharConversionException ex) {
logger.warning("File konnte nicht gelesen werden");
throw new WrongFileException(file.getOriginalFilename());
}
}
visibility = visibility == null;
lecture = lecture != null;
maxInfiniteUsers = maxInfiniteUsers != null;
if (lecture) parent = null;
controllerService.createOrga(account, title, description, visibility, lecture, userMaximum, parent, userList);
controllerService.createOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parent, userList);
return "redirect:/gruppen2/";
}
@ -140,24 +148,32 @@ public class Gruppen2Controller {
@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam(value = "visibility", required = false) Boolean visibility,
@RequestParam("userMaximum") Long userMaximum,
@RequestParam(value = "userMaximum", required = false) Long userMaximum,
@RequestParam(value = "maxInfiniteUsers", required = false) Boolean maxInfiniteUsers,
@RequestParam(value = "parent", required = false) Long parent) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
visibility = visibility == null;
controllerService.createGroup(account, title, description, visibility, userMaximum, parent);
maxInfiniteUsers = maxInfiniteUsers != null;
controllerService.createGroup(account, title, description, visibility, maxInfiniteUsers, userMaximum, parent);
return "redirect:/gruppen2/";
}
@RolesAllowed({"ROLE_orga", "ROLE_actuator)"})
@PostMapping("/details/members/addUsersFromCsv")
public String addUsersFromCsv(@RequestParam("group_id") Long groupId,
public String addUsersFromCsv(KeycloakAuthenticationToken token,
@RequestParam("group_id") Long groupId,
@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<User> userList = new ArrayList<>();
Group group = userService.getGroupById(groupId);
if (!file.isEmpty()) {
try {
userList = CsvService.read(file.getInputStream());
if(userList.size()+group.getMembers().size()>group.getUserMaximum()){
controllerService.updateMaxUser(account, groupId, Long.valueOf(userList.size()) + group.getMembers().size());
}
} catch (UnrecognizedPropertyException | CharConversionException ex) {
throw new WrongFileException(file.getOriginalFilename());
}
@ -191,6 +207,18 @@ public class Gruppen2Controller {
if(group.getTitle() == null){
throw new GroupNotFoundException(this.getClass().toString());
}
if (!group.getMembers().contains(user)){
if (group.getVisibility() == Visibility.PRIVATE){
return "privateGroupNoMember";
}
if (group != null) {
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
return "detailsNoMember";
}
return "detailsNoMember";
}
if (parentId != null) {
parent = userService.getGroupById(parentId);
}
@ -307,14 +335,19 @@ public class Gruppen2Controller {
public String editMembers(Model model, KeycloakAuthenticationToken token, @PathVariable("id") Long groupId) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(groupId);
if (group.getRoles().get(account.getName()) == Role.ADMIN) {
model.addAttribute("account", account);
model.addAttribute("members", group.getMembers());
model.addAttribute("group", group);
model.addAttribute("admin", Role.ADMIN);
return "editMembers";
} else {
return "redirect:/details/";
User user = new User(account.getName(),"", "", "");
if (group.getMembers().contains(user)) {
if (group.getRoles().get(account.getName()) == Role.ADMIN) {
model.addAttribute("account", account);
model.addAttribute("members", group.getMembers());
model.addAttribute("group", group);
model.addAttribute("admin", Role.ADMIN);
return "editMembers";
} else {
return "redirect:/details/";
}
}else {
return "privateGroupNoMember";
}
}
@ -336,6 +369,16 @@ public class Gruppen2Controller {
return "redirect:/gruppen2/details/members/" + groupId;
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@PostMapping("/details/members/changeMaximum")
public String changeMaxSize(@RequestParam("maximum") Long maximum,
@RequestParam("group_id") Long groupId,
KeycloakAuthenticationToken token){
Account account = keyCloakService.createAccountFromPrincipal(token);
controllerService.updateMaxUser(account, groupId, maximum);
return "redirect:/gruppen2/details/members/" + groupId;
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@PostMapping("/details/members/deleteUser")
public String deleteUser(@RequestParam("group_id") Long groupId,

View File

@ -14,6 +14,6 @@ public class EventDTO {
Long event_id;
Long group_id;
String user_id;
String event_type;
String event_payload;
boolean visibility;
}

View File

@ -21,7 +21,8 @@ import mops.gruppen2.domain.exception.GroupIdMismatchException;
@JsonSubTypes.Type(value = UpdateGroupDescriptionEvent.class, name = "UpdateGroupDescriptionEvent"),
@JsonSubTypes.Type(value = UpdateGroupTitleEvent.class, name = "UpdateGroupTitleEvent"),
@JsonSubTypes.Type(value = UpdateRoleEvent.class, name = "UpdateRoleEvent"),
@JsonSubTypes.Type(value = DeleteGroupEvent.class, name = "DeleteGroupEvent")
@JsonSubTypes.Type(value = DeleteGroupEvent.class, name = "DeleteGroupEvent"),
@JsonSubTypes.Type(value = UpdateUserMaxEvent.class, name = "UpdateUserMaxEvent")
})
@Getter
@NoArgsConstructor

View File

@ -0,0 +1,25 @@
package mops.gruppen2.domain.event;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.exception.EventException;
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class UpdateUserMaxEvent extends Event {
private Long userMaximum;
public UpdateUserMaxEvent(Long group_id, String user_id, Long userMaximum) {
super(group_id,user_id);
this.userMaximum = userMaximum;
}
@Override
protected void applyEvent(Group group) throws EventException {
group.setUserMaximum(this.userMaximum);
}
}

View File

@ -20,9 +20,6 @@ public interface EventRepository extends CrudRepository<EventDTO, Long> {
//@Query("SELECT * FROM event WHERE event_id > ?#{[0]}")
//Iterable<EventDTO> findNewEventSinceStatus(@Param("status") Long status);
@Query("select distinct group_id from event where visibility =:vis")
List<Long> findGroup_idsWhereVisibility(@Param("vis") Boolean visibility);
@Query("SELECT DISTINCT group_id FROM event WHERE event_id > :status")
List<Long> findNewEventSinceStatus(@Param("status") Long status);
@ -34,4 +31,7 @@ public interface EventRepository extends CrudRepository<EventDTO, Long> {
@Query("SELECT MAX(group_id) FROM event")
Long getMaxGroupID();
@Query("SELECT * FROM event WHERE event_type = :type")
List<EventDTO> findAllEventsByType(@Param("type") String type);
}

View File

@ -5,13 +5,7 @@ import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.AddUserEvent;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.DeleteGroupEvent;
import mops.gruppen2.domain.event.DeleteUserEvent;
import mops.gruppen2.domain.event.UpdateGroupDescriptionEvent;
import mops.gruppen2.domain.event.UpdateGroupTitleEvent;
import mops.gruppen2.domain.event.UpdateRoleEvent;
import mops.gruppen2.domain.event.*;
import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.UserNotFoundException;
import mops.gruppen2.security.Account;
@ -20,6 +14,7 @@ import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
import static mops.gruppen2.domain.Role.ADMIN;
@ -30,11 +25,13 @@ public class ControllerService {
private final EventService eventService;
private final UserService userService;
private final InviteLinkRepositoryService inviteLinkRepositoryService;
private final Logger logger;
public ControllerService(EventService eventService, UserService userService, InviteLinkRepositoryService inviteLinkRepositoryService) {
this.eventService = eventService;
this.userService = userService;
this.inviteLinkRepositoryService = inviteLinkRepositoryService;
this.logger = Logger.getLogger("controllerServiceLogger");
}
/**
@ -46,7 +43,7 @@ public class ControllerService {
* @param title Gruppentitel
* @param description Gruppenbeschreibung
*/
public void createGroup(Account account, String title, String description, Boolean visibility, Long userMaximum, Long parent) throws EventException {
public void createGroup(Account account, String title, String description, Boolean visibility, Boolean maxInfiniteUsers, Long userMaximum, Long parent) throws EventException {
Visibility visibility1;
Long groupId = eventService.checkGroup();
@ -57,6 +54,10 @@ public class ControllerService {
createInviteLink(groupId);
}
if(maxInfiniteUsers){
userMaximum = 100000L;
}
CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId, account.getName(), parent, GroupType.SIMPLE, visibility1, userMaximum);
eventService.saveEvent(createGroupEvent);
@ -66,7 +67,7 @@ public class ControllerService {
updateRole(account.getName(), groupId);
}
public void createOrga(Account account, String title, String description, Boolean visibility, Boolean lecture, Long userMaximum, Long parent, List<User> users) throws EventException {
public void createOrga(Account account, String title, String description, Boolean visibility, Boolean lecture, Boolean maxInfiniteUsers, Long userMaximum, Long parent, List<User> users) throws EventException {
Visibility visibility1;
Long groupId = eventService.checkGroup();
@ -84,6 +85,11 @@ public class ControllerService {
groupType = GroupType.SIMPLE;
}
if(maxInfiniteUsers){
userMaximum = 100000L;
}
CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId, account.getName(), parent, groupType, visibility1, userMaximum);
eventService.saveEvent(createGroupEvent);
@ -106,8 +112,13 @@ public class ControllerService {
public void addUserList(List<User> users, Long groupId) {
for (User user : users) {
AddUserEvent addUserEvent = new AddUserEvent(groupId, user.getId(), user.getGivenname(), user.getFamilyname(), user.getEmail());
eventService.saveEvent(addUserEvent);
Group group = userService.getGroupById(groupId);
if (group.getMembers().contains(user)) {
logger.info("Benutzer " + user.getId() + " ist bereits in Gruppe");
} else {
AddUserEvent addUserEvent = new AddUserEvent(groupId, user.getId(), user.getGivenname(), user.getFamilyname(), user.getEmail());
eventService.saveEvent(addUserEvent);
}
}
}
@ -121,6 +132,11 @@ public class ControllerService {
eventService.saveEvent(updateGroupDescriptionEvent);
}
public void updateMaxUser(Account account, Long groupId, Long userMaximum) {
UpdateUserMaxEvent updateUserMaxEvent = new UpdateUserMaxEvent(groupId,account.getName(),userMaximum);
eventService.saveEvent(updateUserMaxEvent);
}
public void updateRole(String userId, Long groupId) throws EventException {
UpdateRoleEvent updateRoleEvent;
Group group = userService.getGroupById(groupId);

View File

@ -1,9 +1,7 @@
package mops.gruppen2.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.dto.EventDTO;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository;
import org.springframework.stereotype.Service;
@ -40,11 +38,6 @@ public class EventService {
* @return EventDTO Neues DTO
*/
public EventDTO getDTO(Event event) {
boolean visibility = false;
if (event instanceof CreateGroupEvent) {
visibility = ((CreateGroupEvent) event).getGroupVisibility() == Visibility.PUBLIC;
}
String payload = "";
try {
payload = jsonService.serializeEvent(event);
@ -52,7 +45,13 @@ public class EventService {
e.printStackTrace();
}
return new EventDTO(null, event.getGroupId(), event.getUserId(), payload, visibility);
return new EventDTO(null, event.getGroupId(), event.getUserId(), getEventType(event), payload);
}
private String getEventType(Event event) {
int lastDot = event.getClass().getName().lastIndexOf('.');
return event.getClass().getName().substring(lastDot + 1);
}
/**

View File

@ -2,7 +2,7 @@ package mops.gruppen2.service;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.dto.EventDTO;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.exception.EventException;
@ -14,6 +14,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class GroupService {
@ -52,10 +53,8 @@ public class GroupService {
public List<Group> projectEventList(List<Event> events) throws EventException {
Map<Long, Group> groupMap = new HashMap<>();
for (Event event : events) {
Group group = getOrCreateGroup(groupMap, event.getGroupId());
event.apply(group);
}
events.parallelStream()
.forEachOrdered(event -> event.apply(getOrCreateGroup(groupMap, event.getGroupId())));
return new ArrayList<>(groupMap.values());
}
@ -68,13 +67,6 @@ public class GroupService {
return groups.get(groupId);
}
private List<Group> removeUserGroups(List<Group> visibleGroups, List<Group> userGroups) {
for (Group group : userGroups) {
visibleGroups.remove(group);
}
return visibleGroups;
}
/**
* Sucht alle Zeilen in der DB mit visibility=true.
* Erstellt eine Liste aus öffentlichen Gruppen (ohen bereits beigetretenen Gruppen).
@ -83,33 +75,32 @@ public class GroupService {
* @throws EventException Projektionsfehler
*/
public List<Group> getAllGroupWithVisibilityPublic(String userId) throws EventException {
User user = new User(userId, null, null, null);
List<Event> eventsVisible = eventService.translateEventDTOs(eventRepository.findAllEventsOfGroups(eventRepository.findGroup_idsWhereVisibility(Boolean.TRUE)));
List<Group> visibleGroups = projectEventList(eventsVisible);
List<Event> eventsUser = getGroupEvents(eventRepository.findGroup_idsWhereUser_id(userId));
List<Group> groups = projectEventList(eventsUser);
List<Group> newGroups = new ArrayList<>();
for (Group group : visibleGroups) {
if (group.getMembers().contains(user)) {
newGroups.add(group);
}
}
return removeUserGroups(visibleGroups, newGroups);
List<Event> createEvents = eventService.translateEventDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
createEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("UpdateGroupDescriptionEvent")));
createEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent")));
createEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("DeleteGroupEvent")));
List<Group> visibleGroups = projectEventList(createEvents);
List<Long> userGroupIds = eventRepository.findGroup_idsWhereUser_id(userId);
return visibleGroups.parallelStream()
.filter(group -> group.getType() != null)
.filter(group -> !userGroupIds.contains(group.getId()))
.filter(group -> group.getVisibility() == Visibility.PUBLIC)
.collect(Collectors.toList());
}
public List<Group> getAllLecturesWithVisibilityPublic() throws EventException {
List<Event> eventsVisible = eventService.translateEventDTOs(eventRepository.findAllEventsOfGroups(eventRepository.findGroup_idsWhereVisibility(Boolean.TRUE)));
List<Group> visibleGroups = projectEventList(eventsVisible);
List<Group> visibleLectures = new ArrayList<>();
for (Group group : visibleGroups) {
if (group.getType() == null) {
continue;
}
if (group.getType().equals(GroupType.LECTURE)) {
visibleLectures.add(group);
}
}
return visibleLectures;
List<Event> createEvents = eventService.translateEventDTOs(eventRepository.findAllEventsByType("CreateGroupEvent"));
createEvents.addAll(eventService.translateEventDTOs(eventRepository.findAllEventsByType("UpdateGroupTitleEvent")));
List<Group> visibleGroups = projectEventList(createEvents);
return visibleGroups.parallelStream()
.filter(group -> group.getType() == GroupType.LECTURE)
.filter(group -> group.getVisibility() == Visibility.PUBLIC)
.collect(Collectors.toList());
}
@ -122,18 +113,12 @@ public class GroupService {
* @throws EventException Projektionsfehler
*/
public List<Group> findGroupWith(String search, Account account) throws EventException {
List<Group> groups = new ArrayList<>();
for (Group group : getAllGroupWithVisibilityPublic(account.getName())) {
if (group.getType() == null) {
continue;
}
if (group.getTitle().toLowerCase().contains(search.toLowerCase()) || group.getDescription().toLowerCase().contains(search.toLowerCase())) {
groups.add(group);
}
}
return groups;
return getAllGroupWithVisibilityPublic(account.getName())
.parallelStream()
.filter(group ->
group.getTitle().toLowerCase().contains(search.toLowerCase()) ||
group.getDescription().toLowerCase().contains(search.toLowerCase()))
.collect(Collectors.toList());
}
}