summaryrefslogtreecommitdiff
path: root/src/com/p4square/grow/backend/resources/SurveyResource.java
blob: 83c4cad39b2c2df68de70984c6581f1fc0ca1532 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright 2013 Jesse Morgan
 */

package com.p4square.grow.backend.resources;

import java.io.IOException;

import java.util.Map;
import java.util.HashMap;

import org.codehaus.jackson.map.ObjectMapper;

import org.restlet.data.MediaType;
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;

/**
 * This resource manages assessment questions.
 *
 * @author Jesse Morgan <jesse@jesterpm.net>
 */
public class SurveyResource extends ServerResource {
    private static final Logger LOG = Logger.getLogger(SurveyResource.class);

    private static final ObjectMapper MAPPER = new ObjectMapper();

    private CassandraDatabase mDb;

    private String mQuestionId;

    @Override
    public void doInit() {
        super.doInit();

        final GrowBackend backend = (GrowBackend) getApplication();
        mDb = backend.getDatabase();

        mQuestionId = getAttribute("questionId");
    }

    /**
     * Handle GET Requests.
     */
    @Override
    protected Representation get() {
        String result = "{}";

        if (mQuestionId == null) {
            // TODO: List all question ids

        } else if (mQuestionId.equals("first")) {
            // Get the first question id from db?
            Map<?, ?> questionSummary = getQuestionsSummary();
            mQuestionId = (String) questionSummary.get("first");

        } else if (mQuestionId.equals("count")) {
            // Get the first question id from db?
            Map<?, ?> questionSummary = getQuestionsSummary();

            return new StringRepresentation("{\"count\":" +
                    String.valueOf((Integer) questionSummary.get("count")) + "}");
        }

        if (mQuestionId != null) {
            // Get a question by id
            result = mDb.getKey("strings", "/questions/" + mQuestionId);

            if (result == null) {
                // 404
                setStatus(Status.CLIENT_ERROR_NOT_FOUND);
                return null;
            }
        }

        return new StringRepresentation(result);
    }

    private Map<?, ?> getQuestionsSummary() {
        try {
            String json = mDb.getKey("strings", "/questions");

            if (json != null) {
                return MAPPER.readValue(json, Map.class);
            }

        } catch (IOException e) {
            LOG.info("Exception reading questions summary.", e);
        }

        return null;
    }
}