diff options
Diffstat (limited to 'src/com/p4square')
5 files changed, 214 insertions, 1 deletions
| 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..0dd903b --- /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 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; + +/** + * 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 3f3c96b..a1ed020 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 b6c53f5..4d87027 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); | 
