Mercurial > hg > Members > nobuyasu > Consensus
annotate app/controllers/Claim.java @ 92:35eeb04a788d draft
create getLatestClaimTree action. modified createMention action and viewer.html.
author | one |
---|---|
date | Tue, 12 Mar 2013 16:18:51 +0900 |
parents | d45f76774fd8 |
children | d66ac07e1c36 |
rev | line source |
---|---|
11 | 1 package controllers; |
2 | |
63 | 3 import init.InitialData; |
4 | |
13 | 5 import org.codehaus.jackson.JsonNode; |
29 | 6 import org.codehaus.jackson.node.ObjectNode; |
13 | 7 |
8 import models.ClaimModel; | |
9 import models.NodeModel; | |
10 import models.TPGraph; | |
39 | 11 import models.UserModel; |
13 | 12 |
28 | 13 import com.tinkerpop.blueprints.Vertex; |
13 | 14 |
29 | 15 import play.libs.Json; |
13 | 16 import play.mvc.BodyParser; |
11 | 17 import play.mvc.Controller; |
13 | 18 import play.mvc.Result; |
11 | 19 |
20 public class Claim extends Controller { | |
21 | |
13 | 22 @BodyParser.Of(BodyParser.Json.class) |
57 | 23 public static Result createClaim() { |
13 | 24 JsonNode json = request().body().asJson(); |
47 | 25 if (json == null) { |
26 return badRequest("please set Enitity (Json Data)"); | |
27 } | |
18 | 28 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author |
16 | 29 TPGraph tpGraph = TPGraph.getInstance(); |
76 | 30 if ( tpGraph.getVertex(author) == null) { |
41 | 31 return badRequest("Author "+ author + "does not exist."); |
34 | 32 } |
16 | 33 JsonNode toulmin = json.findPath(NodeModel.TOULMIN); |
34 | 34 if (toulmin.findPath(NodeModel.TITLE) == null) { |
18 | 35 return badRequest("Please set title"); |
34 | 36 } |
72 | 37 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode) |
76 | 38 if (usersJson == null) { |
37 | 39 return badRequest("Please set users"); |
40 } | |
13 | 41 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously) |
76 | 42 ClaimModel newClaim = new ClaimModel(tpGraph.addVertex(null)); |
20 | 43 tpGraph.setLabelToAuthor(newClaim, author); |
18 | 44 newClaim.setClaimProperties(toulmin, type); |
45 String[] users = toStringArray(usersJson); | |
37 | 46 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN); |
70 | 47 tpGraph.setLabelFromRootClaim(newClaim); |
20 | 48 return created(); |
18 | 49 } |
50 | |
25 | 51 @BodyParser.Of(BodyParser.Json.class) |
35 | 52 public static Result editClaim(String id) { |
53 JsonNode json = request().body().asJson(); | |
47 | 54 if (json == null) { |
55 return badRequest(); | |
56 } | |
35 | 57 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author |
58 TPGraph tpGraph = TPGraph.getInstance(); | |
76 | 59 ClaimModel claim = new ClaimModel(tpGraph.getVertex(id)); |
36 | 60 if ( claim.getVertex() == null ) { |
41 | 61 return badRequest("Claim id "+ id + " does not exist."); |
36 | 62 } |
63 if ( !claim.getAuthorId().equals(author)) { | |
64 return badRequest("Wrong Author."); | |
35 | 65 } |
76 | 66 if ( tpGraph.getVertex(author) == null) { |
41 | 67 return badRequest("Author "+ author + " does not exist."); |
35 | 68 } |
69 JsonNode toulmin = json.findPath(NodeModel.TOULMIN); | |
70 if (toulmin.findPath(NodeModel.TITLE) == null) { | |
71 return badRequest("Please set title"); | |
72 } | |
36 | 73 JsonNode usersJson = json.get(NodeModel.USERS); |
37 | 74 if (usersJson == null) { |
75 return badRequest("Please set users"); | |
76 } | |
35 | 77 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously) |
78 claim.setClaimProperties(toulmin, type); | |
36 | 79 Object[] users = toStringObject(usersJson); |
80 claim.editRequestsEdgeUsers(users); | |
70 | 81 tpGraph.setLabelFromRootClaim(claim); |
35 | 82 return created(); |
83 } | |
84 | |
39 | 85 public static Result getUserConsensusStatus(String id, String name) { |
86 TPGraph tpGraph = TPGraph.getInstance(); | |
76 | 87 ClaimModel claim = new ClaimModel(tpGraph.getVertex(id)); |
39 | 88 if (claim.getVertex() == null) { |
89 return badRequest("Claim id "+id+" does not exist"); | |
90 } | |
76 | 91 UserModel user = new UserModel(tpGraph.getVertex(name)); |
39 | 92 if (user.getVertex() == null) { |
93 return badRequest("User "+name+" does not exist"); | |
94 } | |
95 ObjectNode result = claim.getUserRequestStatus(user); | |
96 return ok(result); | |
97 } | |
98 | |
41 | 99 public static Result updateUserConsensusStatus(String id, String name, String status) { |
100 if ( !(status.equals(NodeModel.AGREED) | |
47 | 101 ||status.equals(NodeModel.DENIED) |
41 | 102 ||status.equals(NodeModel.UNKNOWN) |
103 ||status.equals(NodeModel.PEND))) { | |
104 return badRequest("Wrong status type."); | |
105 } | |
106 TPGraph tpGraph = TPGraph.getInstance(); | |
76 | 107 ClaimModel claim = new ClaimModel(tpGraph.getVertex(id)); |
41 | 108 if (claim.getVertex() == null) { |
109 return badRequest("Claim id "+id+" does not exist"); | |
110 } | |
76 | 111 UserModel user = new UserModel(tpGraph.getVertex(name)); |
41 | 112 if (user.getVertex() == null) { |
113 return badRequest("User "+name+" does not exist"); | |
114 } | |
115 claim.updateUserRequestStatus(claim, user, status); | |
42 | 116 claim.computeAndUpdateStatus(); |
41 | 117 return created(); |
118 } | |
35 | 119 |
120 @BodyParser.Of(BodyParser.Json.class) | |
25 | 121 public static Result createMention(String mentionType, String id) { |
122 if ( !(mentionType.equals(NodeModel.L_QUESTION) | |
123 ||mentionType.equals(NodeModel.L_REFUTATION) | |
38 | 124 ||mentionType.equals(NodeModel.L_SUGGESTION))) { |
25 | 125 return badRequest("Wrong mention type."); |
126 } | |
127 JsonNode json = request().body().asJson(); | |
47 | 128 if (json == null) { |
129 return badRequest(); | |
130 } | |
25 | 131 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author |
132 TPGraph tpGraph = TPGraph.getInstance(); | |
76 | 133 if ( tpGraph.getVertex(author) == null) { |
41 | 134 return badRequest("Author "+ author + " does not exist."); |
34 | 135 } |
25 | 136 JsonNode toulmin = json.findPath(NodeModel.TOULMIN); |
34 | 137 if (toulmin.findPath(NodeModel.TITLE) == null) { |
25 | 138 return badRequest("Please set title"); |
34 | 139 } |
25 | 140 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode) |
37 | 141 if (usersJson == null) { |
142 return badRequest("Please set users"); | |
143 } | |
25 | 144 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously) |
76 | 145 ClaimModel newClaim = new ClaimModel(tpGraph.addVertex(null)); |
25 | 146 tpGraph.setLabelToAuthor(newClaim, author); |
92
35eeb04a788d
create getLatestClaimTree action. modified createMention action and viewer.html.
one
parents:
85
diff
changeset
|
147 String timestamp = Long.toString(System.currentTimeMillis()); |
25 | 148 newClaim.setClaimProperties(toulmin, type); |
149 String[] users = toStringArray(usersJson); | |
37 | 150 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN); |
70 | 151 tpGraph.setLabelFromRootClaim(newClaim); |
76 | 152 ClaimModel targetClaim = new ClaimModel(tpGraph.getVertex(id)); |
92
35eeb04a788d
create getLatestClaimTree action. modified createMention action and viewer.html.
one
parents:
85
diff
changeset
|
153 tpGraph.copyConsensusTree(targetClaim, timestamp); |
35eeb04a788d
create getLatestClaimTree action. modified createMention action and viewer.html.
one
parents:
85
diff
changeset
|
154 ClaimModel latestTgtClaim = new ClaimModel(tpGraph.getVertex(tpGraph.getLatestVertexId(targetClaim.getId()))); |
35eeb04a788d
create getLatestClaimTree action. modified createMention action and viewer.html.
one
parents:
85
diff
changeset
|
155 tpGraph.setLabelMention(latestTgtClaim, newClaim, mentionType); |
68 | 156 newClaim.computeAndUpdateStatus(); |
92
35eeb04a788d
create getLatestClaimTree action. modified createMention action and viewer.html.
one
parents:
85
diff
changeset
|
157 latestTgtClaim.computeAndUpdateStatus(); |
25 | 158 return created(); |
159 } | |
75 | 160 |
29 | 161 public static Result getClaimInfo(String id) { |
162 TPGraph tpGraph = TPGraph.getInstance(); | |
76 | 163 Vertex claimV = tpGraph.getVertex(id); |
34 | 164 if (claimV == null) { |
41 | 165 badRequest("Claim id "+id+" does not exist."); |
34 | 166 } |
29 | 167 ClaimModel claim = new ClaimModel(claimV); |
31 | 168 ObjectNode claimInfo = claim.getSimpleClaimInfo(); |
29 | 169 ObjectNode result = Json.newObject(); |
35 | 170 result.put("message", claimInfo); |
29 | 171 return ok(result); |
172 } | |
173 | |
28 | 174 public static Result getClaimTree(String id) { |
175 TPGraph tpGraph = TPGraph.getInstance(); | |
76 | 176 Vertex v = tpGraph.getVertex(id); |
32 | 177 if (v == null) { |
41 | 178 return badRequest("Consensus id "+ id +" does not exist."); |
32 | 179 } |
28 | 180 ClaimModel consensusRoot = new ClaimModel(v); |
31 | 181 ObjectNode resultEntity = consensusRoot.getClaimInfoTraverse(); |
182 return ok(resultEntity); | |
25 | 183 } |
84 | 184 |
92
35eeb04a788d
create getLatestClaimTree action. modified createMention action and viewer.html.
one
parents:
85
diff
changeset
|
185 public static Result getLatestClaimTree(String id) { |
35eeb04a788d
create getLatestClaimTree action. modified createMention action and viewer.html.
one
parents:
85
diff
changeset
|
186 TPGraph tpGraph = TPGraph.getInstance(); |
35eeb04a788d
create getLatestClaimTree action. modified createMention action and viewer.html.
one
parents:
85
diff
changeset
|
187 String latestId = (tpGraph.getLatestVertexId(id)).toString(); |
35eeb04a788d
create getLatestClaimTree action. modified createMention action and viewer.html.
one
parents:
85
diff
changeset
|
188 return getClaimTree(latestId); |
35eeb04a788d
create getLatestClaimTree action. modified createMention action and viewer.html.
one
parents:
85
diff
changeset
|
189 } |
35eeb04a788d
create getLatestClaimTree action. modified createMention action and viewer.html.
one
parents:
85
diff
changeset
|
190 |
84 | 191 public static Result getClaimRevision(String id) { |
192 TPGraph tpGraph = TPGraph.getInstance(); | |
193 Object[] revision = tpGraph.getClaimRevision(id); | |
194 return created(Json.toJson(revision)); | |
195 } | |
35 | 196 |
82 | 197 public static Result copyClaims(String id) { |
198 TPGraph tpGraph = TPGraph.getInstance(); | |
199 ClaimModel claim = new ClaimModel(tpGraph.getVertex(id)); | |
200 if (claim.getVertex() == null) { | |
201 return badRequest("Claim id " + id + " does not exist."); | |
202 } | |
203 String timestamp = Long.toString(System.currentTimeMillis()); | |
85 | 204 Object latestId = tpGraph.getLatestVertexId(claim.getId()); |
205 ClaimModel latestClaim = new ClaimModel(tpGraph.getVertex(latestId)); | |
206 tpGraph.copyConsensusTree(latestClaim, timestamp); | |
82 | 207 return ok("copy success"); |
208 } | |
209 | |
36 | 210 private static Object[] toStringObject(JsonNode jsonNode) { |
37 | 211 if (jsonNode == null) { |
212 return null; | |
213 } | |
36 | 214 int length = jsonNode.size(); |
215 if (length == 0) { | |
216 return null; | |
217 } | |
218 Object[] userArray = new Object[length]; | |
219 for (int i=0; i<length; i++ ) { | |
220 userArray[i] = jsonNode.get(i).getTextValue(); | |
221 } | |
222 return userArray; | |
223 } | |
28 | 224 |
18 | 225 private static String[] toStringArray(JsonNode jsonNode) { |
37 | 226 if (jsonNode == null) { |
227 return null; | |
228 } | |
18 | 229 int length = jsonNode.size(); |
34 | 230 if (length == 0) { |
231 return null; | |
232 } | |
18 | 233 String[] userArray = new String[length]; |
234 for (int i=0; i<length; i++ ) { | |
235 userArray[i] = jsonNode.get(i).getTextValue(); | |
236 } | |
237 return userArray; | |
12 | 238 } |
239 | |
62 | 240 public static Result reset() { |
63 | 241 InitialData.init(); |
62 | 242 return ok(); |
243 } | |
11 | 244 |
245 | |
246 } |