From 598c241ddb15d89a59629b0e3b49803a405e6249 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Tue, 3 Sep 2013 07:11:35 -0700 Subject: Fixing the assessment to take the user to the question after the last answered question --- .../grow/backend/resources/SurveyResource.java | 39 +++++++++++++++++++--- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'src/com/p4square/grow/backend/resources/SurveyResource.java') diff --git a/src/com/p4square/grow/backend/resources/SurveyResource.java b/src/com/p4square/grow/backend/resources/SurveyResource.java index d22d763..76cbe30 100644 --- a/src/com/p4square/grow/backend/resources/SurveyResource.java +++ b/src/com/p4square/grow/backend/resources/SurveyResource.java @@ -4,9 +4,13 @@ package com.p4square.grow.backend.resources; +import java.io.IOException; + import java.util.Map; import java.util.HashMap; +import org.codehaus.jackson.map.ObjectMapper; + import org.restlet.data.MediaType; import org.restlet.data.Status; import org.restlet.resource.ServerResource; @@ -24,7 +28,9 @@ import com.p4square.grow.backend.db.CassandraDatabase; * @author Jesse Morgan */ public class SurveyResource extends ServerResource { - private final static Logger cLog = Logger.getLogger(SurveyResource.class); + private static final Logger LOG = Logger.getLogger(SurveyResource.class); + + private static final ObjectMapper MAPPER = new ObjectMapper(); private CassandraDatabase mDb; @@ -45,16 +51,24 @@ public class SurveyResource extends ServerResource { */ @Override protected Representation get() { - String result = ""; + String result = "{}"; if (mQuestionId == null) { // TODO: List all question ids } else if (mQuestionId.equals("first")) { - // TODO: Get the first question id from db? - result = "1"; + // Get the first question id from db? + Map questionSummary = getQuestionsSummary(); + mQuestionId = (String) questionSummary.get("first"); + + } else if (mQuestionId.equals("count")) { + // Get the first question id from db? + Map questionSummary = getQuestionsSummary(); - } else { + return new StringRepresentation(String.valueOf((Integer) questionSummary.get("count"))); + } + + if (mQuestionId != null) { // Get a question by id result = mDb.getKey("strings", "/questions/" + mQuestionId); @@ -67,4 +81,19 @@ public class SurveyResource extends ServerResource { return new StringRepresentation(result); } + + private Map getQuestionsSummary() { + try { + String json = mDb.getKey("strings", "/questions"); + + if (json != null) { + return MAPPER.readValue(json, Map.class); + } + + } catch (IOException e) { + LOG.info("Exception reading questions summary.", e); + } + + return null; + } } -- cgit v1.2.3