summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/provider
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2013-10-20 23:14:51 -0700
committerJesse Morgan <jesse@jesterpm.net>2013-10-20 23:14:51 -0700
commite77c9b418fdfb935ff8e99f10f607a4bbd7e1c8c (patch)
tree9005f8c0e3b18f853fb8093b01f6aad64949bc13 /src/com/p4square/grow/provider
parent5037f4797461649994068d97a8433b6cd793c523 (diff)
First stage of a major refactoring.
Question and Answer can now be serialized and deserialized to/from JSON. As such, I no longer have to pass awkward maps around. As part of this change I have introduced a Provider interface to abstract out loading and persisting these beans. The scoring logic has been completed factored out of SurveyResultsResource and into the various ScoringEngines. Tests have been added for Question, Answer, and the ScoringEngines. A bug has been fixed in computing the value for slider questions. The label identifiers in the circle questions have changed from all lower case to camel case. That is, topleft is now topLeft. Several issues have been corrected in the circle answers where the point values did not match the labels. Testing and code coverage support and reports have been added.
Diffstat (limited to 'src/com/p4square/grow/provider')
-rw-r--r--src/com/p4square/grow/provider/JsonEncodedProvider.java60
-rw-r--r--src/com/p4square/grow/provider/Provider.java31
-rw-r--r--src/com/p4square/grow/provider/QuestionProvider.java41
3 files changed, 132 insertions, 0 deletions
diff --git a/src/com/p4square/grow/provider/JsonEncodedProvider.java b/src/com/p4square/grow/provider/JsonEncodedProvider.java
new file mode 100644
index 0000000..605b18c
--- /dev/null
+++ b/src/com/p4square/grow/provider/JsonEncodedProvider.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2013 Jesse Morgan
+ */
+
+package com.p4square.grow.provider;
+
+import java.io.IOException;
+
+import org.codehaus.jackson.map.DeserializationConfig;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.SerializationConfig;
+
+/**
+ * Provider provides a simple interface for loading and persisting
+ * objects.
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public abstract class JsonEncodedProvider<K, V> implements Provider<K, V> {
+ public static final ObjectMapper MAPPER = new ObjectMapper();
+ static {
+ MAPPER.configure(SerializationConfig.Feature.WRITE_ENUMS_USING_TO_STRING, true);
+ MAPPER.configure(DeserializationConfig.Feature.READ_ENUMS_USING_TO_STRING, true);
+ MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ }
+
+ private final Class<V> mClazz;
+
+ public JsonEncodedProvider(Class<V> clazz) {
+ mClazz = clazz;
+ }
+
+ /**
+ * Encode the object as JSON.
+ *
+ * @param obj The object to encode.
+ * @return The JSON encoding of obj.
+ * @throws IOException if the object cannot be encoded.
+ */
+ protected String encode(V obj) throws IOException {
+ return MAPPER.writeValueAsString(obj);
+ }
+
+ /**
+ * Decode the JSON string as an object.
+ *
+ * @param blob The JSON data to decode.
+ * @return The decoded object or null if blob is null.
+ * @throws IOException If an object cannot be decoded.
+ */
+ protected V decode(String blob) throws IOException {
+ if (blob == null) {
+ return null;
+ }
+
+ V obj = MAPPER.readValue(blob, mClazz);
+ return obj;
+ }
+}
+
diff --git a/src/com/p4square/grow/provider/Provider.java b/src/com/p4square/grow/provider/Provider.java
new file mode 100644
index 0000000..ca6af25
--- /dev/null
+++ b/src/com/p4square/grow/provider/Provider.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright 2013 Jesse Morgan
+ */
+
+package com.p4square.grow.provider;
+
+import java.io.IOException;
+
+/**
+ * Provider provides a simple interface for loading and persisting
+ * objects.
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public interface Provider<K, V> {
+ /**
+ * Retrieve the object with the given key.
+ *
+ * @param key The key for the object.
+ * @return The object or null if not found.
+ */
+ V get(K key) throws IOException;
+
+ /**
+ * Persist the object with the given key.
+ *
+ * @param key The key for the object.
+ * @param obj The object to persist.
+ */
+ void put(K key, V obj) throws IOException;
+}
diff --git a/src/com/p4square/grow/provider/QuestionProvider.java b/src/com/p4square/grow/provider/QuestionProvider.java
new file mode 100644
index 0000000..b569dc8
--- /dev/null
+++ b/src/com/p4square/grow/provider/QuestionProvider.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2013 Jesse Morgan
+ */
+
+package com.p4square.grow.provider;
+
+import java.io.IOException;
+
+import com.p4square.grow.model.Question;
+
+/**
+ * QuestionProvider wraps an existing Provider to get and put Questions.
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public abstract class QuestionProvider<K> implements Provider<String, Question> {
+
+ private Provider<K, Question> mProvider;
+
+ public QuestionProvider(Provider<K, Question> provider) {
+ mProvider = provider;
+ }
+
+ @Override
+ public Question get(String key) throws IOException {
+ return mProvider.get(makeKey(key));
+ }
+
+ @Override
+ public void put(String key, Question obj) throws IOException {
+ mProvider.put(makeKey(key), obj);
+ }
+
+ /**
+ * Make a Key for questionId.
+ *
+ * @param questionId The question id.
+ * @return a key for questionId.
+ */
+ protected abstract K makeKey(String questionId);
+}