annotate app/controllers/Claim.java @ 34:fac4aa26ea04

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