changeset 6:86924c16b4df draft

modify MakeNode.js
author one
date Sat, 18 Aug 2012 21:40:57 +0900
parents a3dba713f114
children 5e69f162d307
files src/begin/MakeNode.java
diffstat 1 files changed, 86 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/begin/MakeNode.java	Fri Aug 17 19:18:16 2012 +0900
+++ b/src/begin/MakeNode.java	Sat Aug 18 21:40:57 2012 +0900
@@ -1,5 +1,7 @@
 package begin;
 
+import java.net.URI;
+
 import javax.ws.rs.core.MediaType;
 
 import com.sun.jersey.api.client.Client;
@@ -7,27 +9,95 @@
 import com.sun.jersey.api.client.WebResource;
 
 public class MakeNode {
+	final static String SERVER_ROOT_URI = "http://localhost:7474";
+	final static String nodeEntryPointUri = "http://localhost:7474/db/data/node";
+	
+	public static URI createNode() {
+		WebResource resource = Client.create().resource(nodeEntryPointUri);
+		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class);
+			System.out.println(String.format(
+				"POST to [%s], status code [%d], location header [%s]",
+				nodeEntryPointUri, response.getStatus(), response.getLocation()
+						.toString()));
+		return response.getLocation();
+	}
 
-	final static String nodeEntryPointUri = "http://localhost:7474/db/data/node";
+	public static ClientResponse getNode(String nodeUri) {
+		WebResource resource = Client.create().resource(nodeUri);
+		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+		System.out.println(String.format("GET on [%s], status code [%d]",nodeUri, response.getStatus() ));
+		return response;
+	}
+
+	public static void setProperty(String uri,String propertyName,String propertyValue) {
+		String propertyUri = uri + "/properties/" + propertyName;
+		WebResource resource = Client.create().resource(propertyUri); // http://localhost:7474/db/data/node/(node_id)/properties/(property_name)
+		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
+				.type(MediaType.APPLICATION_JSON)
+				.entity(toJsonStringLiteral(propertyValue))
+				.put(ClientResponse.class);
+		System.out.println(String.format("PUT to [%s], status code [%d]",propertyUri, response.getStatus()));
+	}
+
+	public static void addProperty(URI nodeUri, String propertyName,
+			String propertyValue) {
+		setProperty(nodeUri.toString(), propertyName, propertyValue);
+	}
+
+	private static String toJsonStringLiteral(String str) {
+		return "\"" + str + "\"";
+	}
+
+	private static String toJsonStringLiteral(String str1, String str2) {
+		return toJsonStringLiteral(str1) +" : " + toJsonStringLiteral(str2);
+	}
+
+	public static void createRelationship(String uri1, String uri2, String typeValue) {
+		String relationshipUri = uri1 + "/relationships";
+		WebResource resource = Client.create().resource(relationshipUri);
+		String body = "{\n"+toJsonStringLiteral("to",uri2)+"\n"+toJsonStringLiteral("type",typeValue)+"\n}\n";
+		System.out.println(body);
+		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON)
+				.entity(body)
+				.post(ClientResponse.class);
+
+		System.out.println(String.format("POST to [%s], status code [%d]",relationshipUri , response.getStatus()));
+	}
+	
+	
 	
 	public static void main(String[] args) {
 
-		/*
-		 * make node
-		 */
-		WebResource resource = Client.create().resource(nodeEntryPointUri);
-		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).entity(
-				"{}"/*Empty Node*/).post(ClientResponse.class);
-		System.out.println(String.format("POST to [%s], status code [%d], location header [%s]",nodeEntryPointUri, response.getStatus(), response.getLocation().toString()));
+		final String uri1 = "http://localhost:7474/db/data/node/1"; 
+		final String uri2 = "http://localhost:7474/db/data/node/2"; 
+		
+		createRelationship(uri1, uri2, "Respect");
+
 		
-		/*
-		 *  add property
-		 */
-		
-		
-		
-		
-		
+/*
+		URI firstNode = createNode();
+		addProperty(firstNode, "name", "Joe Strummer");
+*/
+/*
+		getNode(uri1);
+		setProperty(uri1, "name", "Jason Kidd");
+		setProperty(uri2, "name", "Magic");
+*/		
+
+	}
+
+	public static void testCreateNodeWithProperty() {
+		WebResource resource = Client.create().resource(nodeEntryPointUri);
+		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
+				.type(MediaType.APPLICATION_JSON).entity("{ \"foo\" : \"hogehoge\" }"/* Empty Node */)
+				.post(ClientResponse.class);
+			System.out.println(String.format(
+				"POST to [%s], status code [%d], location header [%s]",
+				nodeEntryPointUri, response.getStatus(), response.getLocation()
+						.toString()));
 	}
 	
+	
+	
+	
 }