From f565df90055244e1942f150f34d01d1c508220c0 Mon Sep 17 00:00:00 2001
From: Daniel Gerhardt <code@dgerhardt.net>
Date: Thu, 13 Apr 2017 17:27:42 +0200
Subject: [PATCH] Use new MotD views

The following replacements have been made:
* New design document name
    - New view name: (conditions) keys => values
        * Full old view name: (conditions) keys => values

* motd/
    - by_motdkey: motdkey => _rev
        * motd/by_keyword: motdkey => doc
    - doc_by_audience_for_global: (audience != 'session') audience => doc
        * motd/admin: (audience != 'session') 1 => doc
        * motd/for_tutors: (audience == 'tutors' || audience == 'loggedIn') 1 => doc
            - 2 backend requests
        * motd/for_all: (audience == 'all') 1 => doc
        * motd/for_loggedin: (audience == 'loggedIn') 1 => doc
        * motd/for_students: (audience == 'students' || audience == 'loggedIn') 1 => doc
            - 2 backend requests
    - doc_by_sessionkey: (audience == 'session') sessionkey => doc
        * motd/by_sessionkey: (audience == 'session') sessionkey => doc
* motdlist/
    - doc_by_username: username => doc
        * motd/list_by_username: username => doc

The following views have been removed:
* motd/all: 1 => doc (never used)
---
 .../java/de/thm/arsnova/dao/CouchDBDao.java   | 52 ++++++++++++-------
 1 file changed, 34 insertions(+), 18 deletions(-)

diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
index 53ac57c2c..b8ccdd633 100644
--- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
+++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
@@ -2646,42 +2646,57 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware
 
 	@Override
 	public List<Motd> getAdminMotds() {
-		NovaView view = new NovaView("motd/admin");
+		final NovaView view = new NovaView("motd/doc_by_audience_for_global");
 		return getMotds(view);
 	}
 
 	@Override
 	@Cacheable(cacheNames = "motds", key = "'all'")
 	public List<Motd> getMotdsForAll() {
-		NovaView view = new NovaView("motd/for_all");
+		final NovaView view = new NovaView("motd/doc_by_audience_for_global");
 		return getMotds(view);
 	}
 
 	@Override
 	@Cacheable(cacheNames = "motds", key = "'loggedIn'")
 	public List<Motd> getMotdsForLoggedIn() {
-		NovaView view = new NovaView("motd/for_loggedin");
+		final NovaView view = new NovaView("motd/doc_by_audience_for_global");
+		view.setKey("loggedIn");
 		return getMotds(view);
 	}
 
 	@Override
 	@Cacheable(cacheNames = "motds", key = "'tutors'")
 	public List<Motd> getMotdsForTutors() {
-		NovaView view = new NovaView("motd/for_tutors");
-		return getMotds(view);
+		final NovaView view1 = new NovaView("motd/doc_by_audience_for_global");
+		final NovaView view2 = new NovaView("motd/doc_by_audience_for_global");
+		view1.setKey("loggedIn");
+		view2.setKey("tutors");
+		final List<Motd> union = new ArrayList<>();
+		union.addAll(getMotds(view1));
+		union.addAll(getMotds(view2));
+
+		return union;
 	}
 
 	@Override
 	@Cacheable(cacheNames = "motds", key = "'students'")
 	public List<Motd> getMotdsForStudents() {
-		NovaView view = new NovaView("motd/for_students");
-		return getMotds(view);
+		final NovaView view1 = new NovaView("motd/doc_by_audience_for_global");
+		final NovaView view2 = new NovaView("motd/doc_by_audience_for_global");
+		view1.setKey("loggedIn");
+		view2.setKey("students");
+		final List<Motd> union = new ArrayList<>();
+		union.addAll(getMotds(view1));
+		union.addAll(getMotds(view2));
+
+		return union;
 	}
 
 	@Override
 	@Cacheable(cacheNames = "motds", key = "('session').concat(#p0)")
 	public List<Motd> getMotdsForSession(final String sessionkey) {
-		NovaView view = new NovaView("motd/by_sessionkey");
+		final NovaView view = new NovaView("motd/doc_by_sessionkey");
 		view.setKey(sessionkey);
 		return getMotds(view);
 	}
@@ -2710,7 +2725,8 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware
 
 	@Override
 	public Motd getMotdByKey(String key) {
-		NovaView view = new NovaView("motd/by_keyword");
+		final NovaView view = new NovaView("motd/by_motdkey");
+		view.setIncludeDocs(true);
 		view.setKey(key);
 		Motd motd = new Motd();
 
@@ -2718,16 +2734,16 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware
 
 		for (final Document d : results.getResults()) {
 			motd.set_id(d.getId());
-			motd.set_rev(d.getJSONObject("value").getString("_rev"));
-			motd.setMotdkey(d.getJSONObject("value").getString("motdkey"));
-			Date start = new Date(Long.parseLong(d.getJSONObject("value").getString("startdate")));
+			motd.set_rev(d.getJSONObject("doc").getString("_rev"));
+			motd.setMotdkey(d.getJSONObject("doc").getString("motdkey"));
+			Date start = new Date(Long.parseLong(d.getJSONObject("doc").getString("startdate")));
 			motd.setStartdate(start);
-			Date end = new Date(Long.parseLong(d.getJSONObject("value").getString("enddate")));
+			Date end = new Date(Long.parseLong(d.getJSONObject("doc").getString("enddate")));
 			motd.setEnddate(end);
-			motd.setTitle(d.getJSONObject("value").getString("title"));
-			motd.setText(d.getJSONObject("value").getString("text"));
-			motd.setAudience(d.getJSONObject("value").getString("audience"));
-			motd.setSessionkey(d.getJSONObject("value").getString("sessionkey"));
+			motd.setTitle(d.getJSONObject("doc").getString("title"));
+			motd.setText(d.getJSONObject("doc").getString("text"));
+			motd.setAudience(d.getJSONObject("doc").getString("audience"));
+			motd.setSessionkey(d.getJSONObject("doc").getString("sessionkey"));
 		}
 
 		return motd;
@@ -2781,7 +2797,7 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware
 	@Override
 	@Cacheable(cacheNames = "motdlist", key = "#p0")
 	public MotdList getMotdListForUser(final String username) {
-		NovaView view = new NovaView("motd/list_by_username");
+		NovaView view = new NovaView("motdlist/doc_by_username");
 		view.setKey(username);
 
 		ViewResults results = this.getDatabase().view(view);
-- 
GitLab