11
|
1 package controllers;
|
|
2
|
13
|
3 import org.codehaus.jackson.JsonNode;
|
|
4
|
|
5 import models.ClaimModel;
|
|
6 import models.NodeModel;
|
|
7 import models.TPGraph;
|
|
8
|
|
9 import com.tinkerpop.blueprints.Graph;
|
|
10
|
|
11 import play.mvc.BodyParser;
|
11
|
12 import play.mvc.Controller;
|
13
|
13 import play.mvc.Result;
|
11
|
14
|
|
15 public class Claim extends Controller {
|
|
16
|
|
17
|
13
|
18 @BodyParser.Of(BodyParser.Json.class)
|
|
19 public static Result crateClaim() {
|
|
20 JsonNode json = request().body().asJson();
|
18
|
21 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
|
16
|
22
|
|
23 TPGraph tpGraph = TPGraph.getInstance();
|
|
24 Graph graph = tpGraph.getGraph();
|
18
|
25 if ( graph.getVertex(author) == null)
|
|
26 return badRequest("Author "+ author + "is not exist.");
|
16
|
27
|
|
28 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
|
18
|
29 if (toulmin.findPath(NodeModel.TITLE) == null)
|
|
30 return badRequest("Please set title");
|
16
|
31
|
|
32 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode)
|
13
|
33 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
|
12
|
34
|
20
|
35 ClaimModel newClaim = new ClaimModel(graph.addVertex(null));
|
|
36 tpGraph.setLabelToAuthor(newClaim, author);
|
18
|
37 newClaim.setClaimProperties(toulmin, type);
|
20
|
38
|
18
|
39 String[] users = toStringArray(usersJson);
|
20
|
40 tpGraph.setLabelToUsers(newClaim, users, NodeModel.L_REQUEST);
|
13
|
41 tpGraph.setLabelToRootClaim(newClaim);
|
20
|
42
|
|
43
|
|
44 return created();
|
18
|
45 }
|
|
46
|
|
47 private static String[] toStringArray(JsonNode jsonNode) {
|
|
48 int length = jsonNode.size();
|
|
49 if (length == 0) return null;
|
|
50 String[] userArray = new String[length];
|
|
51 for (int i=0; i<length; i++ ) {
|
|
52 userArray[i] = jsonNode.get(i).getTextValue();
|
|
53 }
|
|
54 return userArray;
|
12
|
55 }
|
|
56
|
11
|
57
|
|
58
|
|
59
|
|
60 }
|