forked from smartchicago/opengrid-svc-plenario
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
255 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
opengridservice/src/main/java/org/opengrid/constants/AppObjects.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.opengrid.constants; | ||
|
||
public final class AppObjects { | ||
static public final String QUERY = "query"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
opengridservice/src/main/java/org/opengrid/data/Updatable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.opengrid.data; | ||
|
||
import org.opengrid.service.OpenGridException; | ||
|
||
public interface Updatable { | ||
void update(String id, String entity) throws OpenGridException; | ||
} |
137 changes: 137 additions & 0 deletions
137
opengridservice/src/main/java/org/opengrid/data/impl/QueryMongoDataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package org.opengrid.data.impl; | ||
|
||
import org.opengrid.data.Retrievable; | ||
import org.opengrid.data.MongoDBCollection; | ||
import org.opengrid.data.Updatable; | ||
import org.opengrid.service.OpenGridException; | ||
|
||
import com.mongodb.BasicDBObject; | ||
import com.mongodb.DB; | ||
import com.mongodb.DBCollection; | ||
import com.mongodb.DBCursor; | ||
import com.mongodb.DBObject; | ||
import com.mongodb.util.JSON; | ||
|
||
public class QueryMongoDataProvider implements Retrievable, Updatable { | ||
|
||
@Override | ||
public String getId() { | ||
return org.opengrid.constants.AppObjects.QUERY; | ||
} | ||
|
||
@Override | ||
public String getData(String filter, int max, String sort) throws OpenGridException { | ||
//data is plain json (we'll need to standardize format across the board later) | ||
MongoDBCollection ds = new MongoDBCollection(); | ||
DB db = ds.getConnection(); | ||
|
||
try { | ||
DBCollection c = db.getCollection(org.opengrid.constants.DB.QUERY_COLLECTION_NAME); | ||
|
||
BasicDBObject q = null; | ||
|
||
if (filter !=null && filter.length() > 0) { | ||
q = (BasicDBObject) JSON.parse(filter); | ||
} else { | ||
q = (BasicDBObject) JSON.parse("{}"); //no filter | ||
} | ||
DBCursor cur = c.find(q); | ||
|
||
//return regular json object | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("["); | ||
int i=0; | ||
while(cur.hasNext()) { | ||
if (i==max) | ||
break; | ||
|
||
DBObject doc = cur.next(); | ||
if (i > 0) | ||
sb.append(","); | ||
sb.append(doc.toString()); | ||
i++; | ||
} | ||
sb.append("]"); | ||
return sb.toString(); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
|
||
//bubble up | ||
//wrap and bubble up | ||
throw new OpenGridException(ex); | ||
} finally { | ||
if (ds !=null) { | ||
ds.closeConnection(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String getDescriptor() throws OpenGridException { | ||
// N/A for queries | ||
return "{}"; | ||
} | ||
|
||
@Override | ||
public void update(String id, String entity) throws OpenGridException { | ||
//if id is null, this is a new entity being saved | ||
if (id==null) { | ||
addNewQuery(entity); | ||
} else { | ||
updateQuery(id, entity); | ||
} | ||
|
||
} | ||
|
||
private void updateQuery(String id, String entity) { | ||
MongoDBCollection ds = new MongoDBCollection(); | ||
DB db = ds.getConnection(); | ||
|
||
try { | ||
DBCollection c = db.getCollection(org.opengrid.constants.DB.QUERY_COLLECTION_NAME); | ||
BasicDBObject q = (BasicDBObject) JSON.parse("{\"_id\": {\"$eq\": " + id + "}}"); | ||
BasicDBObject d = (BasicDBObject) JSON.parse(entity); | ||
|
||
//DBObject doc = c.findOne(q); | ||
//doc.append("$set", new BasicDBObject().append("clients", 110)); | ||
|
||
c.update(q, d); | ||
|
||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
|
||
//bubble up | ||
//wrap and bubble up | ||
throw new OpenGridException(ex); | ||
} finally { | ||
if (ds !=null) { | ||
ds.closeConnection(); | ||
} | ||
} | ||
} | ||
|
||
|
||
private void addNewQuery(String entity) { | ||
MongoDBCollection ds = new MongoDBCollection(); | ||
DB db = ds.getConnection(); | ||
|
||
try { | ||
DBCollection c = db.getCollection(org.opengrid.constants.DB.QUERY_COLLECTION_NAME); | ||
BasicDBObject o = (BasicDBObject) JSON.parse(entity); | ||
|
||
c.insert((DBObject)o.get("o")); | ||
|
||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
|
||
//bubble up | ||
//wrap and bubble up | ||
throw new OpenGridException(ex); | ||
} finally { | ||
if (ds !=null) { | ||
ds.closeConnection(); | ||
} | ||
} | ||
} | ||
|
||
} |
4 changes: 2 additions & 2 deletions
4
opengridservice/src/main/java/org/opengrid/data/impl/TwitterFileDataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
opengridservice/src/main/java/org/opengrid/data/impl/WeatherFileDataProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.