view app/models/TPGraph.java @ 16:7cdc9d19834f

modified createClaim
author one
date Tue, 02 Oct 2012 13:52:31 +0900
parents 9b677755cb93
children a134edaebf6f
line wrap: on
line source

package models;

import org.codehaus.jackson.JsonNode;

import scala.reflect.generic.Trees.This;

import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.tg.TinkerGraph;

public class TPGraph {

	private static TPGraph instance = new TPGraph();
	private Object claimRootId;
	private Object userRootId;
	
	/*
	 *  Edge type
	 */
	protected final String CHILD = "child";
	
	
	private TPGraph() {
		
	}
	
	public static TPGraph getInstance() {
		return instance;
	}

	private Graph graph; 
	private String path = null;
	
	public void setPath(String path) {
		this.path = path;
	}

	public Graph newGraph() {
		if (path == null) {
			graph = new TinkerGraph();
		} else {
			graph = new TinkerGraph(path);
			
		}
		return graph;
	}

	public Graph getGraph() {
		return graph;
	}
	
	public void setClaimRootId(Object id) {
		this.claimRootId = id;
	}
	
	public void setUserRootId(Object id) {
		this.userRootId = id;
	}
	
	public Object getClaimRootId() {
		return claimRootId;
	}

	public Object getUserRootId() {
		return userRootId;
	}

	public Vertex getClaimRootVertex() {
		return graph.getVertex(claimRootId);
	}
	
	public Vertex getUserRootVertex() {
		return graph.getVertex(userRootId);
	}

	public void setLabelToRootUser(UserModel user) {
		Vertex rootUser = getUserRootVertex(); 
		/* 
		 *  rootUser ---child---> newUser
		 */
		graph.addEdge(null, rootUser, user.getVertex(), CHILD);
	}

	public void setLabelToRootClaim(ClaimModel claim) {
		Vertex rootClaim = getClaimRootVertex(); 
		/* 
		 *  rootUser ---child---> newUser
		 */
		graph.addEdge(null, rootClaim, claim.getVertex(), CHILD);
	}
	
	public void setLabelToAuthor(String author, ClaimModel claim) {
		Vertex authorVertex = graph.getVertex(author);

		/*
		 *  claim ---author---> authorVertex(userVertex)
		 */
		graph.addEdge(null, claim.getVertex(), authorVertex, NodeModel.AUTHOR);
		
	}
	
	public Boolean updateUserVertex(Vertex claim, JsonNode usersJson) {

		
		return true;
	}
	
	
	public void shutdownGraph() {
		graph.shutdown();
	}
	
	
	
	

	
}