annotate app/controllers/Claim.java @ 32:be6bbd4967e4

create createMentions
author one
date Wed, 03 Oct 2012 16:47:44 +0900
parents 995be14b30a2
children 1ca28e3eb4e9
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;
29
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
4 import org.codehaus.jackson.node.ObjectNode;
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
5
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
6 import models.ClaimModel;
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
7 import models.NodeModel;
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
8 import models.TPGraph;
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
9
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
10 import com.tinkerpop.blueprints.Graph;
28
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
11 import com.tinkerpop.blueprints.Vertex;
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
12
29
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
13 import play.libs.Json;
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
14 import play.mvc.BodyParser;
11
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
15 import play.mvc.Controller;
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
16 import play.mvc.Result;
11
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
17
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
18 public class Claim extends Controller {
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
19
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
20
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
21 @BodyParser.Of(BodyParser.Json.class)
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
22 public static Result crateClaim() {
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
23 JsonNode json = request().body().asJson();
18
one
parents: 17
diff changeset
24 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
16
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
25
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
26 TPGraph tpGraph = TPGraph.getInstance();
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
27 Graph graph = tpGraph.getGraph();
18
one
parents: 17
diff changeset
28 if ( graph.getVertex(author) == null)
one
parents: 17
diff changeset
29 return badRequest("Author "+ author + "is not exist.");
16
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
30
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
31 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
18
one
parents: 17
diff changeset
32 if (toulmin.findPath(NodeModel.TITLE) == null)
one
parents: 17
diff changeset
33 return badRequest("Please set title");
16
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
34
7cdc9d19834f modified createClaim
one
parents: 13
diff changeset
35 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode)
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
36 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
12
ef434ebd64ae fix NodeModel.java
one
parents: 11
diff changeset
37
20
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
38 ClaimModel newClaim = new ClaimModel(graph.addVertex(null));
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
39 tpGraph.setLabelToAuthor(newClaim, author);
18
one
parents: 17
diff changeset
40 newClaim.setClaimProperties(toulmin, type);
20
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
41
18
one
parents: 17
diff changeset
42 String[] users = toStringArray(usersJson);
30
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 29
diff changeset
43 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.FAIL);
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
44 tpGraph.setLabelToRootClaim(newClaim);
20
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
45
34ea98c5a18c modified UserModel/getEdgeInUser
one
parents: 18
diff changeset
46 return created();
18
one
parents: 17
diff changeset
47 }
one
parents: 17
diff changeset
48
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
49 @BodyParser.Of(BodyParser.Json.class)
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
50 public static Result createMention(String mentionType, String id) {
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
51
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
52 if ( !(mentionType.equals(NodeModel.L_QUESTION)
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
53 ||mentionType.equals(NodeModel.L_REFUTATION)
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
54 ||mentionType.equals(NodeModel.L_QUESTION))) {
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
55 return badRequest("Wrong mention type.");
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
56 }
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
57
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
58 JsonNode json = request().body().asJson();
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
59 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
60
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
61 TPGraph tpGraph = TPGraph.getInstance();
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
62 Graph graph = tpGraph.getGraph();
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
63 if ( graph.getVertex(author) == null)
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
64 return badRequest("Author "+ author + "is not exist.");
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
65
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
66 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
67 if (toulmin.findPath(NodeModel.TITLE) == null)
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
68 return badRequest("Please set title");
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
69
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
70 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode)
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
71 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
72
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
73 ClaimModel newClaim = new ClaimModel(graph.addVertex(null));
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
74 tpGraph.setLabelToAuthor(newClaim, author);
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
75 newClaim.setClaimProperties(toulmin, type);
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
76
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
77 String[] users = toStringArray(usersJson);
30
80b5628f17d8 modified ClaimModel/getInfo action
one
parents: 29
diff changeset
78 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.FAIL);
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
79 tpGraph.setLabelToRootClaim(newClaim);
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
80
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
81 ClaimModel mentionV = new ClaimModel(graph.getVertex(id));
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
82 tpGraph.setLabelMention(mentionV, newClaim, mentionType);
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
83
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
84 return created();
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
85 }
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
86
29
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
87 public static Result getClaimInfo(String id) {
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
88 TPGraph tpGraph = TPGraph.getInstance();
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
89 Graph graph = tpGraph.getGraph();
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
90 Vertex claimV = graph.getVertex(id);
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
91 if (claimV == null) badRequest("Claim id "+id+" is not found.");
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
92 ClaimModel claim = new ClaimModel(claimV);
31
995be14b30a2 create ClaimModel/getUsersIdAndStatus method
one
parents: 30
diff changeset
93 ObjectNode claimInfo = claim.getSimpleClaimInfo();
29
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
94 ObjectNode result = Json.newObject();
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
95 result.put("status", "OK");
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
96 result.put("message", claimInfo.toString());
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
97 return ok(result);
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
98 }
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
99
28
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
100 public static Result getClaimTree(String id) {
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
101 TPGraph tpGraph = TPGraph.getInstance();
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
102 Graph graph = tpGraph.getGraph();
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
103 Vertex v = graph.getVertex(id);
32
be6bbd4967e4 create createMentions
one
parents: 31
diff changeset
104 if (v == null) {
be6bbd4967e4 create createMentions
one
parents: 31
diff changeset
105 return badRequest("Consensus id "+ id +" is not found.");
be6bbd4967e4 create createMentions
one
parents: 31
diff changeset
106 }
28
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
107 ClaimModel consensusRoot = new ClaimModel(v);
31
995be14b30a2 create ClaimModel/getUsersIdAndStatus method
one
parents: 30
diff changeset
108 ObjectNode resultEntity = consensusRoot.getClaimInfoTraverse();
995be14b30a2 create ClaimModel/getUsersIdAndStatus method
one
parents: 30
diff changeset
109 return ok(resultEntity);
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
110 }
32
be6bbd4967e4 create createMentions
one
parents: 31
diff changeset
111
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
112
28
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
113
18
one
parents: 17
diff changeset
114 private static String[] toStringArray(JsonNode jsonNode) {
one
parents: 17
diff changeset
115 int length = jsonNode.size();
one
parents: 17
diff changeset
116 if (length == 0) return null;
one
parents: 17
diff changeset
117 String[] userArray = new String[length];
one
parents: 17
diff changeset
118 for (int i=0; i<length; i++ ) {
one
parents: 17
diff changeset
119 userArray[i] = jsonNode.get(i).getTextValue();
one
parents: 17
diff changeset
120 }
one
parents: 17
diff changeset
121 return userArray;
12
ef434ebd64ae fix NodeModel.java
one
parents: 11
diff changeset
122 }
ef434ebd64ae fix NodeModel.java
one
parents: 11
diff changeset
123
11
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
124
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
125
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
126
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
127 }