annotate app/controllers/Claim.java @ 38:a2abe67d7c7a

fix createMention action bug
author one
date Thu, 04 Oct 2012 01:05:19 +0900
parents bc3ac73320f9
children 870553e92e3e
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)
37
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
34 if (usersJson == null) {
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
35 return badRequest("Please set users");
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
36 }
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
37 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
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);
one
parents: 17
diff changeset
41 String[] users = toStringArray(usersJson);
37
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
42 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN);
13
9b677755cb93 create action Claim/createClaim
one
parents: 12
diff changeset
43 tpGraph.setLabelToRootClaim(newClaim);
20
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
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
47 @BodyParser.Of(BodyParser.Json.class)
35
one
parents: 34
diff changeset
48 public static Result editClaim(String id) {
one
parents: 34
diff changeset
49 JsonNode json = request().body().asJson();
one
parents: 34
diff changeset
50 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
one
parents: 34
diff changeset
51 TPGraph tpGraph = TPGraph.getInstance();
one
parents: 34
diff changeset
52 Graph graph = tpGraph.getGraph();
36
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
53 ClaimModel claim = new ClaimModel(graph.getVertex(id));
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
54 if ( claim.getVertex() == null ) {
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
55 return badRequest("Claim id "+ id + "does not exist.");
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
56 }
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
57 if ( !claim.getAuthorId().equals(author)) {
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
58 return badRequest("Wrong Author.");
35
one
parents: 34
diff changeset
59 }
one
parents: 34
diff changeset
60 if ( graph.getVertex(author) == null) {
one
parents: 34
diff changeset
61 return badRequest("Author "+ author + "is not exist.");
one
parents: 34
diff changeset
62 }
one
parents: 34
diff changeset
63 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
one
parents: 34
diff changeset
64 if (toulmin.findPath(NodeModel.TITLE) == null) {
one
parents: 34
diff changeset
65 return badRequest("Please set title");
one
parents: 34
diff changeset
66 }
36
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
67 JsonNode usersJson = json.get(NodeModel.USERS);
37
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
68 if (usersJson == null) {
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
69 return badRequest("Please set users");
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
70 }
35
one
parents: 34
diff changeset
71 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
one
parents: 34
diff changeset
72 claim.setClaimProperties(toulmin, type);
36
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
73 Object[] users = toStringObject(usersJson);
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
74 claim.editRequestsEdgeUsers(users);
35
one
parents: 34
diff changeset
75 tpGraph.setLabelToRootClaim(claim);
one
parents: 34
diff changeset
76 return created();
one
parents: 34
diff changeset
77 }
one
parents: 34
diff changeset
78
one
parents: 34
diff changeset
79
one
parents: 34
diff changeset
80 @BodyParser.Of(BodyParser.Json.class)
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
81 public static Result createMention(String mentionType, String id) {
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
82 if ( !(mentionType.equals(NodeModel.L_QUESTION)
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
83 ||mentionType.equals(NodeModel.L_REFUTATION)
38
a2abe67d7c7a fix createMention action bug
one
parents: 37
diff changeset
84 ||mentionType.equals(NodeModel.L_SUGGESTION))) {
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
85 return badRequest("Wrong mention type.");
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
86 }
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
87 JsonNode json = request().body().asJson();
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
88 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
89 TPGraph tpGraph = TPGraph.getInstance();
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
90 Graph graph = tpGraph.getGraph();
34
one
parents: 33
diff changeset
91 if ( graph.getVertex(author) == null) {
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
92 return badRequest("Author "+ author + "is not exist.");
34
one
parents: 33
diff changeset
93 }
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
94 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
34
one
parents: 33
diff changeset
95 if (toulmin.findPath(NodeModel.TITLE) == null) {
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
96 return badRequest("Please set title");
34
one
parents: 33
diff changeset
97 }
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
98 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode)
37
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
99 if (usersJson == null) {
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
100 return badRequest("Please set users");
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
101 }
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
102 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
103 ClaimModel newClaim = new ClaimModel(graph.addVertex(null));
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
104 tpGraph.setLabelToAuthor(newClaim, author);
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
105 newClaim.setClaimProperties(toulmin, type);
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
106 String[] users = toStringArray(usersJson);
37
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
107 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN);
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
108 tpGraph.setLabelToRootClaim(newClaim);
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
109 ClaimModel mentionV = new ClaimModel(graph.getVertex(id));
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
110 tpGraph.setLabelMention(mentionV, newClaim, mentionType);
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
111 return created();
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
112 }
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
113
29
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
114 public static Result getClaimInfo(String id) {
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
115 TPGraph tpGraph = TPGraph.getInstance();
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
116 Graph graph = tpGraph.getGraph();
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
117 Vertex claimV = graph.getVertex(id);
34
one
parents: 33
diff changeset
118 if (claimV == null) {
one
parents: 33
diff changeset
119 badRequest("Claim id "+id+" is not found.");
one
parents: 33
diff changeset
120 }
29
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
121 ClaimModel claim = new ClaimModel(claimV);
31
995be14b30a2 create ClaimModel/getUsersIdAndStatus method
one
parents: 30
diff changeset
122 ObjectNode claimInfo = claim.getSimpleClaimInfo();
29
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
123 ObjectNode result = Json.newObject();
35
one
parents: 34
diff changeset
124 result.put("message", claimInfo);
29
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
125 return ok(result);
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
126 }
fbb232e78422 create getClaimInfo
one
parents: 28
diff changeset
127
28
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
128 public static Result getClaimTree(String id) {
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
129 TPGraph tpGraph = TPGraph.getInstance();
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
130 Graph graph = tpGraph.getGraph();
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
131 Vertex v = graph.getVertex(id);
32
be6bbd4967e4 create createMentions
one
parents: 31
diff changeset
132 if (v == null) {
be6bbd4967e4 create createMentions
one
parents: 31
diff changeset
133 return badRequest("Consensus id "+ id +" is not found.");
be6bbd4967e4 create createMentions
one
parents: 31
diff changeset
134 }
28
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
135 ClaimModel consensusRoot = new ClaimModel(v);
31
995be14b30a2 create ClaimModel/getUsersIdAndStatus method
one
parents: 30
diff changeset
136 ObjectNode resultEntity = consensusRoot.getClaimInfoTraverse();
995be14b30a2 create ClaimModel/getUsersIdAndStatus method
one
parents: 30
diff changeset
137 return ok(resultEntity);
25
97b249d9fad1 fix infinite loop on getConsensus Action
one
parents: 20
diff changeset
138 }
35
one
parents: 34
diff changeset
139
36
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
140 private static Object[] toStringObject(JsonNode jsonNode) {
37
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
141 if (jsonNode == null) {
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
142 return null;
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
143 }
36
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
144 int length = jsonNode.size();
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
145 if (length == 0) {
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
146 return null;
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
147 }
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
148 Object[] userArray = new Object[length];
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
149 for (int i=0; i<length; i++ ) {
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
150 userArray[i] = jsonNode.get(i).getTextValue();
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
151 }
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
152 return userArray;
5f7fcdf98380 create editClaim
one
parents: 35
diff changeset
153 }
28
7112b826a53a modified RequestTest.java
one
parents: 25
diff changeset
154
18
one
parents: 17
diff changeset
155 private static String[] toStringArray(JsonNode jsonNode) {
37
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
156 if (jsonNode == null) {
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
157 return null;
bc3ac73320f9 fix const value FAILED from FAIL
one
parents: 36
diff changeset
158 }
18
one
parents: 17
diff changeset
159 int length = jsonNode.size();
34
one
parents: 33
diff changeset
160 if (length == 0) {
one
parents: 33
diff changeset
161 return null;
one
parents: 33
diff changeset
162 }
18
one
parents: 17
diff changeset
163 String[] userArray = new String[length];
one
parents: 17
diff changeset
164 for (int i=0; i<length; i++ ) {
one
parents: 17
diff changeset
165 userArray[i] = jsonNode.get(i).getTextValue();
one
parents: 17
diff changeset
166 }
one
parents: 17
diff changeset
167 return userArray;
12
ef434ebd64ae fix NodeModel.java
one
parents: 11
diff changeset
168 }
ef434ebd64ae fix NodeModel.java
one
parents: 11
diff changeset
169
11
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
170
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
171
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
172
6aa0452a7f9f add controllers/Claim.java
one
parents:
diff changeset
173 }