/src/main/java/org/openshift/parks/rest/StoreResource.java

https://gitlab.com/gshipley/kohls-demo · Java · 98 lines · 78 code · 17 blank · 3 comment · 2 complexity · 630dba242ebda2866be6b0880c0d5375 MD5 · raw file

  1. package org.openshift.parks.rest;
  2. import com.mongodb.BasicDBObject;
  3. import com.mongodb.client.MongoCollection;
  4. import com.mongodb.client.MongoCursor;
  5. import org.bson.Document;
  6. import org.bson.types.ObjectId;
  7. import org.openshift.parks.domain.Coordinates;
  8. import org.openshift.parks.domain.Store;
  9. import org.openshift.parks.mongo.DBConnection;
  10. import javax.enterprise.context.RequestScoped;
  11. import javax.inject.Inject;
  12. import javax.ws.rs.GET;
  13. import javax.ws.rs.Path;
  14. import javax.ws.rs.Produces;
  15. import javax.ws.rs.QueryParam;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. @RequestScoped
  19. @Path("/stores")
  20. public class StoreResource {
  21. @Inject
  22. private DBConnection dbConnection;
  23. private Store populateStoreInformation(Document dataValue) {
  24. Store newObject = new Store();
  25. newObject.setId((ObjectId) dataValue.get("_id"));
  26. newObject.setName(dataValue.get("Address"));
  27. Coordinates cord = new Coordinates((List) dataValue.get("pos"));
  28. newObject.setPosition(cord);
  29. newObject.setLatitude(cord.getLatitude());
  30. newObject.setLongitude(cord.getLongitude());
  31. newObject.setPhoneNumber(dataValue.get("Phone Number"));
  32. return newObject;
  33. }
  34. // get all the mlb parks
  35. @GET()
  36. @Produces("application/json")
  37. public List<Store> getAllStores() {
  38. ArrayList<Store> allStoreList = new ArrayList<Store>();
  39. System.out.println("Hi..");
  40. MongoCollection stores = dbConnection.getCollection();
  41. MongoCursor<Document> cursor = stores.find().iterator();
  42. try {
  43. while (cursor.hasNext()) {
  44. allStoreList.add(this.populateStoreInformation(cursor.next()));
  45. }
  46. } finally {
  47. cursor.close();
  48. }
  49. return allStoreList;
  50. }
  51. @GET
  52. @Produces("application/json")
  53. @Path("within")
  54. public List<Store> findStoresWithin(@QueryParam("lat1") float lat1,
  55. @QueryParam("lon1") float lon1, @QueryParam("lat2") float lat2,
  56. @QueryParam("lon2") float lon2) {
  57. // The first thing we want to do is make sure the DB contains data
  58. dbConnection.checkDatabase();
  59. ArrayList<Store> allStoreList = new ArrayList<Store>();
  60. MongoCollection stores = dbConnection.getCollection();
  61. // make the query object
  62. BasicDBObject spatialQuery = new BasicDBObject();
  63. ArrayList<double[]> boxList = new ArrayList<double[]>();
  64. boxList.add(new double[] { new Float(lon2), new Float(lat2) });
  65. boxList.add(new double[] { new Float(lon1), new Float(lat1) });
  66. BasicDBObject boxQuery = new BasicDBObject();
  67. boxQuery.put("$box", boxList);
  68. spatialQuery.put("pos", new BasicDBObject("$within", boxQuery));
  69. System.out.println("Using spatial query: " + spatialQuery.toString());
  70. MongoCursor<Document> cursor = stores.find(spatialQuery).iterator();
  71. try {
  72. while (cursor.hasNext()) {
  73. allStoreList.add(this.populateStoreInformation(cursor.next()));
  74. }
  75. } finally {
  76. cursor.close();
  77. }
  78. System.out.println("Return " + allStoreList.size() + " stores");
  79. return allStoreList;
  80. }
  81. }