view app/controllers/Claim.java @ 76:41b6f3788cc3 draft

TPGraph wrap graph
author one
date Fri, 08 Mar 2013 16:18:53 +0900
parents 9448734399db
children cddb5ed942a6
line wrap: on
line source

package controllers;

import init.InitialData;

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.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 createClaim() {
		JsonNode json = request().body().asJson();
		if (json == null) {
			return badRequest("please set Enitity (Json Data)");
		}
		String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
		TPGraph tpGraph = TPGraph.getInstance();
		if ( tpGraph.getVertex(author) == null) {
			return badRequest("Author "+ author + "does 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(tpGraph.addVertex(null));
		tpGraph.setLabelToAuthor(newClaim, author);
		newClaim.setClaimProperties(toulmin, type);
		String[] users = toStringArray(usersJson);
		tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN);
		tpGraph.setLabelFromRootClaim(newClaim);
		return created();
	}
	
	@BodyParser.Of(BodyParser.Json.class)
	public static Result editClaim(String id) {
		JsonNode json = request().body().asJson();
		if (json == null) {
			return badRequest();
		}
		String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
		TPGraph tpGraph = TPGraph.getInstance();
		ClaimModel claim = new ClaimModel(tpGraph.getVertex(id));
		if ( claim.getVertex() == null ) {
			return badRequest("Claim id "+ id + " does not exist.");
		} 
		if ( !claim.getAuthorId().equals(author)) {
			return badRequest("Wrong Author.");
		}
		if ( tpGraph.getVertex(author) == null) { 
			return badRequest("Author "+ author + " does 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.setLabelFromRootClaim(claim);
		return created();
	}
	
	public static Result getUserConsensusStatus(String id, String name) {
		TPGraph tpGraph = TPGraph.getInstance();
		ClaimModel claim = new ClaimModel(tpGraph.getVertex(id));
		if (claim.getVertex() == null) {
			return badRequest("Claim id "+id+" does not exist");
		}
		UserModel user = new UserModel(tpGraph.getVertex(name));
		if (user.getVertex() == null) {
			return badRequest("User "+name+" does not exist");
		}
		ObjectNode result = claim.getUserRequestStatus(user);
		return ok(result);
	}
	
	public static Result updateUserConsensusStatus(String id, String name, String status) {
		if ( !(status.equals(NodeModel.AGREED) 
				||status.equals(NodeModel.DENIED)
				||status.equals(NodeModel.UNKNOWN)
				||status.equals(NodeModel.PEND))) {
				return badRequest("Wrong status type.");
		}
		TPGraph tpGraph = TPGraph.getInstance();
		ClaimModel claim = new ClaimModel(tpGraph.getVertex(id));
		if (claim.getVertex() == null) {
			return badRequest("Claim id "+id+" does not exist");
		}
		UserModel user = new UserModel(tpGraph.getVertex(name));
		if (user.getVertex() == null) {
			return badRequest("User "+name+" does not exist");
		}
		claim.updateUserRequestStatus(claim, user, status);
		claim.computeAndUpdateStatus();
		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();
		if (json == null) {
			return badRequest();
		}
		String author = json.findPath(NodeModel.L_AUTHOR).getTextValue(); // Author
		TPGraph tpGraph = TPGraph.getInstance();
		if ( tpGraph.getVertex(author) == null) { 
			return badRequest("Author "+ author + " does 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(tpGraph.addVertex(null));
		tpGraph.setLabelToAuthor(newClaim, author);
		newClaim.setClaimProperties(toulmin, type);
		String[] users = toStringArray(usersJson);
		tpGraph.setLabelStatusToUsers(newClaim, users, NodeModel.L_REQUEST, NodeModel.UNKNOWN);
		tpGraph.setLabelFromRootClaim(newClaim);
		ClaimModel targetClaim = new ClaimModel(tpGraph.getVertex(id));
		tpGraph.setLabelMention(targetClaim, newClaim, mentionType);
		newClaim.computeAndUpdateStatus();
		targetClaim.computeAndUpdateStatus();
		return created();
	}

	public static Result copyClaims(String id) {
		TPGraph tpGraph = TPGraph.getInstance();
		ClaimModel claim = new ClaimModel(tpGraph.getVertex(id));
		if ( claim.getVertex() == null) {
			return badRequest("Claim id " + id + " does not exist.");
		}
		tpGraph.copyConsensusTree(claim);
		return ok("copy success");
	}

	public static Result getClaimInfo(String id) {
		TPGraph tpGraph = TPGraph.getInstance();
		Vertex claimV = tpGraph.getVertex(id); 
		if (claimV == null) {
			badRequest("Claim id "+id+" does not exist.");
		}
		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();
		Vertex v = tpGraph.getVertex(id);
		if (v == null) { 
			return badRequest("Consensus id "+ id +" does not exist.");
		}
		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;
	}
	
	public static Result reset() {
		InitialData.init();
		return ok();
	}
	
	
}