PageRenderTime 118ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/itbelts/itbelts
Java | 78 lines | 44 code | 18 blank | 16 comment | 4 complexity | 2d50d6f3fdbee7a34ac39fbf90901fe8 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.ICourseDAO;
  8. import org.itbelts.dao.ITopicDAO;
  9. import org.itbelts.domain.Course;
  10. import org.itbelts.domain.Topic;
  11. import com.google.gson.reflect.TypeToken;
  12. /**
  13. * MongoDB implementation for the ICourseDAO. <br>
  14. * <br>
  15. * <u><i>Version History</i></u>
  16. *
  17. * <pre>
  18. * v2013.10.0 20-okt.-2013 - DKBR813 - initial release
  19. *
  20. * </pre>
  21. *
  22. * @version v2013.10.0 20-okt.-2013
  23. * @author <a href=\"mailto:koenbruyndonckx@gmail.com\"> Koen Bruyndonckx </a>
  24. */
  25. public class CourseDAOImpl extends AbstractMongoLabDAO implements ICourseDAO {
  26. private ITopicDAO myTopicDAO = new TopicDAOImpl();
  27. private static final String PATH = "collections/courses";
  28. @Override
  29. public List<Course> getCourses() {
  30. URL url = buildUrl( PATH, "f={\"_id\":1,\"topicId\":1,\"name\":1}" );
  31. Type theListType = new TypeToken<List<Course>>() {}.getType();
  32. return readFrom( url, theListType );
  33. }
  34. @Override
  35. public List<Course> getCoursesForCommunity( String aCommunityId ) {
  36. // First get a complete list of all courses (we will filter out the ones we do not need later)
  37. List<Course> theFullList = getCourses();
  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 courses 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 Course getCourse( String aCourseId ) {
  55. URL url = buildUrl( PATH, "q={\"_id\":\"" + aCourseId + "\"}" );
  56. Type theListType = new TypeToken<List<Course>>(){}.getType();
  57. List<Course> theList = readFrom( url, theListType );
  58. return theList.get( 0 );
  59. }
  60. }