comparison app/controllers/Claim.java @ 76:41b6f3788cc3 draft

TPGraph wrap graph
author one
date Fri, 08 Mar 2013 16:18:53 +0900
parents 9448734399db
children cddb5ed942a6
comparison
equal deleted inserted replaced
75:9448734399db 76:41b6f3788cc3
8 import models.ClaimModel; 8 import models.ClaimModel;
9 import models.NodeModel; 9 import models.NodeModel;
10 import models.TPGraph; 10 import models.TPGraph;
11 import models.UserModel; 11 import models.UserModel;
12 12
13 import com.tinkerpop.blueprints.Graph;
14 import com.tinkerpop.blueprints.Vertex; 13 import com.tinkerpop.blueprints.Vertex;
15 14
16 import play.libs.Json; 15 import play.libs.Json;
17 import play.mvc.BodyParser; 16 import play.mvc.BodyParser;
18 import play.mvc.Controller; 17 import play.mvc.Controller;
26 if (json == null) { 25 if (json == null) {
27 return badRequest("please set Enitity (Json Data)"); 26 return badRequest("please set Enitity (Json Data)");
28 } 27 }
29 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author 28 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
30 TPGraph tpGraph = TPGraph.getInstance(); 29 TPGraph tpGraph = TPGraph.getInstance();
31 Graph graph = tpGraph.getGraph(); 30 if ( tpGraph.getVertex(author) == null) {
32 if ( graph.getVertex(author) == null) {
33 return badRequest("Author "+ author + "does not exist."); 31 return badRequest("Author "+ author + "does not exist.");
34 } 32 }
35 JsonNode toulmin = json.findPath(NodeModel.TOULMIN); 33 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
36 if (toulmin.findPath(NodeModel.TITLE) == null) { 34 if (toulmin.findPath(NodeModel.TITLE) == null) {
37 return badRequest("Please set title"); 35 return badRequest("Please set title");
38 } 36 }
39 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode) 37 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode)
40 if (usersJson == null) { 38 if (usersJson == null) {
41 return badRequest("Please set users"); 39 return badRequest("Please set users");
42 } 40 }
43 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously) 41 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
44 ClaimModel newClaim = new ClaimModel(graph.addVertex(null)); 42 ClaimModel newClaim = new ClaimModel(tpGraph.addVertex(null));
45 tpGraph.setLabelToAuthor(newClaim, author); 43 tpGraph.setLabelToAuthor(newClaim, author);
46 newClaim.setClaimProperties(toulmin, type); 44 newClaim.setClaimProperties(toulmin, type);
47 String[] users = toStringArray(usersJson); 45 String[] users = toStringArray(usersJson);
48 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN); 46 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN);
49 tpGraph.setLabelFromRootClaim(newClaim); 47 tpGraph.setLabelFromRootClaim(newClaim);
56 if (json == null) { 54 if (json == null) {
57 return badRequest(); 55 return badRequest();
58 } 56 }
59 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author 57 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
60 TPGraph tpGraph = TPGraph.getInstance(); 58 TPGraph tpGraph = TPGraph.getInstance();
61 Graph graph = tpGraph.getGraph(); 59 ClaimModel claim = new ClaimModel(tpGraph.getVertex(id));
62 ClaimModel claim = new ClaimModel(graph.getVertex(id));
63 if ( claim.getVertex() == null ) { 60 if ( claim.getVertex() == null ) {
64 return badRequest("Claim id "+ id + " does not exist."); 61 return badRequest("Claim id "+ id + " does not exist.");
65 } 62 }
66 if ( !claim.getAuthorId().equals(author)) { 63 if ( !claim.getAuthorId().equals(author)) {
67 return badRequest("Wrong Author."); 64 return badRequest("Wrong Author.");
68 } 65 }
69 if ( graph.getVertex(author) == null) { 66 if ( tpGraph.getVertex(author) == null) {
70 return badRequest("Author "+ author + " does not exist."); 67 return badRequest("Author "+ author + " does not exist.");
71 } 68 }
72 JsonNode toulmin = json.findPath(NodeModel.TOULMIN); 69 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
73 if (toulmin.findPath(NodeModel.TITLE) == null) { 70 if (toulmin.findPath(NodeModel.TITLE) == null) {
74 return badRequest("Please set title"); 71 return badRequest("Please set title");
85 return created(); 82 return created();
86 } 83 }
87 84
88 public static Result getUserConsensusStatus(String id, String name) { 85 public static Result getUserConsensusStatus(String id, String name) {
89 TPGraph tpGraph = TPGraph.getInstance(); 86 TPGraph tpGraph = TPGraph.getInstance();
90 Graph graph = tpGraph.getGraph(); 87 ClaimModel claim = new ClaimModel(tpGraph.getVertex(id));
91 ClaimModel claim = new ClaimModel(graph.getVertex(id));
92 if (claim.getVertex() == null) { 88 if (claim.getVertex() == null) {
93 return badRequest("Claim id "+id+" does not exist"); 89 return badRequest("Claim id "+id+" does not exist");
94 } 90 }
95 UserModel user = new UserModel(graph.getVertex(name)); 91 UserModel user = new UserModel(tpGraph.getVertex(name));
96 if (user.getVertex() == null) { 92 if (user.getVertex() == null) {
97 return badRequest("User "+name+" does not exist"); 93 return badRequest("User "+name+" does not exist");
98 } 94 }
99 ObjectNode result = claim.getUserRequestStatus(user); 95 ObjectNode result = claim.getUserRequestStatus(user);
100 return ok(result); 96 return ok(result);
106 ||status.equals(NodeModel.UNKNOWN) 102 ||status.equals(NodeModel.UNKNOWN)
107 ||status.equals(NodeModel.PEND))) { 103 ||status.equals(NodeModel.PEND))) {
108 return badRequest("Wrong status type."); 104 return badRequest("Wrong status type.");
109 } 105 }
110 TPGraph tpGraph = TPGraph.getInstance(); 106 TPGraph tpGraph = TPGraph.getInstance();
111 Graph graph = tpGraph.getGraph(); 107 ClaimModel claim = new ClaimModel(tpGraph.getVertex(id));
112 ClaimModel claim = new ClaimModel(graph.getVertex(id));
113 if (claim.getVertex() == null) { 108 if (claim.getVertex() == null) {
114 return badRequest("Claim id "+id+" does not exist"); 109 return badRequest("Claim id "+id+" does not exist");
115 } 110 }
116 UserModel user = new UserModel(graph.getVertex(name)); 111 UserModel user = new UserModel(tpGraph.getVertex(name));
117 if (user.getVertex() == null) { 112 if (user.getVertex() == null) {
118 return badRequest("User "+name+" does not exist"); 113 return badRequest("User "+name+" does not exist");
119 } 114 }
120 claim.updateUserRequestStatus(claim, user, status); 115 claim.updateUserRequestStatus(claim, user, status);
121 claim.computeAndUpdateStatus(); 116 claim.computeAndUpdateStatus();
133 if (json == null) { 128 if (json == null) {
134 return badRequest(); 129 return badRequest();
135 } 130 }
136 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author 131 String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
137 TPGraph tpGraph = TPGraph.getInstance(); 132 TPGraph tpGraph = TPGraph.getInstance();
138 Graph graph = tpGraph.getGraph(); 133 if ( tpGraph.getVertex(author) == null) {
139 if ( graph.getVertex(author) == null) {
140 return badRequest("Author "+ author + " does not exist."); 134 return badRequest("Author "+ author + " does not exist.");
141 } 135 }
142 JsonNode toulmin = json.findPath(NodeModel.TOULMIN); 136 JsonNode toulmin = json.findPath(NodeModel.TOULMIN);
143 if (toulmin.findPath(NodeModel.TITLE) == null) { 137 if (toulmin.findPath(NodeModel.TITLE) == null) {
144 return badRequest("Please set title"); 138 return badRequest("Please set title");
146 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode) 140 JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode)
147 if (usersJson == null) { 141 if (usersJson == null) {
148 return badRequest("Please set users"); 142 return badRequest("Please set users");
149 } 143 }
150 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously) 144 String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously)
151 ClaimModel newClaim = new ClaimModel(graph.addVertex(null)); 145 ClaimModel newClaim = new ClaimModel(tpGraph.addVertex(null));
152 tpGraph.setLabelToAuthor(newClaim, author); 146 tpGraph.setLabelToAuthor(newClaim, author);
153 newClaim.setClaimProperties(toulmin, type); 147 newClaim.setClaimProperties(toulmin, type);
154 String[] users = toStringArray(usersJson); 148 String[] users = toStringArray(usersJson);
155 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN); 149 tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN);
156 tpGraph.setLabelFromRootClaim(newClaim); 150 tpGraph.setLabelFromRootClaim(newClaim);
157 ClaimModel targetClaim = new ClaimModel(graph.getVertex(id)); 151 ClaimModel targetClaim = new ClaimModel(tpGraph.getVertex(id));
158 tpGraph.setLabelMention(targetClaim, newClaim, mentionType); 152 tpGraph.setLabelMention(targetClaim, newClaim, mentionType);
159 newClaim.computeAndUpdateStatus(); 153 newClaim.computeAndUpdateStatus();
160 targetClaim.computeAndUpdateStatus(); 154 targetClaim.computeAndUpdateStatus();
161 return created(); 155 return created();
162 } 156 }
163 157
164 public static Result copyClaims(String id) { 158 public static Result copyClaims(String id) {
165 TPGraph tpGraph = TPGraph.getInstance(); 159 TPGraph tpGraph = TPGraph.getInstance();
166 Graph graph = tpGraph.getGraph(); 160 ClaimModel claim = new ClaimModel(tpGraph.getVertex(id));
167 ClaimModel claim = new ClaimModel(graph.getVertex(id));
168 if ( claim.getVertex() == null) { 161 if ( claim.getVertex() == null) {
169 return badRequest("Claim id " + id + " does not exist."); 162 return badRequest("Claim id " + id + " does not exist.");
170 } 163 }
171 tpGraph.copyConsensusTree(claim); 164 tpGraph.copyConsensusTree(claim);
172 return ok("copy success"); 165 return ok("copy success");
173 } 166 }
174 167
175 public static Result getClaimInfo(String id) { 168 public static Result getClaimInfo(String id) {
176 TPGraph tpGraph = TPGraph.getInstance(); 169 TPGraph tpGraph = TPGraph.getInstance();
177 Graph graph = tpGraph.getGraph(); 170 Vertex claimV = tpGraph.getVertex(id);
178 Vertex claimV = graph.getVertex(id);
179 if (claimV == null) { 171 if (claimV == null) {
180 badRequest("Claim id "+id+" does not exist."); 172 badRequest("Claim id "+id+" does not exist.");
181 } 173 }
182 ClaimModel claim = new ClaimModel(claimV); 174 ClaimModel claim = new ClaimModel(claimV);
183 ObjectNode claimInfo = claim.getSimpleClaimInfo(); 175 ObjectNode claimInfo = claim.getSimpleClaimInfo();
186 return ok(result); 178 return ok(result);
187 } 179 }
188 180
189 public static Result getClaimTree(String id) { 181 public static Result getClaimTree(String id) {
190 TPGraph tpGraph = TPGraph.getInstance(); 182 TPGraph tpGraph = TPGraph.getInstance();
191 Graph graph = tpGraph.getGraph(); 183 Vertex v = tpGraph.getVertex(id);
192 Vertex v = graph.getVertex(id);
193 if (v == null) { 184 if (v == null) {
194 return badRequest("Consensus id "+ id +" does not exist."); 185 return badRequest("Consensus id "+ id +" does not exist.");
195 } 186 }
196 ClaimModel consensusRoot = new ClaimModel(v); 187 ClaimModel consensusRoot = new ClaimModel(v);
197 ObjectNode resultEntity = consensusRoot.getClaimInfoTraverse(); 188 ObjectNode resultEntity = consensusRoot.getClaimInfoTraverse();