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