diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2013-09-09 23:03:49 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2013-09-09 23:03:49 -0700 |
commit | a159577236f4e0d5c5c519470d730122e000650c (patch) | |
tree | 05dcfd5ddd579283a739c14c2a4bfd335f24e239 /src | |
parent | 36a347eb5eb247da324d1f76116828ab95d255ae (diff) |
Adding the chapter transition page to the training section.
Diffstat (limited to 'src')
-rw-r--r-- | src/com/p4square/grow/frontend/ChapterCompletePage.java | 149 | ||||
-rw-r--r-- | src/com/p4square/grow/frontend/GrowFrontend.java | 1 | ||||
-rw-r--r-- | src/templates/templates/stage-complete.ftl | 2 |
3 files changed, 152 insertions, 0 deletions
diff --git a/src/com/p4square/grow/frontend/ChapterCompletePage.java b/src/com/p4square/grow/frontend/ChapterCompletePage.java new file mode 100644 index 0000000..5cc4724 --- /dev/null +++ b/src/com/p4square/grow/frontend/ChapterCompletePage.java @@ -0,0 +1,149 @@ +/* + * Copyright 2013 Jesse Morgan + */ + +package com.p4square.grow.frontend; + +import java.util.Map; + +import freemarker.template.Template; + +import org.restlet.data.MediaType; +import org.restlet.data.Status; +import org.restlet.representation.Representation; +import org.restlet.representation.StringRepresentation; +import org.restlet.ext.freemarker.TemplateRepresentation; + +import org.apache.log4j.Logger; + +import net.jesterpm.fmfacade.FreeMarkerPageResource; + +import net.jesterpm.fmfacade.json.JsonRequestClient; +import net.jesterpm.fmfacade.json.JsonResponse; +import net.jesterpm.fmfacade.json.ClientException; + +import com.p4square.grow.config.Config; + +/** + * This resource displays the transitional page between chapters. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ +public class ChapterCompletePage extends FreeMarkerPageResource { + private static final Logger LOG = Logger.getLogger(ChapterCompletePage.class); + + private GrowFrontend mGrowFrontend; + private Config mConfig; + private JsonRequestClient mJsonClient; + + private String mUserId; + private String mChapter; + + @Override + public void doInit() { + super.doInit(); + + mGrowFrontend = (GrowFrontend) getApplication(); + mConfig = mGrowFrontend.getConfig(); + + mJsonClient = new JsonRequestClient(getContext().getClientDispatcher()); + + mUserId = getRequest().getClientInfo().getUser().getIdentifier(); + + mChapter = getAttribute("chapter"); + } + + /** + * Return the login page. + */ + @Override + protected Representation get() { + Template t = mGrowFrontend.getTemplate("templates/stage-complete.ftl"); + + try { + if (t == null) { + setStatus(Status.CLIENT_ERROR_NOT_FOUND); + return ErrorPage.TEMPLATE_NOT_FOUND; + } + + Map<String, Object> root = getRootObject(); + + // Get the training summary + Map<String, Object> trainingRecord = null; + Map<String, Boolean> chapters = null; + { + JsonResponse response = backendGet("/accounts/" + mUserId + "/training"); + if (response.getStatus().isSuccess()) { + trainingRecord = response.getMap(); + chapters = (Map<String, Boolean>) trainingRecord.get("chapters"); + } + } + + // Verify they completed the chapter. + Boolean completed = chapters.get(mChapter); + if (completed == null || !completed) { + // Redirect back to training page... + String nextPage = mConfig.getString("dynamicRoot", ""); + nextPage += "/account/training/" + mChapter; + getResponse().redirectSeeOther(nextPage); + return new StringRepresentation("Redirecting to " + nextPage); + } + + // Find the next chapter + String nextChapter = null; + { + int min = Integer.MAX_VALUE; + for (Map.Entry<String, Boolean> chapter : chapters.entrySet()) { + int index = chapterIndex(chapter.getKey()); + if (!chapter.getValue() && index < min) { + min = index; + nextChapter = chapter.getKey(); + } + } + } + + root.put("stage", mChapter); + root.put("nextstage", nextChapter); + return new TemplateRepresentation(t, root, MediaType.TEXT_HTML); + + } 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; + } + + int chapterIndex(String chapter) { + if ("teacher".equals(chapter)) { + return 4; + } else if ("disciple".equals(chapter)) { + return 3; + } else if ("believer".equals(chapter)) { + return 2; + } else { + return 1; + } + } +} diff --git a/src/com/p4square/grow/frontend/GrowFrontend.java b/src/com/p4square/grow/frontend/GrowFrontend.java index ce29794..dd953d5 100644 --- a/src/com/p4square/grow/frontend/GrowFrontend.java +++ b/src/com/p4square/grow/frontend/GrowFrontend.java @@ -112,6 +112,7 @@ public class GrowFrontend extends FMFacade { accountRouter.attach("/assessment/question/{questionId}", SurveyPageResource.class); accountRouter.attach("/assessment/results", AssessmentResultsPage.class); accountRouter.attach("/assessment", SurveyPageResource.class); + accountRouter.attach("/training/{chapter}/completed", ChapterCompletePage.class); accountRouter.attach("/training/{chapter}/videos/{videoId}.json", VideosResource.class); accountRouter.attach("/training/{chapter}", TrainingPageResource.class); accountRouter.attach("/training", TrainingPageResource.class); diff --git a/src/templates/templates/stage-complete.ftl b/src/templates/templates/stage-complete.ftl index 4861015..72dc780 100644 --- a/src/templates/templates/stage-complete.ftl +++ b/src/templates/templates/stage-complete.ftl @@ -50,7 +50,9 @@ </@content> + <#if nextstage??> <div id="getstarted"> <a class="greenbutton" href="${dynamicRoot}/account/training/${nextstage}">Continue GROWing ➙</a> </div> + </#if> </@commonpage> |