Chariot Training Classes

Training Courses

I run Chariot's training and mentoring services. We provide training in AngularJS, HTML5, Spring, Hibernate, Maven, Scala, and more.

Chariot Education Services

Technology

Chariot Emerging Tech

Learn about upcoming technologies and trends from my colleagues at Chariot Solutions.

Resources

Chariot Conferences

Podcasts

« Dojo 1.7 is quite different | Main | Quizzo - it's THURSDAY? Must go into the Dojo and meditate »
Monday
Mar052012

Quizzo - javascript speed bump = fallback plan...

In a word, AARGH.

I got stuck in Javascript / Dojo debug hell the few days leading up to our internal conference. Unfortunately, that meant that Quizzo had to happen without my software. However, that's life, and I had a fallback plan.

For those of you keeping score, it's the second time Dojo / Javascript hung me out to dry - the first time was during the writing of Spring Roo in Action over a year ago. I don't know if it is me, but it certainly is very painful to debug problems with Dojo, at least in the embedded 1.x version and in 1.6.1. I want to upgrade this game to 1.7.2 and see if the debugging got any better.

Incidentally...

So, I apparently don't know my Dojo. Or maybe even how to read. Because there is a (admittedly very old) helpful tutorial on the Dojo site that explains my very problem. Long story short, you have to add debugAtAllCosts: true to your dojo config parameters. Here is my current setting:

  

Ahhhh, if only I read that debugging page before last Saturday, I'd be a much happier person. Without this change, stack traces and errors in Dojo look incredibly incoherent. With it, it actually tells you what is wrong in your Javascript console.

The fallback plan

Ok, so we had to take all of the questions that everybody submitted and import them into a database. Did you know Spring makes an excellent command-line tool?

 public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext(
        "classpath:META-INF/spring/applicationContext*.xml"
    );

    quizService = context.getBean(QuizService.class);
    questionService = context.getBean(QuestionService.class);
    choiceService = context.getBean(ChoiceService.class);


    parseAndProcess(args[0]);

  }

We ended up using it like a swiss army knife to pull the data in using the Apache POI library, and write it out using our services. Here is the parseAndProcess, which I just grabbed from a sample and then manipulated to suit my needs.

Here is a (admittedly quickly dashed off) snippet:

 private static void parseAndProcess(String fileName) {

    Quiz quiz = new Quiz();
    quiz.setTitle("Chariot Day Quizzo!");
    quiz.setDescription("Because a day without night would be, what would it be?");
    quizService.saveQuiz(quiz);

    List cellDataList = new ArrayList();
    try {
      FileInputStream fileInputStream = new FileInputStream(fileName);
      POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
      HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
      HSSFSheet hssfSheet = workBook.getSheetAt(0);
      Iterator rowIterator = hssfSheet.rowIterator();

      rowIterator.next(); // skip top header

      short questionIdx = 0;
      while (rowIterator.hasNext()) {
        questionIdx++;
        HSSFRow hssfRow = (HSSFRow) rowIterator.next();

        // get all quiz rows
        Iterator iterator = hssfRow.cellIterator();
        HSSFCell questionOrderCell = (HSSFCell) iterator.next();
        short order = (short) questionOrderCell.getNumericCellValue();
        String questionText = ((HSSFCell) iterator.next()).getStringCellValue();

        Question question = new Question();
        question.setQuestionText(questionText);
        question.setSortOrder(order);
        questionService.saveQuestion(question);

        HSSFCell correctAnswerCell = (HSSFCell) iterator.next();

        int correctAnswerPosition =
            BigDecimal.valueOf(correctAnswerCell.getNumericCellValue()).intValue() - 1;
        short curIdx = -1;
        while (iterator.hasNext()) {
          String answerText = ((HSSFCell)iterator.next()).getStringCellValue();

          Choice choice = new Choice();
          choice.setQuestion(question);
          choice.setPointValue(new BigDecimal("1.0"));
          choice.setText(answerText);
          curIdx ++;
          choice.setCorrect(curIdx == correctAnswerPosition);

          choiceService.saveChoice(choice);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

I used this entry in my pom.xml:

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.7</version>
    </dependency>

So what did you do?

I ended up using a Ruby-based slide presentation project called Showoff. It requires installation of a Ruby gem, showoff:

$ gem install showoff

You can use showoff to set up a sample slide show:

showoff create presentationname

This command creates a new slideshow. Poke around and have fun. The project website is on github at https://github.com/schacon/showoff.

What's next for Quizzo?

Now that I've been given a reprieve, I'm going to dig deeper. I'll be upgrading Dojo to 1.7.2, showing you how to debug widgets, and cleaning up the user interface. We'll have to tackle some sophisticated problems, such as managing state, what happens when we hit the refresh button, and more.

Stay tuned over the next few weeks. We'll also try to extract some of what we're doing and put it into a new Roo add-on.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>