Skip to content

Commit

Permalink
Sprint 2 code base
Browse files Browse the repository at this point in the history
  • Loading branch information
rladines committed Jun 22, 2015
1 parent 94fc5fc commit 42a385d
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 58 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#OpenGrid Service Changelog


##0.0.1-Sprint 2-dev (master)
* Saving and retrieval of query definition


##0.0.1-Sprint 1-dev (master)

### Initial commit for Sprint 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,17 @@ public String executeOpenGridQueryWithParams(@PathParam("datasetId") final Strin
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML, MediaType.TEXT_PLAIN})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML, MediaType.TEXT_PLAIN})
@Path("/queries")
public String getOpenGridQueriesList();
public String getOpenGridQueriesList(@QueryParam("q") final String filter,
@QueryParam("n") final int max,
@QueryParam("s") final String sort);

@POST
@Description(value="Resource", target="Doctarget.RESOURCE")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML, MediaType.TEXT_PLAIN})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML, MediaType.TEXT_PLAIN})
@Path("/queries")
public String addOpenGridNewQuery();
public String addOpenGridNewQuery(final String requestBody);
//public String addOpenGridNewQuery(@QueryParam("o") final String entity);

@GET
@Description(value="Resource", target="Doctarget.RESOURCE")
Expand All @@ -227,7 +230,8 @@ public String executeOpenGridQueryWithParams(@PathParam("datasetId") final Strin
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML, MediaType.TEXT_PLAIN})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML, MediaType.TEXT_PLAIN})
@Path("/queries/{queryId}")
public OpenGridResult updateOpenGridOneQuery(@PathParam("queryId") final String queryId);
public OpenGridResult updateOpenGridOneQuery(@PathParam("queryId") final String queryId,
@QueryParam("o") final String entity);


@DELETE
Expand Down
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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public final class DB {
static public final String DATA_COLLECTION_NAME = "opengrid_data";
static public final String META_COLLECTION_NAME = "opengrid_meta";
static public final String QUERY_COLLECTION_NAME = "opengrid_query";
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.opengrid.service.OpenGridException;

public interface DataProvider {
public interface Retrievable {
String getId();
String getData(String filter, int max, String sort) throws OpenGridException;
String getDescriptor() throws OpenGridException;
Expand Down
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;
}
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();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.opengrid.data.impl;

import org.opengrid.data.DataProvider;
import org.opengrid.data.Retrievable;
import org.opengrid.service.OpenGridException;
import org.opengrid.util.FileUtil;

public class TwitterFileDataProvider implements DataProvider {
public class TwitterFileDataProvider implements Retrievable {

@Override
public String getData(String filter, int max, String sort) throws OpenGridException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;

import org.opengrid.data.DataProvider;
import org.opengrid.data.Retrievable;
import org.opengrid.data.MongoDBCollection;
import org.opengrid.service.OpenGridException;

Expand All @@ -15,7 +15,7 @@
import com.mongodb.DBObject;
import com.mongodb.util.JSON;

public class TwitterMongoDataProvider implements DataProvider {
public class TwitterMongoDataProvider implements Retrievable {

@Override
public String getData(String filter, int max, String sort) throws OpenGridException {
Expand Down Expand Up @@ -52,7 +52,7 @@ public String getData(String filter, int max, String sort) throws OpenGridExcept
i++;

//temp limit
//if (i==1000)
//if (i==500)
// break;
}
sb.append("],");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.opengrid.data.impl;

import org.opengrid.data.DataProvider;
import org.opengrid.data.Retrievable;
import org.opengrid.service.OpenGridException;
import org.opengrid.util.FileUtil;

public class WeatherFileDataProvider implements DataProvider {
public class WeatherFileDataProvider implements Retrievable {


@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;

import org.opengrid.data.DataProvider;
import org.opengrid.data.Retrievable;
import org.opengrid.data.MongoDBCollection;
import org.opengrid.service.OpenGridException;

Expand All @@ -15,7 +15,7 @@
import com.mongodb.DBObject;
import com.mongodb.util.JSON;

public class WeatherMongoDataProvider implements DataProvider {
public class WeatherMongoDataProvider implements Retrievable {

@Override
public String getData(String filter, int max, String sort) throws OpenGridException {
Expand Down
Loading

0 comments on commit 42a385d

Please sign in to comment.