view app/controllers/Claim.java @ 19:20b2daf76635

delete unnecessary library
author one
date Tue, 02 Oct 2012 21:52:17 +0900
parents c8ad59a52c7e
children 34ea98c5a18c
line wrap: on
line source

package controllers;

import java.util.HashMap;
import java.util.Iterator;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.node.ObjectNode;

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

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)		
		
		Vertex claimVertex = null;
		claimVertex = graph.addVertex(null);
		ClaimModel newClaim = new ClaimModel(claimVertex);
		tpGraph.setLabelToAuthor(author, newClaim);
		newClaim.setClaimProperties(toulmin, type);
		String[] users = toStringArray(usersJson);
		tpGraph.setLabelToUsers(claimVertex, users, NodeModel.L_REQUEST);
		tpGraph.setLabelToRootClaim(newClaim);
		tpGraph.setLabelToAuthor(author, 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;
	}
	
	
	
	
}