Mercurial > hg > Members > nobuyasu > Consensus
view app/controllers/Claim.java @ 38:a2abe67d7c7a
fix createMention action bug
author | one |
---|---|
date | Thu, 04 Oct 2012 01:05:19 +0900 |
parents | bc3ac73320f9 |
children | 870553e92e3e |
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) if (usersJson == null) { return badRequest("Please set users"); } 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.UNKNOWN); 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(); ClaimModel claim = new ClaimModel(graph.getVertex(id)); if ( claim.getVertex() == null ) { return badRequest("Claim id "+ id + "does not exist."); } if ( !claim.getAuthorId().equals(author)) { return badRequest("Wrong Author."); } 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); if (usersJson == null) { return badRequest("Please set users"); } String type = json.findPath(NodeModel.TYPE).getTextValue(); // Type (majority|unanimously) claim.setClaimProperties(toulmin, type); Object[] users = toStringObject(usersJson); claim.editRequestsEdgeUsers(users); 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_SUGGESTION))) { 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) if (usersJson == null) { return badRequest("Please set users"); } 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.UNKNOWN); 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("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 Object[] toStringObject(JsonNode jsonNode) { if (jsonNode == null) { return null; } int length = jsonNode.size(); if (length == 0) { return null; } Object[] userArray = new Object[length]; for (int i=0; i<length; i++ ) { userArray[i] = jsonNode.get(i).getTextValue(); } return userArray; } private static String[] toStringArray(JsonNode jsonNode) { if (jsonNode == null) { return null; } 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; } }