/cloudFoundry/kafana-spring-mongodb/src/main/java/be/kafana/foursquare/down/task/GrabTask.java

https://github.com/mitemitreski/CloudPlayground · Java · 76 lines · 55 code · 19 blank · 2 comment · 2 complexity · 1e8e9b0ae862ef4fd7ca4386345556bb MD5 · raw file

  1. package be.kafana.foursquare.down.task;
  2. import java.util.Iterator;
  3. import java.util.Set;
  4. import org.apache.commons.logging.Log;
  5. import org.apache.commons.logging.LogFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.data.mongodb.MongoDbFactory;
  8. import org.springframework.data.mongodb.core.MongoTemplate;
  9. import org.springframework.scheduling.annotation.Scheduled;
  10. import org.springframework.stereotype.Service;
  11. import be.kafana.foursquare.down.VenueGrabber;
  12. import be.kafana.foursquare.down.data.Photo;
  13. import be.kafana.foursquare.down.data.Venue;
  14. import com.google.gson.Gson;
  15. @Service
  16. public class GrabTask {
  17. @Autowired(required = true)
  18. private MongoDbFactory mongoDbFactory;
  19. @Autowired(required = true)
  20. private VenueGrabber venueGrabber;
  21. @Autowired(required = true)
  22. private MongoTemplate mongoTemplate;
  23. private static final Log LOGGER = LogFactory.getLog(GrabTask.class);
  24. public void doDownload() {
  25. // FIXME
  26. }
  27. @Scheduled(cron = "0 59 14 * * *")
  28. public void doGrab() {
  29. // part of SK
  30. grabAndSaveVenues(41.996243, 21.336708, 42.008999, 21.413612);
  31. }
  32. private void grabAndSaveVenues(double startLatitiude,
  33. double startLongitude,
  34. double endLatitude,
  35. double endLongitude) {
  36. Gson gson = new Gson();
  37. LOGGER.info("Started grabbing ");
  38. createCollection(Venue.class);
  39. Set<Venue> venues = venueGrabber.grab(startLatitiude, startLongitude, endLatitude,
  40. endLongitude, 0.01, 100000);
  41. for (Iterator<Venue> iterator = venues.iterator(); iterator.hasNext();) {
  42. Venue venue = iterator.next();
  43. mongoTemplate.save(venue);
  44. System.out.println(gson.toJson(venue));
  45. }
  46. LOGGER.info("Finished grabbing");
  47. }
  48. public void createCollection(Class<?> clazz) {
  49. if (!mongoTemplate.collectionExists(clazz)) {
  50. mongoTemplate.createCollection(clazz);
  51. }
  52. }
  53. public void grabAndSavePhotos() {
  54. LOGGER.info("Started grabbing ");
  55. createCollection(Photo.class);
  56. }
  57. }