PageRenderTime 98ms CodeModel.GetById 40ms RepoModel.GetById 2ms app.codeStats 0ms

/src/main/java/org/atlasapi/ChannelGroupIngestTask.java

https://github.com/atlasapi/atlas
Java | 118 lines | 94 code | 24 blank | 0 comment | 3 complexity | cc3dcb09f4a22910a52002564c896514 MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.atlasapi;
  2. import static com.google.gson.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES;
  3. import java.net.URL;
  4. import java.net.UnknownHostException;
  5. import java.util.List;
  6. import org.atlasapi.media.channel.ChannelGroup;
  7. import org.atlasapi.media.channel.ChannelGroupWriter;
  8. import org.atlasapi.media.channel.ChannelResolver;
  9. import org.atlasapi.media.channel.MongoChannelGroupStore;
  10. import org.atlasapi.media.channel.MongoChannelStore;
  11. import org.atlasapi.media.channel.Region;
  12. import org.atlasapi.media.entity.Channel;
  13. import org.atlasapi.media.entity.Publisher;
  14. import org.atlasapi.persistence.ids.MongoSequentialIdGenerator;
  15. import com.google.common.base.Charsets;
  16. import com.google.common.base.Function;
  17. import com.google.common.collect.ImmutableSet;
  18. import com.google.common.collect.Iterables;
  19. import com.google.common.io.Resources;
  20. import com.google.gson.Gson;
  21. import com.google.gson.GsonBuilder;
  22. import com.metabroadcast.common.ids.IdGenerator;
  23. import com.metabroadcast.common.intl.Countries;
  24. import com.metabroadcast.common.persistence.mongo.DatabasedMongo;
  25. import com.metabroadcast.common.properties.Configurer;
  26. import com.mongodb.Mongo;
  27. import com.mongodb.MongoException;
  28. public class ChannelGroupIngestTask implements Runnable {
  29. private final Gson gson = new GsonBuilder().setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES).create();
  30. private final ChannelGroupWriter channelGroupWriter;
  31. private final ChannelResolver channelResolver;
  32. private final IdGenerator idGenerator;
  33. public ChannelGroupIngestTask(ChannelGroupWriter channelGroupWriter, ChannelResolver channelResolver, IdGenerator idGenerator) {
  34. this.channelGroupWriter = channelGroupWriter;
  35. this.channelResolver = channelResolver;
  36. this.idGenerator = idGenerator;
  37. }
  38. @Override
  39. public void run() {
  40. try {
  41. URL resource = Resources.getResource("channel-groups.json");
  42. IngestDocument document = gson.fromJson(Resources.toString(resource, Charsets.UTF_8), IngestDocument.class);
  43. for (Platform platform : document.platforms) {
  44. createPlatform(platform, Publisher.METABROADCAST);
  45. }
  46. for (Platform platform : document.bbcRegions) {
  47. createRegion(platform, Publisher.BBC);
  48. }
  49. for (Platform platform : document.itvRegions) {
  50. createRegion(platform, Publisher.ITV);
  51. }
  52. } catch (Exception e) {
  53. throw new RuntimeException(e);
  54. }
  55. }
  56. private void createPlatform(Platform platform, Publisher publisher) {
  57. ChannelGroup channelGroup = new org.atlasapi.media.channel.Platform();
  58. channelGroup.setId(idGenerator.generateRaw());
  59. channelGroup.addTitle(platform.name);
  60. channelGroup.setChannels(Iterables.transform(platform.channels, TO_CHANNEL_ID));
  61. channelGroup.setPublisher(publisher);
  62. channelGroup.setAvailableCountries(ImmutableSet.of(Countries.GB));
  63. channelGroupWriter.createOrUpdate(channelGroup);
  64. }
  65. private void createRegion(Platform platform, Publisher publisher) {
  66. ChannelGroup channelGroup = new Region();
  67. channelGroup.setId(idGenerator.generateRaw());
  68. channelGroup.addTitle(platform.name);
  69. channelGroup.setChannels(Iterables.transform(platform.channels, TO_CHANNEL_ID));
  70. channelGroup.setPublisher(publisher);
  71. channelGroup.setAvailableCountries(ImmutableSet.of(Countries.GB));
  72. channelGroupWriter.createOrUpdate(channelGroup);
  73. }
  74. private Function<String, Long> TO_CHANNEL_ID = new Function<String, Long>() {
  75. @Override
  76. public Long apply(String input) {
  77. return channelResolver.fromUri(Channel.fromFieldName(input).get().uri()).requireValue().getId();
  78. }
  79. };
  80. public static void main(String[] args) throws UnknownHostException, MongoException {
  81. DatabasedMongo mongo = new DatabasedMongo(new Mongo(Configurer.get("mongo.host").get()), Configurer.get("mongo.dbName").get());
  82. MongoChannelGroupStore store = new MongoChannelGroupStore(mongo);
  83. new ChannelGroupIngestTask(new MongoChannelGroupStore(mongo), new MongoChannelStore(mongo, store, store), new MongoSequentialIdGenerator(mongo, "channelGroup")).run();
  84. }
  85. private static class IngestDocument {
  86. private List<Platform> platforms;
  87. private List<Platform> bbcRegions;
  88. private List<Platform> itvRegions;
  89. }
  90. private static class Platform {
  91. String name;
  92. List<String> channels;
  93. }
  94. }