changeset 36:5f7fcdf98380

create editClaim
author one
date Thu, 04 Oct 2012 00:01:40 +0900
parents 5d422941b702
children bc3ac73320f9
files app/controllers/Claim.java app/models/ClaimModel.java app/models/NodeModel.java app/models/TPGraph.java app/models/UserModel.java conf/routes logs/application.log target/.history target/scala-2.9.1/cache/compile/copy-resources test/RequestTest.java
diffstat 10 files changed, 120 insertions(+), 122 deletions(-) [+]
line wrap: on
line diff
--- a/app/controllers/Claim.java	Wed Oct 03 18:43:49 2012 +0900
+++ b/app/controllers/Claim.java	Thu Oct 04 00:01:40 2012 +0900
@@ -36,7 +36,7 @@
 		tpGraph.setLabelToAuthor(newClaim, author);
 		newClaim.setClaimProperties(toulmin, type);
 		String[] users = toStringArray(usersJson);
-		tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.FAIL);
+		Boolean flag = tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.FAIL);
 		tpGraph.setLabelToRootClaim(newClaim);
 		return created();
 	}
@@ -47,9 +47,12 @@
 		String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
 		TPGraph tpGraph = TPGraph.getInstance();
 		Graph graph = tpGraph.getGraph();
-		Vertex claimVertex = graph.getVertex(id);
-		if ( claimVertex == null ) {
-			return badRequest("Claim id "+ id + "is not exist.");		
+		ClaimModel claim = new ClaimModel(graph.getVertex(id));
+		if ( claim.getVertex() == null ) {
+			return badRequest("Claim id "+ id + "does not exist.");
+		} 
+		if ( !claim.getAuthorId().equals(author)) {
+			return badRequest("Wrong Author.");
 		}
 		if ( graph.getVertex(author) == null) { 
 			return badRequest("Author "+ author + "is not exist.");
@@ -58,13 +61,11 @@
 		if (toulmin.findPath(NodeModel.TITLE) == null) { 
 			return badRequest("Please set title");
 		}
-		JsonNode usersJson = json.get(NodeModel.USERS);  
+		JsonNode usersJson = json.get(NodeModel.USERS);
 		String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)		
-		ClaimModel claim = new ClaimModel(claimVertex);
-		tpGraph.setLabelToAuthor(claim, author);
 		claim.setClaimProperties(toulmin, type);
-		String[] users = toStringArray(usersJson);
-		tpGraph.setLabelStatusToUsers(claim, users, NodeModel.L_REQUEST, NodeModel.FAIL);
+		Object[] users = toStringObject(usersJson);
+		claim.editRequestsEdgeUsers(users);
 		tpGraph.setLabelToRootClaim(claim);
 		return created();
 	}
@@ -111,7 +112,6 @@
 		ClaimModel claim = new ClaimModel(claimV);
 		ObjectNode claimInfo = claim.getSimpleClaimInfo();
 		ObjectNode result = Json.newObject();
-		result.put("status", "OK");
 		result.put("message", claimInfo);
 		return ok(result);
 	}
@@ -127,10 +127,18 @@
 		ObjectNode resultEntity = consensusRoot.getClaimInfoTraverse();
 		return ok(resultEntity);
 	}
-	
 
-		
-	
+	private static Object[] toStringObject(JsonNode jsonNode) {
+		int length = jsonNode.size();
+		if (length == 0) {
+			return null;
+		}
+		Object[] userArray = new Object[length];
+		for (int i=0; i<length; i++ ) {
+			userArray[i] = jsonNode.get(i).getTextValue();
+		}
+		return userArray;
+	}
 	
 	private static String[] toStringArray(JsonNode jsonNode) {
 		int length = jsonNode.size();
--- a/app/models/ClaimModel.java	Wed Oct 03 18:43:49 2012 +0900
+++ b/app/models/ClaimModel.java	Thu Oct 04 00:01:40 2012 +0900
@@ -1,6 +1,8 @@
 package models;
 
 import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
 
 import org.codehaus.jackson.JsonNode;
 import org.codehaus.jackson.node.ObjectNode;
@@ -8,6 +10,7 @@
 import play.libs.Json;
 
 import com.tinkerpop.blueprints.Edge;
+import com.tinkerpop.blueprints.Graph;
 import com.tinkerpop.blueprints.Vertex;
 import com.tinkerpop.gremlin.java.GremlinPipeline;
 
@@ -91,11 +94,27 @@
 		return array.toArray();
 	}
 
-
 	public Object[] getRequestUsersId() {
 		return getInfoArray(NodeModel.REQUESTS);
 	}
 	
+	public void editRequestsEdgeUsers(Object[] updateUsers) {
+		TPGraph tpGraph = TPGraph.getInstance();
+		Object[] currentUsers = getUsersId();
+		HashSet<Object> currentUsersHashSet = new HashSet<Object>();
+		for (Object u : currentUsers) {
+			currentUsersHashSet.add(u);
+		}
+		for (Object updateUser : updateUsers) {
+			if (currentUsersHashSet.contains(updateUser)) {
+				currentUsersHashSet.remove(updateUser);
+			} else {
+				tpGraph.setLabelStatusToUser(this, updateUser.toString(), L_REQUEST, UNKNOWN);
+			}
+		}
+		tpGraph.deleteRequestEdge(this, currentUsersHashSet);
+	}
+	
 	public Object getAuthorId() {
 		GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
 		pipe.start(vertex).out(L_AUTHOR);
--- a/app/models/NodeModel.java	Wed Oct 03 18:43:49 2012 +0900
+++ b/app/models/NodeModel.java	Thu Oct 04 00:01:40 2012 +0900
@@ -48,6 +48,7 @@
 	public static  final String FAIL = "fail";
 	public static  final String AGREED = "agreed";
 	public static  final String DENIED = "denied"; 
+	public static  final String UNKNOWN = "unknown"; 
 		
 	// Use This key Json Data
 	public static  final String CLAIM = "claim";
@@ -57,7 +58,11 @@
 	
 	public NodeModel(Vertex vertex) {
 		this.vertex = vertex;
-		this.id = vertex.getId();
+		if (vertex == null) {
+			this.id = null;
+		} else {
+			this.id = vertex.getId();
+		}
 	}
 
 	public void setId(Object id) {
--- a/app/models/TPGraph.java	Wed Oct 03 18:43:49 2012 +0900
+++ b/app/models/TPGraph.java	Thu Oct 04 00:01:40 2012 +0900
@@ -106,12 +106,22 @@
 		return true;
 	}
 	
+	public Boolean setLabelStatusToUser(ClaimModel claim, String userName, String label, String status) {
+		Vertex userVertex = graph.getVertex(userName);
+		if (userVertex == null) { 
+			return false;
+		}
+		Edge edge = setLabel(claim.getVertex(), userVertex, label);
+		edge.setProperty(NodeModel.STATUS, status);
+		return true;
+	}
+	
 	public Boolean setLabelStatusToUsers(ClaimModel claim, String[] users, String label, String status) {	
 		for (String userName: users) {
-			Vertex userVertex = graph.getVertex(userName);
-			if (userVertex == null) return false;
-			Edge edge = setLabel(claim.getVertex(), userVertex, label);
-			edge.setProperty(NodeModel.STATUS, status);
+			Boolean createFlag = setLabelStatusToUser(claim, userName, label, status);
+			if (!createFlag) {
+				return false;
+			}
 		}
 		return true;
 	}
@@ -120,6 +130,24 @@
 		return setLabel(fromClaim.getVertex(), toClaim.getVertex(), label);		
 	}
 	
+	public Boolean deleteRequestEdge(ClaimModel claim, HashSet<Object> userSet) {
+		GremlinPipeline<Vertex,Edge> pipeEdge = new GremlinPipeline<Vertex,Edge>();		
+		pipeEdge.start(claim.getVertex()).outE(NodeModel.L_REQUEST);
+		ArrayList<Edge> deleteEdgeArray = new ArrayList<Edge>();
+		for (Edge e : pipeEdge) {
+			GremlinPipeline<Edge,Vertex> pipeUserVertex = new GremlinPipeline<Edge,Vertex>();		
+			pipeUserVertex.start(e).inV();
+			Vertex userVertex = pipeUserVertex.next();
+			if (userSet.contains(userVertex.getId())) {
+				deleteEdgeArray.add(e);
+			}			
+		}
+		for (Edge e : deleteEdgeArray) {
+			graph.removeEdge(e);			
+		}
+		return true;
+	}
+	
 	public Object[] checkConsensus(HashSet<Object> set) {
 		Iterator<Object> iter = set.iterator();
 		while (iter.hasNext()) {
--- a/app/models/UserModel.java	Wed Oct 03 18:43:49 2012 +0900
+++ b/app/models/UserModel.java	Thu Oct 04 00:01:40 2012 +0900
@@ -48,7 +48,7 @@
 		if (set.size() == 0) return null;
 		return set;
 	}
-
+	
 	/*
 	public Object[] getUserConsensus() {
 		return null;
--- a/conf/routes	Wed Oct 03 18:43:49 2012 +0900
+++ b/conf/routes	Thu Oct 04 00:01:40 2012 +0900
@@ -15,6 +15,7 @@
 
 POST	/claims/create				controllers.Claim.crateClaim()
 POST	/claims/:mentionType/:id/create			controllers.Claim.createMention(mentionType: String ,id: String)
+POST	/claims/edit/:id			controllers.Claim.editClaim(id: String)
 GET		/consensus/browse/:id		controllers.Claim.getClaimTree(id: String)
 
 
--- a/logs/application.log	Wed Oct 03 18:43:49 2012 +0900
+++ b/logs/application.log	Thu Oct 04 00:01:40 2012 +0900
@@ -1,94 +1,12 @@
-2012-10-03 16:31:09,013 - [INFO] - from play in main 
+2012-10-03 23:52:13,026 - [INFO] - from play in main 
 Listening for HTTP on port 9000...
 
-2012-10-03 16:31:15,139 - [INFO] - from play in play-akka.actor.default-dispatcher-2 
-Application started (Dev)
-
-2012-10-03 16:31:45,206 - [INFO] - from application in play-akka.actor.default-dispatcher-1 
-Application shutdown...
-
-2012-10-03 16:31:45,229 - [INFO] - from play in play-akka.actor.default-dispatcher-1 
-Application started (Dev)
-
-2012-10-03 16:35:57,771 - [INFO] - from application in play-akka.actor.default-dispatcher-1 
-Application shutdown...
-
-2012-10-03 16:35:57,790 - [INFO] - from play in play-akka.actor.default-dispatcher-1 
+2012-10-03 23:52:27,823 - [INFO] - from play in play-akka.actor.default-dispatcher-2 
 Application started (Dev)
 
-2012-10-03 16:40:03,578 - [INFO] - from application in play-akka.actor.default-dispatcher-1 
-Application shutdown...
-
-2012-10-03 16:40:03,595 - [INFO] - from play in play-akka.actor.default-dispatcher-1 
-Application started (Dev)
-
-2012-10-03 16:40:04,414 - [ERROR] - from application in play-akka.actor.actions-dispatcher-8 
-
-
-! @6bpi0n84b - Internal server error, for request [GET /claims/browse/10] ->
-
-play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[FastNoSuchElementException: null]]
-	at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:134) [play_2.9.1.jar:2.0.3]
-	at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115) [play_2.9.1.jar:2.0.3]
-	at akka.actor.Actor$class.apply(Actor.scala:318) [akka-actor.jar:2.0.2]
-	at play.core.ActionInvoker.apply(Invoker.scala:113) [play_2.9.1.jar:2.0.3]
-	at akka.actor.ActorCell.invoke(ActorCell.scala:626) [akka-actor.jar:2.0.2]
-	at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197) [akka-actor.jar:2.0.2]
-	at akka.dispatch.Mailbox.run(Mailbox.scala:179) [akka-actor.jar:2.0.2]
-	at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:516) [akka-actor.jar:2.0.2]
-	at akka.jsr166y.ForkJoinTask.doExec(ForkJoinTask.java:259) [akka-actor.jar:2.0.2]
-	at akka.jsr166y.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:975) [akka-actor.jar:2.0.2]
-	at akka.jsr166y.ForkJoinPool.runWorker(ForkJoinPool.java:1479) [akka-actor.jar:2.0.2]
-	at akka.jsr166y.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104) [akka-actor.jar:2.0.2]
-com.tinkerpop.pipes.util.FastNoSuchElementException: null
-
-2012-10-03 16:41:51,310 - [INFO] - from application in play-akka.actor.default-dispatcher-1 
+2012-10-03 23:55:53,105 - [INFO] - from application in play-akka.actor.default-dispatcher-2 
 Application shutdown...
 
-2012-10-03 16:41:51,328 - [INFO] - from play in play-akka.actor.default-dispatcher-1 
-Application started (Dev)
-
-2012-10-03 16:42:05,908 - [INFO] - from application in play-akka.actor.default-dispatcher-1 
-Application shutdown...
-
-2012-10-03 16:42:05,926 - [INFO] - from play in play-akka.actor.default-dispatcher-1 
+2012-10-03 23:55:53,125 - [INFO] - from play in play-akka.actor.default-dispatcher-2 
 Application started (Dev)
 
-2012-10-03 16:42:06,744 - [ERROR] - from application in play-akka.actor.actions-dispatcher-8 
-
-
-! @6bpi0n84c - Internal server error, for request [GET /claims/browse/10] ->
-
-play.core.ActionInvoker$$anonfun$receive$1$$anon$1: Execution exception [[FastNoSuchElementException: null]]
-	at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:134) [play_2.9.1.jar:2.0.3]
-	at play.core.ActionInvoker$$anonfun$receive$1.apply(Invoker.scala:115) [play_2.9.1.jar:2.0.3]
-	at akka.actor.Actor$class.apply(Actor.scala:318) [akka-actor.jar:2.0.2]
-	at play.core.ActionInvoker.apply(Invoker.scala:113) [play_2.9.1.jar:2.0.3]
-	at akka.actor.ActorCell.invoke(ActorCell.scala:626) [akka-actor.jar:2.0.2]
-	at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:197) [akka-actor.jar:2.0.2]
-	at akka.dispatch.Mailbox.run(Mailbox.scala:179) [akka-actor.jar:2.0.2]
-	at akka.dispatch.ForkJoinExecutorConfigurator$MailboxExecutionTask.exec(AbstractDispatcher.scala:516) [akka-actor.jar:2.0.2]
-	at akka.jsr166y.ForkJoinTask.doExec(ForkJoinTask.java:259) [akka-actor.jar:2.0.2]
-	at akka.jsr166y.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:975) [akka-actor.jar:2.0.2]
-	at akka.jsr166y.ForkJoinPool.runWorker(ForkJoinPool.java:1479) [akka-actor.jar:2.0.2]
-	at akka.jsr166y.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:104) [akka-actor.jar:2.0.2]
-com.tinkerpop.pipes.util.FastNoSuchElementException: null
-
-2012-10-03 16:43:42,491 - [INFO] - from application in play-akka.actor.default-dispatcher-1 
-Application shutdown...
-
-2012-10-03 16:43:42,508 - [INFO] - from play in play-akka.actor.default-dispatcher-1 
-Application started (Dev)
-
-2012-10-03 16:52:28,593 - [INFO] - from application in play-akka.actor.default-dispatcher-1 
-Application shutdown...
-
-2012-10-03 16:52:28,609 - [INFO] - from play in play-akka.actor.default-dispatcher-1 
-Application started (Dev)
-
-2012-10-03 16:54:26,214 - [INFO] - from application in play-akka.actor.default-dispatcher-1 
-Application shutdown...
-
-2012-10-03 16:54:26,229 - [INFO] - from play in play-akka.actor.default-dispatcher-1 
-Application started (Dev)
-
--- a/target/.history	Wed Oct 03 18:43:49 2012 +0900
+++ b/target/.history	Thu Oct 04 00:01:40 2012 +0900
@@ -3,3 +3,11 @@
 run
 ~ run 
 ~ run
+run
+console
+help
+help comand
+help command
+help command*
+play starting
+run
Binary file target/scala-2.9.1/cache/compile/copy-resources has changed
--- a/test/RequestTest.java	Wed Oct 03 18:43:49 2012 +0900
+++ b/test/RequestTest.java	Thu Oct 04 00:01:40 2012 +0900
@@ -28,6 +28,11 @@
 		String[] users2 = {user2};
 		createClaim(user1, users2);
 		JsonNode user1Claim =  getUserInfo(user1,"consensus/");
+		getClaimInfo(user1Claim.get(0).asInt());
+		editClaimInfo(user1, users2, user1Claim.get(0).asInt());
+		getClaimInfo(user1Claim.get(0).asInt()); 		
+		
+/*
 		String[] users3 = {user1};
 		for (int i=0; i<user1Claim.size(); i++) {
 			int claimId = user1Claim.get(i).asInt();
@@ -45,23 +50,30 @@
 			int id = user1Claim.get(i).asInt(); 
 			getClaimTree(id); 
 			getClaimInfo(user1Claim.get(i).asInt());
-			editClaimInfo(id);
-			getClaimInfo(user1Claim.get(i).asInt());			
+			editClaimInfo(user1, users1, id);
+			getClaimInfo(user1Claim.get(i).asInt());
 			break;
 		}
+		*/
 	}
 	
-	public static JsonNode editClaimInfo(String id) {
-		
-		return null;
+	public static JsonNode editClaimInfo(String author, String[] users, int id) {
+		ObjectNode jobj =  createEditClaimParameter(author,users);
+		final String uri = SERVER_ROOT_URI + "/claims/edit/"+id;
+		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()));
+		String resStr = response.getEntity(String.class);
+		System.out.println(resStr);
+		return Json.toJson(resStr);		
 	}
 
 	public static JsonNode getClaimTree(int id) {
 		final String uri = SERVER_ROOT_URI + "/consensus/browse/"+id;
 		WebResource resource = Client.create().resource(uri);
-//		ClientResponse response = resource.get(ClientResponse.class);
-		ClientResponse response = resource
-				.get(ClientResponse.class);
+		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);
@@ -105,19 +117,18 @@
 
 	public static ObjectNode createEditClaimParameter(String author, String[] users) {
 		ObjectNode toulmin = Json.newObject();
-		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,"");
+		toulmin.put(NodeModel.TITLE, "Edit Test");
+		toulmin.put(NodeModel.CONTENTS, "Edited Contents");
+		toulmin.put(NodeModel.QUALIFIER, "Edited qualifier");
+		toulmin.put(NodeModel.WARRANT,"Edited warrant");
+		toulmin.put(NodeModel.BACKING, "Edited backing");
+		toulmin.put(NodeModel.DATA,"Edited DATA");
+		toulmin.put(NodeModel.REBUTTLE,"Edited REBUTTLE");
 		ObjectNode jobj = Json.newObject();
 		jobj.put(NodeModel.TOULMIN, toulmin);
 		jobj.put(NodeModel.L_AUTHOR, author);
 		ArrayNode usersArray = jobj.putArray(NodeModel.USERS);
 		for (String u : users) usersArray.add(u);
-
 		jobj.put(NodeModel.TYPE, NodeModel.UNANIMOUSLY);
 		return jobj;
 	}