diff options
Diffstat (limited to 'src/com/p4square/grow/frontend')
4 files changed, 82 insertions, 11 deletions
diff --git a/src/com/p4square/grow/frontend/AssessmentResultsPage.java b/src/com/p4square/grow/frontend/AssessmentResultsPage.java index 9c69c69..ff1832c 100644 --- a/src/com/p4square/grow/frontend/AssessmentResultsPage.java +++ b/src/com/p4square/grow/frontend/AssessmentResultsPage.java @@ -4,6 +4,7 @@ package com.p4square.grow.frontend; +import java.util.Date; import java.util.Map; import freemarker.template.Template; @@ -22,7 +23,12 @@ import com.p4square.fmfacade.json.JsonRequestClient; import com.p4square.fmfacade.json.JsonResponse; import com.p4square.fmfacade.json.ClientException; +import com.p4square.f1oauth.Attribute; +import com.p4square.f1oauth.F1API; +import com.p4square.f1oauth.F1User; + import com.p4square.grow.config.Config; +import com.p4square.grow.provider.JsonEncodedProvider; /** * This page fetches the user's final score and displays the transitional page between @@ -82,6 +88,9 @@ public class AssessmentResultsPage extends FreeMarkerPageResource { return new StringRepresentation("Redirecting to " + nextPage); } + // Publish results in F1 + publishScoreInF1(response.getMap()); + root.put("stage", score); return new TemplateRepresentation(t, root, MediaType.TEXT_HTML); @@ -92,6 +101,33 @@ public class AssessmentResultsPage extends FreeMarkerPageResource { } } + private void publishScoreInF1(Map results) { + if (!(getRequest().getClientInfo().getUser() instanceof F1User)) { + // Only useful if the user is from F1. + return; + } + + F1User user = (F1User) getRequest().getClientInfo().getUser(); + + // Update the attribute. + String attributeName = "Assessment Complete - " + results.get("result"); + + try { + Attribute attribute = new Attribute(); + attribute.setStartDate(new Date()); + attribute.setComment(JsonEncodedProvider.MAPPER.writeValueAsString(results)); + + F1API f1 = mGrowFrontend.getF1Access().getAuthenticatedApi(user); + if (!f1.addAttribute(user.getIdentifier(), attributeName, attribute)) { + LOG.error("addAttribute failed for " + user.getIdentifier() + + " with attribute " + attributeName); + } + } catch (Exception e) { + LOG.error("addAttribute failed for " + user.getIdentifier() + + " with attribute " + attributeName, e); + } + } + /** * @return The backend endpoint URI */ @@ -108,7 +144,8 @@ public class AssessmentResultsPage extends FreeMarkerPageResource { 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()); + LOG.warn("Error making backend request for '" + uri + "'. status = " + + response.getStatus().toString()); } return response; diff --git a/src/com/p4square/grow/frontend/ChapterCompletePage.java b/src/com/p4square/grow/frontend/ChapterCompletePage.java index b3896e9..a2c4ebe 100644 --- a/src/com/p4square/grow/frontend/ChapterCompletePage.java +++ b/src/com/p4square/grow/frontend/ChapterCompletePage.java @@ -4,6 +4,7 @@ package com.p4square.grow.frontend; +import java.util.Date; import java.util.Map; import freemarker.template.Template; @@ -22,10 +23,14 @@ import com.p4square.fmfacade.json.JsonRequestClient; import com.p4square.fmfacade.json.JsonResponse; import com.p4square.fmfacade.json.ClientException; +import com.p4square.f1oauth.Attribute; +import com.p4square.f1oauth.F1API; +import com.p4square.f1oauth.F1User; + import com.p4square.grow.config.Config; import com.p4square.grow.model.TrainingRecord; -import com.p4square.grow.provider.TrainingRecordProvider; import com.p4square.grow.provider.Provider; +import com.p4square.grow.provider.TrainingRecordProvider; /** * This resource displays the transitional page between chapters. @@ -93,6 +98,9 @@ public class ChapterCompletePage extends FreeMarkerPageResource { return new StringRepresentation("Redirecting to " + nextPage); } + // Publish the training chapter complete attribute. + assignAttribute(); + // Find the next chapter String nextChapter = null; { @@ -149,6 +157,32 @@ public class ChapterCompletePage extends FreeMarkerPageResource { } } + private void assignAttribute() { + if (!(getRequest().getClientInfo().getUser() instanceof F1User)) { + // Only useful if the user is from F1. + return; + } + + F1User user = (F1User) getRequest().getClientInfo().getUser(); + + // Update the attribute. + String attributeName = "Training Complete - " + mChapter; + + try { + Attribute attribute = new Attribute(); + attribute.setStartDate(new Date()); + + F1API f1 = mGrowFrontend.getF1Access().getAuthenticatedApi(user); + if (!f1.addAttribute(user.getIdentifier(), attributeName, attribute)) { + LOG.error("addAttribute failed for " + user.getIdentifier() + + " with attribute " + attributeName); + } + } catch (Exception e) { + LOG.error("addAttribute failed for " + user.getIdentifier() + + " with attribute " + attributeName, e); + } + } + /** * @return The backend endpoint URI */ diff --git a/src/com/p4square/grow/frontend/GrowFrontend.java b/src/com/p4square/grow/frontend/GrowFrontend.java index 4b193d0..926670b 100644 --- a/src/com/p4square/grow/frontend/GrowFrontend.java +++ b/src/com/p4square/grow/frontend/GrowFrontend.java @@ -30,7 +30,7 @@ import com.p4square.fmfacade.FreeMarkerPageResource; import com.p4square.grow.config.Config; -import com.p4square.f1oauth.F1OAuthHelper; +import com.p4square.f1oauth.F1Access; import com.p4square.f1oauth.SecondPartyVerifier; import com.p4square.session.SessionCheckingAuthenticator; @@ -49,7 +49,7 @@ public class GrowFrontend extends FMFacade { private Config mConfig; - private F1OAuthHelper mHelper; + private F1Access mHelper; public GrowFrontend() { this(new Config()); @@ -73,13 +73,13 @@ public class GrowFrontend extends FMFacade { super.start(); } - synchronized F1OAuthHelper getHelper() { + synchronized F1Access getF1Access() { if (mHelper == null) { - mHelper = new F1OAuthHelper(getContext(), mConfig.getString("f1ConsumerKey", ""), + mHelper = new F1Access(getContext(), mConfig.getString("f1ConsumerKey", ""), mConfig.getString("f1ConsumerSecret", ""), mConfig.getString("f1BaseUrl", "staging.fellowshiponeapi.com"), mConfig.getString("f1ChurchCode", "pfseawa"), - F1OAuthHelper.UserType.WEBLINK); + F1Access.UserType.WEBLINK); } return mHelper; @@ -129,7 +129,7 @@ public class GrowFrontend extends FMFacade { SessionCheckingAuthenticator sessionChk = new SessionCheckingAuthenticator(context, true); // This is used to authenticate the user - SecondPartyVerifier f1Verifier = new SecondPartyVerifier(context, getHelper()); + SecondPartyVerifier f1Verifier = new SecondPartyVerifier(context, getF1Access()); LoginFormAuthenticator loginAuth = new LoginFormAuthenticator(context, false, f1Verifier); loginAuth.setLoginFormUrl(loginPage); loginAuth.setLoginPostUrl(loginPost); diff --git a/src/com/p4square/grow/frontend/NewAccountResource.java b/src/com/p4square/grow/frontend/NewAccountResource.java index 9155a00..54c1790 100644 --- a/src/com/p4square/grow/frontend/NewAccountResource.java +++ b/src/com/p4square/grow/frontend/NewAccountResource.java @@ -18,7 +18,7 @@ import org.restlet.ext.freemarker.TemplateRepresentation; import org.apache.log4j.Logger; -import com.p4square.f1oauth.F1OAuthHelper; +import com.p4square.f1oauth.F1Access; import com.p4square.restlet.oauth.OAuthException; import com.p4square.fmfacade.FreeMarkerPageResource; @@ -32,7 +32,7 @@ public class NewAccountResource extends FreeMarkerPageResource { private static Logger LOG = Logger.getLogger(NewAccountResource.class); private GrowFrontend mGrowFrontend; - private F1OAuthHelper mHelper; + private F1Access mHelper; private String mErrorMessage; @@ -44,7 +44,7 @@ public class NewAccountResource extends FreeMarkerPageResource { super.doInit(); mGrowFrontend = (GrowFrontend) getApplication(); - mHelper = mGrowFrontend.getHelper(); + mHelper = mGrowFrontend.getF1Access(); mErrorMessage = ""; |