view app/controllers/Claim.java @ 20:34ea98c5a18c

modified UserModel/getEdgeInUser
author one
date Tue, 02 Oct 2012 22:49:16 +0900
parents c8ad59a52c7e
children 97b249d9fad1
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 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();
	}
	
	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;
	}
	
	
	
	
}