From 079b2e52003fdeb57fd720b5f71e32e75fde9de8 Mon Sep 17 00:00:00 2001
From: Christoph Thelen <christoph.thelen@mni.thm.de>
Date: Sat, 18 Apr 2015 15:46:13 +0200
Subject: [PATCH] Fix #15670: Clear statistics on new or deleted session

---
 .../java/de/thm/arsnova/dao/CacheBuster.java  | 10 ++++++
 .../domain/LearningProgressFactory.java       |  9 ++++-
 .../arsnova/events/DeleteSessionEvent.java    | 35 +++++++++++++++++++
 .../thm/arsnova/events/NewSessionEvent.java   | 35 +++++++++++++++++++
 .../thm/arsnova/events/NovaEventVisitor.java  |  3 ++
 .../thm/arsnova/services/SessionService.java  |  8 ++++-
 .../arsnova/socket/ARSnovaSocketIOServer.java | 10 +++++-
 7 files changed, 107 insertions(+), 3 deletions(-)
 create mode 100644 src/main/java/de/thm/arsnova/events/DeleteSessionEvent.java
 create mode 100644 src/main/java/de/thm/arsnova/events/NewSessionEvent.java

diff --git a/src/main/java/de/thm/arsnova/dao/CacheBuster.java b/src/main/java/de/thm/arsnova/dao/CacheBuster.java
index ffa8d0cb..444bf225 100644
--- a/src/main/java/de/thm/arsnova/dao/CacheBuster.java
+++ b/src/main/java/de/thm/arsnova/dao/CacheBuster.java
@@ -28,6 +28,7 @@ import de.thm.arsnova.events.DeleteAnswerEvent;
 import de.thm.arsnova.events.DeleteFeedbackForSessionsEvent;
 import de.thm.arsnova.events.DeleteInterposedQuestionEvent;
 import de.thm.arsnova.events.DeleteQuestionEvent;
+import de.thm.arsnova.events.DeleteSessionEvent;
 import de.thm.arsnova.events.LockQuestionEvent;
 import de.thm.arsnova.events.LockQuestionsEvent;
 import de.thm.arsnova.events.NewAnswerEvent;
@@ -35,6 +36,7 @@ import de.thm.arsnova.events.NewFeedbackEvent;
 import de.thm.arsnova.events.NewInterposedQuestionEvent;
 import de.thm.arsnova.events.NewQuestionEvent;
 import de.thm.arsnova.events.NewQuestionsEvent;
+import de.thm.arsnova.events.NewSessionEvent;
 import de.thm.arsnova.events.NovaEventVisitor;
 import de.thm.arsnova.events.PiRoundDelayedStartEvent;
 import de.thm.arsnova.events.PiRoundEndEvent;
@@ -104,4 +106,12 @@ public class CacheBuster implements ICacheBuster, NovaEventVisitor {
 
 	@Override
 	public void visit(PiRoundEndEvent piRoundEndEvent) {}
+
+	@CacheEvict("statistics")
+	@Override
+	public void visit(NewSessionEvent newSessionEvent) {}
+
+	@CacheEvict("statistics")
+	@Override
+	public void visit(DeleteSessionEvent deleteSessionEvent) {}
 }
diff --git a/src/main/java/de/thm/arsnova/domain/LearningProgressFactory.java b/src/main/java/de/thm/arsnova/domain/LearningProgressFactory.java
index 7306321e..abcb42c3 100644
--- a/src/main/java/de/thm/arsnova/domain/LearningProgressFactory.java
+++ b/src/main/java/de/thm/arsnova/domain/LearningProgressFactory.java
@@ -32,6 +32,7 @@ import de.thm.arsnova.events.DeleteAnswerEvent;
 import de.thm.arsnova.events.DeleteFeedbackForSessionsEvent;
 import de.thm.arsnova.events.DeleteInterposedQuestionEvent;
 import de.thm.arsnova.events.DeleteQuestionEvent;
+import de.thm.arsnova.events.DeleteSessionEvent;
 import de.thm.arsnova.events.LockQuestionEvent;
 import de.thm.arsnova.events.LockQuestionsEvent;
 import de.thm.arsnova.events.NewAnswerEvent;
@@ -39,6 +40,7 @@ import de.thm.arsnova.events.NewFeedbackEvent;
 import de.thm.arsnova.events.NewInterposedQuestionEvent;
 import de.thm.arsnova.events.NewQuestionEvent;
 import de.thm.arsnova.events.NewQuestionsEvent;
+import de.thm.arsnova.events.NewSessionEvent;
 import de.thm.arsnova.events.NovaEventVisitor;
 import de.thm.arsnova.events.PiRoundDelayedStartEvent;
 import de.thm.arsnova.events.PiRoundEndEvent;
@@ -148,9 +150,14 @@ public class LearningProgressFactory implements NovaEventVisitor, ILearningProgr
 	@Override
 	public void visit(PiRoundEndEvent piRoundEndEvent) {}
 
+	@Override
+	public void visit(NewSessionEvent event) {}
+
+	@Override
+	public void visit(DeleteSessionEvent event) {}
+
 	@Override
 	public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
 		this.publisher = publisher;
 	}
-
 }
diff --git a/src/main/java/de/thm/arsnova/events/DeleteSessionEvent.java b/src/main/java/de/thm/arsnova/events/DeleteSessionEvent.java
new file mode 100644
index 00000000..a4eee9df
--- /dev/null
+++ b/src/main/java/de/thm/arsnova/events/DeleteSessionEvent.java
@@ -0,0 +1,35 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2015 The ARSnova Team
+ *
+ * ARSnova Backend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova Backend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.events;
+
+import de.thm.arsnova.entities.Session;
+
+public class DeleteSessionEvent extends SessionEvent {
+
+	private static final long serialVersionUID = 1L;
+
+	public DeleteSessionEvent(Object source, Session session) {
+		super(source, session);
+	}
+
+	@Override
+	public void accept(NovaEventVisitor visitor) {
+		visitor.visit(this);
+	}
+
+}
diff --git a/src/main/java/de/thm/arsnova/events/NewSessionEvent.java b/src/main/java/de/thm/arsnova/events/NewSessionEvent.java
new file mode 100644
index 00000000..1565443b
--- /dev/null
+++ b/src/main/java/de/thm/arsnova/events/NewSessionEvent.java
@@ -0,0 +1,35 @@
+/*
+ * This file is part of ARSnova Backend.
+ * Copyright (C) 2012-2015 The ARSnova Team
+ *
+ * ARSnova Backend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova Backend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.events;
+
+import de.thm.arsnova.entities.Session;
+
+public class NewSessionEvent extends SessionEvent {
+
+	private static final long serialVersionUID = 1L;
+
+	public NewSessionEvent(Object source, Session session) {
+		super(source, session);
+	}
+
+	@Override
+	public void accept(NovaEventVisitor visitor) {
+		visitor.visit(this);
+	}
+
+}
diff --git a/src/main/java/de/thm/arsnova/events/NovaEventVisitor.java b/src/main/java/de/thm/arsnova/events/NovaEventVisitor.java
index c07cc980..26aa8536 100644
--- a/src/main/java/de/thm/arsnova/events/NovaEventVisitor.java
+++ b/src/main/java/de/thm/arsnova/events/NovaEventVisitor.java
@@ -55,4 +55,7 @@ public interface NovaEventVisitor {
 
 	void visit(PiRoundEndEvent piRoundEndEvent);
 
+	void visit(NewSessionEvent newSessionEvent);
+
+	void visit(DeleteSessionEvent deleteSessionEvent);
 }
diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java
index 0fb098d1..caa20313 100644
--- a/src/main/java/de/thm/arsnova/services/SessionService.java
+++ b/src/main/java/de/thm/arsnova/services/SessionService.java
@@ -44,6 +44,8 @@ import de.thm.arsnova.entities.SessionInfo;
 import de.thm.arsnova.entities.User;
 import de.thm.arsnova.entities.transport.ImportExportSession;
 import de.thm.arsnova.entities.transport.LearningProgressValues;
+import de.thm.arsnova.events.DeleteSessionEvent;
+import de.thm.arsnova.events.NewSessionEvent;
 import de.thm.arsnova.events.StatusSessionEvent;
 import de.thm.arsnova.exceptions.BadRequestException;
 import de.thm.arsnova.exceptions.ForbiddenException;
@@ -254,7 +256,9 @@ public class SessionService implements ISessionService, ApplicationEventPublishe
 		sf.setPi(true);
 		session.setFeatures(sf);
 
-		return databaseDao.saveSession(userService.getCurrentUser(), session);
+		final Session result = databaseDao.saveSession(userService.getCurrentUser(), session);
+		this.publisher.publishEvent(new NewSessionEvent(this, result));
+		return result;
 	}
 
 	@Override
@@ -325,6 +329,8 @@ public class SessionService implements ISessionService, ApplicationEventPublishe
 		final Session session = databaseDao.getSessionFromKeyword(sessionkey);
 		databaseDao.deleteAllQuestionsWithAnswers(session);
 		databaseDao.deleteSession(session);
+
+		this.publisher.publishEvent(new DeleteSessionEvent(this, session));
 	}
 
 	@Override
diff --git a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java
index 4d0439ed..a5623b7d 100644
--- a/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java
+++ b/src/main/java/de/thm/arsnova/socket/ARSnovaSocketIOServer.java
@@ -58,6 +58,7 @@ import de.thm.arsnova.events.DeleteAnswerEvent;
 import de.thm.arsnova.events.DeleteFeedbackForSessionsEvent;
 import de.thm.arsnova.events.DeleteInterposedQuestionEvent;
 import de.thm.arsnova.events.DeleteQuestionEvent;
+import de.thm.arsnova.events.DeleteSessionEvent;
 import de.thm.arsnova.events.LockQuestionEvent;
 import de.thm.arsnova.events.LockQuestionsEvent;
 import de.thm.arsnova.events.NewAnswerEvent;
@@ -65,13 +66,14 @@ import de.thm.arsnova.events.NewFeedbackEvent;
 import de.thm.arsnova.events.NewInterposedQuestionEvent;
 import de.thm.arsnova.events.NewQuestionEvent;
 import de.thm.arsnova.events.NewQuestionsEvent;
+import de.thm.arsnova.events.NewSessionEvent;
 import de.thm.arsnova.events.NovaEventVisitor;
 import de.thm.arsnova.events.PiRoundDelayedStartEvent;
 import de.thm.arsnova.events.PiRoundEndEvent;
 import de.thm.arsnova.events.StatusSessionEvent;
-import de.thm.arsnova.exceptions.UnauthorizedException;
 import de.thm.arsnova.exceptions.NoContentException;
 import de.thm.arsnova.exceptions.NotFoundException;
+import de.thm.arsnova.exceptions.UnauthorizedException;
 import de.thm.arsnova.services.IFeedbackService;
 import de.thm.arsnova.services.IQuestionService;
 import de.thm.arsnova.services.ISessionService;
@@ -576,4 +578,10 @@ public class ARSnovaSocketIOServer implements ARSnovaSocket, NovaEventVisitor {
 	public void visit(ChangeLearningProgress event) {
 		broadcastInSession(event.getSession().getKeyword(), "learningProgressChange", null);
 	}
+
+	@Override
+	public void visit(NewSessionEvent event) {}
+
+	@Override
+	public void visit(DeleteSessionEvent event) {}
 }
-- 
GitLab