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