view app/models/TPGraph.java @ 20:34ea98c5a18c

modified UserModel/getEdgeInUser
author one
date Tue, 02 Oct 2012 22:49:16 +0900
parents c8ad59a52c7e
children cad8e054fe4b
line wrap: on
line source

package models;

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);
	}

	private void setLabel(Vertex fromV, Vertex toV, String label) {
		graph.addEdge(null, fromV, toV, label);		
	}
	
	public void setLabelToRootUser(UserModel user) {
		Vertex rootUser = getUserRootVertex(); 

		//  rootUser ---child---> newUser
		setLabel(rootUser, user.getVertex(), CHILD);
	}

	public void setLabelToRootClaim(ClaimModel claim) {
		Vertex rootClaim = getClaimRootVertex(); 

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

		//  claim ---author---> authorVertex(userVertex)
		setLabel(claim.getVertex(), authorVertex, NodeModel.L_AUTHOR);
	}
	
	public Boolean setLabelToUsers(ClaimModel claim, String[] users, String label) {
		for (String userName: users) {
			Vertex userVertex = graph.getVertex(userName);
			if (userVertex == null) return false;
			setLabel(claim.getVertex(), userVertex, label);
		}
		return true;
	}
	
	
	public void shutdownGraph() {
		graph.shutdown();
	}

}