view app/controllers/User.java @ 13:9b677755cb93

create action Claim/createClaim
author one
date Tue, 02 Oct 2012 11:39:39 +0900
parents a8ea4191fa99
children 7cdc9d19834f
line wrap: on
line source

package controllers;

import java.util.HashMap;

import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.Vertex;

import models.NodeModel;
import models.TPGraph;
import models.UserModel;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;

public class User extends Controller {

	public static Result createUser(String name) {
		
		TPGraph tpGraph = TPGraph.getInstance();
		Graph graph = tpGraph.getGraph();
		Vertex v = null;
		UserModel newUser = null; 
		try {
			v = graph.addVertex(name);
		} catch (IllegalArgumentException e) {
			return status(CONFLICT, name+" is already exists");
		}
		newUser = new UserModel(v);
		newUser.setUserInfo();
		tpGraph.setLabelToRootUser(newUser);
//		newUser.setName(name); // user node hasn't name property(only TinkerGraph).
		return created();	
	}


	public static Result getUser(String name) {
		
		TPGraph tpGraph = TPGraph.getInstance();
		Graph graph = tpGraph.getGraph();
		Vertex v = graph.getVertex(name);
		if (v == null) {
			return notFound();
		} else {
			UserModel user = new UserModel(v);
			HashMap<Object,Object> hash = user.getUserProperty();
			return created(Json.toJson(hash));
		}
	}
	
	public static Result getUserRequests(String name) {
		TPGraph tpGraph = TPGraph.getInstance();
		Graph graph = tpGraph.getGraph();
		Vertex v = graph.getVertex(name);
		if (v == null) {
			return notFound("user: "+name+" not found");
		} else {
			UserModel user = new UserModel(v);
			HashMap<Object,Object> hash = user.getUserRequests();
			if (hash == null) return notFound("requests not found");
			return created(Json.toJson(hash));
		}		
	}
	
	public static Result getUserConsensus(String name) {
		TPGraph tpGraph = TPGraph.getInstance();
		Graph graph = tpGraph.getGraph();
		Vertex v = graph.getVertex(name);
		if (v == null) {
			return notFound("user: "+name+" not found");
		} else {
			UserModel user = new UserModel(v);
			HashMap<Object,Object> hash = user.getUserConsensus();
			if (hash == null) return notFound("requests not found");
			return created(Json.toJson(hash));
		}		
	}
	
	public static Result getUserClaims(String name) {
		TPGraph tpGraph = TPGraph.getInstance();
		Graph graph = tpGraph.getGraph();
		Vertex v = graph.getVertex(name);
		if (v == null) {
			return notFound("user: "+name+" not found");
		} else {
			UserModel user = new UserModel(v);
			HashMap<Object,Object> hash = user.getUserClaims();
			if (hash == null) return notFound("requests not found");
			return created(Json.toJson(hash));
		}		
	}
	
	
	
	
}