view app/controllers/Claim.java @ 28:7112b826a53a

modified RequestTest.java
author one
date Wed, 03 Oct 2012 12:53:37 +0900
parents 97b249d9fad1
children fbb232e78422
line wrap: on
line source

package controllers;

import org.codehaus.jackson.JsonNode;

import models.ClaimModel;
import models.NodeModel;
import models.TPGraph;

import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;

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.setLabelToUsers(newClaim, users, NodeModel.L_REQUEST);
		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.setLabelToUsers(newClaim, users, NodeModel.L_REQUEST);
		tpGraph.setLabelToRootClaim(newClaim);

		ClaimModel mentionV = new ClaimModel(graph.getVertex(id));
		tpGraph.setLabelMention(mentionV, newClaim, mentionType);

		return created();
	}
	
	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);
		
		
		
		
		
		
		
		
		return ok();
	}
	
	
	
	
	
	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;
	}
	
	
	
	
}