Skip to content
Snippets Groups Projects
Commit 9d0255e7 authored by Tom Käsler's avatar Tom Käsler Committed by Daniel Gerhardt
Browse files

change contentgroup to array in room model

See merge request !94
parent 7cca177c
Branches
No related merge requests found
......@@ -12,9 +12,20 @@ import java.util.Set;
public class Room extends Entity {
public static class ContentGroup {
private String name;
private Set<String> contentIds;
private boolean autoSort;
@JsonView({View.Persistence.class, View.Public.class})
public String getName() {
return this.name;
}
@JsonView({View.Persistence.class, View.Public.class})
public void setName(final String name) {
this.name = name;
}
@JsonView({View.Persistence.class, View.Public.class})
public Set<String> getContentIds() {
if (contentIds == null) {
......@@ -42,6 +53,7 @@ public class Room extends Entity {
@Override
public String toString() {
return new ToStringCreator(this)
.append("name", name)
.append("contentIds", contentIds)
.append("autoSort", autoSort)
.toString();
......@@ -297,7 +309,7 @@ public class Room extends Entity {
private String abbreviation;
private String description;
private boolean closed;
private Map<String, ContentGroup> contentGroups;
private Set<ContentGroup> contentGroups;
private Settings settings;
private Author author;
private PoolProperties poolProperties;
......@@ -366,16 +378,16 @@ public class Room extends Entity {
}
@JsonView({View.Persistence.class, View.Public.class})
public Map<String, ContentGroup> getContentGroups() {
public Set<ContentGroup> getContentGroups() {
if (contentGroups == null) {
contentGroups = new HashMap<>();
contentGroups = new HashSet<ContentGroup>();
}
return contentGroups;
}
@JsonView({View.Persistence.class, View.Public.class})
public void setContentGroups(final Map<String, ContentGroup> contentGroups) {
public void setContentGroups(final Set<ContentGroup> contentGroups) {
this.contentGroups = contentGroups;
}
......
......@@ -120,13 +120,17 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem
@Override
public Iterable<Content> getByRoomIdAndGroup(final String roomId, final String group) {
final Room room = roomRepository.findOne(roomId);
final Room.ContentGroup contentGroup = room.getContentGroups().get(group);
Room.ContentGroup contentGroup = null;
for (Room.ContentGroup cg : room.getContentGroups()) {
if (cg.getName().equals(group)) {
contentGroup = cg;
}
}
if (contentGroup == null) {
throw new NotFoundException("Content group does not exist.");
}
Set<String> contentIds = contentGroup.getContentIds();
return get(contentIds);
return get(contentGroup.getContentIds());
}
@Override
......@@ -138,7 +142,12 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem
@Override
public int countByRoomIdAndGroup(final String roomId, final String group) {
final Room room = roomRepository.findOne(roomId);
final Room.ContentGroup contentGroup = room.getContentGroups().get(group);
Room.ContentGroup contentGroup = null;
for (Room.ContentGroup cg : room.getContentGroups()) {
if (cg.getName().equals(group)) {
contentGroup = cg;
}
}
if (contentGroup == null) {
throw new NotFoundException("Content group does not exist.");
}
......@@ -173,10 +182,21 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem
public void finalizeCreate(final Content content) {
/* Update content groups of room */
final Room room = roomRepository.findOne(content.getRoomId());
for (final String groupName : content.getGroups()) {
Room.ContentGroup group = room.getContentGroups().getOrDefault(groupName, new Room.ContentGroup());
room.getContentGroups().put(groupName, group);
group.getContentIds().add(content.getId());
final Set<Room.ContentGroup> contentGroups = room.getContentGroups();
for (final Room.ContentGroup cg : contentGroups) {
if (content.getGroups().contains(cg.getName())) {
cg.getContentIds().add(content.getId());
content.getGroups().remove(cg.getName());
}
}
for (final String newContentGroups : content.getGroups()) {
Room.ContentGroup newGroup = new Room.ContentGroup();
Set<String> newContentIds = new HashSet<String>();
newContentIds.add(content.getId());
newGroup.setName(newContentGroups);
newGroup.setAutoSort(true);
newGroup.setContentIds(newContentIds);
room.getContentGroups().add(newGroup);
}
roomRepository.save(room);
......@@ -211,17 +231,26 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem
public void finalizeUpdate(final Content content) {
/* Update content groups of room */
final Room room = roomRepository.findOne(content.getRoomId());
final Set<String> contentsGroupNames = content.getGroups();
final Set<String> allGroupNames = new HashSet<>(contentsGroupNames);
allGroupNames.addAll(room.getContentGroups().keySet());
for (final String groupName : allGroupNames) {
Room.ContentGroup group = room.getContentGroups().getOrDefault(groupName, new Room.ContentGroup());
if (contentsGroupNames.contains(groupName)) {
group.getContentIds().add(content.getId());
for (final Room.ContentGroup cg : room.getContentGroups()) {
if (content.getGroups().contains(cg.getName())) {
cg.getContentIds().add(content.getId());
content.getGroups().remove(cg.getName());
} else {
group.getContentIds().remove(content.getId());
cg.getContentIds().remove(content.getId());
if (cg.getContentIds().isEmpty()) {
room.getContentGroups().remove(cg);
}
}
}
for (final String newContentGroups : content.getGroups()) {
Room.ContentGroup newGroup = new Room.ContentGroup();
Set<String> newContentIds = new HashSet<String>();
newContentIds.add(content.getId());
newGroup.setName(newContentGroups);
newGroup.setAutoSort(true);
newGroup.setContentIds(newContentIds);
room.getContentGroups().add(newGroup);
}
roomRepository.save(room);
/* TODO: not sure yet how to refactor this code - we need access to the old and new entity
......@@ -256,9 +285,10 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem
throw new UnauthorizedException();
}
for (final String groupName : content.getGroups()) {
Room.ContentGroup group = room.getContentGroups().getOrDefault(groupName, new Room.ContentGroup());
group.getContentIds().remove(content.getId());
for (final Room.ContentGroup contentGroup : room.getContentGroups()) {
if (content.getGroups().contains(contentGroup.getName())) {
contentGroup.getContentIds().remove(contentId);
}
}
roomRepository.save(room);
......
......@@ -59,7 +59,6 @@ import java.util.Comparator;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
......@@ -178,7 +177,7 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R
public Room get(final String id) {
Room r = super.get(id);
// creates a set from all room content groups
Set<String> cIdsWithGroup = r.getContentGroups().values().stream()
Set<String> cIdsWithGroup = r.getContentGroups().stream()
.map(Room.ContentGroup::getContentIds)
.flatMap(ids -> ids.stream())
.collect(Collectors.toSet());
......@@ -187,11 +186,12 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R
cIds.removeAll(cIdsWithGroup);
if (!cIds.isEmpty()) {
Map<String, Room.ContentGroup> cgs = r.getContentGroups();
Set<Room.ContentGroup> cgs = r.getContentGroups();
Room.ContentGroup defaultGroup = new Room.ContentGroup();
defaultGroup.setContentIds(cIds);
defaultGroup.setAutoSort(true);
cgs.put("", defaultGroup);
defaultGroup.setName("");
cgs.add(defaultGroup);
}
return r;
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment