From 9ea790248097545535fa7cb9aa7e45c4d3411849 Mon Sep 17 00:00:00 2001 From: Siamak Sadeghianfar Date: Thu, 18 Feb 2016 09:29:34 +0100 Subject: [PATCH 01/10] generalized the geo app --- .gitignore | 5 + pom.xml | 28 +++- .../java/org/openshift/geoapp/domain/POI.java | 31 +++++ .../mongo/DBConnection.java | 21 ++- .../rest/JaxrsConfig.java | 2 +- .../openshift/geoapp/rest/POIResource.java | 121 ++++++++++++++++++ .../org/openshift/geoapp/util/Config.java | 38 ++++++ .../openshift/mlbparks/domain/MLBPark.java | 59 --------- .../mlbparks/rest/MLBParkResource.java | 101 --------------- src/main/resources/config.properties | 8 ++ src/main/webapp/index.html | 12 +- 11 files changed, 245 insertions(+), 181 deletions(-) create mode 100644 .gitignore create mode 100644 src/main/java/org/openshift/geoapp/domain/POI.java rename src/main/java/org/openshift/{mlbparks => geoapp}/mongo/DBConnection.java (84%) rename src/main/java/org/openshift/{mlbparks => geoapp}/rest/JaxrsConfig.java (80%) create mode 100644 src/main/java/org/openshift/geoapp/rest/POIResource.java create mode 100644 src/main/java/org/openshift/geoapp/util/Config.java delete mode 100644 src/main/java/org/openshift/mlbparks/domain/MLBPark.java delete mode 100644 src/main/java/org/openshift/mlbparks/rest/MLBParkResource.java create mode 100644 src/main/resources/config.properties diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8d5f580 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/target/ +.classpath +.project +.settings +.tern-project diff --git a/pom.xml b/pom.xml index 8cbca08..7c6aaf4 100644 --- a/pom.xml +++ b/pom.xml @@ -2,10 +2,10 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 org.openshift - mlbparks + geo-app war - 1.0 - mlbparks + 1.0-SNAPSHOT + geo-app eap @@ -59,7 +59,27 @@ mongo-java-driver 2.9.1 + + commons-configuration + commons-configuration + 1.10 + + + org.antlr + stringtemplate + 4.0.2 + + + + + maven-war-plugin + + false + + + + openshift - mlbparks + geo-app maven-war-plugin diff --git a/src/main/java/org/openshift/geoapp/domain/POI.java b/src/main/java/org/openshift/geoapp/domain/POI.java new file mode 100644 index 0000000..31ef2f5 --- /dev/null +++ b/src/main/java/org/openshift/geoapp/domain/POI.java @@ -0,0 +1,31 @@ +package org.openshift.geoapp.domain; + +public class POI { + private Object info; + private Object position; + private Object id; + + public Object getPosition() { + return position; + } + + public void setPosition(Object position) { + this.position = position; + } + + public Object getId() { + return id; + } + + public void setId(Object id) { + this.id = id; + } + + public Object getInfo() { + return info; + } + + public void setInfo(Object info) { + this.info = info; + } +} diff --git a/src/main/java/org/openshift/mlbparks/mongo/DBConnection.java b/src/main/java/org/openshift/geoapp/mongo/DBConnection.java similarity index 84% rename from src/main/java/org/openshift/mlbparks/mongo/DBConnection.java rename to src/main/java/org/openshift/geoapp/mongo/DBConnection.java index 972788e..233df20 100644 --- a/src/main/java/org/openshift/mlbparks/mongo/DBConnection.java +++ b/src/main/java/org/openshift/geoapp/mongo/DBConnection.java @@ -1,4 +1,4 @@ -package org.openshift.mlbparks.mongo; +package org.openshift.geoapp.mongo; import java.io.BufferedReader; import java.io.InputStreamReader; @@ -7,8 +7,11 @@ import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; import javax.inject.Named; +import org.openshift.geoapp.util.Config; + import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBObject; @@ -19,7 +22,12 @@ @ApplicationScoped public class DBConnection { + public static final String POI_COLLECTION = "poi"; + private DB mongoDB; + + @Inject + private Config config; public DBConnection() { super(); @@ -65,20 +73,19 @@ public DB getDB() { } private void initDatabase(DB mongoDB) { - DBCollection parkListCollection = mongoDB.getCollection("teams"); - int teamsImported = 0; + DBCollection parkListCollection = mongoDB.getCollection(POI_COLLECTION); + int itemsImported = 0; if (parkListCollection.count() < 1) { System.out.println("The database is empty. We need to populate it"); try { String currentLine = new String(); - URL jsonFile = new URL( - "https://raw.githubusercontent.com/gshipley/openshift3mlbparks/master/mlbparks.json"); + URL jsonFile = new URL(config.getDataFile()); BufferedReader in = new BufferedReader(new InputStreamReader(jsonFile.openStream())); while ((currentLine = in.readLine()) != null) { parkListCollection.insert((DBObject) JSON.parse(currentLine.toString())); - teamsImported++; + itemsImported++; } - System.out.println("Successfully imported " + teamsImported + " teams."); + System.out.println("Successfully imported " + itemsImported + " items."); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/org/openshift/mlbparks/rest/JaxrsConfig.java b/src/main/java/org/openshift/geoapp/rest/JaxrsConfig.java similarity index 80% rename from src/main/java/org/openshift/mlbparks/rest/JaxrsConfig.java rename to src/main/java/org/openshift/geoapp/rest/JaxrsConfig.java index 2c0c1ce..cf5843a 100644 --- a/src/main/java/org/openshift/mlbparks/rest/JaxrsConfig.java +++ b/src/main/java/org/openshift/geoapp/rest/JaxrsConfig.java @@ -1,4 +1,4 @@ -package org.openshift.mlbparks.rest; +package org.openshift.geoapp.rest; import javax.ws.rs.ApplicationPath; import javax.ws.rs.core.Application; diff --git a/src/main/java/org/openshift/geoapp/rest/POIResource.java b/src/main/java/org/openshift/geoapp/rest/POIResource.java new file mode 100644 index 0000000..8fcd740 --- /dev/null +++ b/src/main/java/org/openshift/geoapp/rest/POIResource.java @@ -0,0 +1,121 @@ +package org.openshift.geoapp.rest; + +import java.util.ArrayList; +import java.util.List; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; + +import org.openshift.geoapp.domain.POI; +import org.openshift.geoapp.mongo.DBConnection; +import org.openshift.geoapp.util.Config; +import org.stringtemplate.v4.ST; + +import com.mongodb.BasicDBObject; +import com.mongodb.DB; +import com.mongodb.DBCollection; +import com.mongodb.DBCursor; +import com.mongodb.DBObject; + +@RequestScoped +@Path("/poi") +public class POIResource { + + @Inject + private DBConnection dbConnection; + + @Inject + private Config config; + + private DBCollection getPOICollection() { + DB db = dbConnection.getDB(); + DBCollection poiListCollection = db.getCollection(DBConnection.POI_COLLECTION); + + return poiListCollection; + } + + private POI populatePoiInformation(DBObject dataValue) { + POI poi = new POI(); + + poi.setId(dataValue.get("_id")); + poi.setPosition(getPosition(dataValue)); + + ST info = new ST(config.getPopupTemplate(), '{', '}'); + for (String field : config.getDataFields()) { + info.add(field, dataValue.get(field)); + } + poi.setId(info.render()); + + return poi; + } + + private Object getPosition(DBObject dataValue) { + if (dataValue.containsField("position")) { + return dataValue.get("position"); + } else if (dataValue.containsField("pos")) { + return dataValue.get("pos"); + } else if (dataValue.containsField("location")) { + return dataValue.get("location"); + } + + return null; + } + + // get all the POIs + @GET() + @Produces("application/json") + public List getAllPOI() { + ArrayList allPOIList = new ArrayList<>(); + + DBCollection poiCollection = this.getPOICollection(); + DBCursor cursor = poiCollection.find(); + try { + while (cursor.hasNext()) { + allPOIList.add(this.populatePoiInformation(cursor.next())); + } + } finally { + cursor.close(); + } + + return allPOIList; + } + + @GET + @Produces("application/json") + @Path("within") + public List findPOIWithin(@QueryParam("lat1") float lat1, + @QueryParam("lon1") float lon1, @QueryParam("lat2") float lat2, + @QueryParam("lon2") float lon2) { + + ArrayList allPOIList = new ArrayList<>(); + DBCollection poiList = this.getPOICollection(); + + // make the query object + BasicDBObject spatialQuery = new BasicDBObject(); + + ArrayList boxList = new ArrayList(); + boxList.add(new double[] { new Float(lon2), new Float(lat2) }); + boxList.add(new double[] { new Float(lon1), new Float(lat1) }); + + BasicDBObject boxQuery = new BasicDBObject(); + boxQuery.put("$box", boxList); + + spatialQuery.put("coordinates", new BasicDBObject("$within", boxQuery)); + System.out.println("Using spatial query: " + spatialQuery.toString()); + + DBCursor cursor = poiList.find(spatialQuery); + try { + while (cursor.hasNext()) { + allPOIList.add(this.populatePoiInformation(cursor.next())); + } + } finally { + cursor.close(); + } + + return allPOIList; + } +} diff --git a/src/main/java/org/openshift/geoapp/util/Config.java b/src/main/java/org/openshift/geoapp/util/Config.java new file mode 100644 index 0000000..53df866 --- /dev/null +++ b/src/main/java/org/openshift/geoapp/util/Config.java @@ -0,0 +1,38 @@ +package org.openshift.geoapp.util; + +import java.util.Arrays; +import java.util.List; + +import javax.annotation.PostConstruct; +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Named; + +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.PropertiesConfiguration; + +@Named +@ApplicationScoped +public class Config { + private static final String DATA_POPUP_TEMPLATE = "data.popup.template"; + private static final String DATA_FILE = "data.file"; + private static final String DATA_FIELDS = "data.fields"; + + private PropertiesConfiguration config; + + @PostConstruct + public void init() throws ConfigurationException { + config = new PropertiesConfiguration("config.properties"); + } + + public String getDataFile() { + return config.getString(DATA_FILE, ""); + } + + public List getDataFields() { + return Arrays.asList(config.getStringArray(DATA_FIELDS)); + } + + public String getPopupTemplate() { + return config.getString(DATA_POPUP_TEMPLATE); + } +} diff --git a/src/main/java/org/openshift/mlbparks/domain/MLBPark.java b/src/main/java/org/openshift/mlbparks/domain/MLBPark.java deleted file mode 100644 index 1bb7d10..0000000 --- a/src/main/java/org/openshift/mlbparks/domain/MLBPark.java +++ /dev/null @@ -1,59 +0,0 @@ -package org.openshift.mlbparks.domain; - -public class MLBPark { - private Object name; - private Object position; - private Object id; - private Object ballpark; - private Object payroll; - private Object league; - - public Object getName() { - return name; - } - - public void setName(Object name) { - this.name = name; - } - - public Object getPosition() { - return position; - } - - public void setPosition(Object position) { - this.position = position; - } - - public Object getId() { - return id; - } - - public void setId(Object id) { - this.id = id; - } - - public Object getBallpark() { - return ballpark; - } - - public void setBallpark(Object ballpark) { - this.ballpark = ballpark; - } - - public Object getPayroll() { - return payroll; - } - - public void setPayroll(Object payroll) { - this.payroll = payroll; - } - - public Object getLeague() { - return league; - } - - public void setLeague(Object league) { - this.league = league; - } - -} diff --git a/src/main/java/org/openshift/mlbparks/rest/MLBParkResource.java b/src/main/java/org/openshift/mlbparks/rest/MLBParkResource.java deleted file mode 100644 index 480906a..0000000 --- a/src/main/java/org/openshift/mlbparks/rest/MLBParkResource.java +++ /dev/null @@ -1,101 +0,0 @@ -package org.openshift.mlbparks.rest; - -import java.util.ArrayList; -import java.util.List; - -import javax.enterprise.context.RequestScoped; -import javax.inject.Inject; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; - -import org.openshift.mlbparks.domain.MLBPark; -import org.openshift.mlbparks.mongo.DBConnection; - -import com.mongodb.BasicDBObject; -import com.mongodb.DB; -import com.mongodb.DBCollection; -import com.mongodb.DBCursor; -import com.mongodb.DBObject; - -@RequestScoped -@Path("/parks") -public class MLBParkResource { - - @Inject - private DBConnection dbConnection; - - private DBCollection getMLBParksCollection() { - DB db = dbConnection.getDB(); - DBCollection parkListCollection = db.getCollection("teams"); - - return parkListCollection; - } - - private MLBPark populateParkInformation(DBObject dataValue) { - MLBPark thePark = new MLBPark(); - thePark.setName(dataValue.get("name")); - thePark.setPosition(dataValue.get("coordinates")); - thePark.setId(dataValue.get("_id").toString()); - thePark.setBallpark(dataValue.get("ballpark")); - thePark.setLeague(dataValue.get("league")); - thePark.setPayroll(dataValue.get("payroll")); - - return thePark; - } - - // get all the mlb parks - @GET() - @Produces("application/json") - public List getAllParks() { - ArrayList allParksList = new ArrayList(); - - DBCollection mlbParks = this.getMLBParksCollection(); - DBCursor cursor = mlbParks.find(); - try { - while (cursor.hasNext()) { - allParksList.add(this.populateParkInformation(cursor.next())); - } - } finally { - cursor.close(); - } - - return allParksList; - } - - @GET - @Produces("application/json") - @Path("within") - public List findParksWithin(@QueryParam("lat1") float lat1, - @QueryParam("lon1") float lon1, @QueryParam("lat2") float lat2, - @QueryParam("lon2") float lon2) { - - ArrayList allParksList = new ArrayList(); - DBCollection mlbParks = this.getMLBParksCollection(); - - // make the query object - BasicDBObject spatialQuery = new BasicDBObject(); - - ArrayList boxList = new ArrayList(); - boxList.add(new double[] { new Float(lon2), new Float(lat2) }); - boxList.add(new double[] { new Float(lon1), new Float(lat1) }); - - BasicDBObject boxQuery = new BasicDBObject(); - boxQuery.put("$box", boxList); - - spatialQuery.put("coordinates", new BasicDBObject("$within", boxQuery)); - System.out.println("Using spatial query: " + spatialQuery.toString()); - - DBCursor cursor = mlbParks.find(spatialQuery); - try { - while (cursor.hasNext()) { - allParksList.add(this.populateParkInformation(cursor.next())); - } - } finally { - cursor.close(); - } - - return allParksList; - } -} diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties new file mode 100644 index 0000000..6a0086f --- /dev/null +++ b/src/main/resources/config.properties @@ -0,0 +1,8 @@ +# JSON file containing the data +data.file=https://raw.githubusercontent.com/gshipley/openshift3mlbparks/master/mlbparks.json + +# Comma separated fields from the JSON object that should be displayed on the map +data.fields=name,coordinates,ballpark,league,payroll + +# Popup template to display for each pin on the map +data.popup.template={name}
{ballpark}
Team Payroll: {payroll}
League: {league}
\ No newline at end of file diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index dc9b42f..b3f0346 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -31,7 +31,7 @@ -

MLB Stadiums on OpenShift 3

+

GeoApp on OpenShift 3