From 205ca260d9542b8c81eeafde83ef4ae66ace372d Mon Sep 17 00:00:00 2001
From: Christoph Thelen <christoph.thelen@mni.thm.de>
Date: Mon, 4 Feb 2013 16:29:35 +0100
Subject: [PATCH] Server side implementation for #4858

---
 .../arsnova/controller/SessionController.java | 11 +++++++
 .../java/de/thm/arsnova/dao/CouchDBDao.java   | 29 +++++++++++++++++++
 .../java/de/thm/arsnova/dao/IDatabaseDao.java |  2 ++
 .../thm/arsnova/services/ISessionService.java |  3 ++
 .../thm/arsnova/services/SessionService.java  |  5 ++++
 .../de/thm/arsnova/dao/StubDatabaseDao.java   |  6 ++++
 6 files changed, 56 insertions(+)

diff --git a/src/main/java/de/thm/arsnova/controller/SessionController.java b/src/main/java/de/thm/arsnova/controller/SessionController.java
index 00adecaca..95603e1c6 100644
--- a/src/main/java/de/thm/arsnova/controller/SessionController.java
+++ b/src/main/java/de/thm/arsnova/controller/SessionController.java
@@ -142,4 +142,15 @@ public class SessionController extends AbstractController {
 		}
 		return sessions;
 	}
+
+	@RequestMapping(value = "/session/visitedsessions", method = RequestMethod.GET)
+	@ResponseBody
+	public final List<Session> getMyVisitedSession(final HttpServletResponse response) {
+		List<Session> sessions = sessionService.getMyVisitedSessions(userService.getCurrentUser());
+		if (sessions == null || sessions.isEmpty()) {
+			response.setStatus(HttpStatus.NO_CONTENT.value());
+			return null;
+		}
+		return sessions;
+	}
 }
diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
index 2fcaf7846..c33c890a0 100644
--- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
+++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
@@ -1237,4 +1237,33 @@ public class CouchDBDao implements IDatabaseDao {
 		document.put("read", question.isRead());
 		this.getDatabase().saveDocument(document);
 	}
+
+	@Override
+	public List<Session> getMyVisitedSessions(User user) {
+		try {
+			View view = new View("logged_in/visited_sessions_by_user");
+			view.setKey(URLEncoder.encode("\"" + user.getUsername() + "\"", "UTF-8"));
+			ViewResults sessions = this.getDatabase().view(view);
+			List<Session> allSessions = new ArrayList<Session>();
+			for (Document d : sessions.getResults()) {
+				@SuppressWarnings("unchecked")
+				Collection<Session> visitedSessions =  JSONArray.toCollection(
+						d.getJSONObject().getJSONArray("value"),
+						Session.class
+				);
+				allSessions.addAll(visitedSessions);
+			}
+			// Do these sessions still exist?
+			List<Session> result = new ArrayList<Session>();
+			for (Session s : allSessions) {
+				Session session = this.getSessionFromKeyword(s.getKeyword());
+				if (session != null) {
+					result.add(session);
+				}
+			}
+			return result;
+		} catch (UnsupportedEncodingException e) {
+			return null;
+		}
+	}
 }
diff --git a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java
index b8837942e..b8a16641a 100644
--- a/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java
+++ b/src/main/java/de/thm/arsnova/dao/IDatabaseDao.java
@@ -111,4 +111,6 @@ public interface IDatabaseDao {
 	InterposedQuestion getInterposedQuestion(String questionId) throws IOException;
 
 	void markInterposedQuestionAsRead(InterposedQuestion question) throws IOException;
+
+	List<Session> getMyVisitedSessions(User user);
 }
diff --git a/src/main/java/de/thm/arsnova/services/ISessionService.java b/src/main/java/de/thm/arsnova/services/ISessionService.java
index 865fc2fcb..7175464fa 100644
--- a/src/main/java/de/thm/arsnova/services/ISessionService.java
+++ b/src/main/java/de/thm/arsnova/services/ISessionService.java
@@ -36,7 +36,10 @@ public interface ISessionService {
 
 	List<Session> getMySessions(String username);
 
+	List<Session> getMyVisitedSessions(User currentUser);
+
 	LoggedIn registerAsOnlineUser(User user, String sessionkey);
 
 	int countActiveUsers(String sessionkey);
+
 }
diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java
index c99377734..23f6f881b 100644
--- a/src/main/java/de/thm/arsnova/services/SessionService.java
+++ b/src/main/java/de/thm/arsnova/services/SessionService.java
@@ -57,6 +57,11 @@ public class SessionService implements ISessionService {
 		return databaseDao.getMySessions(username);
 	}
 
+	@Override
+	public final List<Session> getMyVisitedSessions(final User user) {
+		return databaseDao.getMyVisitedSessions(user);
+	}
+
 	@Override
 	@Authenticated
 	public final Session saveSession(final Session session) {
diff --git a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java
index 79f290201..cc9673804 100644
--- a/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java
+++ b/src/test/java/de/thm/arsnova/dao/StubDatabaseDao.java
@@ -363,4 +363,10 @@ public class StubDatabaseDao implements IDatabaseDao {
 	public void markInterposedQuestionAsRead(InterposedQuestion question) throws IOException {
 		this.interposedQuestion.setRead(true);
 	}
+
+	@Override
+	public List<Session> getMyVisitedSessions(User user) {
+		// TODO Auto-generated method stub
+		return null;
+	}
 }
-- 
GitLab