PageRenderTime 161ms CodeModel.GetById 27ms RepoModel.GetById 5ms app.codeStats 0ms

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

https://bitbucket.org/itbelts/itbelts
Java | 78 lines | 44 code | 18 blank | 16 comment | 4 complexity | b3b79caf31e2431a9501b1ec2ca206cb MD5 | raw file
  1. package org.itbelts.dao.mongolab;
  2. import java.lang.reflect.Type;
  3. import java.net.URL;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.itbelts.dao.IExamDAO;
  8. import org.itbelts.dao.ITopicDAO;
  9. import org.itbelts.domain.Exam;
  10. import org.itbelts.domain.Topic;
  11. import com.google.gson.reflect.TypeToken;
  12. /**
  13. * MongoDB implementation for the IExamDAO. <br>
  14. * <br>
  15. * <u><i>Version History</i></u>
  16. *
  17. * <pre>
  18. * v2013.12.0 28-okt.-2013 - DKBR813 - initial release
  19. *
  20. * </pre>
  21. *
  22. * @version v2013.12.0 28-okt.-2013
  23. * @author <a href=\"mailto:koenbruyndonckx@gmail.com\"> Koen Bruyndonckx </a>
  24. */
  25. public class ExamDAOImpl extends AbstractMongoLabDAO implements IExamDAO {
  26. private ITopicDAO myTopicDAO = new TopicDAOImpl();
  27. private static final String PATH = "collections/exams";
  28. @Override
  29. public List<Exam> getExams() {
  30. URL url = buildUrl( PATH, "f={\"_id\":1,\"topicId\":1,\"name\":1}" );
  31. Type theListType = new TypeToken<List<Exam>>() {}.getType();
  32. return readFrom( url, theListType );
  33. }
  34. @Override
  35. public List<Exam> getExamsForCommunity( String aCommunityId ) {
  36. // First get a complete list of all Exams (we will filter out the ones we do not need later)
  37. List<Exam> theFullList = getExams();
  38. // Build a small Map of topic-ids for all topics belonging to the given community
  39. Map<String,Boolean> theTopicMap = new HashMap<String,Boolean>();
  40. List<Topic> theTopics = myTopicDAO.getTopicsForCommunity( aCommunityId );
  41. for ( Topic t : theTopics ) {
  42. theTopicMap.put( t.get_id(), Boolean.TRUE );
  43. }
  44. // Then remove all Exams that have a topic id that is not in the Map
  45. for ( int i = theFullList.size() -1 ; i >= 0 ; i-- ) {
  46. String theTopicId = theFullList.get( i ).getTopicId();
  47. if ( theTopicMap.get( theTopicId ) == null ) {
  48. theFullList.remove( i );
  49. }
  50. }
  51. return theFullList;
  52. }
  53. @Override
  54. public Exam getExam( String aExamId ) {
  55. URL url = buildUrl( PATH, "q={\"_id\":\"" + aExamId + "\"}" );
  56. Type theListType = new TypeToken<List<Exam>>(){}.getType();
  57. List<Exam> theList = readFrom( url, theListType );
  58. return theList.get( 0 );
  59. }
  60. }