Mercurial > hg > Members > nobuyasu > Consensus
view app/controllers/Claim.java @ 35:5d422941b702
fix
author | one |
---|---|
date | Wed, 03 Oct 2012 18:43:49 +0900 |
parents | fac4aa26ea04 |
children | 5f7fcdf98380 |
line wrap: on
line source
package controllers; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.node.ObjectNode; import models.ClaimModel; import models.NodeModel; import models.TPGraph; import com.tinkerpop.blueprints.Graph; import com.tinkerpop.blueprints.Vertex; import play.libs.Json; import play.mvc.BodyParser; import play.mvc.Controller; import play.mvc.Result; public class Claim extends Controller { @BodyParser.Of(BodyParser.Json.class) public static Result crateClaim() { JsonNode json = request().body().asJson(); String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); if ( graph.getVertex(author) == null) { return badRequest("Author "+ author + "is not exist."); } JsonNode toulmin = json.findPath(NodeModel.TOULMIN); if (toulmin.findPath(NodeModel.TITLE) == null) { return badRequest("Please set title"); } JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode) String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously) ClaimModel newClaim = new ClaimModel(graph.addVertex(null)); tpGraph.setLabelToAuthor(newClaim, author); newClaim.setClaimProperties(toulmin, type); String[] users = toStringArray(usersJson); tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.FAIL); tpGraph.setLabelToRootClaim(newClaim); return created(); } @BodyParser.Of(BodyParser.Json.class) public static Result editClaim(String id) { JsonNode json = request().body().asJson(); String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); Vertex claimVertex = graph.getVertex(id); if ( claimVertex == null ) { return badRequest("Claim id "+ id + "is not exist."); } if ( graph.getVertex(author) == null) { return badRequest("Author "+ author + "is not exist."); } JsonNode toulmin = json.findPath(NodeModel.TOULMIN); if (toulmin.findPath(NodeModel.TITLE) == null) { return badRequest("Please set title"); } JsonNode usersJson = json.get(NodeModel.USERS); String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously) ClaimModel claim = new ClaimModel(claimVertex); tpGraph.setLabelToAuthor(claim, author); claim.setClaimProperties(toulmin, type); String[] users = toStringArray(usersJson); tpGraph.setLabelStatusToUsers(claim, users, NodeModel.L_REQUEST, NodeModel.FAIL); tpGraph.setLabelToRootClaim(claim); return created(); } @BodyParser.Of(BodyParser.Json.class) public static Result createMention(String mentionType, String id) { if ( !(mentionType.equals(NodeModel.L_QUESTION) ||mentionType.equals(NodeModel.L_REFUTATION) ||mentionType.equals(NodeModel.L_QUESTION))) { return badRequest("Wrong mention type."); } JsonNode json = request().body().asJson(); String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); if ( graph.getVertex(author) == null) { return badRequest("Author "+ author + "is not exist."); } JsonNode toulmin = json.findPath(NodeModel.TOULMIN); if (toulmin.findPath(NodeModel.TITLE) == null) { return badRequest("Please set title"); } JsonNode usersJson = json.get(NodeModel.USERS); // Users (class JsonNode) String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously) ClaimModel newClaim = new ClaimModel(graph.addVertex(null)); tpGraph.setLabelToAuthor(newClaim, author); newClaim.setClaimProperties(toulmin, type); String[] users = toStringArray(usersJson); tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.FAIL); tpGraph.setLabelToRootClaim(newClaim); ClaimModel mentionV = new ClaimModel(graph.getVertex(id)); tpGraph.setLabelMention(mentionV, newClaim, mentionType); return created(); } public static Result getClaimInfo(String id) { TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); Vertex claimV = graph.getVertex(id); if (claimV == null) { badRequest("Claim id "+id+" is not found."); } ClaimModel claim = new ClaimModel(claimV); ObjectNode claimInfo = claim.getSimpleClaimInfo(); ObjectNode result = Json.newObject(); result.put("status", "OK"); result.put("message", claimInfo); return ok(result); } public static Result getClaimTree(String id) { TPGraph tpGraph = TPGraph.getInstance(); Graph graph = tpGraph.getGraph(); Vertex v = graph.getVertex(id); if (v == null) { return badRequest("Consensus id "+ id +" is not found."); } ClaimModel consensusRoot = new ClaimModel(v); ObjectNode resultEntity = consensusRoot.getClaimInfoTraverse(); return ok(resultEntity); } private static String[] toStringArray(JsonNode jsonNode) { int length = jsonNode.size(); if (length == 0) { return null; } String[] userArray = new String[length]; for (int i=0; i<length; i++ ) { userArray[i] = jsonNode.get(i).getTextValue(); } return userArray; } }