changeset 16:7cdc9d19834f

modified createClaim
author one
date Tue, 02 Oct 2012 13:52:31 +0900
parents 0adbec4c7091
children a134edaebf6f
files app/controllers/Claim.java app/controllers/User.java app/models/ClaimModel.java app/models/TPGraph.java conf/routes test/RequestTest.java
diffstat 6 files changed, 88 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/app/controllers/Claim.java	Tue Oct 02 11:41:23 2012 +0900
+++ b/app/controllers/Claim.java	Tue Oct 02 13:52:31 2012 +0900
@@ -1,6 +1,7 @@
 package controllers;
 
 import java.util.HashMap;
+import java.util.Iterator;
 
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.node.ObjectNode;
@@ -24,37 +25,35 @@
 	@BodyParser.Of(BodyParser.Json.class)
 	public static Result crateClaim() {
 		JsonNode json = request().body().asJson();
+		String author = json.findPath(NodeModel.AUTHOR).getTextValue(); // Author
+
+		TPGraph tpGraph = TPGraph.getInstance();
+		Graph graph = tpGraph.getGraph();
+		if ( graph.getVertex(author) == null) return badRequest("Author "+ author + "is not exist.");
+		
+		JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
 		ObjectNode result = Json.newObject();
-		String title = json.findPath("title").getTextValue();
-		if (title == null) {
-			result.put("status", "KO");
+		if (toulmin.findPath(NodeModel.TITLE) == null) {
 			result.put("message", "Please set title");
 			return badRequest(result);
 		} 
-		String contents = json.findPath("contents").getTextValue();
-		String author = json.findPath(NodeModel.AUTHOR).getTextValue(); // Author
-		String q = json.findPath(NodeModel.QUALIFIER).getTextValue(); // Qualifier
-		String d = json.findPath(NodeModel.DATA).getTextValue(); // Data
-		String w = json.findPath(NodeModel.WARRANT).getTextValue(); // Warrant
-		String b = json.findPath(NodeModel.BACKING).getTextValue(); // Backing
-		String r = json.findPath(NodeModel.REBUTTLE).getTextValue(); // Rebuttle
-		String users = json.get(NodeModel.USERS).toString(); // Users
+
+		JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode) 
 		String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)		
 		
-		TPGraph tpGraph = TPGraph.getInstance();
-		Graph graph = tpGraph.getGraph();
-		Vertex v = null;
-		v = graph.addVertex(null);
-		ClaimModel newClaim = new ClaimModel(v);
-		newClaim.setClaimProperties(author, title, contents, q, d, w, b, r, users, type);
+		Vertex claimVertex = null;
+		claimVertex = graph.addVertex(null);
+		ClaimModel newClaim = new ClaimModel(claimVertex);
+		newClaim.setClaimProperties(toulmin, author, usersJson.toString(), type);
+		tpGraph.updateUserVertex(claimVertex, usersJson);
 		tpGraph.setLabelToRootClaim(newClaim);
-//		newUser.setName(name); // user node hasn't name property(only TinkerGraph).
+		tpGraph.setLabelToAuthor(author, newClaim);
 
-		HashMap<Object, Object> property = newClaim.getAllProperty();
-		for (Object key: property.keySet()) {
-			System.out.println("key = "+ key + " value = "+ property.get(key));
-		}
-		
+  		 for (Iterator<JsonNode> iter = usersJson.getElements(); iter.hasNext();) {
+			 JsonNode j = iter.next();
+			 System.out.println(j.toString());
+		 }
+
 		return created();		
 
 		
--- a/app/controllers/User.java	Tue Oct 02 11:41:23 2012 +0900
+++ b/app/controllers/User.java	Tue Oct 02 13:52:31 2012 +0900
@@ -42,7 +42,7 @@
 			return notFound();
 		} else {
 			UserModel user = new UserModel(v);
-			HashMap<Object,Object> hash = user.getUserProperty();
+			HashMap<Object,Object> hash = user.getAllProperty();
 			return created(Json.toJson(hash));
 		}
 	}
--- a/app/models/ClaimModel.java	Tue Oct 02 11:41:23 2012 +0900
+++ b/app/models/ClaimModel.java	Tue Oct 02 13:52:31 2012 +0900
@@ -1,5 +1,6 @@
 package models;
 
+import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.node.ObjectNode;
 
 import play.libs.Json;
@@ -16,30 +17,30 @@
 	}
 	
 	
-	public void setClaimProperties(String author, String title, String contents, String q,
-			String d, String w, String b, String r, String users, String type) {
-		
+	public void setClaimProperties(JsonNode toulmin, String author, String users, String type) {
+		String title = toulmin.findPath(NodeModel.TITLE).getTextValue();
+		String contents = toulmin.findPath(NodeModel.CONTENTS).getTextValue();
+		String q = toulmin.findPath(NodeModel.QUALIFIER).getTextValue(); // Qualifier
+		String d = toulmin.findPath(NodeModel.DATA).getTextValue(); // Data
+		String w = toulmin.findPath(NodeModel.WARRANT).getTextValue(); // Warrant
+		String b = toulmin.findPath(NodeModel.BACKING).getTextValue(); // Backing
+		String r = toulmin.findPath(NodeModel.REBUTTLE).getTextValue(); // Rebuttle
 
-		
-		ObjectNode toulmin = Json.newObject();
-		toulmin.put(TITLE, title);
-		toulmin.put(CONTENTS, contents);
-		toulmin.put(QUALIFIER, q);
-		toulmin.put(DATA, d);
-		toulmin.put(WARRANT, w);
-		toulmin.put(BACKING, b);
-		toulmin.put(REBUTTLE, r);
+		ObjectNode t = Json.newObject();
+		t.put(TITLE, title);
+		t.put(CONTENTS, contents);
+		t.put(QUALIFIER, q);
+		t.put(DATA, d);
+		t.put(WARRANT, w);
+		t.put(BACKING, b);
+		t.put(REBUTTLE, r);
 
 		setProperty(AUTHOR,author);
 		setProperty(USERS, users);
 		setProperty(TYPE, type);
 		setProperty(MENTIONS, null);
 		setProperty(STATUS, FAIL);
-		setProperty(TOULMIN, toulmin);
-		
-		
-		
-		
+		setProperty(TOULMIN, t);
 	}
 	
 	
--- a/app/models/TPGraph.java	Tue Oct 02 11:41:23 2012 +0900
+++ b/app/models/TPGraph.java	Tue Oct 02 13:52:31 2012 +0900
@@ -1,5 +1,7 @@
 package models;
 
+import org.codehaus.jackson.JsonNode;
+
 import scala.reflect.generic.Trees.This;
 
 import com.tinkerpop.blueprints.Graph;
@@ -84,9 +86,24 @@
 		/* 
 		 *  rootUser ---child---> newUser
 		 */
-		graph.addEdge(null, rootUser, user.getVertex(), CHILD);
+		graph.addEdge(null, rootClaim, claim.getVertex(), CHILD);
 	}
 	
+	public void setLabelToAuthor(String author, ClaimModel claim) {
+		Vertex authorVertex = graph.getVertex(author);
+
+		/*
+		 *  claim ---author---> authorVertex(userVertex)
+		 */
+		graph.addEdge(null, claim.getVertex(), authorVertex, NodeModel.AUTHOR);
+		
+	}
+	
+	public Boolean updateUserVertex(Vertex claim, JsonNode usersJson) {
+
+		
+		return true;
+	}
 	
 	
 	public void shutdownGraph() {
--- a/conf/routes	Tue Oct 02 11:41:23 2012 +0900
+++ b/conf/routes	Tue Oct 02 13:52:31 2012 +0900
@@ -10,6 +10,7 @@
 GET		/users/consensus/:name		controllers.User.getUserConsensus(name: String)
 GET		/users/claims/:name		controllers.User.getUserClaims(name: String)
 
+POST	/claims/create			controllers.Claim.crateClaim()
 
 
 # test action
--- a/test/RequestTest.java	Tue Oct 02 11:41:23 2012 +0900
+++ b/test/RequestTest.java	Tue Oct 02 13:52:31 2012 +0900
@@ -1,5 +1,7 @@
 import javax.ws.rs.core.MediaType;
 
+import models.NodeModel;
+
 import org.json.JSONException;
 import org.json.JSONObject;
 
@@ -14,26 +16,43 @@
 	public static void main(String[] args) throws JSONException {
 
 		createUser("taro");
+		/*
 		getUser("taro");
 		getUserInfo("taro","requests/");
 		getUserInfo("taro","claims/");
 		getUserInfo("taro","consensus/");
-		createClaim();
+*/
+		createClaim("taro");
 
 
 		
 	}
 	
-	public static void createClaim() throws JSONException {
+	public static void createClaim(String author) throws JSONException {
 		JSONObject toulmin = new JSONObject();
-		toulmin.put("title", "アプリでGraphDBを利用する。");
-		toulmin.put("contents", "最近話題のデータベースとしてGraphDBがある。我々のアプリでは、新しい技術のためにもGraphDBを利用したい。");
-		toulmin.put("d","絶対");
-		toulmin.put("w","GraphDBの実用例 etc...");
-		toulmin.put("b", "GraphDBの最新動向 etc...");
-		toulmin.put("r","GraphDBの実用例 etc...");
-		toulmin.put("q", "");
+		toulmin.put(NodeModel.TITLE, "アプリでGraphDBを利用する。");
+		toulmin.put(NodeModel.CONTENTS, "最近話題のデータベースとしてGraphDBがある。我々のアプリでは、新しい技術のためにもGraphDBを利用したい。");
+		toulmin.put(NodeModel.QUALIFIER, "絶対");
+		toulmin.put(NodeModel.WARRANT,"GraphDBの実用例 etc...");
+		toulmin.put(NodeModel.BACKING, "GraphDBの最新動向 etc...");
+		toulmin.put(NodeModel.DATA,"GraphDBの実用例 etc...");
+		toulmin.put(NodeModel.REBUTTLE,"");
+
 		JSONObject jobj = new JSONObject();
+		jobj.put(NodeModel.TOULMIN, toulmin);
+		jobj.put(NodeModel.AUTHOR, author);
+		String[] users = {"akifumi","yosiaki"};
+		jobj.put(NodeModel.USERS,users);
+		jobj.put(NodeModel.TYPE, "unanimously");
+
+		final String uri = SERVER_ROOT_URI + "/claims/create";
+		WebResource resource = Client.create().resource(uri);
+		ClientResponse response = resource.header("Content-type",MediaType.APPLICATION_JSON)
+				.entity(jobj.toString())
+				.post(ClientResponse.class);
+		System.out.println(String.format("POST on [%s], status code [%d]", uri, response.getStatus()));
+		System.out.println(response.getEntity(String.class));		
+		
 	}
 	
 	public static void getUserInfo(String name, String preUri) {