From 6f0b2b9b04a315bf5732155da085f988826e7ede Mon Sep 17 00:00:00 2001 From: Christoph Thelen <christoph.thelen@mni.thm.de> Date: Wed, 21 Jan 2015 12:49:22 +0100 Subject: [PATCH] Add support for querying stale views --- .../java/de/thm/arsnova/dao/NovaView.java | 20 +++++++++++++++++++ .../java/de/thm/arsnova/dao/NovaViewTest.java | 20 +++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/thm/arsnova/dao/NovaView.java b/src/main/java/de/thm/arsnova/dao/NovaView.java index 56e16c2d8..9c46b1b4f 100644 --- a/src/main/java/de/thm/arsnova/dao/NovaView.java +++ b/src/main/java/de/thm/arsnova/dao/NovaView.java @@ -28,8 +28,14 @@ import com.fourspaces.couchdb.View; public class NovaView extends View { + public enum StaleMode { + NONE, OK, UPDATE_AFTER + } + protected String keys; + protected StaleMode stale = StaleMode.NONE; + public NovaView(final String fullname) { super(fullname); } @@ -89,6 +95,10 @@ public class NovaView extends View { this.keys = toJsonArray(keys.toArray(new String[keys.size()])); } + public void setStale(StaleMode stale) { + this.stale = stale; + } + @Override public String getQueryString() { final String tempQuery = super.getQueryString(); @@ -102,6 +112,16 @@ public class NovaView extends View { } query.append("keys=" + keys); } + if (stale != null && stale != StaleMode.NONE) { + if (query.length() > 0) { + query.append("&"); + } + if (stale == StaleMode.OK) { + query.append("stale=ok"); + } else if (stale == StaleMode.UPDATE_AFTER) { + query.append("stale=update_after"); + } + } if (query.length() == 0) { return null; diff --git a/src/test/java/de/thm/arsnova/dao/NovaViewTest.java b/src/test/java/de/thm/arsnova/dao/NovaViewTest.java index 2d8d14e6a..dfe826300 100644 --- a/src/test/java/de/thm/arsnova/dao/NovaViewTest.java +++ b/src/test/java/de/thm/arsnova/dao/NovaViewTest.java @@ -18,8 +18,7 @@ */ package de.thm.arsnova.dao; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; @@ -27,6 +26,8 @@ import java.util.Arrays; import org.junit.Test; +import de.thm.arsnova.dao.NovaView.StaleMode; + public class NovaViewTest { @Test @@ -133,6 +134,21 @@ public class NovaViewTest { assertEncodedEquals("keys", "[]", v5.getQueryString()); } + @Test + public void shouldSupportStaleViews() { + final NovaView v1 = new NovaView(null); + final NovaView v2 = new NovaView(null); + final NovaView v3 = new NovaView(null); + final NovaView v4 = new NovaView(null); + v1.setStale(StaleMode.NONE); + v2.setStale(StaleMode.OK); + v3.setStale(StaleMode.UPDATE_AFTER); + assertNull(v1.getQueryString()); + assertEncodedEquals("stale", "ok", v2.getQueryString()); + assertEncodedEquals("stale", "update_after", v3.getQueryString()); + assertNull(v4.getQueryString()); + } + private void assertEncodedEquals(final String key, final String expected, final String actual) { try { assertEquals(key + "=" + URLEncoder.encode(expected, "UTF-8"), actual); -- GitLab