changeset 29:fbb232e78422

create getClaimInfo
author one
date Wed, 03 Oct 2012 13:28:00 +0900
parents 7112b826a53a
children 80b5628f17d8
files app/controllers/Claim.java app/models/ClaimModel.java app/models/NodeModel.java conf/routes logs/application.log target/scala-2.9.1/cache/compile/compile test/RequestTest.java
diffstat 7 files changed, 87 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/app/controllers/Claim.java	Wed Oct 03 12:53:37 2012 +0900
+++ b/app/controllers/Claim.java	Wed Oct 03 13:28:00 2012 +0900
@@ -1,6 +1,7 @@
 package controllers;
 
 import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.node.ObjectNode;
 
 import models.ClaimModel;
 import models.NodeModel;
@@ -9,6 +10,7 @@
 import com.tinkerpop.blueprints.Graph;
 import com.tinkerpop.blueprints.Vertex;
 
+import play.libs.Json;
 import play.mvc.BodyParser;
 import play.mvc.Controller;
 import play.mvc.Result;
@@ -82,6 +84,19 @@
 		return created();
 	}
 	
+	public static Result getClaimInfo(String id) {
+		TPGraph tpGraph = TPGraph.getInstance();
+		Graph graph = tpGraph.getGraph();
+		Vertex claimV = graph.getVertex(id); 
+		if (claimV == null) badRequest("Claim id "+id+" is not found.");
+		ClaimModel claim = new ClaimModel(claimV);
+		ObjectNode claimInfo = claim.getClaimInfoFromGraph();
+		ObjectNode result = Json.newObject();
+		result.put("status", "OK");
+		result.put("message", claimInfo.toString());
+		return ok(result);
+	}
+	
 	public static Result getClaimTree(String id) {
 		TPGraph tpGraph = TPGraph.getInstance();
 		Graph graph = tpGraph.getGraph();
@@ -89,9 +104,7 @@
 		Vertex v = graph.getVertex(id);
 		if (v == null) return badRequest("Consensus id "+ id +" is not found.");
 		ClaimModel consensusRoot = new ClaimModel(v);
-		
-		
-		
+
 		
 		
 		
--- a/app/models/ClaimModel.java	Wed Oct 03 12:53:37 2012 +0900
+++ b/app/models/ClaimModel.java	Wed Oct 03 13:28:00 2012 +0900
@@ -1,41 +1,73 @@
 package models;
 
+import java.util.ArrayList;
+
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.node.ObjectNode;
 
 import play.libs.Json;
 
 import com.tinkerpop.blueprints.Vertex;
+import com.tinkerpop.gremlin.java.GremlinPipeline;
 
 public class ClaimModel extends NodeModel {
 
 	
-
-	
 	public ClaimModel(Vertex vertex) {
 		super(vertex);
 	}
 	
-	public ObjectNode getClaimPropertyFromGraph() {
+		
+	public ObjectNode getClaimInfoFromGraph() {
 		
-		ObjectNode t = Json.newObject();
+		ObjectNode property = Json.newObject();
+		property.put(TYPE, Json.toJson(getProperty(TYPE)));
+		property.put(STATUS, Json.toJson(getProperty(STATUS)));
+		property.put(TOULMIN, Json.toJson(getProperty(TOULMIN)));
 		
+		property.put(L_AUTHOR, Json.toJson(getAuthor()));
+		property.put(MENTIONS, Json.toJson(getMentions()));
+		property.put(USERS, Json.toJson(getUsers()));
 		
-		
-		
-		
-		return null;
+		return property;
+	}
+	
+	public Object getAuthor() {
+		GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
+		pipe.start(vertex).out(L_AUTHOR);
+		if (pipe.hasNext()) return null;
+		Vertex authorV = pipe.next();
+		return authorV.getId();
+	}
+	
+	public Object[] getMentions() {
+		GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
+		pipe.start(vertex).out(L_QUESTION,L_REFUTATION,L_SUGGESTION);
+		ArrayList<Object> array = new ArrayList<Object>();
+		for (Vertex v : pipe) array.add(v.getId());
+		if (array.size() == 0) return null;
+		return array.toArray();
+	}
+	
+	public Object[] getUsers() {
+		GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
+		pipe.start(vertex).out(L_REQUEST);
+		ArrayList<Object> array = new ArrayList<Object>();
+		for (Vertex v : pipe) array.add(v.getId());
+		if (array.size() == 0) return null;
+		return array.toArray();
 	}
 	
 	
+	
 	public void setClaimProperties(JsonNode toulmin, 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
+		String title = toulmin.findPath(TITLE).getTextValue();
+		String contents = toulmin.findPath(CONTENTS).getTextValue();
+		String q = toulmin.findPath(QUALIFIER).getTextValue(); // Qualifier
+		String d = toulmin.findPath(DATA).getTextValue(); // Data
+		String w = toulmin.findPath(WARRANT).getTextValue(); // Warrant
+		String b = toulmin.findPath(BACKING).getTextValue(); // Backing
+		String r = toulmin.findPath(REBUTTLE).getTextValue(); // Rebuttle
 
 		ObjectNode t = Json.newObject();
 		t.put(TITLE, title);
@@ -47,7 +79,6 @@
 		t.put(REBUTTLE, r);
 
 		setProperty(TYPE, type);
-		setProperty(MENTIONS, null);
 		setProperty(STATUS, FAIL); // Default Status is fail.
 		setProperty(TOULMIN, t);
 	}
--- a/app/models/NodeModel.java	Wed Oct 03 12:53:37 2012 +0900
+++ b/app/models/NodeModel.java	Wed Oct 03 13:28:00 2012 +0900
@@ -31,7 +31,7 @@
 	
 	
 	/*
-	 * Claim property key.
+	 * Claim information key.
 	 */
 	public static  final String TOULMIN = "toulmin";
 	public static  final String TITLE = "title";
@@ -75,6 +75,10 @@
 	public void setProperty(String key, Object value) {
 		this.vertex.setProperty(key, value);
 	}
+
+	public Object getProperty(String key) {
+		return this.vertex.getProperty(key);
+	}
 	
 	public HashMap<Object,Object> getAllProperty() {
 		for (String key : vertex.getPropertyKeys()) {
--- a/conf/routes	Wed Oct 03 12:53:37 2012 +0900
+++ b/conf/routes	Wed Oct 03 13:28:00 2012 +0900
@@ -10,6 +10,8 @@
 GET		/users/consensus/:name		controllers.User.getUserConsensus(name: String)
 GET		/users/claims/:name		controllers.User.getUserClaims(name: String)
 
+GET		/claims/browse/:id		controllers.Claim.getClaimInfo(id: String)
+
 POST	/claims/create			controllers.Claim.crateClaim()
 POST	/claims/:mentionType/:id/create			controllers.Claim.createMention(mentionType: String ,id: String)
 
--- a/logs/application.log	Wed Oct 03 12:53:37 2012 +0900
+++ b/logs/application.log	Wed Oct 03 13:28:00 2012 +0900
@@ -1,6 +1,6 @@
-2012-10-03 04:40:32,947 - [INFO] - from play in main 
+2012-10-03 13:27:42,762 - [INFO] - from play in main 
 Listening for HTTP on port 9000...
 
-2012-10-03 04:40:40,342 - [INFO] - from play in play-akka.actor.default-dispatcher-1 
+2012-10-03 13:27:47,831 - [INFO] - from play in play-akka.actor.default-dispatcher-1 
 Application started (Dev)
 
Binary file target/scala-2.9.1/cache/compile/compile has changed
--- a/test/RequestTest.java	Wed Oct 03 12:53:37 2012 +0900
+++ b/test/RequestTest.java	Wed Oct 03 13:28:00 2012 +0900
@@ -31,9 +31,7 @@
 		createClaim(user1, users1);
 		String[] users2 = {user2};
 		createClaim(user1, users2);
-
 		JsonNode user1Claim =  getUserInfo(user1,"consensus/");
-
 		String[] users3 = {user1};
 		for (int i=0; i<user1Claim.size(); i++) {
 			int claimId = user1Claim.get(i).asInt();
@@ -41,10 +39,8 @@
 			createMention(user3, users3, NodeModel.L_REFUTATION, claimId);
 		}
 
-
 		getUserInfo(user1,"requests/");
 		getUserInfo(user1,"claims/");
-
 		getUserInfo(user1,"consensus/");
 
 		getUserInfo(user2,"claims/");
@@ -52,14 +48,24 @@
 		
 		getUserInfo(user3,"claims/");
 		getUserInfo(user3,"consensus/");
-
-		getUserInfo(user3,"requests/");
-		getUserInfo(user3,"claims/");
-		getUserInfo(user2,"consensus/");
-		getUserInfo(user3,"consensus/");
+		
+		for (int i=0; i<user1Claim.size(); i++ ) {
+			getClaimInfo(user1Claim.get(i).asInt());
+		}
 
 	}
 
+	public static JsonNode getClaimInfo(int id) {
+		final String uri = SERVER_ROOT_URI + "/claims/browse/"+id;
+		WebResource resource = Client.create().resource(uri);
+		ClientResponse response = resource.get(ClientResponse.class);
+		System.out.println(String.format("GET on [%s], status code [%d]", uri, response.getStatus()));
+		String resStr = response.getEntity(String.class);
+		System.out.println(resStr);
+		return Json.parse(resStr);
+		
+	}
+	
 	public static void createMention(String author, String[] users, String type, Object id) {
 		ObjectNode jobj = createClaimParameter(author, users);
 		final String uri = SERVER_ROOT_URI + "/claims/"+type+"/"+id+"/create";