1

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

# Conflicts:
#	src/test/java/mops/gruppen2/service/EventServiceTest.java
This commit is contained in:
Mahgs
2020-03-16 13:14:10 +01:00
25 changed files with 411 additions and 254 deletions

View File

@ -2,22 +2,24 @@ package mops.gruppen2.controller;
import mops.gruppen2.config.Gruppen2Config;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Group;
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.UpdateGroupDescriptionEvent;
import mops.gruppen2.domain.event.UpdateGroupTitleEvent;
import mops.gruppen2.security.Account;
import mops.gruppen2.service.*;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import javax.annotation.security.RolesAllowed;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Controller
@RequestMapping("/gruppen2")
@ -55,6 +57,7 @@ public class Gruppen2Controller {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
model.addAttribute("gruppen", userService.getUserGroups(user.getUser_id()));
model.addAttribute("user",user);
return "index";
}
@ -67,20 +70,46 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@GetMapping("/findGroup")
public String findGroup(KeycloakAuthenticationToken token, Model model) {
public String findGroup(KeycloakAuthenticationToken token, Model model, @RequestParam(value = "suchbegriff", required = false) String suchbegriff) throws EventException {
List<Group> groupse = new ArrayList<>();
if(suchbegriff!=null) {
groupse = groupService.findGroupWith(suchbegriff);
}
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
model.addAttribute("gruppen",groupse);
return "search";
}
@PostMapping("/createGroup")
public String pCreateGroup(KeycloakAuthenticationToken token,
@RequestParam(value = "title") String title,
@RequestParam(value = "beschreibung") String beschreibung) {
@RequestParam(value = "beschreibung") String beschreibung,
@RequestParam(value = "visibility", required = false) Boolean visibility) {
Account account = keyCloakService.createAccountFromPrincipal(token);
controllerService.createGroup(account, title, beschreibung);
if (visibility == null) {
visibility = true;
}else{
visibility = false;
}
controllerService.createGroup(account, title, beschreibung, visibility);
return "redirect:/gruppen2/";
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@GetMapping("/details")
public String showGroupDetails(KeycloakAuthenticationToken token, Model model, @RequestParam (value="id") Long id) throws EventException, ResponseStatusException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(id);
Account account = keyCloakService.createAccountFromPrincipal (token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
if(group!= null) {
model.addAttribute("group", group);
model.addAttribute("role", group.getRoles().get(user.getUser_id()));
return "detailsMember";
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Group not found");
}
}

View File

@ -3,6 +3,8 @@ package mops.gruppen2.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class MopsController {
@ -10,4 +12,10 @@ public class MopsController {
public String redirect(){
return "redirect:/gruppen2/";
}
@GetMapping("/logout")
public String logout(HttpServletRequest request) throws Exception {
request.logout();
return "redirect:/gruppen2/";
}
}

View File

@ -12,4 +12,5 @@ public class EventDTO {
Long group_id;
String user_id;
String event_payload;
boolean visibility;
}

View File

@ -13,4 +13,4 @@ import java.util.List;
public class UpdatedGroupRequestMapper {
private Long status;
private List<Group> groupList;
}
}

View File

@ -30,6 +30,7 @@ public class Event {
Long group_id;
String user_id;
public Event(Long group_id,String user_id){
this.group_id = group_id;
this.user_id = user_id;

View File

@ -17,9 +17,10 @@ public class UpdateRoleEvent extends Event {
super(event_id, group_id, user_id);
this.newRole = newRole;
}
public UpdateRoleEvent(Long group_id, String user_id, Role newRole) {
super(group_id, user_id);
this.newRole = newRole;
}
}

View File

@ -16,6 +16,12 @@ public interface EventRepository extends CrudRepository<EventDTO, Long> {
@Query("select * from event where group_id =:id")
List<EventDTO> findEventDTOByGroup_id(@Param("id") Long group_id);
//@Query("SELECT * FROM event WHERE event_id > ?#{[0]}")
//Iterable<EventDTO> findNewEventSinceStatus(@Param("status") Long status);
@Query("select * from event where visibility =:vis")
List<EventDTO> findEventDTOByVisibility(@Param("vis") Boolean visibility);
@Query("SELECT DISTINCT group_id FROM event WHERE event_id > :status")
public List<Long> findNewEventSinceStatus(@Param("status") Long status);

View File

@ -1,5 +1,6 @@
package mops.gruppen2.service;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.Visibility;
@ -26,15 +27,26 @@ public class ControllerService {
* @param title Gruppentitel
* @param description Gruppenbeschreibung
*/
public void createGroup(Account account, String title, String description) {
public void createGroup(Account account, String title, String description, Boolean visibility) {
Visibility visibility1;
if (visibility){
visibility1 = Visibility.PUBLIC;
}else{
visibility1 = Visibility.PRIVATE;
}
List<Event> eventList = new ArrayList<>();
Collections.addAll(eventList, new CreateGroupEvent(eventService.checkGroup(), account.getName(), null , GroupType.LECTURE, Visibility.PUBLIC),
Collections.addAll(eventList, new CreateGroupEvent(eventService.checkGroup(), account.getName(), null , GroupType.LECTURE, visibility1),
new AddUserEvent(eventService.checkGroup(), account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail()),
new UpdateRoleEvent(eventService.checkGroup(), account.getName(), Role.ADMIN),
new UpdateGroupTitleEvent(eventService.checkGroup(), account.getName(), title),
new UpdateGroupDescriptionEvent(eventService.checkGroup(), account.getName(), description));
new UpdateGroupDescriptionEvent(eventService.checkGroup(), account.getName(), description),
new UpdateRoleEvent(eventService.checkGroup(),account.getName(), Role.ADMIN));
eventService.saveEventList(eventList);
}
public void addUser(Account account, Group group){
AddUserEvent addUserEvent = new AddUserEvent(eventService.checkGroup(),group.getId(),account.getName(),account.getGivenname(),account.getFamilyname(),account.getEmail());
eventService.saveEvent(addUserEvent);
}
}

View File

@ -2,9 +2,12 @@ package mops.gruppen2.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import mops.gruppen2.domain.EventDTO;
import mops.gruppen2.domain.Exceptions.EventException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.CreateGroupEvent;
import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository;
import org.bouncycastle.jcajce.provider.asymmetric.ec.KeyFactorySpi;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
@ -29,15 +32,25 @@ public class EventService {
eventStore.save(eventDTO);
}
/** Erzeugt aus einem Event Objekt ein EventDTO Objekt
/** Erzeugt aus einem Event Objekt ein EventDTO Objekt.
* Ist die Gruppe öffentlich, dann wird die visibility auf true gesetzt.
*
* @param event
* @return EventDTO
*/
public EventDTO getDTO(Event event){
public EventDTO getDTO(Event event) {
EventDTO eventDTO = new EventDTO();
eventDTO.setGroup_id(event.getGroup_id());
eventDTO.setUser_id(event.getUser_id());
if(event instanceof CreateGroupEvent) {
if(((CreateGroupEvent) event).getGroupVisibility() == Visibility.PRIVATE) {
eventDTO.setVisibility(false);
}else {
eventDTO.setVisibility(true);
}
}
try {
eventDTO.setEvent_payload(serializationService.serializeEvent(event));
} catch (JsonProcessingException e) {
@ -76,7 +89,7 @@ public class EventService {
return translateEventDTOs(groupEventDTOS);
}
/** Erzeugt aus der Datenbank eine Liste von Events
/** Erzeugt aus einer Liste von eventDTOs eine Liste von Events
*
* @param eventDTOS
* @return Liste von Events
@ -87,7 +100,6 @@ public class EventService {
for (EventDTO eventDTO : eventDTOS) {
try {
events.add(serializationService.deserializeEvent(eventDTO.getEvent_payload()));
}catch (JsonProcessingException e) {
e.printStackTrace();
}

View File

@ -23,6 +23,12 @@ public class GroupService {
this.eventRepository = eventRepository;
}
/** 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.
*
* @param group_ids
* @return
*/
public List<Event> getGroupEvents(List<Long> group_ids) {
List<EventDTO> eventDTOS = new ArrayList<>();
List<Event> events = new ArrayList<>();
@ -32,8 +38,13 @@ public class GroupService {
return events = eventService.translateEventDTOs(eventDTOS);
}
/** 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
* @return
* @throws EventException
*/
public List<Group> projectEventList(List<Event> events) throws EventException {
Map<Long, Group> groupMap = new HashMap<>();
@ -44,7 +55,13 @@ public class GroupService {
return new ArrayList<>(groupMap.values());
}
//
/** guckt in der Map anhand der Id nach ob die Gruppe schon in der Map vorhanden ist, wenn nicht wird eine neue
* Gruppe erzeugt
*
* @param groups
* @param group_id
* @return
*/
private Group getOrCreateGroup(Map<Long, Group> groups, long group_id) {
if (!groups.containsKey(group_id)) {
groups.put(group_id, new Group());
@ -52,4 +69,33 @@ public class GroupService {
return groups.get(group_id);
}
/**
* sucht alle Zeilen in der DB wo die Visibility gleich true ist und wandelt diese in
* eine Liste von Gruppen
* @return
* @throws EventException
*/
public List<Group> getAllGroupWithVisibilityPublic() throws EventException {
return projectEventList(eventService.translateEventDTOs(eventRepository.findEventDTOByVisibility(Boolean.TRUE)));
}
/**
* Filtert alle öffentliche Gruppen nach dem suchbegriff und gibt diese als Liste von Gruppen zurück.
* Groß und kleinschreibung wird beachtet.
* @param search
* @return
* @throws EventException
*/
public List<Group> findGroupWith(String search) throws EventException {
List<Group> groups = new ArrayList<>();
for (Group group: getAllGroupWithVisibilityPublic()) {
if (group.getTitle().contains(search)){
groups.add(group);
}
}
return groups;
}
}

View File

@ -29,6 +29,7 @@ public class SerializationService {
* @return JSON-Event-Payload als String
* @throws JsonProcessingException
*/
public String serializeEvent(Event event) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(event);

View File

@ -6,6 +6,7 @@ import mops.gruppen2.domain.event.Event;
import mops.gruppen2.repository.EventRepository;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
@ -26,4 +27,11 @@ public class UserService {
List<Event> events = groupService.getGroupEvents(group_ids);
return groupService.projectEventList(events);
}
public Group getGroupById(Long group_id) throws EventException {
List<Long> group_ids = new ArrayList<>();
group_ids.add(group_id);
List<Event> events = groupService.getGroupEvents(group_ids);
return groupService.projectEventList(events).get(0);
}
}