changeset 4:e12d0b6dbe02 draft default tip

add some files
author e085711
date Wed, 26 Sep 2012 14:43:57 +0900
parents 5a097a2336fa
children
files app/Bootstrap.java app/controllers/db/Data.java app/models/Claim.java app/models/NodeModel.java app/models/TPGraph.java lib/blueprints-core-2.2.0-SNAPSHOT.jar lib/gremlin-java-2.2.0-SNAPSHOT.jar lib/pipes-2.2.0-SNAPSHOT.jar test/CreateJson.java test/RequestTest.java
diffstat 10 files changed, 337 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/Bootstrap.java	Wed Sep 26 14:43:57 2012 +0900
@@ -0,0 +1,17 @@
+import models.TPGraph;
+import play.jobs.Job;
+import play.jobs.OnApplicationStart;
+import play.test.Fixtures;
+
+
+@OnApplicationStart
+public class Bootstrap extends Job {
+	
+	public void doJob() {
+		TPGraph tpgraph = TPGraph.getInstance();
+		tpgraph.setPath("/tmp/db");
+		tpgraph.newGraph();
+	}
+
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/controllers/db/Data.java	Wed Sep 26 14:43:57 2012 +0900
@@ -0,0 +1,92 @@
+package controllers.db;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Map;
+
+import javax.ws.rs.core.MediaType;
+
+import models.NodeModel;
+import models.TPGraph;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.sun.corba.se.impl.orbutil.graph.Node;
+import com.tinkerpop.blueprints.Graph;
+import com.tinkerpop.blueprints.Vertex;
+
+import play.mvc.Controller;
+import play.mvc.Http;
+
+public class Data extends Controller {
+
+	public static void index() {
+		
+		
+		renderText("renderText");
+	}
+	
+	
+	public static void node() throws JSONException, IOException {
+		Http.Header accept = request.headers.get("accept");
+		if (!(accept.toString()).equals("["+MediaType.APPLICATION_JSON+"]") ) {
+			renderText("please set accept application/json");
+			return;
+		}
+
+		TPGraph tpgraph = TPGraph.getInstance();
+		Graph graph = tpgraph.getGraph();
+
+		Vertex v = graph.addVertex(null);
+    	NodeModel node = new NodeModel(v);
+
+		String str="";
+		InputStream in = request.body;
+		BufferedReader br = new BufferedReader(new InputStreamReader(in));
+		String tmp ="";
+		while ( (tmp = br.readLine()) != null) str += tmp;
+    	
+		node.setPropetiesFromJson(new JSONObject(str));
+		renderJSON(node.getNodeJson().toString());
+		
+		
+	}	
+	
+
+	public static void node(String id) throws JSONException {
+		Http.Header accept = request.headers.get("accept");
+		if (!(accept.toString()).equals("["+MediaType.APPLICATION_JSON+"]") ) {
+			renderText("please set accept application/json");
+			return;
+		}
+		
+		TPGraph tpgraph = TPGraph.getInstance();
+		Graph graph = tpgraph.getGraph();
+		if (request.method.equals("GET")) {
+			Vertex v = graph.getVertex(id);
+			if (v == null) {
+				renderText("Not exist Vertex ID "+id);
+				return;
+			} else {
+				NodeModel node = new NodeModel(v);
+				node.getProperties();
+				JSONObject jobj = node.getNodeJson();
+				renderJSON(jobj.toString());
+				return;
+			}
+		}
+	}
+
+
+	public static void test(String id) {
+		renderText(id);		
+		
+	}
+	
+	
+	
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/models/Claim.java	Wed Sep 26 14:43:57 2012 +0900
@@ -0,0 +1,14 @@
+package models;
+
+import com.tinkerpop.blueprints.Vertex;
+
+public class Claim extends NodeModel {
+
+	Claim(Vertex v) {
+		super(v);
+	}
+
+	
+	
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/models/NodeModel.java	Wed Sep 26 14:43:57 2012 +0900
@@ -0,0 +1,62 @@
+package models;
+
+import java.util.HashMap;
+import java.util.Iterator;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import com.tinkerpop.blueprints.Vertex;
+
+public class NodeModel {
+
+	private Vertex vertex;
+	private Object id;
+	final String NODE_TYPE = "nodeType";
+	
+	JSONObject properties = new JSONObject();
+	
+	public NodeModel(Vertex vertex) {
+		this.vertex = vertex;
+		this.id = vertex.getId();
+	}
+	
+	public JSONObject getProperties() throws JSONException {
+		for (String key: vertex.getPropertyKeys() ) {
+			properties.put(key, vertex.getProperty(key));
+		}
+		return properties;
+	}
+	
+	public void setId(Object id) {
+		this.id = id;
+	}
+
+	public Object getId() {
+		return this.id;
+	}
+	
+	public Vertex getVertex() {
+		return this.vertex;
+	}
+	
+	public void setProperty(String key, Object value) throws JSONException {
+		properties.put(key, value);
+	}
+	
+	public void setPropetiesFromJson(JSONObject jobj) throws JSONException {
+		Iterator<String> iter = jobj.keys();
+		for (String key=iter.next(); iter.hasNext(); key=iter.next()) {
+			this.setProperty(key, jobj.get(key));
+		}
+	}
+	
+	public JSONObject getNodeJson() throws JSONException {
+		JSONObject jobj = new JSONObject();
+		jobj.put("id",this.id);
+		jobj.put("data", this.properties);
+		return jobj; 
+	}
+	
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/models/TPGraph.java	Wed Sep 26 14:43:57 2012 +0900
@@ -0,0 +1,40 @@
+package models;
+
+import com.tinkerpop.blueprints.Graph;
+import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
+
+public class TPGraph {
+
+	private static TPGraph instance = new TPGraph();
+	
+	private TPGraph() {
+		
+	}
+	
+	public static TPGraph getInstance() {
+		return instance;
+	}
+
+	private Graph graph; 
+	private String path;
+	
+	public void setPath(String path) {
+		this.path = path;
+	}
+
+	public Graph newGraph() {
+		graph = new TinkerGraph(path);
+		return graph;
+	}
+
+	public Graph getGraph() {
+		return graph;
+	}
+	
+	public void shutdownGraph() {
+		graph.shutdown();
+	}
+	
+
+	
+}
Binary file lib/blueprints-core-2.2.0-SNAPSHOT.jar has changed
Binary file lib/gremlin-java-2.2.0-SNAPSHOT.jar has changed
Binary file lib/pipes-2.2.0-SNAPSHOT.jar has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/CreateJson.java	Wed Sep 26 14:43:57 2012 +0900
@@ -0,0 +1,87 @@
+import org.json.JSONException;
+import org.json.JSONObject;
+
+
+public class CreateJson {
+
+	public static void main(String[] args) throws JSONException {
+		JSONObject user1 = makeUser("1", "miyagi");
+		JSONObject user2 = makeUser("2", "higa");
+		JSONObject user3 = makeUser("3", "kinjo");
+
+		JSONObject claim1 = makeClaim("4", "GraphDBは使いやすい","グラフの関係を使った計算がしやすい");
+		JSONObject claim2 = makeClaim("5", "RDBの方が良い","RDBには今までの経験がある");
+		JSONObject claim3 = makeClaim("6","RDBはスケールしない","スケールするRDBの方がよい");
+		
+		JSONObject rel1 = makeRelationship("Author", "4", "1");
+		JSONObject rel2 = makeRelationship("AgreementRequest", "Agree", "4", "2");
+		JSONObject rel3 = makeRelationship("AgreementRequest", "Agree","4", "3");
+		JSONObject rel4 = makeRelationship("CounterArgument", "5", "4");
+		JSONObject rel5 = makeRelationship("Author", "5", "2");
+		JSONObject rel6 = makeRelationship("AgreementRequest", "Agree","5", "1");
+		JSONObject rel7 = makeRelationship("Author", "6", "1");
+		JSONObject rel8 = makeRelationship("AgreementRequest", "Agree","6", "2");
+		JSONObject rel9 = makeRelationship("CounterArgument", "6", "5");
+		
+		JSONObject[] claim1Edges = {rel1, rel2, rel3, rel4};
+		claim1.put("Edges", claim1Edges);
+		
+		rel1.put("Node",user1);
+		rel2.put("Node", user2);
+		rel3.put("Node", user3);
+		rel4.put("Node", claim2);
+		
+		JSONObject[] claim2Edges = {rel5, rel6, rel9};
+		claim2.put("Edges", claim2Edges);
+		rel5.put("Node",user2);
+		rel6.put("Node",user1);
+		rel9.put("Node", claim3);
+		
+		JSONObject[] claim3Edges = {rel7, rel8};
+		claim3.put("Edges", claim3Edges);
+		rel7.put("Node", user1);
+		rel8.put("Node",user2);
+		
+		System.out.println(claim1.toString());
+	}
+
+	public static JSONObject makeClaim(String id, String title, String content) throws JSONException {
+		JSONObject jobj = new JSONObject();
+		jobj.put("id",id);
+		JSONObject data = new JSONObject();
+		data.put("title",title);
+		data.put("content",content);
+		data.put("nodeType","claim");
+		jobj.put("data",data);
+		return jobj;
+	}
+
+	public static JSONObject makeUser(String id,String name) throws JSONException {
+		JSONObject jobj = new JSONObject();
+		jobj.put("id", id);
+
+		JSONObject data = new JSONObject(); 
+		data.put("name",name);
+		data.put("nodeType","user");
+		jobj.put("data", data);
+		return jobj;
+	}
+	
+	public static JSONObject makeRelationship(String type, String state, String start, String end) throws JSONException {
+		JSONObject jobj = new JSONObject();
+		jobj.put("type",type);
+		jobj.put("start",start);
+		jobj.put("end",end);
+		JSONObject stateObj = new JSONObject();
+		if (state != null) stateObj.put("state",state);
+		jobj.put("data",stateObj);
+		return jobj;
+	}
+	
+	public static JSONObject makeRelationship(String type,String start, String end) throws JSONException {
+		JSONObject jobj = makeRelationship(type,null,start,end);
+		return jobj;
+	}
+	
+	
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/RequestTest.java	Wed Sep 26 14:43:57 2012 +0900
@@ -0,0 +1,25 @@
+import javax.ws.rs.core.MediaType;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.WebResource;
+
+
+public class RequestTest {
+	final static String SERVER_ROOT_URI = "http://localhost:9000/";
+
+	//	final static String neo4jEntryPointUri = "http://localhost:7474/db/data/node";
+	
+	public static void main(String[] args) {
+		final String uri = SERVER_ROOT_URI + "db/data/node";
+		WebResource resource = Client.create().resource(uri);
+		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class);
+		System.out.println(String.format("POST on [%s], status code [%d]", SERVER_ROOT_URI, response.getStatus()));
+		System.out.println(response.getEntity(String.class));
+		
+
+		
+	}
+	
+	
+}