diff --git a/src/main/java/de/thm/arsnova/controller/v2/CommentController.java b/src/main/java/de/thm/arsnova/controller/v2/CommentController.java index dab81e01c66ac7c6fe96c0cd3319224892597779..83811330c90e535f838e0e9d9aaed2b6991b624d 100644 --- a/src/main/java/de/thm/arsnova/controller/v2/CommentController.java +++ b/src/main/java/de/thm/arsnova/controller/v2/CommentController.java @@ -126,6 +126,6 @@ public class CommentController extends PaginationController { nickname = "deleteComment") @RequestMapping(value = "/{commentId}", method = RequestMethod.DELETE) public void deleteComment(@ApiParam(value = "ID of the comment that needs to be deleted", required = true) @PathVariable final String commentId) { - commentService.delete(commentId); + commentService.delete(commentService.get(commentId)); } } diff --git a/src/main/java/de/thm/arsnova/controller/v2/ContentController.java b/src/main/java/de/thm/arsnova/controller/v2/ContentController.java index b9239788f0b3635c964a20f4cdb69b529682d058..cc1a20422db636341d4fe152722291151a183262 100644 --- a/src/main/java/de/thm/arsnova/controller/v2/ContentController.java +++ b/src/main/java/de/thm/arsnova/controller/v2/ContentController.java @@ -556,7 +556,7 @@ public class ContentController extends PaginationController { @PathVariable final String answerId, final HttpServletResponse response ) { - answerService.deleteAnswer(contentId, answerId); + answerService.delete(answerService.get(answerId)); } @ApiOperation(value = "Delete answers from a content, identified by content ID", diff --git a/src/main/java/de/thm/arsnova/controller/v2/MotdController.java b/src/main/java/de/thm/arsnova/controller/v2/MotdController.java index 812f9ab18d95fa03b4171c5449ede78204e45267..7e65c4400a55610e6aa4acdb8dbb5962465ad796 100644 --- a/src/main/java/de/thm/arsnova/controller/v2/MotdController.java +++ b/src/main/java/de/thm/arsnova/controller/v2/MotdController.java @@ -148,11 +148,7 @@ public class MotdController extends AbstractController { @RequestMapping(value = "/{motdId}", method = RequestMethod.DELETE) public void deleteMotd(@ApiParam(value = "Motd-key from the message that shall be deleted", required = true) @PathVariable final String motdId) { de.thm.arsnova.model.Motd motd = motdService.get(motdId); - if (motd.getAudience() == de.thm.arsnova.model.Motd.Audience.ROOM) { - motdService.deleteByRoomId(motd.getRoomId(), motd); - } else { - motdService.delete(motd); - } + motdService.delete(motd); } @RequestMapping(value = "/userlist", method = RequestMethod.GET) diff --git a/src/main/java/de/thm/arsnova/service/AnswerService.java b/src/main/java/de/thm/arsnova/service/AnswerService.java index 759815ccf0dd77f8afccf504940dc9c6c2794c6d..01a1a6fc37fdfa5cffd3875ab94a9bff4983168f 100644 --- a/src/main/java/de/thm/arsnova/service/AnswerService.java +++ b/src/main/java/de/thm/arsnova/service/AnswerService.java @@ -42,8 +42,6 @@ public interface AnswerService extends EntityService<Answer> { Answer updateAnswer(Answer answer); - void deleteAnswer(String contentId, String answerId); - Map<String, Object> countAnswersAndAbstentionsInternal(String contentId); int countLectureContentAnswers(String roomId); diff --git a/src/main/java/de/thm/arsnova/service/AnswerServiceImpl.java b/src/main/java/de/thm/arsnova/service/AnswerServiceImpl.java index bc0b146cb937bee6a1dca389b4c634e046b42a5c..8bf36e4fccaa8d8c390a02ed6a59b3e429831164 100644 --- a/src/main/java/de/thm/arsnova/service/AnswerServiceImpl.java +++ b/src/main/java/de/thm/arsnova/service/AnswerServiceImpl.java @@ -27,8 +27,6 @@ import de.thm.arsnova.model.Room; import de.thm.arsnova.model.TextAnswer; import de.thm.arsnova.model.transport.AnswerQueueElement; import de.thm.arsnova.persistence.AnswerRepository; -import de.thm.arsnova.persistence.ContentRepository; -import de.thm.arsnova.persistence.RoomRepository; import de.thm.arsnova.security.User; import de.thm.arsnova.web.exceptions.NotFoundException; import de.thm.arsnova.web.exceptions.UnauthorizedException; @@ -58,23 +56,21 @@ public class AnswerServiceImpl extends DefaultEntityServiceImpl<Answer> implemen private final Queue<AnswerQueueElement> answerQueue = new ConcurrentLinkedQueue<>(); - private RoomRepository roomRepository; - private ContentRepository contentRepository; - private AnswerRepository answerRepository; + private RoomService roomService; private ContentService contentService; + private AnswerRepository answerRepository; private UserService userService; public AnswerServiceImpl( AnswerRepository repository, - ContentRepository contentRepository, - RoomRepository roomRepository, + RoomService roomService, ContentService contentService, UserService userService, @Qualifier("defaultJsonMessageConverter") MappingJackson2HttpMessageConverter jackson2HttpMessageConverter) { super(Answer.class, repository, jackson2HttpMessageConverter.getObjectMapper()); this.answerRepository = repository; - this.contentRepository = contentRepository; - this.roomRepository = roomRepository; + this.roomService = roomService; + this.contentService = contentService; this.contentService = contentService; this.userService = userService; } @@ -110,7 +106,7 @@ public class AnswerServiceImpl extends DefaultEntityServiceImpl<Answer> implemen @Override @PreAuthorize("hasPermission(#contentId, 'content', 'owner')") public void deleteAnswers(final String contentId) { - final Content content = contentRepository.findOne(contentId); + final Content content = contentService.get(contentId); content.resetState(); /* FIXME: cancel timer */ contentService.update(content); @@ -129,7 +125,7 @@ public class AnswerServiceImpl extends DefaultEntityServiceImpl<Answer> implemen @Override public void getFreetextAnswerAndMarkRead(final String answerId, final String userId) { - final Answer answer = answerRepository.findOne(answerId); + final Answer answer = get(answerId); if (!(answer instanceof TextAnswer)) { throw new NotFoundException(); } @@ -137,17 +133,17 @@ public class AnswerServiceImpl extends DefaultEntityServiceImpl<Answer> implemen if (textAnswer.isRead()) { return; } - final Room room = roomRepository.findOne(textAnswer.getRoomId()); + final Room room = roomService.get(textAnswer.getRoomId()); if (room.getOwnerId().equals(userId)) { textAnswer.setRead(true); - answerRepository.save(textAnswer); + update(textAnswer); } } @Override @PreAuthorize("isAuthenticated()") public AnswerStatistics getStatistics(final String contentId, final int round) { - final ChoiceQuestionContent content = (ChoiceQuestionContent) contentRepository.findOne(contentId); + final ChoiceQuestionContent content = (ChoiceQuestionContent) contentService.get(contentId); if (content == null) { throw new NotFoundException(); } @@ -191,7 +187,7 @@ public class AnswerServiceImpl extends DefaultEntityServiceImpl<Answer> implemen @PreAuthorize("isAuthenticated()") public List<TextAnswer> getTextAnswers(final String contentId, final int piRound, final int offset, final int limit) { /* FIXME: round support not implemented */ - final Content content = contentRepository.findOne(contentId); + final Content content = contentService.get(contentId); if (content == null) { throw new NotFoundException(); } @@ -320,7 +316,7 @@ public class AnswerServiceImpl extends DefaultEntityServiceImpl<Answer> implemen if (content == null) { throw new NotFoundException(); } - final Room room = roomRepository.findOne(content.getRoomId()); + final Room room = roomService.get(content.getRoomId()); answer.setCreatorId(user.getId()); answer.setContentId(content.getId()); @@ -371,43 +367,22 @@ public class AnswerServiceImpl extends DefaultEntityServiceImpl<Answer> implemen content.checkTextStrictOptions(realAnswer); } */ - final Room room = roomRepository.findOne(content.getRoomId()); + final Room room = roomService.get(content.getRoomId()); answer.setCreatorId(user.getId()); answer.setContentId(content.getId()); answer.setRoomId(room.getId()); - this.eventPublisher.publishEvent(new BeforeCreationEvent<>(this, realAnswer)); - answerRepository.save(realAnswer); - this.eventPublisher.publishEvent(new AfterCreationEvent<>(this, realAnswer)); + update(realAnswer); return answer; } - /* FIXME: Remove, this should be handled by EntityService! */ - @Override - @PreAuthorize("isAuthenticated()") - @CacheEvict(value = "answerlists", allEntries = true) - public void deleteAnswer(final String contentId, final String answerId) { - final Content content = contentRepository.findOne(contentId); - if (content == null) { - throw new NotFoundException(); - } - final User user = userService.getCurrentUser(); - final Room room = roomRepository.findOne(content.getRoomId()); - if (user == null || room == null || !room.getOwnerId().equals(user.getId())) { - throw new UnauthorizedException(); - } - //this.eventPublisher.publishEvent(new BeforeDeletionEvent<>(answer)); - answerRepository.deleteById(answerId); - //this.eventPublisher.publishEvent(new AfterDeletionEvent<>(answer)); - } - /* * The "internal" suffix means it is called by internal services that have no authentication! * TODO: Find a better way of doing this... */ @Override public int countLectureQuestionAnswersInternal(final String roomId) { - return answerRepository.countByRoomIdOnlyLectureVariant(roomRepository.findOne(roomId).getId()); + return answerRepository.countByRoomIdOnlyLectureVariant(roomService.get(roomId).getId()); } @Override @@ -444,6 +419,6 @@ public class AnswerServiceImpl extends DefaultEntityServiceImpl<Answer> implemen */ @Override public int countPreparationQuestionAnswersInternal(final String roomId) { - return answerRepository.countByRoomIdOnlyPreparationVariant(roomRepository.findOne(roomId).getId()); + return answerRepository.countByRoomIdOnlyPreparationVariant(roomService.get(roomId).getId()); } } diff --git a/src/main/java/de/thm/arsnova/service/CommentService.java b/src/main/java/de/thm/arsnova/service/CommentService.java index a359bf881312b0c1f99846ade9140444c125e1be..5c0202e3210517b5349185075ce7431fc1498995 100644 --- a/src/main/java/de/thm/arsnova/service/CommentService.java +++ b/src/main/java/de/thm/arsnova/service/CommentService.java @@ -15,7 +15,5 @@ public interface CommentService extends EntityService<Comment> { Comment getAndMarkRead(String commentId) throws IOException; - void delete(String commentId); - void deleteByRoomId(String roomId); } diff --git a/src/main/java/de/thm/arsnova/service/CommentServiceImpl.java b/src/main/java/de/thm/arsnova/service/CommentServiceImpl.java index 91e7708ef82922b67b44d8a33183f1af1c6d83f8..69c1241f46d801983ed2bf70d58793a6605cb6ab 100644 --- a/src/main/java/de/thm/arsnova/service/CommentServiceImpl.java +++ b/src/main/java/de/thm/arsnova/service/CommentServiceImpl.java @@ -1,12 +1,9 @@ package de.thm.arsnova.service; -import de.thm.arsnova.event.AfterDeletionEvent; -import de.thm.arsnova.event.BeforeDeletionEvent; import de.thm.arsnova.model.Comment; import de.thm.arsnova.model.Room; import de.thm.arsnova.model.migration.v2.CommentReadingCount; import de.thm.arsnova.persistence.CommentRepository; -import de.thm.arsnova.persistence.RoomRepository; import de.thm.arsnova.security.User; import de.thm.arsnova.web.exceptions.ForbiddenException; import de.thm.arsnova.web.exceptions.NotFoundException; @@ -29,25 +26,25 @@ import java.util.Map; public class CommentServiceImpl extends DefaultEntityServiceImpl<Comment> implements CommentService { private UserService userService; - private CommentRepository commentRepository; + private RoomService roomService; - private RoomRepository roomRepository; + private CommentRepository commentRepository; public CommentServiceImpl( CommentRepository repository, - RoomRepository roomRepository, + RoomService roomService, UserService userService, @Qualifier("defaultJsonMessageConverter") MappingJackson2HttpMessageConverter jackson2HttpMessageConverter) { super(Comment.class, repository, jackson2HttpMessageConverter.getObjectMapper()); this.commentRepository = repository; - this.roomRepository = roomRepository; + this.roomService = roomService; this.userService = userService; } @Override @PreAuthorize("isAuthenticated()") public void prepareCreate(final Comment comment) { - final Room room = roomRepository.findOne(comment.getRoomId()); + final Room room = roomService.get(comment.getRoomId()); final User user = userService.getCurrentUser(); comment.setCreatorId(user.getId()); comment.setRead(false); @@ -57,23 +54,10 @@ public class CommentServiceImpl extends DefaultEntityServiceImpl<Comment> implem /* TODO: fire event */ } - /* FIXME: Remove, EntityService should handle this! */ - @Override - @PreAuthorize("hasPermission(#commentId, 'comment', 'owner')") - public void delete(final String commentId) { - final Comment comment = commentRepository.findOne(commentId); - if (comment == null) { - throw new NotFoundException(); - } - eventPublisher.publishEvent(new BeforeDeletionEvent<>(this, comment)); - commentRepository.delete(comment); - eventPublisher.publishEvent(new AfterDeletionEvent<>(this, comment)); - } - @Override @PreAuthorize("isAuthenticated()") public void deleteByRoomId(final String roomId) { - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); if (room == null) { throw new UnauthorizedException(); } @@ -109,7 +93,7 @@ public class CommentServiceImpl extends DefaultEntityServiceImpl<Comment> implem @Override @PreAuthorize("isAuthenticated()") public List<Comment> getByRoomId(final String roomId, final int offset, final int limit) { - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); final User user = getCurrentUser(); if (room.getOwnerId().equals(user.getId())) { return commentRepository.findByRoomId(room.getId(), offset, limit); @@ -121,7 +105,7 @@ public class CommentServiceImpl extends DefaultEntityServiceImpl<Comment> implem @Override @PreAuthorize("hasPermission(#commentId, 'comment', 'update')") public Comment getAndMarkRead(final String commentId) throws IOException { - final Comment comment = commentRepository.findOne(commentId); + final Comment comment = get(commentId); if (comment == null) { throw new NotFoundException(); } diff --git a/src/main/java/de/thm/arsnova/service/ContentServiceImpl.java b/src/main/java/de/thm/arsnova/service/ContentServiceImpl.java index 265382e26b2ab4c0b83e5fbb97be01c1d7b1c620..1f31288f168518873d2d5ce184c80e54efde0d15 100644 --- a/src/main/java/de/thm/arsnova/service/ContentServiceImpl.java +++ b/src/main/java/de/thm/arsnova/service/ContentServiceImpl.java @@ -17,16 +17,11 @@ */ package de.thm.arsnova.service; -import de.thm.arsnova.event.AfterCreationEvent; -import de.thm.arsnova.event.AfterDeletionEvent; -import de.thm.arsnova.event.BeforeCreationEvent; -import de.thm.arsnova.event.BeforeDeletionEvent; import de.thm.arsnova.model.Content; import de.thm.arsnova.model.Room; import de.thm.arsnova.persistence.AnswerRepository; import de.thm.arsnova.persistence.ContentRepository; import de.thm.arsnova.persistence.LogEntryRepository; -import de.thm.arsnova.persistence.RoomRepository; import de.thm.arsnova.security.User; import de.thm.arsnova.web.exceptions.NotFoundException; import de.thm.arsnova.web.exceptions.UnauthorizedException; @@ -58,9 +53,9 @@ import java.util.stream.Collectors; public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implements ContentService { private UserService userService; - private LogEntryRepository dbLogger; + private RoomService roomService; - private RoomRepository roomRepository; + private LogEntryRepository dbLogger; private ContentRepository contentRepository; @@ -70,15 +65,15 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem public ContentServiceImpl( ContentRepository repository, + RoomService roomService, AnswerRepository answerRepository, - RoomRepository roomRepository, LogEntryRepository dbLogger, UserService userService, @Qualifier("defaultJsonMessageConverter") MappingJackson2HttpMessageConverter jackson2HttpMessageConverter) { super(Content.class, repository, jackson2HttpMessageConverter.getObjectMapper()); this.contentRepository = repository; + this.roomService = roomService; this.answerRepository = answerRepository; - this.roomRepository = roomRepository; this.dbLogger = dbLogger; this.userService = userService; } @@ -94,7 +89,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem } //content.setSessionKeyword(roomRepository.getSessionFromId(content.getRoomId()).getKeyword()); - Room room = roomRepository.findOne(content.getRoomId()); + Room room = roomService.get(content.getRoomId()); content.setGroups(room.getContentGroups().stream() .map(Room.ContentGroup::getName).collect(Collectors.toSet())); @@ -111,7 +106,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem @PreAuthorize("isAuthenticated()") //@Cacheable("contentlists") public List<Content> getByRoomId(final String roomId) { - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); final User user = userService.getCurrentUser(); if (room.getOwnerId().equals(user.getId())) { return contentRepository.findByRoomIdForSpeaker(roomId); @@ -122,7 +117,7 @@ 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 room = roomService.get(roomId); Room.ContentGroup contentGroup = null; for (Room.ContentGroup cg : room.getContentGroups()) { if (cg.getName().equals(group)) { @@ -144,7 +139,7 @@ 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 room = roomService.get(roomId); Room.ContentGroup contentGroup = null; for (Room.ContentGroup cg : room.getContentGroups()) { if (cg.getName().equals(group)) { @@ -184,7 +179,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem @Override public void finalizeCreate(final Content content) { /* Update content groups of room */ - final Room room = roomRepository.findOne(content.getRoomId()); + final Room room = roomService.get(content.getRoomId()); final Set<Room.ContentGroup> contentGroups = room.getContentGroups(); for (final Room.ContentGroup cg : contentGroups) { if (content.getGroups().contains(cg.getName())) { @@ -201,20 +196,18 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem newGroup.setContentIds(newContentIds); room.getContentGroups().add(newGroup); } - eventPublisher.publishEvent(new BeforeCreationEvent<>(this, content)); - roomRepository.save(room); - eventPublisher.publishEvent(new AfterCreationEvent<>(this, content)); + roomService.update(room); } @Override public void prepareUpdate(final Content content) { final User user = userService.getCurrentUser(); - final Content oldContent = contentRepository.findOne(content.getId()); + final Content oldContent = get(content.getId()); if (null == oldContent) { throw new NotFoundException(); } - final Room room = roomRepository.findOne(content.getRoomId()); + final Room room = roomService.get(content.getRoomId()); if (user == null || room == null || !room.getOwnerId().equals(user.getId())) { throw new UnauthorizedException(); } @@ -232,7 +225,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem @Override public void finalizeUpdate(final Content content) { /* Update content groups of room */ - final Room room = roomRepository.findOne(content.getRoomId()); + final Room room = roomService.get(content.getRoomId()); for (final Room.ContentGroup cg : room.getContentGroups()) { if (content.getGroups().contains(cg.getName())) { cg.getContentIds().add(content.getId()); @@ -253,7 +246,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem newGroup.setContentIds(newContentIds); room.getContentGroups().add(newGroup); } - roomRepository.save(room); + roomService.update(room); /* TODO: not sure yet how to refactor this code - we need access to the old and new entity if (!oldContent.getState().isVisible() && content.getState().isVisible()) { @@ -277,28 +270,14 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem @CacheEvict(value = "preparationcontentlists", allEntries = true /*, condition = "#content.getGroups().contains('preparation')"*/), @CacheEvict(value = "flashcardcontentlists", allEntries = true /*, condition = "#content.getGroups().contains('flashcard')"*/) }) public void delete(final String contentId) { - final Content content = contentRepository.findOne(contentId); + final Content content = get(contentId); if (content == null) { throw new NotFoundException(); } - final Room room = roomRepository.findOne(content.getRoomId()); - if (room == null) { - throw new UnauthorizedException(); - } - - for (final Room.ContentGroup contentGroup : room.getContentGroups()) { - if (content.getGroups().contains(contentGroup.getName())) { - contentGroup.getContentIds().remove(contentId); - } - } - roomRepository.save(room); - try { final int count = answerRepository.deleteByContentId(contentId); - eventPublisher.publishEvent(new BeforeDeletionEvent<>(this, content)); - contentRepository.deleteById(contentId); - eventPublisher.publishEvent(new AfterDeletionEvent<>(this, content)); + delete(content); dbLogger.log("delete", "type", "content", "answerCount", count); } catch (final IllegalArgumentException e) { logger.error("Could not delete content {}.", contentId, e); @@ -358,7 +337,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem @Override @PreAuthorize("hasPermission(#contentId, 'content', 'owner')") public void setVotingAdmission(final String contentId, final boolean disableVoting) { - final Content content = contentRepository.findOne(contentId); + final Content content = get(contentId); content.getState().setResponsesEnabled(!disableVoting); if (!disableVoting && !content.getState().isVisible()) { @@ -378,7 +357,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem @CacheEvict(value = "flashcardcontentlists", key = "#roomId") }) public void setVotingAdmissions(final String roomId, final boolean disableVoting, Iterable<Content> contents) { final User user = getCurrentUser(); - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); if (!room.getOwnerId().equals(user.getId())) { throw new UnauthorizedException(); } @@ -395,7 +374,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem private Room getRoomWithAuthCheck(final String roomId) { final User user = userService.getCurrentUser(); - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); if (user == null || room == null || !room.getOwnerId().equals(user.getId())) { throw new UnauthorizedException(); } @@ -455,7 +434,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem public void publishAll(final String roomId, final boolean publish) throws IOException { /* TODO: resolve redundancies */ final User user = getCurrentUser(); - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); if (!room.getOwnerId().equals(user.getId())) { throw new UnauthorizedException(); } @@ -472,7 +451,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem @CacheEvict(value = "flashcardcontentlists", key = "#roomId") }) public void publishContents(final String roomId, final boolean publish, Iterable<Content> contents) throws IOException { final User user = getCurrentUser(); - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); if (!room.getOwnerId().equals(user.getId())) { throw new UnauthorizedException(); } @@ -485,7 +464,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem @CacheEvict(value = "answerlists", allEntries = true) public void deleteAllContentsAnswers(final String roomId) { final User user = getCurrentUser(); - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); if (!room.getOwnerId().equals(user.getId())) { throw new UnauthorizedException(); } @@ -503,7 +482,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem @PreAuthorize("hasPermission(#roomId, 'room', 'owner')") @CacheEvict(value = "answerlists", allEntries = true) public void deleteAllPreparationAnswers(String roomId) { - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); final List<Content> contents = contentRepository.findByRoomIdAndVariantAndActive(room.getId(), "preparation"); resetContentsRoundState(room.getId(), contents); @@ -518,7 +497,7 @@ public class ContentServiceImpl extends DefaultEntityServiceImpl<Content> implem @PreAuthorize("hasPermission(#roomId, 'room', 'owner')") @CacheEvict(value = "answerlists", allEntries = true) public void deleteAllLectureAnswers(String roomId) { - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); final List<Content> contents = contentRepository.findByRoomIdAndVariantAndActive(room.getId(), "lecture"); resetContentsRoundState(room.getId(), contents); diff --git a/src/main/java/de/thm/arsnova/service/DefaultEntityServiceImpl.java b/src/main/java/de/thm/arsnova/service/DefaultEntityServiceImpl.java index 1cd54312953fe8c3ff5677acfadae915d6daa3e6..194822867c9ed71650d7d63f0e50de47bfa4b4c0 100644 --- a/src/main/java/de/thm/arsnova/service/DefaultEntityServiceImpl.java +++ b/src/main/java/de/thm/arsnova/service/DefaultEntityServiceImpl.java @@ -101,7 +101,7 @@ public class DefaultEntityServiceImpl<T extends Entity> implements EntityService eventPublisher.publishEvent(new BeforeCreationEvent<>(this, entity)); final T createdEntity = repository.save(entity); eventPublisher.publishEvent(new AfterCreationEvent<>(this, createdEntity)); - finalizeCreate(entity); + finalizeCreate(createdEntity); modifyRetrieved(entity); return createdEntity; diff --git a/src/main/java/de/thm/arsnova/service/FeedbackServiceImpl.java b/src/main/java/de/thm/arsnova/service/FeedbackServiceImpl.java index ea4b87312625056348c3d7d38f5bdc3c9916b396..b542ec44c93b21d52664c224b71e7af12b7adbe7 100644 --- a/src/main/java/de/thm/arsnova/service/FeedbackServiceImpl.java +++ b/src/main/java/de/thm/arsnova/service/FeedbackServiceImpl.java @@ -21,7 +21,6 @@ import de.thm.arsnova.event.DeleteFeedbackForRoomsEvent; import de.thm.arsnova.event.NewFeedbackEvent; import de.thm.arsnova.model.Feedback; import de.thm.arsnova.model.Room; -import de.thm.arsnova.persistence.RoomRepository; import de.thm.arsnova.web.exceptions.NoContentException; import de.thm.arsnova.web.exceptions.NotFoundException; import org.springframework.beans.factory.annotation.Value; @@ -51,15 +50,15 @@ public class FeedbackServiceImpl implements FeedbackService, ApplicationEventPub @Value("${feedback.cleanup}") private int cleanupFeedbackDelay; - private RoomRepository roomRepository; + private RoomService roomService; private FeedbackStorageService feedbackStorage; private ApplicationEventPublisher publisher; - public FeedbackServiceImpl(FeedbackStorageService feedbackStorage, RoomRepository roomRepository) { + public FeedbackServiceImpl(FeedbackStorageService feedbackStorage, RoomService roomService) { this.feedbackStorage = feedbackStorage; - this.roomRepository = roomRepository; + this.roomService = roomService; } @Override @@ -101,7 +100,7 @@ public class FeedbackServiceImpl implements FeedbackService, ApplicationEventPub @Override public void cleanFeedbackVotesByRoomId(final String roomId, final int cleanupFeedbackDelayInMins) { - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); List<String> affectedUserIds = feedbackStorage.cleanVotesByRoom(room, cleanupFeedbackDelayInMins); Set<Room> sessionSet = new HashSet<>(); sessionSet.add(room); @@ -116,7 +115,7 @@ public class FeedbackServiceImpl implements FeedbackService, ApplicationEventPub @Override public Feedback getByRoomId(final String roomId) { - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); if (room == null) { throw new NotFoundException(); } @@ -133,7 +132,7 @@ public class FeedbackServiceImpl implements FeedbackService, ApplicationEventPub @Override public double calculateAverageFeedback(final String roomId) { - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); if (room == null) { throw new NotFoundException(); } @@ -157,7 +156,7 @@ public class FeedbackServiceImpl implements FeedbackService, ApplicationEventPub @Override public boolean save(final String roomId, final int value, final String userId) { - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); if (room == null) { throw new NotFoundException(); } @@ -169,7 +168,7 @@ public class FeedbackServiceImpl implements FeedbackService, ApplicationEventPub @Override public Integer getByRoomIdAndUserId(final String roomId, final String userId) { - final Room room = roomRepository.findOne(roomId); + final Room room = roomService.get(roomId); if (room == null) { throw new NotFoundException(); } diff --git a/src/main/java/de/thm/arsnova/service/MotdService.java b/src/main/java/de/thm/arsnova/service/MotdService.java index 6de4cc1d24135a3bec0015c6dfca451ae92683fc..c0ce86e000ce5b6274bd3863afb5813fa06b7432 100644 --- a/src/main/java/de/thm/arsnova/service/MotdService.java +++ b/src/main/java/de/thm/arsnova/service/MotdService.java @@ -38,10 +38,6 @@ public interface MotdService extends EntityService<Motd> { List<Motd> filterMotdsByList(List<Motd> list, List<String> ids); - void delete(Motd motd); - - void deleteByRoomId(final String roomId, Motd motd); - Motd save(Motd motd); Motd save(final String roomId, final Motd motd); diff --git a/src/main/java/de/thm/arsnova/service/MotdServiceImpl.java b/src/main/java/de/thm/arsnova/service/MotdServiceImpl.java index 6a2b09d40b421e1b9d3a5ddb98f1351dbdc0c97e..53dcf4fb6198b5e9fe1151ef72be5ce4350208e2 100644 --- a/src/main/java/de/thm/arsnova/service/MotdServiceImpl.java +++ b/src/main/java/de/thm/arsnova/service/MotdServiceImpl.java @@ -135,7 +135,7 @@ public class MotdServiceImpl extends DefaultEntityServiceImpl<Motd> implements M @CacheEvict(cacheNames = "motds", key = "#motd.audience + #motd.roomId") private Motd createOrUpdateMotd(final Motd motd) { if (motd.getId() != null) { - Motd oldMotd = motdRepository.findOne(motd.getId()); + Motd oldMotd = get(motd.getId()); if (!(motd.getId().equals(oldMotd.getId()) && motd.getRoomId().equals(oldMotd.getRoomId()) && motd.getAudience().equals(oldMotd.getAudience()))) { throw new BadRequestException(); @@ -151,17 +151,4 @@ public class MotdServiceImpl extends DefaultEntityServiceImpl<Motd> implements M return super.create(motd); } - - @Override - @PreAuthorize("hasPermission('', 'motd', 'admin')") - @CacheEvict(cacheNames = "motds", key = "#motd.audience + #motd.roomId") - public void delete(Motd motd) { - motdRepository.delete(motd); - } - - @Override - @PreAuthorize("hasPermission(#roomId, 'room', 'owner')") - public void deleteByRoomId(final String roomId, Motd motd) { - motdRepository.delete(motd); - } } diff --git a/src/main/java/de/thm/arsnova/service/RoomService.java b/src/main/java/de/thm/arsnova/service/RoomService.java index 036e097ad96b5ce63c2af2cb3f2b5b0220181284..1660058b6a2cba8e95e855b64f78fae471692696 100644 --- a/src/main/java/de/thm/arsnova/service/RoomService.java +++ b/src/main/java/de/thm/arsnova/service/RoomService.java @@ -19,7 +19,6 @@ package de.thm.arsnova.service; import de.thm.arsnova.connector.model.Course; import de.thm.arsnova.model.Room; -import de.thm.arsnova.model.migration.v2.ClientAuthentication; import de.thm.arsnova.model.transport.ImportExportContainer; import de.thm.arsnova.model.transport.ScoreStatistics; @@ -63,8 +62,6 @@ public interface RoomService extends EntityService<Room> { Room updateCreator(String id, String newCreator); - Room updateInternal(Room room, ClientAuthentication user); - int[] deleteCascading(Room room); ScoreStatistics getLearningProgress(String id, String type, String questionVariant); diff --git a/src/main/java/de/thm/arsnova/service/RoomServiceImpl.java b/src/main/java/de/thm/arsnova/service/RoomServiceImpl.java index ec193d3c0480c23eafc1c98fd05ec350f5c6ba14..6d9b4fef6dab946c1be23cf39866cdf8a550f4d8 100644 --- a/src/main/java/de/thm/arsnova/service/RoomServiceImpl.java +++ b/src/main/java/de/thm/arsnova/service/RoomServiceImpl.java @@ -19,12 +19,9 @@ package de.thm.arsnova.service; import de.thm.arsnova.connector.client.ConnectorClient; import de.thm.arsnova.connector.model.Course; -import de.thm.arsnova.event.AfterDeletionEvent; -import de.thm.arsnova.event.BeforeDeletionEvent; import de.thm.arsnova.event.FlipFlashcardsEvent; import de.thm.arsnova.model.Room; import de.thm.arsnova.model.UserProfile; -import de.thm.arsnova.model.migration.v2.ClientAuthentication; import de.thm.arsnova.model.transport.ImportExportContainer; import de.thm.arsnova.model.transport.ScoreStatistics; import de.thm.arsnova.persistence.AnswerRepository; @@ -96,25 +93,37 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R public RoomServiceImpl( RoomRepository repository, - ContentRepository contentRepository, - AnswerRepository answerRepository, - CommentRepository commentRepository, LogEntryRepository dbLogger, UserService userService, - FeedbackService feedbackService, ScoreCalculatorFactory scoreCalculatorFactory, @Qualifier("defaultJsonMessageConverter") MappingJackson2HttpMessageConverter jackson2HttpMessageConverter) { super(Room.class, repository, jackson2HttpMessageConverter.getObjectMapper()); this.roomRepository = repository; - this.contentRepository = contentRepository; - this.answerRepository = answerRepository; - this.commentRepository = commentRepository; this.dbLogger = dbLogger; this.userService = userService; - this.feedbackService = feedbackService; this.scoreCalculatorFactory = scoreCalculatorFactory; } + @Autowired + public void setCommentRepository(final CommentRepository commentRepository) { + this.commentRepository = commentRepository; + } + + @Autowired + public void setContentRepository(final ContentRepository contentRepository) { + this.contentRepository = contentRepository; + } + + @Autowired + public void setAnswerRepository(final AnswerRepository answerRepository) { + this.answerRepository = answerRepository; + } + + @Autowired + public void setFeedbackService(final FeedbackService feedbackService) { + this.feedbackService = feedbackService; + } + public static class RoomNameComparator implements Comparator<Room>, Serializable { private static final long serialVersionUID = 1L; @@ -196,7 +205,7 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R @Override public Room join(final String id, final UUID socketId) { - Room room = null != id ? roomRepository.findOne(id) : null; + Room room = null != id ? get(id) : null; if (null == room) { userService.removeUserFromRoomBySocketId(socketId); return null; @@ -240,7 +249,7 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R @PreAuthorize("hasPermission(#id, 'room', 'owner')") public Room getForAdmin(final String id) { - return roomRepository.findOne(id); + return get(id); } /* @@ -249,7 +258,7 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R */ @Override public Room getInternal(final String id, final String userId) { - final Room room = roomRepository.findOne(id); + final Room room = get(id, true); if (room == null) { throw new NotFoundException(); } @@ -401,7 +410,7 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R @Override @PreAuthorize("hasPermission(#id, 'room', 'owner')") public Room setActive(final String id, final Boolean lock) throws IOException { - final Room room = roomRepository.findOne(id); + final Room room = get(id); patch(room, Collections.singletonMap("closed", lock)); return room; @@ -411,7 +420,7 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R /* TODO: move caching to DefaultEntityServiceImpl */ //@CachePut(value = "rooms", key = "#room") protected void prepareUpdate(final Room room) { - final Room existingRoom = roomRepository.findOne(room.getId()); + final Room existingRoom = get(room.getId()); room.setOwnerId(existingRoom.getOwnerId()); handleLogo(room); @@ -427,19 +436,6 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R throw new UnsupportedOperationException("No longer implemented."); } - /* - * The "internal" suffix means it is called by internal services that have no authentication! - * TODO: Find a better way of doing this... - */ - @Override - public Room updateInternal(final Room room, final ClientAuthentication user) { - if (room.getOwnerId().equals(user.getId())) { - roomRepository.save(room); - return room; - } - return null; - } - @Override @PreAuthorize("hasPermission(#room, 'owner')") @Caching(evict = { @@ -452,9 +448,7 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R count[2] = commentRepository.deleteByRoomId(room.getId()); count[1] = answerRepository.deleteByContentIds(contentIds); count[0] = contentRepository.deleteByRoomId(room.getId()); - this.eventPublisher.publishEvent(new BeforeDeletionEvent<>(this, room)); - roomRepository.delete(room); - this.eventPublisher.publishEvent(new AfterDeletionEvent<>(this, room)); + delete(room); logger.debug("Deleted room document {} and related data.", room.getId()); dbLogger.log("delete", "type", "session", "id", room.getId()); @@ -464,7 +458,7 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R @Override @PreAuthorize("hasPermission(#id, 'room', 'read')") public ScoreStatistics getLearningProgress(final String id, final String type, final String questionVariant) { - final Room room = roomRepository.findOne(id); + final Room room = get(id); ScoreCalculator scoreCalculator = scoreCalculatorFactory.create(type, questionVariant); return scoreCalculator.getCourseProgress(room); } @@ -472,7 +466,7 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R @Override @PreAuthorize("hasPermission(#id, 'room', 'read')") public ScoreStatistics getMyLearningProgress(final String id, final String type, final String questionVariant) { - final Room room = roomRepository.findOne(id); + final Room room = get(id); final User user = userService.getCurrentUser(); ScoreCalculator scoreCalculator = scoreCalculatorFactory.create(type, questionVariant); return scoreCalculator.getMyProgress(room, user.getId()); @@ -508,17 +502,16 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R @Override @PreAuthorize("hasPermission(#id, 'room', 'read')") public Room.Settings getFeatures(String id) { - return roomRepository.findOne(id).getSettings(); + return get(id).getSettings(); } @Override @PreAuthorize("hasPermission(#id, 'room', 'owner')") public Room.Settings updateFeatures(String id, Room.Settings settings) { - final Room room = roomRepository.findOne(id); + final Room room = get(id); room.setSettings(settings); - - roomRepository.save(room); + update(room); return room.getSettings(); } @@ -526,7 +519,7 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R @Override @PreAuthorize("hasPermission(#id, 'room', 'owner')") public boolean lockFeedbackInput(String id, Boolean lock) throws IOException { - final Room room = roomRepository.findOne(id); + final Room room = get(id); if (!lock) { feedbackService.cleanFeedbackVotesByRoomId(id, 0); } @@ -538,7 +531,7 @@ public class RoomServiceImpl extends DefaultEntityServiceImpl<Room> implements R @Override @PreAuthorize("hasPermission(#id, 'room', 'owner')") public boolean flipFlashcards(String id, Boolean flip) { - final Room room = roomRepository.findOne(id); + final Room room = get(id); this.eventPublisher.publishEvent(new FlipFlashcardsEvent(this, room.getId())); return flip; diff --git a/src/main/java/de/thm/arsnova/service/TimerServiceImpl.java b/src/main/java/de/thm/arsnova/service/TimerServiceImpl.java index 43bbea949a4bf33c08b134393d08b9bfdf6df9cf..5a4fad25a5b18feae35a0012d4f943011b8205d3 100644 --- a/src/main/java/de/thm/arsnova/service/TimerServiceImpl.java +++ b/src/main/java/de/thm/arsnova/service/TimerServiceImpl.java @@ -3,7 +3,6 @@ package de.thm.arsnova.service; import de.thm.arsnova.model.Content; import de.thm.arsnova.model.Room; import de.thm.arsnova.persistence.AnswerRepository; -import de.thm.arsnova.persistence.RoomRepository; import de.thm.arsnova.security.User; import org.springframework.cache.annotation.CacheEvict; import org.springframework.security.access.prepost.PreAuthorize; @@ -18,14 +17,14 @@ import java.util.TimerTask; public class TimerServiceImpl implements TimerService { private HashMap<String, Timer> timerList = new HashMap<>(); private UserService userService; - private RoomRepository roomRepository; + private RoomService roomService; private ContentService contentService; private AnswerRepository answerRepository; - public TimerServiceImpl(final UserService userService, final RoomRepository roomRepository, + public TimerServiceImpl(final UserService userService, final RoomService roomService, final ContentService contentService, final AnswerRepository answerRepository) { this.userService = userService; - this.roomRepository = roomRepository; + this.roomService = roomService; this.contentService = contentService; this.answerRepository = answerRepository; } @@ -34,7 +33,7 @@ public class TimerServiceImpl implements TimerService { @PreAuthorize("isAuthenticated() and hasPermission(#contentId, 'content', 'owner')") public void startNewRound(final String contentId) { final Content content = contentService.get(contentId); - final Room room = roomRepository.findOne(content.getRoomId()); + final Room room = roomService.get(content.getRoomId()); cancelDelayedRoundChange(contentId); @@ -49,7 +48,7 @@ public class TimerServiceImpl implements TimerService { public void startNewRoundDelayed(final String contentId, final int time) { final User user = userService.getCurrentUser(); final Content content = contentService.get(contentId); - final Room room = roomRepository.findOne(content.getRoomId()); + final Room room = roomService.get(content.getRoomId()); final Date date = new Date(); final Timer timer = new Timer(); @@ -71,7 +70,7 @@ public class TimerServiceImpl implements TimerService { @PreAuthorize("hasPermission(#contentId, 'content', 'owner')") public void cancelRoundChange(final String contentId) { final Content content = contentService.get(contentId); - final Room room = roomRepository.findOne(content.getRoomId()); + final Room room = roomService.get(content.getRoomId()); cancelDelayedRoundChange(contentId); resetRoundManagementState(content); @@ -100,7 +99,7 @@ public class TimerServiceImpl implements TimerService { @CacheEvict("answerlists") public void resetRoundState(final String contentId) { final Content content = contentService.get(contentId); - final Room room = roomRepository.findOne(content.getRoomId()); + final Room room = roomService.get(content.getRoomId()); cancelDelayedRoundChange(contentId); if (Content.Format.TEXT == content.getFormat()) {