diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2013-10-20 23:14:51 -0700 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2013-10-20 23:14:51 -0700 |
commit | e77c9b418fdfb935ff8e99f10f607a4bbd7e1c8c (patch) | |
tree | 9005f8c0e3b18f853fb8093b01f6aad64949bc13 /src/com/p4square/grow/model/Question.java | |
parent | 5037f4797461649994068d97a8433b6cd793c523 (diff) |
First stage of a major refactoring.
Question and Answer can now be serialized and deserialized to/from JSON.
As such, I no longer have to pass awkward maps around. As part of this
change I have introduced a Provider interface to abstract out loading
and persisting these beans.
The scoring logic has been completed factored out of
SurveyResultsResource and into the various ScoringEngines. Tests have
been added for Question, Answer, and the ScoringEngines. A bug has been
fixed in computing the value for slider questions.
The label identifiers in the circle questions have changed from all
lower case to camel case. That is, topleft is now topLeft. Several
issues have been corrected in the circle answers where the point values
did not match the labels.
Testing and code coverage support and reports have been added.
Diffstat (limited to 'src/com/p4square/grow/model/Question.java')
-rw-r--r-- | src/com/p4square/grow/model/Question.java | 134 |
1 files changed, 99 insertions, 35 deletions
diff --git a/src/com/p4square/grow/model/Question.java b/src/com/p4square/grow/model/Question.java index 387d723..37deffa 100644 --- a/src/com/p4square/grow/model/Question.java +++ b/src/com/p4square/grow/model/Question.java @@ -4,76 +4,125 @@ package com.p4square.grow.model; -import java.util.Collections; import java.util.HashMap; import java.util.Map; +import org.codehaus.jackson.annotate.JsonSubTypes; +import org.codehaus.jackson.annotate.JsonSubTypes.Type; +import org.codehaus.jackson.annotate.JsonTypeInfo; + /** * Model of an assessment question. * * @author Jesse Morgan <jesse@jesterpm.net> */ -public class Question { - public static enum QuestionType { - TEXT, IMAGE, SLIDER, QUAD, CIRCLE; +@JsonTypeInfo( + use = JsonTypeInfo.Id.NAME, + include = JsonTypeInfo.As.PROPERTY, + property = "type") +@JsonSubTypes({ + @Type(value = TextQuestion.class, name = "text"), + @Type(value = ImageQuestion.class, name = "image"), + @Type(value = SliderQuestion.class, name = "slider"), + @Type(value = QuadQuestion.class, name = "quad"), + @Type(value = CircleQuestion.class, name = "circle"), +}) +public abstract class Question { + /** + * QuestionType indicates the type of Question. + * + * @author Jesse Morgan <jesse@jesterpm.net> + */ + public enum QuestionType { + TEXT, + IMAGE, + SLIDER, + QUAD, + CIRCLE; + + @Override + public String toString() { + return name().toLowerCase(); + } } - private final Map<String, Object> mMap; - private final String mQuestionId; - private final QuestionType mType; - private final String mQuestionText; + private String mQuestionId; + private QuestionType mType; + private String mQuestionText; private Map<String, Answer> mAnswers; - private final String mPreviousQuestionId; - private final String mNextQuestionId; - - public Question(final Map<String, Object> map) { - mMap = map; - mQuestionId = (String) map.get("id"); - mType = QuestionType.valueOf(((String) map.get("type")).toUpperCase()); - - mQuestionText = (String) map.get("text"); - - mPreviousQuestionId = (String) map.get("previousQuestion"); - mNextQuestionId = (String) map.get("nextQuestion"); + private String mPreviousQuestionId; + private String mNextQuestionId; + public Question() { mAnswers = new HashMap<String, Answer>(); - for (Map.Entry<String, Object> answer : - ((Map<String, Object>) map.get("answers")).entrySet()) { - - final String id = answer.getKey(); - final Map<String, Object> answerMap = (Map<String, Object>) answer.getValue(); - final Answer answerObj = new Answer(id, answerMap); - mAnswers.put(id, answerObj); - } } + /** + * @return the id String for this question. + */ public String getId() { return mQuestionId; } - public QuestionType getType() { - return mType; + /** + * Set the id String for this question. + * @param id New id + */ + public void setId(String id) { + mQuestionId = id; } - public String getText() { + /** + * @return The Question text. + */ + public String getQuestion() { return mQuestionText; } + /** + * Set the question text. + * @param value The new question text. + */ + public void setQuestion(String value) { + mQuestionText = value; + } + + /** + * @return The id String of the previous question or null if no previous question exists. + */ public String getPreviousQuestion() { return mPreviousQuestionId; } + /** + * Set the id string of the previous question. + * @param id Previous question id or null if there is no previous question. + */ + public void setPreviousQuestion(String id) { + mPreviousQuestionId = id; + } + + /** + * @return The id String of the next question or null if no next question exists. + */ public String getNextQuestion() { return mNextQuestionId; } - public Map<String, Answer> getAnswers() { - return Collections.unmodifiableMap(mAnswers); + /** + * Set the id string of the next question. + * @param id next question id or null if there is no next question. + */ + public void setNextQuestion(String id) { + mNextQuestionId = id; } - public Map<String, Object> getMap() { - return Collections.unmodifiableMap(mMap); + /** + * @return a map of Answer id Strings to Answer objects. + */ + public Map<String, Answer> getAnswers() { + return mAnswers; } /** @@ -98,4 +147,19 @@ public class Question { return nextQuestion; } + + /** + * Update the score based on the answer to this question. + * + * @param score The running score to update. + * @param answer The answer give to this question. + * @return true if scoring should continue, false if this answer trumps everything else. + */ + public abstract boolean scoreAnswer(Score score, RecordedAnswer answer); + + /** + * @return the QuestionType of this question. + */ + public abstract QuestionType getType(); + } |