DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Rapid Development of a Quiz Application using Spring Roo

Rapid Development of a Quiz Application using Spring Roo

Ken Rimple user avatar by
Ken Rimple
·
Feb. 20, 12 · Interview
Like (0)
Save
Tweet
Share
8.04K Views

Join the DZone community and get the full member experience.

Join For Free

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

Spring Roo application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Rust vs Go: Which Is Better?
  • Master Spring Boot 3 With GraalVM Native Image
  • Required Knowledge To Pass AWS Certified Solutions Architect — Professional Exam
  • Using GPT-3 in Our Applications

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: