summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/frontend
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/p4square/grow/frontend')
-rw-r--r--src/com/p4square/grow/frontend/AccountRedirectResource.java64
-rw-r--r--src/com/p4square/grow/frontend/FeedData.java15
-rw-r--r--src/com/p4square/grow/frontend/SurveyPageResource.java47
-rw-r--r--src/com/p4square/grow/frontend/TrainingPageResource.java17
4 files changed, 56 insertions, 87 deletions
diff --git a/src/com/p4square/grow/frontend/AccountRedirectResource.java b/src/com/p4square/grow/frontend/AccountRedirectResource.java
index 25467cd..b363c89 100644
--- a/src/com/p4square/grow/frontend/AccountRedirectResource.java
+++ b/src/com/p4square/grow/frontend/AccountRedirectResource.java
@@ -14,13 +14,13 @@ import org.restlet.resource.ServerResource;
import org.apache.log4j.Logger;
-import com.p4square.fmfacade.json.JsonRequestClient;
-import com.p4square.fmfacade.json.JsonResponse;
-import com.p4square.fmfacade.json.ClientException;
-
import com.p4square.fmfacade.FreeMarkerPageResource;
import com.p4square.grow.config.Config;
+import com.p4square.grow.model.UserRecord;
+import com.p4square.grow.provider.Provider;
+import com.p4square.grow.provider.DelegateProvider;
+import com.p4square.grow.provider.JsonEncodedProvider;
/**
* This resource simply redirects the user to either the assessment
@@ -32,7 +32,7 @@ public class AccountRedirectResource extends ServerResource {
private static final Logger LOG = Logger.getLogger(AccountRedirectResource.class);
private Config mConfig;
- private JsonRequestClient mJsonClient;
+ private Provider<String, UserRecord> mUserRecordProvider;
// Fields pertaining to this request.
private String mUserId;
@@ -44,7 +44,14 @@ public class AccountRedirectResource extends ServerResource {
GrowFrontend growFrontend = (GrowFrontend) getApplication();
mConfig = growFrontend.getConfig();
- mJsonClient = new JsonRequestClient(getContext().getClientDispatcher());
+ mUserRecordProvider = new DelegateProvider<String, String, UserRecord>(
+ new JsonRequestProvider<UserRecord>(getContext().getClientDispatcher(),
+ UserRecord.class)) {
+ @Override
+ public String makeKey(String userid) {
+ return getBackendEndpoint() + "/accounts/" + userid;
+ }
+ };
mUserId = getRequest().getClientInfo().getUser().getIdentifier();
}
@@ -56,25 +63,17 @@ public class AccountRedirectResource extends ServerResource {
protected Representation get() {
try {
// Fetch account Map.
- Map account = new HashMap();
- try {
- JsonResponse response = backendGet("/accounts/" + mUserId);
- if (response.getStatus().isSuccess()) {
- account = response.getMap();
- }
- } catch (ClientException e) {
-
- }
+ UserRecord user = mUserRecordProvider.get(mUserId);
// Check for the new believers cookie
String cookie = getRequest().getCookies().getFirstValue(NewBelieverResource.COOKIE_NAME);
if (cookie != null && cookie.length() != 0) {
- account.put("landing", "training");
- account.put("newbeliever", "true");
- backendPut("/accounts/" + mUserId, account);
+ user.setLanding("training");
+ user.setNewBeliever(true);
+ mUserRecordProvider.put(mUserId, user);
}
- String landing = (String) account.get("landing");
+ String landing = user.getLanding();
if (landing == null) {
landing = "assessment";
}
@@ -97,31 +96,4 @@ public class AccountRedirectResource extends ServerResource {
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;
- }
-
- protected JsonResponse backendPut(final String uri, final Map data) {
- LOG.debug("Sending backend PUT " + uri);
-
- final JsonResponse response = mJsonClient.put(getBackendEndpoint() + uri, data);
- 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/FeedData.java b/src/com/p4square/grow/frontend/FeedData.java
index 55e845c..feb03a1 100644
--- a/src/com/p4square/grow/frontend/FeedData.java
+++ b/src/com/p4square/grow/frontend/FeedData.java
@@ -62,8 +62,15 @@ public class FeedData {
mMessageProvider = new JsonRequestProvider<Message>(clientDispatcher, Message.class);
}
- public List<MessageThread> getThreads(final String topic) throws IOException {
- return mThreadsProvider.get(makeUrl(topic));
+ /**
+ * Get the threads for a topic.
+ *
+ * @param topic The topic to request threads for.
+ * @param limit The maximum number of threads.
+ * @return A list of MessageThread objects.
+ */
+ public List<MessageThread> getThreads(final String topic, final int limit) throws IOException {
+ return mThreadsProvider.get(makeUrl(limit, topic));
}
public List<Message> getMessages(final String topic, final String threadId) throws IOException {
@@ -91,4 +98,8 @@ public class FeedData {
return url;
}
+
+ private String makeUrl(int limit, String... parts) {
+ return makeUrl(parts) + "?limit=" + limit;
+ }
}
diff --git a/src/com/p4square/grow/frontend/SurveyPageResource.java b/src/com/p4square/grow/frontend/SurveyPageResource.java
index f864014..1f9c56c 100644
--- a/src/com/p4square/grow/frontend/SurveyPageResource.java
+++ b/src/com/p4square/grow/frontend/SurveyPageResource.java
@@ -29,8 +29,11 @@ import com.p4square.fmfacade.FreeMarkerPageResource;
import com.p4square.grow.config.Config;
import com.p4square.grow.model.Question;
-import com.p4square.grow.provider.QuestionProvider;
+import com.p4square.grow.model.UserRecord;
+import com.p4square.grow.provider.DelegateProvider;
+import com.p4square.grow.provider.JsonEncodedProvider;
import com.p4square.grow.provider.Provider;
+import com.p4square.grow.provider.QuestionProvider;
/**
* SurveyPageResource handles rendering the survey and processing user's answers.
@@ -49,6 +52,7 @@ public class SurveyPageResource extends FreeMarkerPageResource {
private Template mSurveyTemplate;
private JsonRequestClient mJsonClient;
private Provider<String, Question> mQuestionProvider;
+ private Provider<String, UserRecord> mUserRecordProvider;
// Fields pertaining to this request.
private String mQuestionId;
@@ -74,6 +78,15 @@ public class SurveyPageResource extends FreeMarkerPageResource {
}
};
+ mUserRecordProvider = new DelegateProvider<String, String, UserRecord>(
+ new JsonRequestProvider<UserRecord>(getContext().getClientDispatcher(),
+ UserRecord.class)) {
+ @Override
+ public String makeKey(String userid) {
+ return getBackendEndpoint() + "/accounts/" + userid;
+ }
+ };
+
mQuestionId = getAttribute("questionId");
mUserId = getRequest().getClientInfo().getUser().getIdentifier();
}
@@ -205,24 +218,6 @@ 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 Question getQuestion(String id) {
try {
return mQuestionProvider.get(id);
@@ -252,12 +247,16 @@ public class SurveyPageResource extends FreeMarkerPageResource {
if (nextQuestionId == null) {
// Just finished the last question. Update the user's account
- Map account = getAccount(mUserId);
- if (account == null) {
- account = new HashMap();
+ try {
+ UserRecord account = mUserRecordProvider.get(mUserId);
+ if (account == null) {
+ account = new UserRecord();
+ }
+ account.setLanding("training");
+ mUserRecordProvider.put(mUserId, account);
+ } catch (IOException e) {
+ LOG.warn("IOException updating landing for " + mUserId, e);
}
- account.put("landing", "training");
- backendPut("/accounts/" + mUserId, account);
String nextPage = mConfig.getString("dynamicRoot", "");
nextPage += "/account/assessment/results";
diff --git a/src/com/p4square/grow/frontend/TrainingPageResource.java b/src/com/p4square/grow/frontend/TrainingPageResource.java
index 0c8f656..ae2f3a7 100644
--- a/src/com/p4square/grow/frontend/TrainingPageResource.java
+++ b/src/com/p4square/grow/frontend/TrainingPageResource.java
@@ -214,21 +214,8 @@ public class TrainingPageResource extends FreeMarkerPageResource {
root.put("videos", videos);
root.put("allowUserToSkip", allowUserToSkip);
- // Optionally show the feed.
- boolean showfeed = "true".equals(getRequest().getCookies().getFirstValue("showfeed"));
- if (getQueryValue("showfeed") != null) {
- CookieSetting cookie = new CookieSetting("showfeed", "true");
- cookie.setPath("/");
- if ("true".equals(getQueryValue("showfeed"))) {
- showfeed = true;
- getResponse().getCookieSettings().add(cookie);
- } else {
- showfeed = false;
- cookie.setValue("false");
- cookie.setMaxAge(0);
- getResponse().getCookieSettings().add(cookie);
- }
- }
+ // Determine if we should show the feed.
+ boolean showfeed = true;
// Don't show the feed if the topic isn't allowed.
if (!FeedData.TOPICS.contains(mChapter)) {