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;
|
39
|
9 import models.UserModel;
|
13
|
10
|
|
11 import com.tinkerpop.blueprints.Graph;
|
28
|
12 import com.tinkerpop.blueprints.Vertex;
|
13
|
13
|
29
|
14 import play.libs.Json;
|
13
|
15 import play.mvc.BodyParser;
|
11
|
16 import play.mvc.Controller;
|
13
|
17 import play.mvc.Result;
|
11
|
18
|
|
19 public class Claim extends Controller {
|
|
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 TPGraph tpGraph = TPGraph.getInstance();
|
|
26 Graph graph = tpGraph.getGraph();
|
34
|
27 if ( graph.getVertex(author) == null) {
|
41
|
28 return badRequest("Author "+ author + "does not exist.");
|
34
|
29 }
|
16
|
30 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
|
34
|
31 if (toulmin.findPath(NodeModel.TITLE) == null) {
|
18
|
32 return badRequest("Please set title");
|
34
|
33 }
|
16
|
34 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode)
|
37
|
35 if (usersJson == null) {
|
|
36 return badRequest("Please set users");
|
|
37 }
|
13
|
38 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
|
20
|
39 ClaimModel newClaim = new ClaimModel(graph.addVertex(null));
|
|
40 tpGraph.setLabelToAuthor(newClaim, author);
|
18
|
41 newClaim.setClaimProperties(toulmin, type);
|
|
42 String[] users = toStringArray(usersJson);
|
37
|
43 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN);
|
13
|
44 tpGraph.setLabelToRootClaim(newClaim);
|
20
|
45 return created();
|
18
|
46 }
|
|
47
|
25
|
48 @BodyParser.Of(BodyParser.Json.class)
|
35
|
49 public static Result editClaim(String id) {
|
|
50 JsonNode json = request().body().asJson();
|
|
51 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
|
|
52 TPGraph tpGraph = TPGraph.getInstance();
|
|
53 Graph graph = tpGraph.getGraph();
|
36
|
54 ClaimModel claim = new ClaimModel(graph.getVertex(id));
|
|
55 if ( claim.getVertex() == null ) {
|
41
|
56 return badRequest("Claim id "+ id + " does not exist.");
|
36
|
57 }
|
|
58 if ( !claim.getAuthorId().equals(author)) {
|
|
59 return badRequest("Wrong Author.");
|
35
|
60 }
|
|
61 if ( graph.getVertex(author) == null) {
|
41
|
62 return badRequest("Author "+ author + " does not exist.");
|
35
|
63 }
|
|
64 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
|
|
65 if (toulmin.findPath(NodeModel.TITLE) == null) {
|
|
66 return badRequest("Please set title");
|
|
67 }
|
36
|
68 JsonNode usersJson = json.get(NodeModel.USERS);
|
37
|
69 if (usersJson == null) {
|
|
70 return badRequest("Please set users");
|
|
71 }
|
35
|
72 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
|
|
73 claim.setClaimProperties(toulmin, type);
|
36
|
74 Object[] users = toStringObject(usersJson);
|
|
75 claim.editRequestsEdgeUsers(users);
|
35
|
76 tpGraph.setLabelToRootClaim(claim);
|
|
77 return created();
|
|
78 }
|
|
79
|
39
|
80 public static Result getUserConsensusStatus(String id, String name) {
|
|
81 TPGraph tpGraph = TPGraph.getInstance();
|
|
82 Graph graph = tpGraph.getGraph();
|
|
83 ClaimModel claim = new ClaimModel(graph.getVertex(id));
|
|
84 if (claim.getVertex() == null) {
|
|
85 return badRequest("Claim id "+id+" does not exist");
|
|
86 }
|
|
87 UserModel user = new UserModel(graph.getVertex(name));
|
|
88 if (user.getVertex() == null) {
|
|
89 return badRequest("User "+name+" does not exist");
|
|
90 }
|
|
91 ObjectNode result = claim.getUserRequestStatus(user);
|
|
92 return ok(result);
|
|
93 }
|
|
94
|
41
|
95 public static Result updateUserConsensusStatus(String id, String name, String status) {
|
|
96 if ( !(status.equals(NodeModel.AGREED)
|
|
97 ||status.equals(NodeModel.FAILED)
|
|
98 ||status.equals(NodeModel.UNKNOWN)
|
|
99 ||status.equals(NodeModel.PEND))) {
|
|
100 return badRequest("Wrong status type.");
|
|
101 }
|
|
102 TPGraph tpGraph = TPGraph.getInstance();
|
|
103 Graph graph = tpGraph.getGraph();
|
|
104 ClaimModel claim = new ClaimModel(graph.getVertex(id));
|
|
105 if (claim.getVertex() == null) {
|
|
106 return badRequest("Claim id "+id+" does not exist");
|
|
107 }
|
|
108 UserModel user = new UserModel(graph.getVertex(name));
|
|
109 if (user.getVertex() == null) {
|
|
110 return badRequest("User "+name+" does not exist");
|
|
111 }
|
|
112 claim.updateUserRequestStatus(claim, user, status);
|
42
|
113 claim.computeAndUpdateStatus();
|
41
|
114 return created();
|
|
115 }
|
35
|
116
|
|
117 @BodyParser.Of(BodyParser.Json.class)
|
25
|
118 public static Result createMention(String mentionType, String id) {
|
|
119 if ( !(mentionType.equals(NodeModel.L_QUESTION)
|
|
120 ||mentionType.equals(NodeModel.L_REFUTATION)
|
38
|
121 ||mentionType.equals(NodeModel.L_SUGGESTION))) {
|
25
|
122 return badRequest("Wrong mention type.");
|
|
123 }
|
|
124 JsonNode json = request().body().asJson();
|
|
125 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
|
|
126 TPGraph tpGraph = TPGraph.getInstance();
|
|
127 Graph graph = tpGraph.getGraph();
|
34
|
128 if ( graph.getVertex(author) == null) {
|
41
|
129 return badRequest("Author "+ author + " does not exist.");
|
34
|
130 }
|
25
|
131 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
|
34
|
132 if (toulmin.findPath(NodeModel.TITLE) == null) {
|
25
|
133 return badRequest("Please set title");
|
34
|
134 }
|
25
|
135 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode)
|
37
|
136 if (usersJson == null) {
|
|
137 return badRequest("Please set users");
|
|
138 }
|
25
|
139 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
|
|
140 ClaimModel newClaim = new ClaimModel(graph.addVertex(null));
|
|
141 tpGraph.setLabelToAuthor(newClaim, author);
|
|
142 newClaim.setClaimProperties(toulmin, type);
|
|
143 String[] users = toStringArray(usersJson);
|
37
|
144 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN);
|
25
|
145 tpGraph.setLabelToRootClaim(newClaim);
|
|
146 ClaimModel mentionV = new ClaimModel(graph.getVertex(id));
|
|
147 tpGraph.setLabelMention(mentionV, newClaim, mentionType);
|
|
148 return created();
|
|
149 }
|
42
|
150
|
29
|
151 public static Result getClaimInfo(String id) {
|
|
152 TPGraph tpGraph = TPGraph.getInstance();
|
|
153 Graph graph = tpGraph.getGraph();
|
|
154 Vertex claimV = graph.getVertex(id);
|
34
|
155 if (claimV == null) {
|
41
|
156 badRequest("Claim id "+id+" does not exist.");
|
34
|
157 }
|
29
|
158 ClaimModel claim = new ClaimModel(claimV);
|
31
|
159 ObjectNode claimInfo = claim.getSimpleClaimInfo();
|
29
|
160 ObjectNode result = Json.newObject();
|
35
|
161 result.put("message", claimInfo);
|
29
|
162 return ok(result);
|
|
163 }
|
|
164
|
28
|
165 public static Result getClaimTree(String id) {
|
|
166 TPGraph tpGraph = TPGraph.getInstance();
|
|
167 Graph graph = tpGraph.getGraph();
|
|
168 Vertex v = graph.getVertex(id);
|
32
|
169 if (v == null) {
|
41
|
170 return badRequest("Consensus id "+ id +" does not exist.");
|
32
|
171 }
|
28
|
172 ClaimModel consensusRoot = new ClaimModel(v);
|
31
|
173 ObjectNode resultEntity = consensusRoot.getClaimInfoTraverse();
|
|
174 return ok(resultEntity);
|
25
|
175 }
|
35
|
176
|
36
|
177 private static Object[] toStringObject(JsonNode jsonNode) {
|
37
|
178 if (jsonNode == null) {
|
|
179 return null;
|
|
180 }
|
36
|
181 int length = jsonNode.size();
|
|
182 if (length == 0) {
|
|
183 return null;
|
|
184 }
|
|
185 Object[] userArray = new Object[length];
|
|
186 for (int i=0; i<length; i++ ) {
|
|
187 userArray[i] = jsonNode.get(i).getTextValue();
|
|
188 }
|
|
189 return userArray;
|
|
190 }
|
28
|
191
|
18
|
192 private static String[] toStringArray(JsonNode jsonNode) {
|
37
|
193 if (jsonNode == null) {
|
|
194 return null;
|
|
195 }
|
18
|
196 int length = jsonNode.size();
|
34
|
197 if (length == 0) {
|
|
198 return null;
|
|
199 }
|
18
|
200 String[] userArray = new String[length];
|
|
201 for (int i=0; i<length; i++ ) {
|
|
202 userArray[i] = jsonNode.get(i).getTextValue();
|
|
203 }
|
|
204 return userArray;
|
12
|
205 }
|
|
206
|
11
|
207
|
|
208
|
|
209
|
|
210 }
|