summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2013-09-10 00:06:54 -0700
committerJesse Morgan <jesse@jesterpm.net>2013-09-10 00:06:54 -0700
commit5306005833083589d0f93b0966b458da969e5f38 (patch)
tree9779a58d78bdcf27311e2593174afb83041a21fa
parent90c95d9bf125dabb5bd34954394127cf2c1bb1b1 (diff)
Adding redirect page /account.
/account redirects to either the assessment or the training page, as appropriate. This change adds the /accounts/UID backend resource, which simply stores an arbitrary json document for now.
-rw-r--r--devfiles/scripts/cassandra-bootstrap.cql5
-rw-r--r--src/com/p4square/grow/backend/GrowBackend.java7
-rw-r--r--src/com/p4square/grow/backend/resources/AccountResource.java71
-rw-r--r--src/com/p4square/grow/frontend/AccountRedirectResource.java110
-rw-r--r--src/com/p4square/grow/frontend/GrowFrontend.java1
-rw-r--r--src/com/p4square/grow/frontend/SurveyPageResource.java26
6 files changed, 219 insertions, 1 deletions
diff --git a/devfiles/scripts/cassandra-bootstrap.cql b/devfiles/scripts/cassandra-bootstrap.cql
index c7d6de8..db2e949 100644
--- a/devfiles/scripts/cassandra-bootstrap.cql
+++ b/devfiles/scripts/cassandra-bootstrap.cql
@@ -11,6 +11,11 @@ create column family strings
and comparator = 'UTF8Type'
and default_validation_class = 'UTF8Type';
+create column family accounts
+ with key_validation_class = 'UTF8Type'
+ and comparator = 'UTF8Type'
+ and default_validation_class = 'UTF8Type';
+
create column family assessments
with key_validation_class = 'UTF8Type'
and comparator = 'UTF8Type'
diff --git a/src/com/p4square/grow/backend/GrowBackend.java b/src/com/p4square/grow/backend/GrowBackend.java
index d072dfb..09c1d84 100644
--- a/src/com/p4square/grow/backend/GrowBackend.java
+++ b/src/com/p4square/grow/backend/GrowBackend.java
@@ -15,10 +15,12 @@ import org.restlet.routing.Router;
import com.p4square.grow.config.Config;
import com.p4square.grow.backend.db.CassandraDatabase;
+
+import com.p4square.grow.backend.resources.AccountResource;
import com.p4square.grow.backend.resources.SurveyResource;
import com.p4square.grow.backend.resources.SurveyResultsResource;
-import com.p4square.grow.backend.resources.TrainingResource;
import com.p4square.grow.backend.resources.TrainingRecordResource;
+import com.p4square.grow.backend.resources.TrainingResource;
/**
* Main class for the backend application.
@@ -40,6 +42,9 @@ public class GrowBackend extends Application {
public Restlet createInboundRoot() {
Router router = new Router(getContext());
+ // Account API
+ router.attach("/accounts/{userId}", AccountResource.class);
+
// Survey API
router.attach("/assessment/question/{questionId}", SurveyResource.class);
diff --git a/src/com/p4square/grow/backend/resources/AccountResource.java b/src/com/p4square/grow/backend/resources/AccountResource.java
new file mode 100644
index 0000000..f3404c0
--- /dev/null
+++ b/src/com/p4square/grow/backend/resources/AccountResource.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2013 Jesse Morgan
+ */
+
+package com.p4square.grow.backend.resources;
+
+import java.io.IOException;
+
+import org.restlet.data.Status;
+import org.restlet.resource.ServerResource;
+import org.restlet.representation.Representation;
+import org.restlet.representation.StringRepresentation;
+
+import org.apache.log4j.Logger;
+
+import com.p4square.grow.backend.GrowBackend;
+import com.p4square.grow.backend.db.CassandraDatabase;
+
+/**
+ * Stores a document about a user.
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public class AccountResource extends ServerResource {
+ private static final Logger LOG = Logger.getLogger(AccountResource.class);
+
+ private CassandraDatabase mDb;
+
+ private String mUserId;
+
+ @Override
+ public void doInit() {
+ super.doInit();
+
+ final GrowBackend backend = (GrowBackend) getApplication();
+ mDb = backend.getDatabase();
+
+ mUserId = getAttribute("userId");
+ }
+
+ /**
+ * Handle GET Requests.
+ */
+ @Override
+ protected Representation get() {
+ String result = mDb.getKey("accounts", mUserId);
+
+ if (result == null) {
+ setStatus(Status.CLIENT_ERROR_NOT_FOUND);
+ return null;
+ }
+
+ return new StringRepresentation(result);
+ }
+
+ /**
+ * Handle PUT requests
+ */
+ @Override
+ protected Representation put(Representation entity) {
+ try {
+ mDb.putKey("accounts", mUserId, entity.getText());
+ setStatus(Status.SUCCESS_NO_CONTENT);
+
+ } catch (IOException e) {
+ setStatus(Status.SERVER_ERROR_INTERNAL);
+ }
+
+ return null;
+ }
+}
diff --git a/src/com/p4square/grow/frontend/AccountRedirectResource.java b/src/com/p4square/grow/frontend/AccountRedirectResource.java
new file mode 100644
index 0000000..1c034ac
--- /dev/null
+++ b/src/com/p4square/grow/frontend/AccountRedirectResource.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2013 Jesse Morgan
+ */
+
+package com.p4square.grow.frontend;
+
+import java.util.Map;
+
+import org.restlet.data.Status;
+import org.restlet.representation.Representation;
+import org.restlet.representation.StringRepresentation;
+import org.restlet.resource.ServerResource;
+
+import org.apache.log4j.Logger;
+
+import net.jesterpm.fmfacade.json.JsonRequestClient;
+import net.jesterpm.fmfacade.json.JsonResponse;
+import net.jesterpm.fmfacade.json.ClientException;
+
+import net.jesterpm.fmfacade.FreeMarkerPageResource;
+
+import com.p4square.grow.config.Config;
+
+/**
+ * This resource simply redirects the user to either the assessment
+ * or the training page.
+ *
+ * @author Jesse Morgan <jesse@jesterpm.net>
+ */
+public class AccountRedirectResource extends ServerResource {
+ private static final Logger LOG = Logger.getLogger(AccountRedirectResource.class);
+
+ private Config mConfig;
+ private JsonRequestClient mJsonClient;
+
+ // Fields pertaining to this request.
+ private String mUserId;
+
+ @Override
+ public void doInit() {
+ super.doInit();
+
+ GrowFrontend growFrontend = (GrowFrontend) getApplication();
+ mConfig = growFrontend.getConfig();
+
+ mJsonClient = new JsonRequestClient(getContext().getClientDispatcher());
+
+ mUserId = getRequest().getClientInfo().getUser().getIdentifier();
+ }
+
+ /**
+ * Redirect to the correct landing.
+ */
+ @Override
+ protected Representation get() {
+ try {
+ Map<?, ?> account = null;
+ try {
+ JsonResponse response = backendGet("/accounts/" + mUserId);
+ if (response.getStatus().isSuccess()) {
+ account = response.getMap();
+ }
+ } catch (ClientException e) {
+
+ }
+
+ String landing = null;
+
+ if (account != null) {
+ landing = (String) account.get("landing");
+ }
+
+ if (landing == null) {
+ landing = "assessment";
+ }
+
+ String nextPage = mConfig.getString("dynamicRoot", "");
+ nextPage += "/account/" + landing;
+ getResponse().redirectSeeOther(nextPage);
+ return new StringRepresentation("Redirecting to " + nextPage);
+
+ } catch (Exception e) {
+ LOG.fatal("Could not render page: " + e.getMessage(), e);
+ setStatus(Status.SERVER_ERROR_INTERNAL);
+ return ErrorPage.RENDER_ERROR;
+ }
+ }
+
+ /**
+ * @return The backend endpoint URI
+ */
+ private String getBackendEndpoint() {
+ return mConfig.getString("backendUri", "riap://component/backend");
+ }
+
+ /**
+ * Helper method to send a GET to the backend.
+ */
+ private JsonResponse backendGet(final String uri) {
+ LOG.debug("Sending backend GET " + uri);
+
+ final JsonResponse response = mJsonClient.get(getBackendEndpoint() + uri);
+ final Status status = response.getStatus();
+ if (!status.isSuccess() && !Status.CLIENT_ERROR_NOT_FOUND.equals(status)) {
+ LOG.warn("Error making backend request for '" + uri + "'. status = " + response.getStatus().toString());
+ }
+
+ return response;
+ }
+}
diff --git a/src/com/p4square/grow/frontend/GrowFrontend.java b/src/com/p4square/grow/frontend/GrowFrontend.java
index dd953d5..5252078 100644
--- a/src/com/p4square/grow/frontend/GrowFrontend.java
+++ b/src/com/p4square/grow/frontend/GrowFrontend.java
@@ -109,6 +109,7 @@ public class GrowFrontend extends FMFacade {
final Router accountRouter = new Router(getContext());
accountRouter.attach("/authenticate", AuthenticatedResource.class);
+ accountRouter.attach("", AccountRedirectResource.class);
accountRouter.attach("/assessment/question/{questionId}", SurveyPageResource.class);
accountRouter.attach("/assessment/results", AssessmentResultsPage.class);
accountRouter.attach("/assessment", SurveyPageResource.class);
diff --git a/src/com/p4square/grow/frontend/SurveyPageResource.java b/src/com/p4square/grow/frontend/SurveyPageResource.java
index 1389f11..59be0fa 100644
--- a/src/com/p4square/grow/frontend/SurveyPageResource.java
+++ b/src/com/p4square/grow/frontend/SurveyPageResource.java
@@ -195,6 +195,24 @@ public class SurveyPageResource extends FreeMarkerPageResource {
}
}
+ private Map<?, ?> getAccount(String id) {
+ try {
+ Map<?, ?> account = null;
+
+ JsonResponse response = backendGet("/accounts/" + id);
+ if (!response.getStatus().isSuccess()) {
+ return null;
+ }
+ account = response.getMap();
+
+ return account;
+
+ } catch (ClientException e) {
+ LOG.warn("Error fetching account.", e);
+ return null;
+ }
+ }
+
private Map<?, ?> getQuestion(String id) {
try {
Map<?, ?> questionData = null;
@@ -217,6 +235,14 @@ public class SurveyPageResource extends FreeMarkerPageResource {
String nextQuestionId = (String) questionData.get("nextQuestion");
if (nextQuestionId == null) {
+ // Just finished the last question. Update the user's account
+ Map account = getAccount(mUserId);
+ if (account == null) {
+ account = new HashMap();
+ }
+ account.put("landing", "training");
+ backendPut("/accounts/" + mUserId, account);
+
String nextPage = mConfig.getString("dynamicRoot", "");
nextPage += "/account/assessment/results";
getResponse().redirectSeeOther(nextPage);