Rapid Development of a Quiz Application using Spring Roo
Truth be told, I've spent so much time running a training practice, writing a book and dealing with Rails apps that my brain needed a bit of adjustment to long-form, more than one day coding assignments. I tried to start with a simple JPA/hibernate model, but the mental feedback loop, even with Roo at the helm, was too slow for me.
(Remember, I am on a two week deadline right now to write two presentations AND get the quiz working if I have a shot at this. So I am trying to control a flailing feeling. And thinking that I'm only doing one presentation next time.)
I went through three stages of thought before writing this down:
Stage 1 - Let's focus on the model
I figured that the data was key, and that I could model from the database up. I ended up floundering after a few hours of this, realizing that we have two problems here - the model of the quiz structure, and dealing with running instances of the quiz, recording answers, and reporting results.
The results were rather throwaway, so I won't bore you with them.
So, I went to Stage 2
Stage 2 - Go all design-patterny on it
Ok, focus on the objects and the in-memory model. That was a great turn, as it got me thinking about the contracts, building up the objects, etc. I figured I could have a Quiz in-memory model, and that I could build it using a builder. So I spent time writing up builders that looked something like this:
package com.chariot.games.quizzo.model.quiz;
public class QuestionBuilder {
Question question;
public QuestionBuilder() {
question = new Question();
}
public QuestionBuilder title(String title) {
question.setTitle(title);
return this;
}
public QuestionBuilder choice(Choice choice) {
question.getChoices().add(choice);
return this;
}
public Question asQuestion() {
return question;
}
}
Then I could use the builder in tests, ala:
@Test
public void testBuildQuestionWithBooleanAnswer()
throws CloneNotSupportedException {
Question q = new QuestionBuilder()
.title("Studies say people are crazy.")
.choice(new ChoiceBuilder()
.booleanChoice(true)
.text("They are crazy")
.asChoice()
).asQuestion();
assertEquals("Studies say people are crazy.", q.getTitle());
assertEquals(1, q.getChoices().size());
assertEquals("They are crazy", q.getChoices().get(0).getAnswerText());
assertEquals(BooleanChoice.class, q.getChoices().get(0).getClass());
Question q2 = q.clone();
assertEquals(q2, q);
assertNotSame(q2, q);
}
Stage 3 - Prototype quickly on paper, models again
But in the end, what I really need is a quiz definition, with questions, etc., and I need to persist them at some point. So, it was fun, and helped me understand the relationships, but back to JPA.
My model so far...
Ok, so far I have the following model elements:
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Choice {
@NotNull
@Size(max = 300)
private String text;
@NotNull
private Boolean correct;
@NotNull
@ManyToOne
private Question question;
}
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Question {
@ManyToOne
private Quiz quiz;
@NotNull
@Size(max = 300)
private String text;
@OneToMany(cascade = CascadeType.ALL)
private Set<Choice> choices = new HashSet<Choice>();
}
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Quiz {
@NotNull
@Size(max = 200)
private String title;
@NotNull
@Size(max = 500)
private String description;
}
@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class Team {
@NotNull
@Size(max = 80)
private String name;
@NotNull
private String mission;
@ElementCollection
private List<TeamMember> teamMembers;
}
@RooJavaBean
@RooToString
@Embeddable
public class TeamMember {
@NotNull
@Size(max = 40)
private String name;
}
Did I mention - Git branches are cheap?
Git is great for things like this - since it keeps versions and branches within your .git database directory, you can branch and merge at will, and then push the repository updates when you are ready.
From http://www.rimple.com/tech/2012/2/19/quizzo-in-roo-truth-be-told.html
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





