view app/models/ClaimModel.java @ 38:a2abe67d7c7a

fix createMention action bug
author one
date Thu, 04 Oct 2012 01:05:19 +0900
parents bc3ac73320f9
children 870553e92e3e
line wrap: on
line source

package models;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

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

import play.libs.Json;

import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.gremlin.java.GremlinPipeline;

public class ClaimModel extends NodeModel {

	
	public ClaimModel(Vertex vertex) {
		super(vertex);
	}
	
	public ObjectNode getSimpleClaimInfo() {
		ObjectNode property = Json.newObject();
		property.put(TYPE, Json.toJson(getProperty(TYPE)));
		property.put(STATUS, Json.toJson(getProperty(STATUS)));
		property.put(TOULMIN, Json.toJson(getProperty(TOULMIN)));
		property.put(L_AUTHOR, Json.toJson(getAuthorId()));
		property.put(MENTIONS, Json.toJson(getMentionsId()));
		property.put(USERS, Json.toJson(getUsersId()));
		return property;
	}
	
	public ObjectNode getClaimInfoTraverse() {
		ObjectNode property = Json.newObject();
		property.put(TYPE, Json.toJson(getProperty(TYPE)));
		property.put(STATUS, Json.toJson(getProperty(STATUS)));
		property.put(TOULMIN, Json.toJson(getProperty(TOULMIN)));
		property.put(L_AUTHOR, Json.toJson(getAuthorId()));
		property.put(MENTIONS, Json.toJson(getClaimMentionsRecursive()));
		property.put(USERS, Json.toJson(getUsersIdAndStatus()));
		return property;
	}
	
	public Object[] getClaimMentionsRecursive() {
		GremlinPipeline<Vertex,Edge> pipe = new GremlinPipeline<Vertex,Edge>();
		pipe.start(vertex).outE(L_QUESTION,L_REFUTATION,L_SUGGESTION);		
		ArrayList<Object> array = new ArrayList<Object>();		
		for (Edge e:pipe) {
			String label = e.getLabel();
			ObjectNode info = Json.newObject();
			info.put(TYPE, Json.toJson(label));
			GremlinPipeline<Edge,Vertex> pipeChildVertex = new GremlinPipeline<Edge,Vertex>();		
			pipeChildVertex.start(e).inV();
			ClaimModel childClaim = new ClaimModel(pipeChildVertex.next());
			info.put(CLAIM, childClaim.getClaimInfoTraverse());
			info.put(ID, Json.toJson(childClaim.getId()));
			array.add(info);			
		}
		return array.toArray();
	}

	public Object[] getInfoArray(String... labels) {
		GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
		pipe.start(vertex).out(labels);
		ArrayList<Object> array = new ArrayList<Object>();
		for (Vertex v : pipe) array.add(v.getId());
		if (array.size() == 0) return null;
		return array.toArray();
	}

	public Object[] getMentionsId() {
		return getInfoArray(L_QUESTION,L_REFUTATION,L_SUGGESTION);
	}
	
	public Object[] getUsersId() {
		return getInfoArray(L_REQUEST);
	}
	
	public Object[] getUsersIdAndStatus() {
		GremlinPipeline<Vertex,Edge> pipeEdge = new GremlinPipeline<Vertex,Edge>();		
		pipeEdge.start(vertex).outE(L_REQUEST);
		ArrayList<Object> array = new ArrayList<Object>();
		for (Edge e : pipeEdge) {
			GremlinPipeline<Edge,Vertex> pipeChildVertex = new GremlinPipeline<Edge,Vertex>();		
			ObjectNode info = Json.newObject();
			pipeChildVertex.start(e).inV();
			Vertex childVertex = pipeChildVertex.next();
			info.put(ID, Json.toJson(childVertex.getId()));
			info.put(STATUS, Json.toJson(e.getProperty(STATUS)));
			array.add(info);
		}
		return array.toArray();
	}

	public Object[] getRequestUsersId() {
		return getInfoArray(NodeModel.REQUESTS);
	}
	
	public void editRequestsEdgeUsers(Object[] updateUsers) {
		TPGraph tpGraph = TPGraph.getInstance();
		Object[] currentUsers = getUsersId();
		HashSet<Object> currentUsersHashSet = new HashSet<Object>();
		for (Object u : currentUsers) {
			currentUsersHashSet.add(u);
		}
		for (Object updateUser : updateUsers) {
			if (currentUsersHashSet.contains(updateUser)) {
				currentUsersHashSet.remove(updateUser);
			} else {
				tpGraph.setLabelStatusToUser(this, updateUser.toString(), L_REQUEST, UNKNOWN);
			}
		}
		tpGraph.deleteRequestEdge(this, currentUsersHashSet);
	}
	
	public Object getAuthorId() {
		GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
		pipe.start(vertex).out(L_AUTHOR);
		if (!pipe.hasNext()) {
			return null;
		}
		Vertex authorV = pipe.next();
		return authorV.getId();
	}
	
	public void setClaimProperties(JsonNode toulmin, String type) {
		String title = toulmin.findPath(TITLE).getTextValue();
		String contents = toulmin.findPath(CONTENTS).getTextValue();
		String q = toulmin.findPath(QUALIFIER).getTextValue(); // Qualifier
		String d = toulmin.findPath(DATA).getTextValue(); // Data
		String w = toulmin.findPath(WARRANT).getTextValue(); // Warrant
		String b = toulmin.findPath(BACKING).getTextValue(); // Backing
		String r = toulmin.findPath(REBUTTLE).getTextValue(); // Rebuttle
		ObjectNode t = Json.newObject();
		t.put(TITLE, title);
		t.put(CONTENTS, contents);
		t.put(QUALIFIER, q);
		t.put(DATA, d);
		t.put(WARRANT, w);
		t.put(BACKING, b);
		t.put(REBUTTLE, r);
		setProperty(TYPE, type);
		setProperty(STATUS, UNKNOWN); // Default Status is unknown.
		setProperty(TOULMIN, t);
	}
}