/src/main/java/org/atlasapi/persistence/content/mongo/MongoContentGroupResolver.java

https://github.com/atlasapi/atlas-persistence · Java · 71 lines · 58 code · 13 blank · 0 comment · 7 complexity · 76102edd581ecc0ebdb8c43d34b226a6 MD5 · raw file

  1. package org.atlasapi.persistence.content.mongo;
  2. import org.atlasapi.persistence.content.ResolvedContent;
  3. import org.atlasapi.persistence.content.ResolvedContent.ResolvedContentBuilder;
  4. import com.metabroadcast.common.persistence.mongo.DatabasedMongo;
  5. import com.mongodb.DBCollection;
  6. import com.mongodb.DBObject;
  7. import org.atlasapi.media.entity.ContentGroup;
  8. import org.atlasapi.persistence.content.ContentCategory;
  9. import org.atlasapi.persistence.content.ContentGroupResolver;
  10. import org.atlasapi.persistence.media.entity.ContentGroupTranslator;
  11. import org.atlasapi.persistence.media.entity.IdentifiedTranslator;
  12. import static com.metabroadcast.common.persistence.mongo.MongoBuilders.where;
  13. import com.mongodb.DBCursor;
  14. import java.util.HashSet;
  15. import java.util.Set;
  16. public class MongoContentGroupResolver implements ContentGroupResolver {
  17. private final ContentGroupTranslator contentGroupTranslator;
  18. private final DBCollection contentGroups;
  19. public MongoContentGroupResolver(DatabasedMongo mongo) {
  20. MongoContentTables contentTables = new MongoContentTables(mongo);
  21. this.contentGroups = contentTables.collectionFor(ContentCategory.CONTENT_GROUP);
  22. this.contentGroupTranslator = new ContentGroupTranslator();
  23. }
  24. @Override
  25. public ResolvedContent findByCanonicalUris(Iterable<String> uris) {
  26. ResolvedContentBuilder results = ResolvedContent.builder();
  27. for (String uri : uris) {
  28. DBObject found = contentGroups.findOne(where().fieldEquals(IdentifiedTranslator.CANONICAL_URL, uri).build());
  29. if (found != null) {
  30. ContentGroup contentGroup = contentGroupTranslator.fromDBObject(found, new ContentGroup());
  31. results.put(uri, contentGroup);
  32. }
  33. }
  34. return results.build();
  35. }
  36. @Override
  37. public ResolvedContent findByIds(Iterable<Long> ids) {
  38. ResolvedContentBuilder results = ResolvedContent.builder();
  39. for (Long id : ids) {
  40. DBObject found = contentGroups.findOne(where().fieldEquals(IdentifiedTranslator.ID, id).build());
  41. if (found != null) {
  42. ContentGroup contentGroup = contentGroupTranslator.fromDBObject(found, new ContentGroup());
  43. results.put(id.toString(), contentGroup);
  44. }
  45. }
  46. return results.build();
  47. }
  48. @Override
  49. public Iterable<ContentGroup> findAll() {
  50. Set<ContentGroup> results = new HashSet<ContentGroup>();
  51. DBCursor cursor = contentGroups.find();
  52. for (DBObject current : cursor) {
  53. ContentGroup contentGroup = contentGroupTranslator.fromDBObject(current, new ContentGroup());
  54. results.add(contentGroup);
  55. }
  56. return results;
  57. }
  58. }