view app/models/UserModel.java @ 39:870553e92e3e

create getUserConsensusStatus action
author one
date Thu, 04 Oct 2012 01:39:44 +0900
parents 5f7fcdf98380
children f78442777849
line wrap: on
line source

package models;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

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

public class UserModel extends NodeModel {
	
	
	public UserModel(Vertex vertex) {
		super(vertex);
	}
	
	private Object[] getEdgeInUser(String labels) {
		GremlinPipeline<Vertex,Vertex> pipe = new GremlinPipeline<Vertex,Vertex>();
		pipe.start(this.vertex).in(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[] getUserRequests() {
		return getEdgeInUser(L_REQUEST);
	}
	
	public Object[] getUserClaims() {
		return getEdgeInUser(L_AUTHOR);
	}
	
	public HashSet<Object> getClaimsAndRequests() {
		Object[] requests = getUserRequests();
		Object[] claims = getUserClaims();
		return makeSet(requests, claims);
	}
	
	private HashSet<Object> makeSet(Object[] obj1, Object[] obj2) {
		HashSet<Object> set = new HashSet<Object>();
		if (obj1 != null)
			for (Object o : obj1) set.add(o);
		if (obj2 != null) 
			for (Object o : obj2) set.add(o);
		if (set.size() == 0) return null;
		return set;
	}
	
	/*
	public Object[] getUserConsensus() {
		return null;
	}
	 */
	
	public HashMap<Object,Object[]> getUserInfo() {
		TPGraph tpGraph = TPGraph.getInstance();
		Object[] requests = getUserRequests();
		Object[] claims = getUserClaims();
		HashMap<Object,Object[]> hash = new HashMap<Object, Object[]>();
		hash.put(NodeModel.REQUESTS, requests);
		hash.put(NodeModel.CLAIM, claims);
		HashSet<Object> set = makeSet(requests, claims);
		hash.put(NodeModel.CONSENSUS, tpGraph.checkConsensus(set));
		return hash;
	}

		
	
}