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