view app/controllers/Claim.java @ 30:80b5628f17d8

modified ClaimModel/getInfo action
author one
date Wed, 03 Oct 2012 14:27:44 +0900 (2012-10-03)
parents fbb232e78422
children 995be14b30a2
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 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.getClaimInfoFromGraph();
		ObjectNode result = Json.newObject();
		result.put("status", "OK");
		result.put("message", claimInfo.toString());
		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 consensusObj = consensusRoot.getClaimInfoFromGraph();
		
		JsonNode jobj = consensusObj.findValue(NodeModel.TOULMIN);
		
		
		
		ObjectNode result = Json.newObject();
		result.put("message",jobj.toString());
		return ok(result);
	}
	
	
	
	
	
	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;
	}
	
	
	
	
}