PageRenderTime 62ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/org.itbelts.domain/src/main/java/org/itbelts/dao/mongolab/QuestionDAOImpl.java

https://bitbucket.org/itbelts/itbelts
Java | 106 lines | 68 code | 22 blank | 16 comment | 7 complexity | a6ea093bd4876ad888285e07b6bc190d MD5 | raw file
  1. package org.itbelts.dao.mongolab;
  2. import java.lang.reflect.Type;
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Random;
  9. import org.itbelts.dao.IQuestionDAO;
  10. import org.itbelts.domain.AnswerContainer;
  11. import org.itbelts.domain.Question;
  12. import org.itbelts.domain.QuestionCategory;
  13. import org.itbelts.domain.UserExam;
  14. import com.google.gson.reflect.TypeToken;
  15. /**
  16. * MongoDB implementation for the IQuestionDAO. <br>
  17. * <br>
  18. * <u><i>Version History</i></u>
  19. *
  20. * <pre>
  21. * v2013.12.0 01-nov.-2013 - DKBR813 - initial release
  22. *
  23. * </pre>
  24. *
  25. * @version v2013.12.0 01-nov.-2013
  26. * @author <a href=\"mailto:koenbruyndonckx@gmail.com\"> Koen Bruyndonckx </a>
  27. */
  28. public class QuestionDAOImpl extends AbstractMongoLabDAO implements IQuestionDAO {
  29. private static final String PATH = "collections/questions";
  30. private static final String CATEGORY_PATH = "collections/questioncategories";
  31. @Override
  32. public List<QuestionCategory> getQuestionsCategoriesForExam( String anExamId ) {
  33. URL url = buildUrl( CATEGORY_PATH, "q={\"examId\":\"" + anExamId + "\"}" );
  34. Type theListType = new TypeToken<List<QuestionCategory>>(){}.getType();
  35. return readFrom( url, theListType );
  36. }
  37. @Override
  38. public List<Question> getQuestionsForCategory( String aCategoryId ) {
  39. URL url = buildUrl( PATH, "q={\"questionCategoryId\":\"" + aCategoryId + "\"}" );
  40. Type theListType = new TypeToken<List<Question>>(){}.getType();
  41. return readFrom( url, theListType );
  42. }
  43. @Override
  44. public Map<String,Question> getQuestionsForUserExam( UserExam aUserExam ) {
  45. Map<String,Question> theMap = new HashMap<>();
  46. if ( aUserExam.getAnswers() == null || aUserExam.getAnswers().size() == 0 ) {
  47. return theMap;
  48. }
  49. try {
  50. for ( AnswerContainer ac : aUserExam.getAnswers() ) {
  51. theMap.put( ac.getQuestionId(), getQuestion( ac.getQuestionId() ) );
  52. }
  53. } catch ( Exception e ) {
  54. // This could fail if the userExam points to questions that are no longer available. We'll deal with this in the view controller of the exam
  55. }
  56. return theMap;
  57. }
  58. @Override
  59. public Question getQuestion( String aQuestionId ) {
  60. URL url = buildUrl( PATH, "q={\"_id\":\"" + aQuestionId + "\"}" );
  61. Type theListType = new TypeToken<List<Question>>(){}.getType();
  62. List<Question> theList = readFrom( url, theListType );
  63. return theList.get( 0 );
  64. }
  65. @Override
  66. public List<Question> buildExam( String anExamId ) {
  67. List<Question> theExam = new ArrayList<Question>();
  68. // the list of questions needs to come from all question categories. So we first read all categories for the exam.
  69. List<QuestionCategory> theCategories = getQuestionsCategoriesForExam( anExamId );
  70. // Then, for every category, according to its number of questions, we compose a random set
  71. Random r = new Random();
  72. for ( QuestionCategory c : theCategories ) {
  73. List<Question> theQuestions = getQuestionsForCategory( c.get_id() );
  74. int theAmount = c.getNumberOfQuestions();
  75. while ( theAmount > 0 ) {
  76. int theChosenQuestionIndex = r.nextInt( theQuestions.size() );
  77. theExam.add( theQuestions.get( theChosenQuestionIndex ) );
  78. theQuestions.remove( theChosenQuestionIndex );
  79. theAmount--;
  80. }
  81. }
  82. return theExam;
  83. }
  84. }