annotate app/controllers/Claim.java @ 23:cad8e054fe4b

modifying getConsensus action
author one
date Tue, 02 Oct 2012 23:46:55 +0900
parents 34ea98c5a18c
children 97b249d9fad1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
1 package controllers;
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
2
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
3 import org.codehaus.jackson.JsonNode;
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
4
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
5 import models.ClaimModel;
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
6 import models.NodeModel;
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
7 import models.TPGraph;
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
8
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
9 import com.tinkerpop.blueprints.Graph;
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
10
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
11 import play.mvc.BodyParser;
11
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
12 import play.mvc.Controller;
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
13 import play.mvc.Result;
11
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
14
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
15 public class Claim extends Controller {
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
16
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
17
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
18 @BodyParser.Of(BodyParser.Json.class)
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
19 public static Result crateClaim() {
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
20 JsonNode json = request().body().asJson();
18
one
parents: 17
diff changeset
21 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
16
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
22
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
23 TPGraph tpGraph = TPGraph.getInstance();
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
24 Graph graph = tpGraph.getGraph();
18
one
parents: 17
diff changeset
25 if ( graph.getVertex(author) == null)
one
parents: 17
diff changeset
26 return badRequest("Author "+ author + "is not exist.");
16
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
27
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
28 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
18
one
parents: 17
diff changeset
29 if (toulmin.findPath(NodeModel.TITLE) == null)
one
parents: 17
diff changeset
30 return badRequest("Please set title");
16
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
31
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
32 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode)
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
33 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
12
ef434ebd64ae fix NodeModel.java
one
parents: 11
diff changeset
34
20
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
35 ClaimModel newClaim = new ClaimModel(graph.addVertex(null));
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
36 tpGraph.setLabelToAuthor(newClaim, author);
18
one
parents: 17
diff changeset
37 newClaim.setClaimProperties(toulmin, type);
20
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
38
18
one
parents: 17
diff changeset
39 String[] users = toStringArray(usersJson);
20
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
40 tpGraph.setLabelToUsers(newClaim, users, NodeModel.L_REQUEST);
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
41 tpGraph.setLabelToRootClaim(newClaim);
20
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
42
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
43
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
44 return created();
18
one
parents: 17
diff changeset
45 }
one
parents: 17
diff changeset
46
one
parents: 17
diff changeset
47 private static String[] toStringArray(JsonNode jsonNode) {
one
parents: 17
diff changeset
48 int length = jsonNode.size();
one
parents: 17
diff changeset
49 if (length == 0) return null;
one
parents: 17
diff changeset
50 String[] userArray = new String[length];
one
parents: 17
diff changeset
51 for (int i=0; i<length; i++ ) {
one
parents: 17
diff changeset
52 userArray[i] = jsonNode.get(i).getTextValue();
one
parents: 17
diff changeset
53 }
one
parents: 17
diff changeset
54 return userArray;
12
ef434ebd64ae fix NodeModel.java
one
parents: 11
diff changeset
55 }
ef434ebd64ae fix NodeModel.java
one
parents: 11
diff changeset
56
11
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
57
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
58
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
59
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
60 }